diff options
author | JensDiemer <git@jensdiemer.de> | 2011-08-03 16:17:16 +0200 |
---|---|---|
committer | JensDiemer <git@jensdiemer.de> | 2011-08-03 16:17:16 +0200 |
commit | e2f3007e3f5645c220dab6f0a8c96a2ff22c13da (patch) | |
tree | 6303db2599919085cce6b614038e99c60c8ffd83 /creole/shared | |
parent | 2e117fa3f23f1163f98362a3d6ca9bf0fcd221bb (diff) | |
download | creole-e2f3007e3f5645c220dab6f0a8c96a2ff22c13da.tar.gz |
NEW: Add a html2reStructuredText converter
Diffstat (limited to 'creole/shared')
-rw-r--r-- | creole/shared/markup_table.py | 85 | ||||
-rw-r--r-- | creole/shared/rest.py | 112 |
2 files changed, 156 insertions, 41 deletions
diff --git a/creole/shared/markup_table.py b/creole/shared/markup_table.py index 476047d..abda575 100644 --- a/creole/shared/markup_table.py +++ b/creole/shared/markup_table.py @@ -3,30 +3,6 @@ class MarkupTable(object): """ Container for holding table data and render the data in creole markup. Format every cell width to the same col width. - - >>> def debug_msg(*args): pass - >>> t = MarkupTable(head_prefix="* ", debug_msg=debug_msg) - >>> t.add_tr() - >>> t.add_th(u"head1") - >>> t.add_th(u"head2") - >>> t.add_tr() - >>> t.add_td(u"1.1.") - >>> t.add_td(u"1.2.") - >>> t.add_tr() - >>> t.add_td(u"2.1.") - >>> t.add_td(u"2.2.") - >>> t.get_table_markup().splitlines() - [u'|* head1 |* head2 |', u'| 1.1. | 1.2. |', u'| 2.1. | 2.2. |'] - - >>> t = MarkupTable(head_prefix="_. ", auto_width=False, debug_msg=debug_msg) - >>> t.add_tr() - >>> t.add_th(u"head1") - >>> t.add_th(u"head2") - >>> t.add_tr() - >>> t.add_td(u"1.1.") - >>> t.add_td(u"1.2.") - >>> t.get_table_markup().splitlines() - [u'|_. head1|_. head2|', u'|1.1.|1.2.|'] """ def __init__(self, head_prefix="= ", auto_width=True, debug_msg=None): self.head_prefix = head_prefix @@ -58,30 +34,34 @@ class MarkupTable(object): self.debug_msg("Table.add_td", text) self.rows[self.row_index].append(text) + def _get_preformat_info(self): + cells = [] + for row in self.rows: + line_cells = [] + for cell in row: + cell = cell.strip() + if cell != "": + if self.head_prefix and cell.startswith(self.head_prefix): + cell += " " # Headline + else: + cell = " %s " % cell # normal cell + line_cells.append(cell) + cells.append(line_cells) + + # Build a list of max len for every column + widths = [max(map(len, col)) for col in zip(*cells)] + + return cells, widths + def get_table_markup(self): - """ return the table data in creole markup. """ + """ return the table data in creole/textile markup. """ if not self.auto_width: - # lines = [] for row in self.rows: lines.append("|" + "|".join([cell for cell in row]) + "|") else: # preformat every table cell - cells = [] - for row in self.rows: - line_cells = [] - for cell in row: - cell = cell.strip() - if cell != "": - if cell.startswith(self.head_prefix): - cell += " " # Headline - else: - cell = " %s " % cell # normal cell - line_cells.append(cell) - cells.append(line_cells) - - # Build a list of max len for every column - widths = [max(map(len, col)) for col in zip(*cells)] + cells, widths = self._get_preformat_info() # Join every line with ljust lines = [] @@ -94,6 +74,29 @@ class MarkupTable(object): self.debug_msg("Table.get_table_markup", result) return result + def get_rest_table(self): + """ return the table data in ReSt markup. """ + # preformat every table cell + cells, widths = self._get_preformat_info() + + separator_line = "+%s+" % "+".join(["-"*width for width in widths]) + headline_separator = "+%s+" % "+".join(["="*width for width in widths]) + + lines = [] + for no, row in enumerate(cells): + if no == 1: + lines.append(headline_separator) + else: + lines.append(separator_line) + + # Join every line with ljust + cells = [cell.ljust(width) for cell, width in zip(row, widths)] + lines.append("|" + "|".join(cells) + "|") + + lines.append(separator_line) + + return "\n".join(lines) + if __name__ == '__main__': import doctest print doctest.testmod() diff --git a/creole/shared/rest.py b/creole/shared/rest.py new file mode 100644 index 0000000..15c7d0b --- /dev/null +++ b/creole/shared/rest.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python +# coding: utf-8 + +""" + Some code stolen from: + http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html +""" + + +import warnings + + +try: + from docutils.core import publish_parts +except ImportError: + REST_INSTALLED = False + warnings.warn( + "Markup error: 'Python Documentation Utilities' isn't installed. Can't test reStructuredText." + " Download: http://pypi.python.org/pypi/docutils" + ) +else: + REST_INSTALLED = True + + +from docutils.writers import html4css1 + + +DEBUG = False +#DEBUG = True + +IGNORE_ATTR = ( + "class", +) + + +class CleanHTMLWriter(html4css1.Writer): + """ + This docutils writer will use the CleanHTMLTranslator class below. + """ + def __init__(self): + html4css1.Writer.__init__(self) + self.translator_class = CleanHTMLTranslator + +class CleanHTMLTranslator(html4css1.HTMLTranslator): + """ + This is a translator class for the docutils system. + It will produce a minimal set of html output. + (No extry divs, classes oder ids.) + """ + def starttag(self, node, tagname, suffix='\n', empty=0, **attributes): + if DEBUG: + print "ids: %r" % getattr(node, "ids", "-") + node.ids = [] + if DEBUG: + print "attributes: %r" % attributes + for attr in IGNORE_ATTR: + for attr2 in (attr, attr.lower(), attr.upper()): + if attr2 in attributes: + del(attributes[attr2]) + + return html4css1.HTMLTranslator.starttag(self, node, tagname, suffix, empty, **attributes) + + def visit_section(self, node): + self.section_level += 1 + + def depart_section(self, node): + self.section_level -= 1 + + #__________________________________________________________________________ + # Clean table: + + def do_nothing(self, node): + pass + visit_thead = do_nothing + depart_thead = do_nothing + visit_tbody = do_nothing + depart_tbody = do_nothing + + def visit_table(self, node): + self.body.append(self.starttag(node, 'table')) + + def visit_tgroup(self, node): + node.stubs = [] + +def rest2html(content): + """ + >>> rest2html(u"- bullet list") + u'<ul>\\n<li>bullet list</li>\\n</ul>\\n' + """ + parts = publish_parts( + source=content, +# writer_name="html4css1", + writer=CleanHTMLWriter(), + settings_overrides={ + "input_encoding": "unicode", + "doctitle_xform": False, + }, + ) + return parts["fragment"] + + +if __name__ == '__main__': + import doctest + print doctest.testmod() + + print rest2html(u""" ++------------+------------+ +| Headline 1 | Headline 2 | ++============+============+ +| cell one | cell two | ++------------+------------+ + """) |