def _extract_structure_information(document: Document, result: Metadata) -> None:
    """Extract language and subtitle from document structure."""
    if document.structure:
        languages = set()
        subtitle = None

        def extract_languages(elements: list[Any]) -> None:
            nonlocal subtitle
            for element in elements:
                if hasattr(element, "language") and element.language:
                    languages.add(element.language.lower())

                if (
                    subtitle is None
                    and hasattr(element, "role")
                    and element.role == "H1"
                    and hasattr(element, "text")
                    and element.text
                ):
                    subtitle = decode_text(element.text)

                if hasattr(element, "children") and element.children:
                    extract_languages(element.children)

        extract_languages(cast("list[Any]", document.structure))

        if languages:
            result["languages"] = list(languages)

        if subtitle and "title" in result and subtitle != result["title"]:
            result["subtitle"] = subtitle
Extract language and subtitle from document structure.
kreuzberg/_playa.py