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

Fixed lists inside tables when include_tables=True #534

Merged
merged 6 commits into from
Apr 11, 2024
Merged
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
12 changes: 10 additions & 2 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,13 +1038,21 @@ def test_table_processing():
</td>
</tr></table>
""")
processed_table = handle_table(table_with_list, TAG_CATALOG, options)
processed_table = handle_table(copy(table_with_list), TAG_CATALOG, options)
result = [
(el.tag, el.text) if el.text is not None and el.text.strip() else el.tag
for el in processed_table.iter()
]
# assert result == ["table", "row", "cell", ("p", "a list"), "list", ("item", "one"), ("item", "two"),]
assert result == ['table', 'row', 'cell', ('p', 'a list'), 'list']

options.recall = True
processed_table = handle_table(copy(table_with_list), TAG_CATALOG, options)
result = [
(el.tag, el.text) if el.text is not None and el.text.strip() else el.tag
for el in processed_table.iter()
]
assert result == ["table", "row", "cell", ("p", "a list"), 'list', ("item", "one"), ("item", "two"),]

broken_table = html.fromstring("<table><td>cell1</td><tr><td>cell2</td></tr></table>")
processed_table = handle_table(broken_table, TAG_CATALOG, options)
result = [el.tag for el in processed_table.iter()]
Expand Down
5 changes: 5 additions & 0 deletions trafilatura/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ def handle_table(table_elem, potential_tags, options):
child.tag = "cell"
processed_subchild = handle_textnode(child, options, preserve_spaces=True, comments_fix=True)
# todo: lists in table cells
elif child.tag == "list" and options.recall:
processed_subchild = handle_lists(child, options)
if processed_subchild is not None:
new_child_elem.append(processed_subchild)
processed_subchild = None # don't handle it anymore
else:
# subcell_elem = Element(child.tag)
processed_subchild = handle_textelem(child, potential_tags.union(["div"]), options)
Expand Down
Loading