| OLD | NEW |
| (Empty) |
| 1 """ | |
| 2 Tables Extension for Python-Markdown | |
| 3 ==================================== | |
| 4 | |
| 5 Added parsing of tables to Python-Markdown. | |
| 6 | |
| 7 A simple example: | |
| 8 | |
| 9 First Header | Second Header | |
| 10 ------------- | ------------- | |
| 11 Content Cell | Content Cell | |
| 12 Content Cell | Content Cell | |
| 13 | |
| 14 Copyright 2009 - [Waylan Limberg](http://achinghead.com) | |
| 15 """ | |
| 16 | |
| 17 from __future__ import absolute_import | |
| 18 from __future__ import unicode_literals | |
| 19 from . import Extension | |
| 20 from ..blockprocessors import BlockProcessor | |
| 21 from ..util import etree | |
| 22 | |
| 23 class TableProcessor(BlockProcessor): | |
| 24 """ Process Tables. """ | |
| 25 | |
| 26 def test(self, parent, block): | |
| 27 rows = block.split('\n') | |
| 28 return (len(rows) > 2 and '|' in rows[0] and | |
| 29 '|' in rows[1] and '-' in rows[1] and | |
| 30 rows[1].strip()[0] in ['|', ':', '-']) | |
| 31 | |
| 32 def run(self, parent, blocks): | |
| 33 """ Parse a table block and build table. """ | |
| 34 block = blocks.pop(0).split('\n') | |
| 35 header = block[0].strip() | |
| 36 seperator = block[1].strip() | |
| 37 rows = block[2:] | |
| 38 # Get format type (bordered by pipes or not) | |
| 39 border = False | |
| 40 if header.startswith('|'): | |
| 41 border = True | |
| 42 # Get alignment of columns | |
| 43 align = [] | |
| 44 for c in self._split_row(seperator, border): | |
| 45 if c.startswith(':') and c.endswith(':'): | |
| 46 align.append('center') | |
| 47 elif c.startswith(':'): | |
| 48 align.append('left') | |
| 49 elif c.endswith(':'): | |
| 50 align.append('right') | |
| 51 else: | |
| 52 align.append(None) | |
| 53 # Build table | |
| 54 table = etree.SubElement(parent, 'table') | |
| 55 thead = etree.SubElement(table, 'thead') | |
| 56 self._build_row(header, thead, align, border) | |
| 57 tbody = etree.SubElement(table, 'tbody') | |
| 58 for row in rows: | |
| 59 self._build_row(row.strip(), tbody, align, border) | |
| 60 | |
| 61 def _build_row(self, row, parent, align, border): | |
| 62 """ Given a row of text, build table cells. """ | |
| 63 tr = etree.SubElement(parent, 'tr') | |
| 64 tag = 'td' | |
| 65 if parent.tag == 'thead': | |
| 66 tag = 'th' | |
| 67 cells = self._split_row(row, border) | |
| 68 # We use align here rather than cells to ensure every row | |
| 69 # contains the same number of columns. | |
| 70 for i, a in enumerate(align): | |
| 71 c = etree.SubElement(tr, tag) | |
| 72 try: | |
| 73 c.text = cells[i].strip() | |
| 74 except IndexError: | |
| 75 c.text = "" | |
| 76 if a: | |
| 77 c.set('align', a) | |
| 78 | |
| 79 def _split_row(self, row, border): | |
| 80 """ split a row of text into list of cells. """ | |
| 81 if border: | |
| 82 if row.startswith('|'): | |
| 83 row = row[1:] | |
| 84 if row.endswith('|'): | |
| 85 row = row[:-1] | |
| 86 return row.split('|') | |
| 87 | |
| 88 | |
| 89 class TableExtension(Extension): | |
| 90 """ Add tables to Markdown. """ | |
| 91 | |
| 92 def extendMarkdown(self, md, md_globals): | |
| 93 """ Add an instance of TableProcessor to BlockParser. """ | |
| 94 md.parser.blockprocessors.add('table', | |
| 95 TableProcessor(md.parser), | |
| 96 '<hashheader') | |
| 97 | |
| 98 | |
| 99 def makeExtension(configs={}): | |
| 100 return TableExtension(configs=configs) | |
| OLD | NEW |