From b6def457d78db8a9d056251239b4cbfd8c5cfda7 Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 09:46:16 -0800 Subject: [PATCH 1/9] Ignore .idea directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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 From b750e6c207cb62119f547d632a12da1ac470a02e Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 11:02:52 -0800 Subject: [PATCH 2/9] Added headers to wiki table Also indentation --- lib/markdown2.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index 49bedd06..cfb3ee87 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,36 @@ 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'), ''] + hlines = [] + def add_hline(line, indents=0): + hlines.append((self.tab * indents) + line) + add_hline('' % self._html_class_str_from_tag('table')) + create_thead = rows and rows[0] and re.match(r"\s*~", rows[0][0]) + add_hline('<{}>'.format("thead" if create_thead else "tbody"), 1) for row in rows: - hrow = [''] + add_hline('', 2) for cell in row: - hrow.append('') - hrow.append(self._run_span_gamut(cell)) - hrow.append('') - hrow.append('') - hlines.append(''.join(hrow)) - hlines += ['', ''] + cell_type = "td" + cell = cell.strip(" ") + if cell.strip(" ").startswith("~"): + cell = cell[1:].strip(" ") + cell_type = "th" + add_hline('<{0}>{1}'.format(cell_type, self._run_span_gamut(cell)), 3) + add_hline('', 2) + if create_thead: + add_hline('', 1) + add_hline('', 1) + create_thead = False + add_hline('', 1) + add_hline('') return '\n'.join(hlines) + '\n' def _do_wiki_tables(self, text): From f2d187ba1757eb08ff60dc80ad86fc23d53f0dd0 Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 11:03:28 -0800 Subject: [PATCH 3/9] Added header row test --- test/tm-cases/wiki_tables.html | 90 ++++++++++++++++++++++++++++------ test/tm-cases/wiki_tables.tags | 2 +- test/tm-cases/wiki_tables.text | 5 ++ 3 files changed, 80 insertions(+), 17 deletions(-) diff --git a/test/tm-cases/wiki_tables.html b/test/tm-cases/wiki_tables.html index 45d7cc35..f61f109e 100644 --- a/test/tm-cases/wiki_tables.html +++ b/test/tm-cases/wiki_tables.html @@ -1,28 +1,83 @@ - - - - - - + + + + + + + + + + + + + + + + + + + + + + +
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

just one line

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

blockquote

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

-- Dr. Seuss

@@ -31,7 +86,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..ce11b203 100644 --- a/test/tm-cases/wiki_tables.text +++ b/test/tm-cases/wiki_tables.text @@ -3,6 +3,11 @@ || 1910 || -15 || 30 || || 1920 || -10 || 32 || +# With header row + +||~ Name ||~ Class ||~ Race ||~ Level || +|| Vlad || Barbarian || Dragonborn || 12 || +|| Jimbo || Rogue || Halfling || 13 || # just one line From 0a4b5065d175fb9958c197d8104cbc7b1141d64d Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 11:07:14 -0800 Subject: [PATCH 4/9] Alternate spacing test --- test/tm-cases/wiki_tables.html | 27 +++++++++++++++++++++++++++ test/tm-cases/wiki_tables.text | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/test/tm-cases/wiki_tables.html b/test/tm-cases/wiki_tables.html index f61f109e..cd04d795 100644 --- a/test/tm-cases/wiki_tables.html +++ b/test/tm-cases/wiki_tables.html @@ -50,6 +50,33 @@

With header row

+

With header row, alternate spacing

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

just one line

diff --git a/test/tm-cases/wiki_tables.text b/test/tm-cases/wiki_tables.text index ce11b203..e60c5c13 100644 --- a/test/tm-cases/wiki_tables.text +++ b/test/tm-cases/wiki_tables.text @@ -9,6 +9,12 @@ || Vlad || Barbarian || Dragonborn || 12 || || Jimbo || Rogue || Halfling || 13 || +# With header row, alternate spacing + +|| ~Name || ~Class || ~Race || ~Level || +|| Vlad || Barbarian || Dragonborn || 12 || +|| Jimbo || Rogue || Halfling || 13 || + # just one line ||foo||bar||baz|| From 7fc8f480017feac2b98ba5c6ea18f3115011e6d4 Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 16:10:25 -0800 Subject: [PATCH 5/9] Added "tables" extra and added leading pipe to empty cells Empty cells must be completely surrounded by pipes --- test/php-markdown-extra-cases/Tables.html | 2 +- test/php-markdown-extra-cases/Tables.opts | 1 + test/php-markdown-extra-cases/Tables.text | 9 ++++----- 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 test/php-markdown-extra-cases/Tables.opts 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 - From ebd8be8e0c9e6eb5ddec52912f917741da4ad90a Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 16:15:20 -0800 Subject: [PATCH 6/9] Indent and add proper headers to more wiki tables --- test/tm-cases/html_classes.html | 30 ++++++++++++++++++++++++------ test/tm-cases/html_classes.text | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) 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 || From 4f5efc0b98aea66aa5c5a63ecb9a54acf0d0f906 Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 17:03:04 -0800 Subject: [PATCH 7/9] Removed extraneous strip() --- lib/markdown2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index cfb3ee87..e7f4bd08 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -1090,7 +1090,7 @@ def add_hline(line, indents=0): for cell in row: cell_type = "td" cell = cell.strip(" ") - if cell.strip(" ").startswith("~"): + if cell.startswith("~"): cell = cell[1:].strip(" ") cell_type = "th" add_hline('<{0}>{1}'.format(cell_type, self._run_span_gamut(cell)), 3) From c9ba74779ea11b18b5883e9d1e0f1d3d170be5db Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 22:31:36 -0800 Subject: [PATCH 8/9] Cleaned up wiki table logic --- lib/markdown2.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index e7f4bd08..766e1e1a 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -1080,26 +1080,33 @@ def _wiki_table_sub(self, match): # from pprint import pprint # pprint(rows) 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')) - create_thead = rows and rows[0] and re.match(r"\s*~", rows[0][0]) - add_hline('<{}>'.format("thead" if create_thead else "tbody"), 1) - for row in rows: + # 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 row: - cell_type = "td" - cell = cell.strip(" ") - if cell.startswith("~"): - cell = cell[1:].strip(" ") - cell_type = "th" - add_hline('<{0}>{1}'.format(cell_type, self._run_span_gamut(cell)), 3) + for cell in rows[0]: + add_hline("{}".format(format_cell(cell)), 3) add_hline('', 2) - if create_thead: - add_hline('', 1) - add_hline('', 1) - create_thead = False - add_hline('', 1) + 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' From 7f91f7683268024f509b15aec37515ed8b52ee04 Mon Sep 17 00:00:00 2001 From: ryanvilbrandt Date: Thu, 21 Nov 2019 22:31:52 -0800 Subject: [PATCH 9/9] Header-only test --- test/tm-cases/wiki_tables.html | 13 +++++++++++++ test/tm-cases/wiki_tables.text | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/test/tm-cases/wiki_tables.html b/test/tm-cases/wiki_tables.html index cd04d795..64bc1b52 100644 --- a/test/tm-cases/wiki_tables.html +++ b/test/tm-cases/wiki_tables.html @@ -50,6 +50,19 @@

With header row

+

With only header row

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

With header row, alternate spacing

diff --git a/test/tm-cases/wiki_tables.text b/test/tm-cases/wiki_tables.text index e60c5c13..1128d4bf 100644 --- a/test/tm-cases/wiki_tables.text +++ b/test/tm-cases/wiki_tables.text @@ -9,6 +9,10 @@ || 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 ||