diff options
author | Artem Yunusov <nedrlab@gmail.com> | 2008-08-12 16:25:16 +0500 |
---|---|---|
committer | Artem Yunusov <nedrlab@gmail.com> | 2008-08-12 16:25:16 +0500 |
commit | 90c4d5b13b231b34841aab698ca5499a30213f61 (patch) | |
tree | 64b383c2a8732ab60ab33ae79a98273a229dc580 /markdown_extensions/tables.py | |
parent | 2215f99b0c5d82b3d53306f762a6135adeade384 (diff) | |
parent | 57efe86c611f9347d0259b2dbbab26d79385a58c (diff) | |
download | markdown-90c4d5b13b231b34841aab698ca5499a30213f61.tar.gz markdown-90c4d5b13b231b34841aab698ca5499a30213f61.tar.bz2 markdown-90c4d5b13b231b34841aab698ca5499a30213f61.zip |
Merge git@gitorious.org:python-markdown/mainline
Diffstat (limited to 'markdown_extensions/tables.py')
-rw-r--r-- | markdown_extensions/tables.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/markdown_extensions/tables.py b/markdown_extensions/tables.py new file mode 100644 index 0000000..829044c --- /dev/null +++ b/markdown_extensions/tables.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +""" +Table extension for Python-Markdown +""" + +import markdown +from markdown import etree + +class TablePattern(markdown.Pattern) : + def __init__ (self, md): + markdown.Pattern.__init__(self, r'(^|\n)\|([^\n]*)\|') + self.md = md + + def handleMatch(self, m): + + # a single line represents a row + tr = etree.Element('tr') + + # chunks between pipes represent cells + + for t in m.group(3).split('|'): + + if len(t) >= 2 and t.startswith('*') and t.endswith('*'): + # if a cell is bounded by asterisks, it is a <th> + td = etree.Element('th') + t = t[1:-1] + else: + # otherwise it is a <td> + td = etree.Element('td') + + # add text ot inline section, later it will be + # processed by core + inline = etree.SubElement(td, "inline") + inline.text = t + + tr.append(td) + tr.tail = "\n" + + return tr + + +class TablePostprocessor: + + def _findElement(self, element, name): + result = [] + for child in element: + if child.tag == name: + result.append(child) + result += self._findElement(child, name) + return result + + def run(self, root): + + for element in self._findElement(root, "p"): + for child in element: + if child.tail: + element.tag = "table" + break + + + + +class TableExtension(markdown.Extension): + def extendMarkdown(self, md, md_globals): + md.inlinePatterns.insert(0, TablePattern(md)) + md.postprocessors.append(TablePostprocessor()) + + +def makeExtension(configs): + return TableExtension(configs) + |