changeset 488:ea71d3ed8f79

Rudimentary support of macros and a macro for syntax highlighting
author sheep@ghostwheel
date Sat, 11 Oct 2008 11:39:55 +0200
parents a1eeb01c1e42
children 11fbe9ed4fff
files dandelion/syntax.py dandelion/viewer.py dandelion/wiki.py dandelion/wikiparser.py
diffstat 4 files changed, 50 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dandelion/syntax.py	Sat Oct 11 11:39:55 2008 +0200
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+from pygments import highlight
+from pygments.util import ClassNotFound
+from pygments.lexers import get_lexer_by_name, TextLexer
+from pygments.formatter import Formatter
+import template
+
+class GenshiFormatter(Formatter):
+    def __init__(self):
+        Formatter.__init__(self)
+        self.tokens = []
+
+    def format(self, tokensource, outfile):
+        self.tokens.append(template.html_start("pre", class_='syntax'))
+        for ttype, value in tokensource:
+            self.tokens.append(template.html_start("span", class_=str(ttype).replace(".", " ")))
+            self.tokens.append(template.html_text(value))
+            self.tokens.append(template.html_end("span"))
+        self.tokens.append(template.html_end("pre"))
+
+def syntax_macro(wiki, name, text, **kw):
+    syntax = kw.get('syntax', name)
+    formatter = GenshiFormatter()
+    try:
+        lexer = get_lexer_by_name(syntax)
+    except ClassNotFound:
+        lexer = TextLexer()
+    highlight(text, lexer, formatter)
+    return formatter.tokens
--- a/dandelion/viewer.py	Wed Oct 08 16:56:55 2008 +0200
+++ b/dandelion/viewer.py	Sat Oct 11 11:39:55 2008 +0200
@@ -36,7 +36,7 @@
     return wiki.template("page.html", page, content=stream)
 
 def wiki_viewer(wiki, page):
-    content = wikiparser.WikiParser(page.open(), wiki.wiki_link, wiki.wiki_image).parse()
+    content = wikiparser.WikiParser(page.open(), wiki.wiki_link, wiki.wiki_image, wiki.wiki_macro).parse()
     return wiki.template("page.html", page, content=content)
 
 def image_viewer(wiki, page):
--- a/dandelion/wiki.py	Wed Oct 08 16:56:55 2008 +0200
+++ b/dandelion/wiki.py	Sat Oct 11 11:39:55 2008 +0200
@@ -20,6 +20,7 @@
 import wikipage
 import template
 import request
+import syntax
 
 class Wiki(object):
     """ Collect the parts that make up the wiki engine. Can be monkey-patched
@@ -82,6 +83,10 @@
                 os.path.join(data_dir, 'templates'),
                 os.path.join(lib_dir, 'templates'),
             ])
+        self.macros = {
+            "default": self.default_macro,
+            'syntax': syntax.syntax_macro,
+        }
         self.template = self.templates.template
         self.template_stream = self.templates.template_stream
         self.search_index = word_search.SearchIndex(index_file)
@@ -124,6 +129,13 @@
             url = self[target].get_url(action or "download")
         return url, css
 
+    def wiki_macro(self, name, text, **kw):
+        macro = self.macros.get(name, self.macros["default"])
+        return macro(self, name, text, **kw)
+
+    def default_macro(self, wiki, name, text, **kw):
+        return u"unknown macro %s" % name
+
     def page_url(self, title, action=None, rev=None, **kw):
         return self[title].get_url(action, rev, **kw)
 
--- a/dandelion/wikiparser.py	Wed Oct 08 16:56:55 2008 +0200
+++ b/dandelion/wikiparser.py	Sat Oct 11 11:39:55 2008 +0200
@@ -232,7 +232,8 @@
         if self.wiki_macro is None:
             yield html_text(text)
         else:
-            yield self.wiki_macro(name, text)
+            for part in self.wiki_macro(name, text):
+                yield part
         yield html_end('span')
 
     def block_code(self, block):
@@ -264,7 +265,8 @@
             if self.wiki_macro is None:
                 yield html_text(inside)
             else:
-                yield self.wiki_macro(name, inside)
+                for part in self.wiki_macro(name, inside):
+                    yield part
             yield html_end('div')
 
     def block_syntax(self, block):
@@ -280,7 +282,9 @@
                 line = unicode(self.lines.next(), "utf-8", "replace")
             inside = u"\n".join(line.rstrip() for line in lines)
             yield html_start('pre', class_='syntax %s' % syntax)
-            yield html_text(inside)
+            for part in self.wiki_macro("syntax", inside, syntax=syntax):
+                yield part
+#            yield html_text(inside)
             yield html_end('pre')
 
     def block_paragraph(self, block):