Skip to content

Commit 7f4db16

Browse files
Merge pull request #421 from tatuylonen/table-caption-attributes-bug
Fix bug in to_wikitext() with caption attributes
2 parents 9380997 + 81db751 commit 7f4db16

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/wikitextprocessor/node_expand.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ def recurse(node: Union[GeneralNode, WikiNodeListArgs]) -> str:
139139
parts.append(recurse(node.children))
140140
parts.append("\n|}\n")
141141
elif kind == NodeKind.TABLE_CAPTION:
142-
parts.append("\n|+ {}\n".format(to_attrs(node)))
142+
if tc_attrs := to_attrs(node):
143+
parts.append("\n|+ {} |\n".format(tc_attrs))
144+
else:
145+
parts.append("\n|+\n")
143146
parts.append(recurse(node.children))
144147
elif kind == NodeKind.TABLE_ROW:
145148
parts.append("\n|- {}\n".format(to_attrs(node)))

src/wikitextprocessor/parser.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,8 @@ def table_start_fn(ctx: "Wtp", token: str) -> None:
14141414

14151415
# something=other, something="other", something = 'other'
14161416
attr_assignment_pair = (
1417-
r"""\s*[^"'>/=\0-\037\s]+""" r"""\s*=\s*("[^"]*"|'[^']*'|[^"'<>`\s]+)"""
1417+
r"""\s*[^"'>/=\0-\037\s]+"""
1418+
r"""\s*=\s*("[^"]*"|'[^']*'|[^"'<>`\s]+)"""
14181419
)
14191420

14201421
attr_assignments_re = re.compile(
@@ -1428,7 +1429,7 @@ def check_for_attributes(ctx: "Wtp", node: WikiNode) -> tuple[bool, str]:
14281429

14291430
# Old behavior added here to return earlier without needing
14301431
# to use regex matching; if the old version worked, why not?
1431-
# If this fail, then resort to the reverse parsing + regex.
1432+
# If this fails, then resort to the reverse parsing + regex.
14321433
_parser_merge_str_children(ctx)
14331434
if len(node.children) == 1 and isinstance(node.children[0], str):
14341435
ret = node.children.pop()
@@ -1623,7 +1624,10 @@ def table_cell_fn(ctx: "Wtp", token: str) -> None:
16231624
if len(node.children) == 1 and isinstance(
16241625
attrs := node.children[0], str
16251626
):
1626-
node.children.pop()
1627+
# At this point of parsing, we're just behind the start
1628+
# of one of the above node types; if they are followed
1629+
# by a `|`, that means the first child is an attr section
1630+
node.children.pop(0)
16271631
# Using the walrus operator and pop()ing without return
16281632
# is just to make the type-checker happy without using
16291633
# an assert that attrs is definitely a str...

tests/test_node_expand.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ def test_table2(self):
154154
self.backcvt('{| class="x"\n|}', '\n{| class="x"\n\n|}\n')
155155

156156
def test_tablecaption1(self):
157-
self.backcvt("{|\n|+\ncapt\n|}", "\n{| \n\n|+ \n\ncapt\n\n|}\n")
157+
self.backcvt("{|\n|+\ncapt\n|}", "\n{| \n\n|+\n\ncapt\n\n|}\n")
158+
159+
def test_tablecaption2(self):
160+
self.backcvt(
161+
"{|\n |+ class='foo' |\ncapt\n|}",
162+
'\n{| \n\n|+ class="foo" |\n\ncapt\n\n|}\n',
163+
)
158164

159165
def test_tablerowcell1(self):
160166
self.backcvt(

0 commit comments

Comments
 (0)