changeset 2399:9430e08ac757

Fix #87: Add display math syntax, and more math_url options.
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 12 May 2013 23:12:52 +0200
parents 019e3bfa96ee
children 3a0f768ee5b4 101ee945a170
files hatta/config.py hatta/page.py hatta/parser.py
diffstat 3 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/hatta/config.py	Sun May 12 19:10:45 2013 +0200
+++ b/hatta/config.py	Sun May 12 23:12:52 2013 +0200
@@ -54,6 +54,9 @@
     help='Enable WikiWord links', action="store_true")
 _add('-I', '--ignore-indent', dest='ignore_indent',
     help='Treat indented lines as normal text', action="store_true")
+_add('-M', '--math-url', dest='math_url',
+    help='Use the URL for rendering the equations. Empty to disable math.',
+    metavar='URL')
 _add('-P', '--pygments-style', dest='pygments_style',
     help='Use the STYLE pygments style for highlighting',
     metavar='STYLE')
--- a/hatta/page.py	Sun May 12 19:10:45 2013 +0200
+++ b/hatta/page.py	Sun May 12 23:12:52 2013 +0200
@@ -481,14 +481,21 @@
                              self.highlight, self.wiki_math, smilies)
         return content
 
-    def wiki_math(self, math):
+    def wiki_math(self, math_text, display=False):
         math_url = self.config.get('math_url',
                             'http://www.mathtran.org/cgi-bin/mathtran?tex=')
+        if math_url == '':
+            return werkzeug.escape(math_text)
+        elif math_url == '$':
+            if display:
+                return werkzeug.escape(u"$$\n%s\n$$" % math_text)
+            else:
+                return werkzeug.escape(u"$%s$" % math_text)
         if '%s' in math_url:
-            url = math_url % werkzeug.url_quote(math)
+            url = math_url % werkzeug.url_quote(math_text)
         else:
-            url = '%s%s' % (math_url, werkzeug.url_quote(math))
-        label = werkzeug.escape(math, quote=True)
+            url = '%s%s' % (math_url, werkzeug.url_quote(math_text))
+        label = werkzeug.escape(math_text, quote=True)
         return werkzeug.html.img(src=url, alt=label, class_="math")
 
     def dependencies(self):
--- a/hatta/parser.py	Sun May 12 19:10:45 2013 +0200
+++ b/hatta/parser.py	Sun May 12 23:12:52 2013 +0200
@@ -184,6 +184,7 @@
         self.macro_close_re = re.compile(ur"^>>\s*$", re.U)
         self.conflict_close_re = re.compile(ur"^>>>>>>> other\s*$", re.U)
         self.conflict_sep_re = re.compile(ur"^=======\s*$", re.U)
+        self.display_math_close_re = re.compile(ur"^[$][$]\s*$", re.U)
         self.image_re = re.compile(self.image_pat, re.U)
         smileys = ur"|".join(re.escape(k) for k in self.smilies)
         smiley_pat = (ur"(^|\b|(?<=\s))(?P<smiley_face>%s)"
@@ -325,9 +326,10 @@
     @markup_rules(ur"\$\$(?P<math_text>[^$]+)\$\$", 100)
     def _line_math(self, math_text):
         if self.wiki_math:
-            return self.wiki_math(math_text)
+            math_text = self.wiki_math(math_text, False)
         else:
-            return "<var>%s</var>" % werkzeug.escape(math_text)
+            math_text = werkzeug.escape(math_text)
+        return werkzeug.html.var(math_text, class_="inline-math")
 
     @markup_rules(ur"[{][{][{](?P<code_text>([^}]|[^}][}]|[^}][}][}])"
                   ur"*[}]*)[}][}][}]", 20)
@@ -379,6 +381,20 @@
 
 # methods for the block (multiline) markup:
 
+    @block_rules(r"^[$][$]\s*$", 25)
+    def _block_display_math(self, block):
+        for self.line_no, part in block:
+            math_text = u"\n".join(self.lines_until(self.display_math_close_re))
+            if self.wiki_math:
+                math_text = self.wiki_math(math_text, True)
+            else:
+                math_text = werkzeug.escape(math_text)
+            yield werkzeug.html.pre(
+                math_text,
+                class_="display-math",
+                id="line_%d" % self.line_no,
+            )
+
     @block_rules(ur"^[{][{][{]+\s*$", 20)
     def _block_code(self, block):
         for self.line_no, part in block: