diff options
Diffstat (limited to 'mdx_tables.py')
-rwxr-xr-x[-rw-r--r--] | mdx_tables.py | 102 |
1 files changed, 57 insertions, 45 deletions
diff --git a/mdx_tables.py b/mdx_tables.py index c5c84a4..5c7881a 100644..100755 --- a/mdx_tables.py +++ b/mdx_tables.py @@ -5,61 +5,73 @@ 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 __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 - def handleMatch(self, m, doc) : - # a single line represents a row - tr = doc.createElement('tr') - tr.appendChild(doc.createTextNode('\n')) - # chunks between pipes represent cells - for t in m.group(2).split('|'): - if len(t) >= 2 and t.startswith('*') and t.endswith('*'): - # if a cell is bounded by asterisks, it is a <th> - td = doc.createElement('th') - t = t[1:-1] - else: - # otherwise it is a <td> - td = doc.createElement('td') - # apply inline patterns on chunks - for n in self.md._handleInline(t): - if(type(n) == unicode): - td.appendChild(doc.createTextNode(n)) - else: - td.appendChild(n) - tr.appendChild(td) - # very long lines are evil - tr.appendChild(doc.createTextNode('\n')) - return tr + 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') + + # apply inline patterns on chunks + '''for n in self.md._handleInline(t): + if(type(n) == unicode): + td.text = n + else: + td.appendChild(n)''' + inline = etree.SubElement(td, "inline") + inline.text = t + + tr.append(td) + tr.tail = "\n" + + #print etree.tostring(tr) + return tr class TablePostprocessor: - def run(self, doc): - # markdown wrapped our <tr>s in a <p>, we fix that here - def test_for_p(element): - return element.type == 'element' and element.nodeName == 'p' - # replace "p > tr" with "table > tr" - for element in doc.find(test_for_p): - for node in element.childNodes: - if(node.type == 'text' and node.value.strip() == ''): - # skip leading whitespace - continue - if (node.type == 'element' and node.nodeName == 'tr'): - element.nodeName = 'table' - break + + 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 extendMarkdown(self, md, md_globals): + md.inlinePatterns.insert(0, TablePattern(md)) + md.postprocessors.append(TablePostprocessor()) def makeExtension(configs): - return TableExtension(configs) - + return TableExtension(configs) |