from unstructured.partition.html import partition_html
def _by_text(elements):
return {(e.text or ""): e for e in elements}
def test_category_depth_follows_heading_level_on_a_multi_level_page():
"""ML-1328 AC1/AC3: a heading's `category_depth` is its heading level (h1->0, h2->1, h3->2),
and `parent_id` chains each subsection under its enclosing section heading."""
# language=HTML
html = """
Cost Share Summary
Intro paragraph.
Accumulation Period
Accumulation body.
Cost Share Summary Tables by Benefit
How to read the Cost Share summary tables
How-to body.
"""
elements = partition_html(text=html, html_parser_version="v2", unique_element_ids=False)
by_text = _by_text(elements)
section = by_text["Cost Share Summary"]
subsection_a = by_text["Accumulation Period"]
subsection_b = by_text["Cost Share Summary Tables by Benefit"]
sub_subsection = by_text["How to read the Cost Share summary tables"]
# -- depth tracks heading level --
assert section.metadata.category_depth == 0
assert subsection_a.metadata.category_depth == 1
assert subsection_b.metadata.category_depth == 1
assert sub_subsection.metadata.category_depth == 2
# -- parent_id chains section -> subsection -> sub-subsection --
assert subsection_a.metadata.parent_id == section.id
assert subsection_b.metadata.parent_id == section.id
assert sub_subsection.metadata.parent_id == subsection_b.id
# -- body text parents to its enclosing heading, not the page/column container --
assert by_text["How-to body."].metadata.parent_id == sub_subsection.id
# -- v2's defining behavior is preserved: every element keeps its text_as_html --
assert all(e.metadata.text_as_html for e in elements)
def test_category_depth_does_not_change_with_multi_column_layout():
"""ML-1328 AC2: the same heading reads the same `category_depth` whether the page is single- or
multi-column. Layout nesting (Page -> Column) must not bump the depth."""
# language=HTML
single_column = """