Hatta Devel

changeset 621:633ab90042f4

completely overhaul the table parsing to allow links with | in them
author sheep@ghostwheel
date Wed Oct 21 22:22:30 2009 +0200 (2009-10-21)
parents 7158127dbae0
children f66f2f540684
files hatta.py tests/test_parser.txt
line diff
     1.1 --- a/hatta.py	Sun Oct 18 23:55:25 2009 +0200
     1.2 +++ b/hatta.py	Wed Oct 21 22:22:30 2009 +0200
     1.3 @@ -703,6 +703,7 @@
     1.4          "newline": ur"\n",
     1.5          "punct": (ur'(^|\b|(?<=\s))(%s)((?=[\s.,:;!?)/&=+])|\b|$)' %
     1.6                    ur"|".join(re.escape(k) for k in punct)),
     1.7 +        "table": ur"=?\|=?",
     1.8          "text": ur".+?",
     1.9      } # note that the priority is alphabetical
    1.10  
    1.11 @@ -815,6 +816,9 @@
    1.12  
    1.13  # methods for the markup inside lines:
    1.14  
    1.15 +    def _line_table(self, groups):
    1.16 +        return groups["table"]
    1.17 +
    1.18      def _line_linebreak(self, groups):
    1.19          return u'<br>'
    1.20  
    1.21 @@ -959,12 +963,34 @@
    1.22                  in_head = False
    1.23                  yield '</thead>'
    1.24              yield '<tr>'
    1.25 -            for cell in table_row.strip('|').split('|'):
    1.26 -                if cell.startswith('='):
    1.27 -                    head = cell.strip('=')
    1.28 -                    yield '<th>%s</th>' % u"".join(self.parse_line(head))
    1.29 +            in_cell = False
    1.30 +            in_th = False
    1.31 +
    1.32 +            for part in self.parse_line(table_row):
    1.33 +                if part in ('=|', '|', '=|=', '|='):
    1.34 +                    if in_cell:
    1.35 +                        if in_th:
    1.36 +                            yield '</th>'
    1.37 +                        else:
    1.38 +                            yield '</td>'
    1.39 +                        in_cell = False
    1.40 +                    if part in ('=|=', '|='):
    1.41 +                        in_th = True
    1.42 +                    else:
    1.43 +                        in_th = False
    1.44                  else:
    1.45 -                    yield '<td>%s</td>' % u"".join(self.parse_line(cell))
    1.46 +                    if not in_cell:
    1.47 +                        if in_th:
    1.48 +                            yield '<th>'
    1.49 +                        else:
    1.50 +                            yield '<td>'
    1.51 +                        in_cell = True
    1.52 +                    yield part
    1.53 +            if in_cell:
    1.54 +                if in_th:
    1.55 +                    yield '</th>'
    1.56 +                else:
    1.57 +                    yield '</td>'
    1.58              yield '</tr>'
    1.59          yield u'</table>'
    1.60  
     2.1 --- a/tests/test_parser.txt	Sun Oct 18 23:55:25 2009 +0200
     2.2 +++ b/tests/test_parser.txt	Wed Oct 21 22:22:30 2009 +0200
     2.3 @@ -175,3 +175,23 @@
     2.4  >>> parse(u'{{{#!c++\nint eger;\n}}}')
     2.5  <div class="highlight"><pre><div id="line_1"><span class="kt">int</span> <span class="n">eger</span><span class="p">;</span></div></pre></div>
     2.6  
     2.7 +>>> parse(u'|table|')
     2.8 +<table id="line_0"><tr><td>table</td></tr></table>
     2.9 +
    2.10 +>>> parse(u'|table| cell |')
    2.11 +<table id="line_0"><tr><td>table</td><td> cell </td></tr></table>
    2.12 +
    2.13 +>>> parse(u'|table|=head=|')
    2.14 +<table id="line_0"><tr><td>table</td><th>head</th></tr></table>
    2.15 +
    2.16 +>>> parse(u'|=table=|=head=|')
    2.17 +<table id="line_0"><thead><tr><th>table</th><th>head</th></tr></thead></table>
    2.18 +
    2.19 +>>> parse(u'|table|[[link|link]]|')
    2.20 +<table id="line_0"><tr><td>table</td><td><a href="link">link</a></td></tr></table>
    2.21 +
    2.22 +>>> parse(u'|table|{{img|img}}|')
    2.23 +<table id="line_0"><tr><td>table</td><td><img src="img" alt="img"></td></tr></table>
    2.24 +
    2.25 +>>> parse(u'|table|{{{code|code}}}|')
    2.26 +<table id="line_0"><tr><td>table</td><td><code>code|code</code></td></tr></table>