"""Tests for HTML -> markdown conversion."""
from __future__ import annotations
from wikikb import html2md
_HTML = """
[edit]
A count can hold up to 4 counties.
navigation
"""
def test_clean_html_strips_chrome_keeps_content() -> None:
cleaned = html2md.clean_html(_HTML)
assert "editsection" not in cleaned
assert "navbox" not in cleaned
assert "count" in cleaned
def test_clean_html_without_parser_output_wrapper() -> None:
cleaned = html2md.clean_html("bare
")
assert "bare" in cleaned
def test_html_to_markdown_preserves_table_numbers() -> None:
md = html2md.html_to_markdown(_HTML)
assert "Genius" in md
assert "+5" in md
assert "\n\n\n" not in md # blank lines collapsed
def test_build_document_frontmatter_and_quote_escaping() -> None:
doc = html2md.build_document(
title='War of the "Roses"',
url="https://wiki.example/War",
revid=42,
categories=["Warfare", "1.19"],
html="hello
",
)
assert doc.startswith("---\n")
assert 'title: "War of the \\"Roses\\""' in doc
assert "revid: 42" in doc
assert 'categories: ["Warfare", "1.19"]' in doc
assert "# War of the" in doc
assert "hello" in doc
# A cut-down Fandom PortableInfobox: labels are siblings of their values, not
# table cells, and the tier variants are inlined behind tabs.
_INFOBOX = """
"""
def test_infobox_becomes_a_table_with_labels_bound_to_values() -> None:
md = html2md.html_to_markdown(_INFOBOX)
# The point of the transform: not merely that "74" survives, but that it stays
# attached to "Stab dmg." Plain markdownify emits headings then orphaned numbers.
assert "| Stab dmg. | 74 |" in md
assert "| Slash dmg. | 71 |" in md
assert "| Classification | Weapons |" in md
def test_infobox_tier_tabs_are_labelled() -> None:
md = html2md.html_to_markdown(_INFOBOX)
# Without the tab label, both tiers' stat blocks are indistinguishable.
assert "## Infobox: Attributes (I)" in md
assert "## Infobox: Attributes (II)" in md
assert "| Stab dmg. | 92 |" in md
def test_infobox_lead_text_gets_its_own_section() -> None:
md = html2md.html_to_markdown(_INFOBOX)
# The lead paragraph must not be filed under the last infobox group.
assert md.index("## Overview") < md.index("The lead sentence.")
def test_infobox_without_a_group_header_is_named_plainly() -> None:
html = (
''
)
md = html2md.html_to_markdown(html)
assert "## Infobox\n" in md
assert "| ID | 4 |" in md
def test_empty_infobox_is_dropped() -> None:
# Theme/dummy infoboxes hold no data and would otherwise leave a stray heading.
md = html2md.html_to_markdown(
''
)
assert "Infobox" not in md
assert "body" in md
def test_mismatched_smart_group_keeps_the_pairs_that_line_up() -> None:
html = (
'"
)
md = html2md.html_to_markdown(html)
assert "| A | 1 |" in md
assert "| B |" not in md
def test_incomplete_pi_data_is_skipped_without_losing_the_rest() -> None:
# A hand-edited infobox row can carry a label with no value. Skip that row and
# keep going rather than emitting a label bound to nothing.
html = (
'"
)
md = html2md.html_to_markdown(html)
assert "| Weight | 3.9 |" in md
assert "Orphan" not in md
def test_page_tabber_switcher_is_stripped() -> None:
# It links to the other game's page; keeping it would put a bare "KCD" link at
# the top of every dual-game article.
html = (
'"
)
md = html2md.html_to_markdown(html)
assert "real content" in md
assert "/wiki/Alchemy" not in md