import typing


import lxml.etree as etree
import nh3


def extract_paragraphs(html: str) -> typing.Iterator[str]:
    html = _clean_html(html)
    parser = etree.HTMLParser()
    element = etree.fromstring(html, parser=parser)

    xpath = ".//p | .//h1 | .//h2 | .//h3 | .//h4 | .//h5 | .//h6"
    for paragraph in element.xpath(xpath):
        if paragraph.text:
            yield paragraph.text


def _clean_html(html) -> str:
    html = nh3.clean(
        html,
        tags={
            "p",
            "h1",
            "h2",
            "h3",
            "h4",
            "h5",
            "h6",
        },
    )
    html = f"<div>{html}</div>"
    return html
