Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions commonmark_extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning 2 makes it a leaf node, which in practice means that it doesn't slurp up whitespace after the |, meaning the pipe can be added back simply.

return 0


Expand All @@ -43,7 +44,8 @@ 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 ("", ">", "`"):
container.string_content += "|"
elif not parser.indented and commonmark.blocks.peek(ln, parser.next_nonspace) not in ("", ">", "`", None):
pass
else:
return 1
Expand All @@ -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:
Expand All @@ -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("")

Expand All @@ -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.
Expand All @@ -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() == "":
Expand All @@ -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'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in commit message, this doesn't actually change the Python type of the block, but it does produce the right output on rendering so likely doesn't matter.

return
# The first time, we shift to the tbody.
table_parts.append([])

Expand Down Expand Up @@ -138,6 +151,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.
Expand Down
3 changes: 3 additions & 0 deletions commonmark_extensions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def test_break2(self):
"""| abc | def |
| --- | --- |
| bar | baz |
bar

bar""",

"""<table>
Expand All @@ -141,6 +143,7 @@ def test_break2(self):
</tr>
</tbody>
</table>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This differs slightly from the spec, but doesn't actually change the visual output of the html (as blank lines aren't treated specially).

If you are bothered by this, let me know and I can try work out why the new line appears and whether we can avoid it.

<p>bar</p>""")


Expand Down