From ce5a4dbd3de540928b7019a716859a551961105c Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Mon, 17 Aug 2009 09:48:38 +0200 Subject: [PATCH] Implement variable substitution in includes --- geekigeeki.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/geekigeeki.py b/geekigeeki.py index 5a401ab..eb51489 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -83,18 +83,18 @@ def permalink(s): def humanlink(s): return re.sub(r'(?:.*[/:]|)([^:/\.]+)(?:\.[^/:]+|)$', r'\1', s.replace('_', ' ')) -# Split arg lists like "blah| blah blah| width=100 | align = center", +# Split arg lists like "blah|blah blah| width=100 | align = center", # return a list containing anonymous arguments and a map containing the named arguments def parse_args(s): args = [] - kwargs = {} + kvargs = {} for arg in s.strip('<[{}]>').split('|'): m = re.match('\s*(\w+)\s*=\s*(.+)\s*', arg) if m is not None: - kwargs[m.group(1)] = m.group(2) + kvargs[m.group(1)] = m.group(2) else: args.append(arg.strip()) - return (args, kwargs) + return (args, kvargs) def url_args(kvargs): argv = [] @@ -227,7 +227,7 @@ def link_inline(name, descr=None, kvargs={}): elif image_re.match(name): return '%s' % (url, url + url_args(kvargs), descr) elif file_re.match(name) and not ext_re.search(name): # FIXME: this guesses a wiki page - return Page(name).send_naked() + return Page(name).send_naked(kvargs) else: return '' \ % (url, url, name) @@ -337,17 +337,14 @@ def send_footer(mod_string=None): cgi.print_arguments() cgi.print_form(form) cgi.print_environ() - #FIXME link_inline("sys/footer") - print(''' -') + link_inline("sys/footer", kvargs= { 'LAST_MODIFIED': mod_string }) + print("") + +def _macro_ELAPSED_TIME(*args, **kvargs): + return "%03f" % (clock() - start_time) + +def _macro_VERSION(*args, **kvargs): + return __version__ class WikiFormatter: """Object that turns Wiki markup into HTML. @@ -355,8 +352,9 @@ class WikiFormatter: All formatting commands can be parsed one line at a time, though some state is carried over between lines. """ - def __init__(self, raw): + def __init__(self, raw, kvargs=None): self.raw = raw + self.kvargs = kvargs or {} self.h_level = 0 self.in_pre = self.in_html = self.in_table = self.in_li = False self.in_header = True @@ -402,14 +400,16 @@ class WikiFormatter: def _macro_repl(self, word): try: - args, kwargs = parse_args(word) + args, kvargs = parse_args(word) + if args[0] in self.kvargs: + return self.kvargs[args[0]] macro = globals().get('_macro_' + args[0]) if not macro: - exec(open("macros/" + name + ".py").read(), globals()) - macro = globals().get('_macro_' + name) - return macro(*args, **kwargs) - except Exception: - msg = cgi.escape(word) + exec(open("macros/" + args[0] + ".py").read(), globals()) + macro = globals().get('_macro_' + args[0]) + return macro(*args, **kvargs) + except Exception, e: + msg = cgi.escape(word) + ": " + cgi.escape(e.message) if not self.in_html: msg = '' + msg + '' return msg @@ -706,9 +706,9 @@ class Page: def can_read(self): return self.can("read", True) - def send_naked(self): + def send_naked(self, kvargs=None): if self.can_read(): - WikiFormatter(self.get_raw_body()).print_html() + WikiFormatter(self.get_raw_body(), kvargs).print_html() else: send_guru("Read access denied by ACLs", "notice") -- 2.25.1