site: Redo the menu generation.
authorMatthijs Kooijman <matthijs@stdin.nl>
Thu, 29 Apr 2010 10:47:10 +0000 (12:47 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Thu, 29 Apr 2010 10:47:10 +0000 (12:47 +0200)
Previously there was some very specific parsing code, now we just parse
and show the entire Menu page.

plugin/theme/site.py

index 226680ba6dbcb6a39c6f2dc9a86ebac326f681b7..c2e1659eb6dd4b155bd1769b050244319d15fea0 100644 (file)
@@ -15,6 +15,7 @@
 from MoinMoin.theme import ThemeBase
 from MoinMoin.Page import Page
 from MoinMoin import wikiutil
 from MoinMoin.theme import ThemeBase
 from MoinMoin.Page import Page
 from MoinMoin import wikiutil
+from StringIO import StringIO
 
 class SiteTheme(ThemeBase):
 
 
 class SiteTheme(ThemeBase):
 
@@ -138,18 +139,8 @@ class SiteTheme(ThemeBase):
             @rtype:   unicode
             @return: menu html
         """
             @rtype:   unicode
             @return: menu html
         """
-        items = Page(self.request, 'Site/Menu').data.split('\n')
-        
-        html = '<ul id="menubar">'
-
-        for item in items:
-            if (not item.startswith("#")):
-                (pagename, link) = self.splitNavilink(item)
-                html = html + ("<li class=\"menulink\">%s</li>" % link)
-        
-        html = html + '</ul>'
-
-        return html
+        menu = Page(self.request, 'Site/Menu')
+        return u'<div id="menubar">%s</div>' % parse_wiki_page(self.request, menu)
 
     def theme_script(self, name):
         """ Format script html from this theme's static dir """
 
     def theme_script(self, name):
         """ Format script html from this theme's static dir """
@@ -187,4 +178,34 @@ class SiteTheme(ThemeBase):
         # This adds #pagebottom and closes #page
         return html
 
         # This adds #pagebottom and closes #page
         return html
 
+def parse_wiki_page(request, page):
+    """
+    This is an ugly hack to render a page into a string. By default,
+    formatters render using request.write automatically, which prevents us
+    from capturing the output. By disguising a StringIO buffer as a request
+    object, we manage to get at the rendered contents.
+
+    However, when {{{#!wiki or similar blocks are used, stuff breaks (since
+    that creates a second parser that doesn't get our StringIO buffer).
+    """
+    Parser = wikiutil.searchAndImportPlugin(request.cfg, "parser", 'wiki')
+    # Create a stringIO buffer, to capture the output
+    buffer = StringIO()
+    # Make the buffer look like the request, since the parser writes
+    # directly to the request
+    buffer.form = request.form
+    buffer.getText = request.getText
+    buffer.cfg = request.cfg
+    # Create a new formatter. Since we need to set its page, we can't use
+    # request.formatter.
+    from MoinMoin.formatter.text_html import Formatter
+    formatter = Formatter(request)
+    formatter.setPage(page)
+
+    # Create the parser and parse the page
+    parser = Parser(page.data, buffer)
+    parser.format(formatter)
+    # Return the captured buffer
+    return buffer.getvalue()
+
 # vim: set sw=4 sts=4 expandtab:
 # vim: set sw=4 sts=4 expandtab: