From f200c865fb8a5daae30ff9c070eb5c929c720a4b Mon Sep 17 00:00:00 2001 From: Mason Larobina Date: Thu, 24 Dec 2009 00:27:19 +0800 Subject: Moved expand function to external plugin to reduce code duplication. --- examples/data/uzbl/plugins/cmd_expand.py | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/data/uzbl/plugins/cmd_expand.py (limited to 'examples/data/uzbl/plugins/cmd_expand.py') diff --git a/examples/data/uzbl/plugins/cmd_expand.py b/examples/data/uzbl/plugins/cmd_expand.py new file mode 100644 index 0000000..a5c279d --- /dev/null +++ b/examples/data/uzbl/plugins/cmd_expand.py @@ -0,0 +1,43 @@ +__export__ = ['cmd_expand',] + + +def escape(str): + for (level, char) in [(3, '\\'), (2, "'"), (2, '"'), (1, '@')]: + str = str.replace(char, (level * '\\') + char) + + return str + + +def cmd_expand(uzbl, cmd, args): + '''Exports a function that provides the following + expansions in any uzbl command string: + + %s = replace('%s', ' '.join(args)) + %r = replace('%r', "'%s'" % escaped(' '.join(args))) + %1 = replace('%1', arg[0]) + %2 = replace('%2', arg[1]) + %n = replace('%n', arg[n-1]) + ''' + + # Ensure (1) all string representable and (2) correct string encoding. + args = map(unicode, args) + + # Direct string replace. + if '%s' in cmd: + cmd = cmd.replace('%s', ' '.join(args)) + + # Escaped and quoted string replace. + if '%r' in cmd: + cmd = cmd.replace('%r', "'%s'" % escape(' '.join(args))) + + # Arg index string replace. + for (index, arg) in enumerate(args): + index += 1 + if '%%%d' % index in cmd: + cmd = cmd.replace('%%%d' % index, unicode(arg)) + + return cmd + + +def init(*args): + pass -- cgit v1.2.3