Add inline HTML.
[geekigeeki.git] / geekigeeki.py
index 116b03b480911485162d8c6bacc49b3cddd1f523..46c61c55ad5343f95bbf6541eb959a6cab0f0638 100755 (executable)
@@ -107,7 +107,7 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'):
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 """
     print "<head><title>%s: %s</title>" % (site_name, text)
-    print ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'
+    print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
     if not name:
         print ' <meta name="robots" content="noindex,nofollow" />'
     if css_url:
@@ -264,10 +264,10 @@ def send_footer(name, mod_string=None):
 
 # ----------------------------------------------------------
 # Macros
-def _macro_TitleSearch():
+def _macro_TitleSearch(*vargs):
     return _macro_search("titlesearch")
 
-def _macro_FullSearch():
+def _macro_FullSearch(*vargs):
     return _macro_search("fullsearch")
 
 def _macro_search(type):
@@ -275,9 +275,9 @@ def _macro_search(type):
         default = form["value"].value
     else:
         default = ''
-    return """<form method="get"><input name="%s" size="30" value="%s"><input type="submit" value="Go" /></form>""" % (type, default)
+    return """<form method="get"><input name="%s" size="30" value="%s" /><input type="submit" value="Search" /></form>""" % (type, default)
 
-def _macro_WordIndex():
+def _macro_WordIndex(*vargs):
     s = make_index_key()
     pages = list(page_list())
     map = {}
@@ -310,7 +310,7 @@ def _macro_WordIndex():
     return s
 
 
-def _macro_TitleIndex():
+def _macro_TitleIndex(*vargs):
     s = make_index_key()
     pages = list(page_list())
     pages.sort()
@@ -337,98 +337,124 @@ class PageFormatter:
         self.raw = raw
         self.is_em = self.is_b = 0
         self.h_level = 0
+        self.h_count = 0
         self.list_indents = []
-        self.in_pre = 0
-        self.in_var = 0
+        self.in_pre = False
+        self.in_table = False
+        self.tr_cnt = 0
+        self.in_var = False
         self.in_header = True
 
     def _emph_repl(self, word):
         if len(word) == 3:
             self.is_b = not self.is_b
-            return ['</b>', '<b>'][self.is_b]
+            return ['</strong>', '<strong>'][self.is_b]
         else:
             self.is_em = not self.is_em
             return ['</em>', '<em>'][self.is_em]
 
     def _tit_repl(self, word):
         if self.h_level:
-            result = "</h%d>" % self.h_level
+            result = "</h%d></a>" % self.h_level
             self.h_level = 0
         else:
             self.h_level = len(word) - 1
-            result = "<h%d>" % self.h_level
+            self.h_count += 1
+            result = '<a href="#%d"><h%d id="%d">' % (self.h_count, self.h_level, self.h_count)
         return result
 
     def _rule_repl(self, word):
-        s = self._undent()
-        if len(word) <= 3:
-            s = s + "\n<hr size='1' noshade=\"noshade\" />\n"
-        else:
-            s = s + "\n<hr size='%d' noshade=\"noshade\" />\n" % (len(word) - 2 )
-        return s
+        return self._undent() + '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
 
     def _word_repl(self, word):
         return Page(word).link_to()
 
     def _img_repl(self, word):
-        return '<img border="0" src="%s/%s" />' % (script_name(), word)
+        path = script_name() + '/' + word;
+        return '<a href="%s"><img border="0" src="%s" /></a>' % (path, path)
 
     def _url_repl(self, word):
         if img_re.match(word):
-            return '<img border="0" src="%s" />' % word
+            return '<a href="%s"><img border="0" src="%s" /></a>' % (word, word)
         else:
             return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
 
     def _hurl_repl(self, word):
-        m = re.compile("\[\[(\S+)\ (.+)\]\]").match(word)
-        anchor = m.group(1)
-        descr = m.group(2)
-        if img_re.match(anchor):
-            return '<img border="0" src="%s" alt="%s" />' % (anchor, descr)
-        elif url_re.match(anchor):
-            return '<a href="%s" rel="nofollow" class="external">%s</a>' % (anchor, descr)
-        elif anchor.startswith('/'):
-            return '<a href="%s">%s</a>' % (anchor, descr)
+        m = re.compile("\[\[(\S+)([^\]]*)\]\]").match(word)
+        name = m.group(1)
+        descr = m.group(2).strip() or name
+
+        macro = globals().get('_macro_' + name)
+        if macro:
+            return apply(macro, (name, descr))
+        elif img_re.match(name):
+            return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (name, name, descr)
+        elif url_re.match(name):
+            return '<a href="%s" rel="nofollow" class="external">%s</a>' % (name, descr)
+        elif name.startswith('/'):
+            return '<a href="%s">%s</a>' % (name, descr)
         else:
-            return link_tag(anchor, descr)
+            return link_tag(name, descr)
 
     def _email_repl(self, word):
         return '<a href="mailto:%s">%s</a>' % (word, word)
 
+    def _html_repl(self, word):
+        return word; # Pass through
 
     def _ent_repl(self, s):
         return {'&': '&amp;',
                 '<': '&lt;',
                 '>': '&gt;'}[s]
 
-
     def _li_repl(self, match):
         return '<li>'
 
-
     def _pre_repl(self, word):
         if word == '{{{' and not self.in_pre:
-            self.in_pre = 1
+            self.in_pre = True
             return '<pre>'
         elif self.in_pre:
-            self.in_pre = 0
+            self.in_pre = False
             return '</pre>'
-        else:
-            return ''
+        return ''
+
+    def _hi_repl(self, word):
+        if word == 'FIXME':
+            cl = 'error'
+        elif word == 'DONE':
+            cl = 'success'
+        elif word == 'TODO':
+            cl = 'notice'
+        return '<strong class="highlight ' + cl + '">' + word + '</strong>'
 
     def _var_repl(self, word):
         if word == '{{' and not self.in_var:
-            self.in_var = 1
+            self.in_var = True
             return '<code>'
         elif self.in_var:
-            self.in_var = 0
+            self.in_var = False
             return '</code>'
-        else:
-            return ''
-    def _macro_repl(self, word):
-        macro_name = word[2:-2]
-        # TODO: Somehow get the default value into the search field
-        return apply(globals()['_macro_' + macro_name], ())
+        return ''
+
+    def _tr_repl(self, word):
+        out = ''
+        if not self.in_table:
+            self.in_table = True
+            self.tr_cnt = 0
+            out = '</p><table><tbody>\n'
+        self.tr_cnt += 1
+        return out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '"><td>'
+
+    def _tre_repl(self, word):
+        if self.in_table:
+            return '</td></tr>'
+        return ''
+
+    def _td_repl(self, word):
+        if self.in_table:
+            return '</td><td>'
+        return ''
 
     def _indent_level(self):
         return len(self.list_indents) and self.list_indents[-1]
@@ -465,29 +491,42 @@ class PageFormatter:
 
         # For each line, we scan through looking for magic
         # strings, outputting verbatim any intervening text
+        # TODO: highlight search words (look at referer)
         scan_re = re.compile(
             r"(?:"
+            # Formatting
             + r"(?P<emph>'{2,3})"
             + r"|(?P<tit>\={2,6})"
+            + r"|(?P<rule>^-{3,})"
+            + r"|(?P<html><(/|)(div|span|iframe)[^<>]*>)"
             + r"|(?P<ent>[<>&])"
+            + r"|(?P<hi>\b(FIXME|TODO|DONE)\b)"
+
+            # Links
             + r"|(?P<img>\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))"
             + r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)"
-            + r"|(?P<rule>^-{3,})"
-            + r"|(?P<hurl>\[\[\S+\s+.+\]\])"
-            + r"|(?P<url>(http|ftp|nntp|news|mailto)\:[^\s'\"]+\S)"
+            + r"|(?P<hurl>\[\[\S+[^\]]*\]\])"
+            + r"|(?P<url>(http|https|ftp|mailto)\:[^\s'\"]+\S)"
             + r"|(?P<email>[-\w._+]+\@[\w.-]+)"
+
+            # Lists, divs, spans
             + r"|(?P<li>^\s+\*)"
             + r"|(?P<pre>(\{\{\{|\s*\}\}\}))"
             + r"|(?P<var>(\{\{|\}\}))"
-            + r"|(?P<macro>\[\[(TitleSearch|FullSearch|WordIndex|TitleIndex)\]\])"
+
+            # Tables
+            + r"|(?P<tr>^\s*\|\|\s*)"
+            + r"|(?P<tre>\s*\|\|\s*$)"
+            + r"|(?P<td>\s*\|\|\s*)"
             + r")")
         pre_re = re.compile(
             r"(?:"
             + r"(?P<pre>\s*\}\}\})"
             + r")")
-        blank_re = re.compile("^\s*$")
-        indent_re = re.compile("^\s*")
-        eol_re = re.compile(r'\r?\n')
+        blank_re = re.compile(r"^\s*$")
+        indent_re = re.compile(r"^\s*")
+        tr_re = re.compile(r"^\s*\|\|")
+        eol_re = re.compile(r"\r?\n")
         raw = string.expandtabs(self.raw)
         for line in eol_re.split(raw):
             # Skip ACLs
@@ -495,17 +534,23 @@ class PageFormatter:
                 if line.startswith('#'):
                    continue
                 self.in_header = False
+
             if self.in_pre:
                 print re.sub(pre_re, self.replace, line)
             else:
-                # XXX: Should we check these conditions in this order?
+                if self.in_table and not tr_re.match(line):
+                    self.in_table = False
+                    print '</tbody></table><p>'
+
                 if blank_re.match(line):
                     print '</p><p>'
-                    continue
-                indent = indent_re.match(line)
-                print self._indent_to(len(indent.group(0)))
-                print re.sub(scan_re, self.replace, line)
+                else:
+                    indent = indent_re.match(line)
+                    print self._indent_to(len(indent.group(0)))
+                    print re.sub(scan_re, self.replace, line)
+
         if self.in_pre: print '</pre>'
+        if self.in_table: print '</tbody></table><p>'
         print self._undent()
         print "</p></div>"