Add support for external macros
authorBernie Innocenti <bernie@codewiz.org>
Tue, 24 Jun 2008 03:36:36 +0000 (09:21 +0545)
committerBernie Innocenti <bernie@codewiz.org>
Tue, 24 Jun 2008 03:37:30 +0000 (09:22 +0545)
geekigeeki.py
macros/HttpGet.py [new file with mode: 0755]
macros/Include.py [new file with mode: 0755]
macros/TitleIndex.py [new file with mode: 0755]
macros/WordIndex.py [new file with mode: 0755]

index ca59dcc8a150d690b92ca4c1f43ff68b25e15666..b2e2895f4562af32fed2ea813c805e09d1ac07cb 100755 (executable)
@@ -264,61 +264,6 @@ def send_footer(name, mod_string=None):
         print '<p class="modified">last modified %s</p>' % mod_string
     print '</div></body></html>'
 
-# Macros ------------------------------------------------------------
-def _macro_HttpGet(argv):
-    if argv[1] in form:
-        return form[argv[1]].value
-    elif len(argv) > 2:
-        return argv[2] # default value
-    return ''
-
-def _macro_WordIndex(argv):
-    s = make_index_key()
-    pages = list(page_list())
-    map = {}
-    word_re = re.compile('[A-Z][a-z]+')
-    for name in pages:
-        for word in word_re.findall(name):
-            try:
-                map[word].append(name)
-            except KeyError:
-                map[word] = [name]
-
-    all_words = map.keys()
-    all_words.sort()
-    last_letter = None
-    # set title
-    for word in all_words:
-        letter = word[0].lower()
-        if letter != last_letter:
-            s = s + '; <a name="%s"><h3>%s</h3></a>' % (letter, letter)
-            last_letter = letter
-
-        s = s + '<b>%s</b><ul>' % word
-        links = map[word]
-        links.sort()
-        last_page = None
-        for name in links:
-            if name == last_page: continue
-            s = s + '<li>' + Page(name).link_to()
-        s = s + '</ul>'
-    return s
-
-def _macro_TitleIndex(argv):
-    s = make_index_key()
-    pages = list(page_list())
-    pages.sort()
-    current_letter = None
-    for name in pages:
-        letter = name[0].lower()
-        if letter != current_letter:
-            s += '<a name="%s"><h3>%s</h3></a>' % (letter, letter)
-            current_letter = letter
-        else:
-            s += '<br />'
-        s += Page(name).link_to()
-    return s
-
 class PageFormatter:
     """Object that turns Wiki markup into HTML.
 
@@ -388,6 +333,9 @@ class PageFormatter:
         argv = map(str.strip, argv)
 
         macro = globals().get('_macro_' + name)
+        if not macro:
+            execfile("macros/" + name + ".py", globals())
+            macro = globals().get('_macro_' + name)
         if macro:
             return macro(argv)
         else:
@@ -653,6 +601,12 @@ class Page:
     def can_read(self):
         return self.can("read", True)
 
+    def send_naked(self):
+        if self.can_read():
+            PageFormatter(self.get_raw_body()).print_html()
+        else:
+            send_guru("Read access denied by ACLs", "notice")
+
     def send_page(self):
         page_name = None
         if self.can_write():
@@ -663,10 +617,7 @@ class Page:
         css_url = self.get_attr("css", "").split() + css_url
 
         send_title(page_name, self.split_title(), msg_text=self.msg_text, msg_type=self.msg_type)
-        if self.can_read():
-            PageFormatter(self.get_raw_body()).print_html()
-        else:
-            send_guru("Read access denied by ACLs", "notice")
+        self.send_naked()
         send_footer(page_name, self._last_modified())
 
     def _last_modified(self):
diff --git a/macros/HttpGet.py b/macros/HttpGet.py
new file mode 100755 (executable)
index 0000000..7a4b548
--- /dev/null
@@ -0,0 +1,6 @@
+def _macro_HttpGet(argv):
+    if argv[1] in form:
+        return form[argv[1]].value
+    elif len(argv) > 2:
+        return argv[2] # default value
+    return ''
diff --git a/macros/Include.py b/macros/Include.py
new file mode 100755 (executable)
index 0000000..d46faf2
--- /dev/null
@@ -0,0 +1,3 @@
+def _macro_Include(argv):
+    Page(argv[1]).send_naked()
+    return ''
diff --git a/macros/TitleIndex.py b/macros/TitleIndex.py
new file mode 100755 (executable)
index 0000000..04d5019
--- /dev/null
@@ -0,0 +1,14 @@
+def _macro_TitleIndex(argv):
+    s = make_index_key()
+    pages = list(page_list())
+    pages.sort()
+    current_letter = None
+    for name in pages:
+        letter = name[0].lower()
+        if letter != current_letter:
+            s += '<a name="%s"><h3>%s</h3></a>' % (letter, letter)
+            current_letter = letter
+        else:
+            s += '<br />'
+        s += Page(name).link_to()
+    return s
diff --git a/macros/WordIndex.py b/macros/WordIndex.py
new file mode 100755 (executable)
index 0000000..81cd0f9
--- /dev/null
@@ -0,0 +1,31 @@
+def _macro_WordIndex(argv):
+    s = make_index_key()
+    pages = list(page_list())
+    map = {}
+    word_re = re.compile('[A-Z][a-z]+')
+    for name in pages:
+        for word in word_re.findall(name):
+            try:
+                map[word].append(name)
+            except KeyError:
+                map[word] = [name]
+
+    all_words = map.keys()
+    all_words.sort()
+    last_letter = None
+    # set title
+    for word in all_words:
+        letter = word[0].lower()
+        if letter != last_letter:
+            s = s + '; <a name="%s"><h3>%s</h3></a>' % (letter, letter)
+            last_letter = letter
+
+        s = s + '<b>%s</b><ul>' % word
+        links = map[word]
+        links.sort()
+        last_page = None
+        for name in links:
+            if name == last_page: continue
+            s = s + '<li>' + Page(name).link_to()
+        s = s + '</ul>'
+    return s