From d5b884540b4e5a9b994ea12c07d4a5a7fabb69f9 Mon Sep 17 00:00:00 2001 From: Hugh Davenport Date: Thu, 26 Nov 2020 14:11:05 +1300 Subject: [PATCH 1/3] Fix test test_break2 Also fixes #3. Checks whether the next nonspace is None which means that it is a blank line. Alters the test case to fit in with the Google Flavoured Markdown (GFM) spec. --- commonmark_extensions/tables.py | 2 +- commonmark_extensions/tests.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/commonmark_extensions/tables.py b/commonmark_extensions/tables.py index 04ea95a..4e5ac69 100644 --- a/commonmark_extensions/tables.py +++ b/commonmark_extensions/tables.py @@ -43,7 +43,7 @@ def continue_(parser=None, container=None): if not parser.indented and commonmark.blocks.peek(ln, parser.next_nonspace) == "|": parser.advance_next_nonspace() parser.advance_offset(1, False) - elif not parser.indented and commonmark.blocks.peek(ln, parser.next_nonspace) not in ("", ">", "`"): + elif not parser.indented and commonmark.blocks.peek(ln, parser.next_nonspace) not in ("", ">", "`", None): pass else: return 1 diff --git a/commonmark_extensions/tests.py b/commonmark_extensions/tests.py index f9df165..fb719be 100644 --- a/commonmark_extensions/tests.py +++ b/commonmark_extensions/tests.py @@ -121,6 +121,8 @@ def test_break2(self): """| abc | def | | --- | --- | | bar | baz | +bar + bar""", """ @@ -141,6 +143,7 @@ def test_break2(self):
+

bar

""") From 1e8df98b2358b3e96d910c6abf0083e03652f8e0 Mon Sep 17 00:00:00 2001 From: Hugh Davenport Date: Thu, 26 Nov 2020 14:53:01 +1300 Subject: [PATCH 2/3] Fix test_cols_need_not_match Removes extraneous cells if more columns than header row. --- commonmark_extensions/tables.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/commonmark_extensions/tables.py b/commonmark_extensions/tables.py index 4e5ac69..9a5ea68 100644 --- a/commonmark_extensions/tables.py +++ b/commonmark_extensions/tables.py @@ -138,6 +138,10 @@ def finalize(parser=None, block=None): # Fill in empty rows if fewer than the header. if len(table_parts) > 1 and len(table_parts[0][0]) > len(table_parts[-1][-1]): table_parts[-1][-1].extend( ["" for _ in range(len(table_parts[0][0]) - len(table_parts[-1][-1])) ] ) + + # Remove excess cells if more than number of columns + if len(table_parts) > 1 and len(table_parts[0][0]) < len(table_parts[-1][-1]): + table_parts[-1][-1] = table_parts[-1][-1][:len(table_parts[0][0])] else: # Multline mode. Merge this row with the previous one. From 3eba8b09cb912c48e60d720f0ce2716c5a48b960 Mon Sep 17 00:00:00 2001 From: Hugh Davenport Date: Thu, 26 Nov 2020 15:02:53 +1300 Subject: [PATCH 3/3] Fix text_header_cells_count_matches_delimiter_row This makes a few changes - The `table` function in `BlockStarts` returns 2 now - Returning 2 now sets `matched_leaf = True` in `commonmark`, which a table is a leaf node in markdown - I found this meant that it didn't slurp up the whitespace on the first line, yet still passed all the tests. - Add the pipe symbol back to the start of each line after slurping in `node.string_content` - In the `finalize` function, ignore the first pipe on the line (keeping the same functionality of before when it got slurped) - Also in `finalize`, if a mix-match between the delimiter row and the header row is found, set the node type to `paragraph` N.B. This doesn't change the underlying python object type, just sets `block.t = 'paragraph` which appears to work on rendering --- commonmark_extensions/tables.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/commonmark_extensions/tables.py b/commonmark_extensions/tables.py index 9a5ea68..5816e7d 100644 --- a/commonmark_extensions/tables.py +++ b/commonmark_extensions/tables.py @@ -25,7 +25,8 @@ def table(parser, container): parser.advance_offset(1, False) parser.close_unmatched_blocks() parser.add_child('table', parser.next_nonspace) - return 1 + parser.tip.string_content = '|' + return 2 return 0 @@ -43,6 +44,7 @@ def continue_(parser=None, container=None): if not parser.indented and commonmark.blocks.peek(ln, parser.next_nonspace) == "|": parser.advance_next_nonspace() parser.advance_offset(1, False) + container.string_content += "|" elif not parser.indented and commonmark.blocks.peek(ln, parser.next_nonspace) not in ("", ">", "`", None): pass else: @@ -60,6 +62,7 @@ def finalize(parser=None, block=None): table = [[""]] escape = False newrowbars = False + ignore_pipe = True for c in block.string_content.rstrip(): # \-escaping if escape: @@ -71,6 +74,9 @@ def finalize(parser=None, block=None): # New cell is begun by a bar. Right-strip the cell we're # ending. elif c == "|": + if ignore_pipe: + ignore_pipe = False + continue table[-1][-1] = table[-1][-1].rstrip() table[-1].append("") @@ -80,6 +86,8 @@ def finalize(parser=None, block=None): elif c == "\n": if table[-1][-1].strip() == "": table[-1].pop(-1) table.append([""]) + ignore_pipe = True + continue # Ignore space at start of cell. An escaped space # can force a space. @@ -90,6 +98,8 @@ def finalize(parser=None, block=None): else: table[-1][-1] += c + ignore_pipe = False + # Remove the last cell if it's empty since it's caused # by the final pipe at the end of the last line. if table[-1][-1].strip() == "": @@ -107,6 +117,9 @@ def finalize(parser=None, block=None): if len(list(filter(lambda cell : not re.match(r"[-=:]+$", cell), row))) == 0: # This row has cells of just dahses. if len(table_parts) == 1: + if len(row) != len(table_parts[0][0]): + block.t = 'paragraph' + return # The first time, we shift to the tbody. table_parts.append([])