diff --git a/.gitignore b/.gitignore index 722351f9..f02ff09f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ sandbox/*.html __pycache__ .tox *.egg-info +*.idea diff --git a/lib/markdown2.py b/lib/markdown2.py index 49bedd06..766e1e1a 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -217,6 +217,7 @@ def __init__(self, html4tags=False, tab_width=4, safe_mode=None, else: self.empty_element_suffix = " />" self.tab_width = tab_width + self.tab = tab_width * " " # For compatibility with earlier markdown2.py and with # markdown.py's safe_mode being a boolean, @@ -1070,23 +1071,43 @@ def _do_tables(self, text): def _wiki_table_sub(self, match): ttext = match.group(0).strip() - # print 'wiki table: %r' % match.group(0) + # print('wiki table: %r' % match.group(0)) rows = [] for line in ttext.splitlines(0): line = line.strip()[2:-2].strip() row = [c.strip() for c in re.split(r'(?' % self._html_class_str_from_tag('table'), ''] - for row in rows: - hrow = [''] - for cell in row: - hrow.append('') - hrow.append(self._run_span_gamut(cell)) - hrow.append('') - hrow.append('') - hlines.append(''.join(hrow)) - hlines += ['', ''] + hlines = [] + + def add_hline(line, indents=0): + hlines.append((self.tab * indents) + line) + + def format_cell(text): + return self._run_span_gamut(re.sub(r"^\s*~", "", cell).strip(" ")) + + add_hline('' % self._html_class_str_from_tag('table')) + # Check if first cell of first row is a header cell. If so, assume the whole row is a header row. + if rows and rows[0] and re.match(r"^\s*~", rows[0][0]): + add_hline('', 1) + add_hline('', 2) + for cell in rows[0]: + add_hline("{}".format(format_cell(cell)), 3) + add_hline('', 2) + add_hline('', 1) + # Only one header row allowed. + rows = rows[1:] + # If no more rows, don't create a tbody. + if rows: + add_hline('', 1) + for row in rows: + add_hline('', 2) + for cell in row: + add_hline('{}'.format(format_cell(cell)), 3) + add_hline('', 2) + add_hline('', 1) + add_hline('') return '\n'.join(hlines) + '\n' def _do_wiki_tables(self, text): diff --git a/test/php-markdown-extra-cases/Tables.html b/test/php-markdown-extra-cases/Tables.html index e36286c8..408693ec 100644 --- a/test/php-markdown-extra-cases/Tables.html +++ b/test/php-markdown-extra-cases/Tables.html @@ -307,4 +307,4 @@

Missing tailing pipe

Cell - \ No newline at end of file + diff --git a/test/php-markdown-extra-cases/Tables.opts b/test/php-markdown-extra-cases/Tables.opts new file mode 100644 index 00000000..23983798 --- /dev/null +++ b/test/php-markdown-extra-cases/Tables.opts @@ -0,0 +1 @@ +{"extras": ["tables"]} diff --git a/test/php-markdown-extra-cases/Tables.text b/test/php-markdown-extra-cases/Tables.text index 71b93ca6..3deb3a69 100644 --- a/test/php-markdown-extra-cases/Tables.text +++ b/test/php-markdown-extra-cases/Tables.text @@ -73,10 +73,10 @@ Table alignement (alternate spacing): | A | B | | C | | -Header 1 | Header 2 ---------- | --------- -A | B - | D +| Header 1 | Header 2 +| --------- | --------- +| A | B +| | D * * * @@ -101,4 +101,3 @@ Header 1 | Header 2 | --------- | --------- | Cell | Cell | Cell | Cell - diff --git a/test/tm-cases/html_classes.html b/test/tm-cases/html_classes.html index e5d4bc00..f40aeda0 100644 --- a/test/tm-cases/html_classes.html +++ b/test/tm-cases/html_classes.html @@ -18,12 +18,30 @@ - - - - - - + + + + + + + + + + + + + + + + + + + + + + + +
YearTemperature (low)Temperature (high)
1900-1025
1910-1530
1920-1032
YearTemperature (low)Temperature (high)
1900-1025
1910-1530
1920-1032

For example:

diff --git a/test/tm-cases/html_classes.text b/test/tm-cases/html_classes.text index 61972c80..190b037d 100644 --- a/test/tm-cases/html_classes.text +++ b/test/tm-cases/html_classes.text @@ -3,7 +3,7 @@ | `Cell 1` | [Cell 2](http://example.com) link | | Cell 3 | **Cell 4** | -|| *Year* || *Temperature (low)* || *Temperature (high)* || +||~ Year ||~ Temperature (low) ||~ Temperature (high) || || 1900 || -10 || 25 || || 1910 || -15 || 30 || || 1920 || -10 || 32 || diff --git a/test/tm-cases/wiki_tables.html b/test/tm-cases/wiki_tables.html index 45d7cc35..64bc1b52 100644 --- a/test/tm-cases/wiki_tables.html +++ b/test/tm-cases/wiki_tables.html @@ -1,28 +1,123 @@ - - - - - - + + + + + + + + + + + + + + + + + + + + + + +
YearTemperature (low)Temperature (high)
1900-1025
1910-1530
1920-1032
YearTemperature (low)Temperature (high)
1900-1025
1910-1530
1920-1032
+ +

With header row

+ + + + + + + + + + + + + + + + + + + + + + + + +
NameClassRaceLevel
VladBarbarianDragonborn12
JimboRogueHalfling13
+ +

With only header row

+ + + + + + + + + + +
NameClassRaceLevel
+ +

With header row, alternate spacing

+ + + + + + + + + + + + + + + + + + + + + + + +
NameClassRaceLevel
VladBarbarianDragonborn12
JimboRogueHalfling13

just one line

- - - + + + + + + +
foobarbaz
foobarbaz

blockquote

- - - - + + + + + + + + + + + +
grinchstolexmas
greeneggsham
grinchstolexmas
greeneggsham

-- Dr. Seuss

@@ -31,7 +126,10 @@

blockquote

end of file

- - - + + + + + +
ResourceNotFoundIf :login does not exist
ResourceNotFoundIf :login does not exist
diff --git a/test/tm-cases/wiki_tables.tags b/test/tm-cases/wiki_tables.tags index b333de63..362500d5 100644 --- a/test/tm-cases/wiki_tables.tags +++ b/test/tm-cases/wiki_tables.tags @@ -1 +1 @@ -extra wiki-tables issue66 +extra wiki-tables diff --git a/test/tm-cases/wiki_tables.text b/test/tm-cases/wiki_tables.text index a707804b..1128d4bf 100644 --- a/test/tm-cases/wiki_tables.text +++ b/test/tm-cases/wiki_tables.text @@ -3,6 +3,21 @@ || 1910 || -15 || 30 || || 1920 || -10 || 32 || +# With header row + +||~ Name ||~ Class ||~ Race ||~ Level || +|| Vlad || Barbarian || Dragonborn || 12 || +|| Jimbo || Rogue || Halfling || 13 || + +# With only header row + +||~ Name ||~ Class ||~ Race ||~ Level || + +# With header row, alternate spacing + +|| ~Name || ~Class || ~Race || ~Level || +|| Vlad || Barbarian || Dragonborn || 12 || +|| Jimbo || Rogue || Halfling || 13 || # just one line