Implement numbered lists
authorBernie Innocenti <bernie@codewiz.org>
Fri, 11 Sep 2009 21:46:48 +0000 (17:46 -0400)
committerBernie Innocenti <bernie@codewiz.org>
Fri, 11 Sep 2009 21:46:48 +0000 (17:46 -0400)
geekigeeki.py

index 9a05b85ecfa8834fba0636a338e6542966e1c83b..8c9fe64f7bf37cfd82261ac492957b5149e4ac13 100755 (executable)
@@ -373,7 +373,7 @@ class WikiFormatter:
         self.h_level = 0
         self.in_pre = self.in_html = self.in_table = self.in_li = False
         self.in_header = True
-        self.list_indents = []
+        self.list_indents = [] # a list of pairs (indent_level, list_type) to track nested lists
         self.tr_cnt = 0
         self.styles = {
             #wiki   html   enabled?
@@ -411,7 +411,7 @@ class WikiFormatter:
         return '<br />'
 
     def _rule_repl(self, word):
-        return self._undent() + '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
+        return '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
 
     def _macro_repl(self, word):
         try:
@@ -518,31 +518,26 @@ class WikiFormatter:
         return ''
 
     def _indent_level(self):
-        return len(self.list_indents) and self.list_indents[-1]
+        return len(self.list_indents) and self.list_indents[-1][0]
 
-    def _indent_to(self, new_level):
+    def _indent_to(self, new_level, list_type=''):
         if self._indent_level() == new_level:
             return ''
         s = '</p>'
         while self._indent_level() > new_level:
-            del(self.list_indents[-1])
             if self.in_li:
                 s += '</li>'
                 self.in_li = False # FIXME
-            s += '</ul>\n'
+            s += '</' + self.list_indents[-1][1] + '>\n'
+            del(self.list_indents[-1])
+
+        list_type = ('ul', 'ol')[list_type == '#']
         while self._indent_level() < new_level:
-            self.list_indents.append(new_level)
-            s += '<ul>\n'
+            self.list_indents.append((new_level, list_type))
+            s += '<' + list_type + '>\n'
         s += '<p>'
         return s
 
-    def _undent(self):
-        res = '</p>'
-        res += '</ul>' * len(self.list_indents)
-        res += '<p>'
-        self.list_indents = []
-        return res
-
     def replace(self, match):
         for rule, hit in list(match.groupdict().items()):
             if hit:
@@ -594,7 +589,7 @@ class WikiFormatter:
             | (?P<ent>[<>&])"
             )""", re.VERBOSE)
         blank_re = re.compile(r"^\s*$")
-        indent_re = re.compile(r"^\s*")
+        indent_re = re.compile(r"^(\s*)(\*|\#|)")
         tr_re = re.compile(r"^\s*\|\|")
         eol_re = re.compile(r"\r?\n")
         # For each line, we scan through looking for magic strings, outputting verbatim any intervening text
@@ -618,12 +613,12 @@ class WikiFormatter:
                 else:
                     indent = indent_re.match(self.line)
                     #3.0: print(self._indent_to(len(indent.group(0))), end=' ')
-                    print(self._indent_to(len(indent.group(0))))
+                    print(self._indent_to(len(indent.group(1)), indent.group(2)))
                     print(re.sub(scan_re, self.replace, self.line))
 
         if self.in_pre: print('</pre>')
         if self.in_table: print('</tbody></table><p>')
-        print(self._undent())
+        print(self._indent_to(0))
         print('</p></div>')
 
 class Page: