
    JKix                       S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	J
r
JrJrJrJr  SSKrSSKJr  SSKJrJr  SSKJr  SS	KJr  \
(       a  SS
KJrJrJrJr  SSKJr   SSK r Sr! SSK#J$r$J%r%  SSKJ&r&J'r'  Sr( SSK)J*r*  Sr+ " S S\5      r,SS.     SS jjr-SSS.       SS jjr. " S S5      r/ " S S5      r0\" 5        " S S\5      5       r1g! \" a    Sr! Nzf = f! \" a    Sr( Nuf = f! \" a    Sr+ Nzf = f)zHTML text splitters.    )annotationsN)StringIO)IOTYPE_CHECKINGAnyLiteral	TypedDictcast)beta)BaseDocumentTransformerDocument)override)RecursiveCharacterTextSplitter)CallableIterableIteratorSequence)	ResultSetTF)BeautifulSoupTag)NavigableStringPageElement)etreec                  B    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	rg
)ElementType5   zElement type as typed dict.strurlxpathcontentdict[str, str]metadata N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r#       _/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langchain_text_splitters/html.pyr   r   5   s    %	HJLr+   r   	recursivec               "    U R                  SUS9$ )NT)stringr.   find_all)tagr.   s     r,   _find_all_stringsr4   B   s    
 <<ty<99r+   )namer.   c                    U R                  XS9$ )Nr-   r1   )r3   r5   r.   s      r,   _find_all_tagsr7   J   s     <<<22r+   c                  n    \ rS rSrSr S
     SS jjrSS jr S       SS jjrSS jrSS jr	Sr
g	)HTMLHeaderTextSplitterS   a5	  Split HTML content into structured Documents based on specified headers.

Splits HTML content by detecting specified header tags and creating hierarchical
`Document` objects that reflect the semantic structure of the original content. For
each identified section, the splitter associates the extracted text with metadata
corresponding to the encountered headers.

If no specified headers are found, the entire content is returned as a single
`Document`. This allows for flexible handling of HTML input, ensuring that
information is organized according to its semantic headers.

The splitter provides the option to return each HTML element as a separate
`Document` or aggregate them into semantically meaningful chunks. It also
gracefully handles multiple levels of nested headers, creating a rich,
hierarchical representation of the content.

Example:
    ```python
    from langchain_text_splitters.html_header_text_splitter import (
        HTMLHeaderTextSplitter,
    )

    # Define headers for splitting on h1 and h2 tags.
    headers_to_split_on = [("h1", "Main Topic"), ("h2", "Sub Topic")]

    splitter = HTMLHeaderTextSplitter(
        headers_to_split_on=headers_to_split_on,
        return_each_element=False
    )

    html_content = """
    <html>
        <body>
            <h1>Introduction</h1>
            <p>Welcome to the introduction section.</p>
            <h2>Background</h2>
            <p>Some background details here.</p>
            <h1>Conclusion</h1>
            <p>Final thoughts.</p>
        </body>
    </html>
    """

    documents = splitter.split_text(html_content)

    # 'documents' now contains Document objects reflecting the hierarchy:
    # - Document with metadata={"Main Topic": "Introduction"} and
    #   content="Introduction"
    # - Document with metadata={"Main Topic": "Introduction"} and
    #   content="Welcome to the introduction section."
    # - Document with metadata={"Main Topic": "Introduction",
    #   "Sub Topic": "Background"} and content="Background"
    # - Document with metadata={"Main Topic": "Introduction",
    #   "Sub Topic": "Background"} and content="Some background details here."
    # - Document with metadata={"Main Topic": "Conclusion"} and
    #   content="Conclusion"
    # - Document with metadata={"Main Topic": "Conclusion"} and
    #   content="Final thoughts."
    ```
c                    [        US S9U l        [        U R                  5      U l        U R                   VVs/ s H  u  p4UPM	     snnU l        X l        gs  snnf )a  Initialize with headers to split on.

Args:
    headers_to_split_on: A list of `(header_tag,
        header_name)` pairs representing the headers that define splitting
        boundaries.

        For example, `[("h1", "Header 1"), ("h2", "Header 2")]` will split
        content by `h1` and `h2` tags, assigning their textual content to the
        `Document` metadata.
    return_each_element: If `True`, every HTML element encountered
        (including headers, paragraphs, etc.) is returned as a separate
        `Document`.

        If `False`, content under the same header hierarchy is aggregated into
        fewer `Document` objects.
c                $    [        U S   SS  5      $ )Nr      )int)xs    r,   <lambda>1HTMLHeaderTextSplitter.__init__.<locals>.<lambda>   s    s1Q48}r+   )keyN)sortedheaders_to_split_ondictheader_mappingheader_tagsreturn_each_element)selfrD   rH   r3   _s        r,   __init__HTMLHeaderTextSplitter.__init__   sY    . $*%<$
  #4#;#;<.2.F.FG.FFCC.FG#6  Hs   Ac                6    U R                  [        U5      5      $ )a4  Split the given text into a list of `Document` objects.

Args:
    text: The HTML text to split.

Returns:
    A list of split `Document` objects.

        Each `Document` contains `page_content` holding the extracted text and
        `metadata` that maps the header hierarchy to their corresponding titles.
split_text_from_filer   rI   texts     r,   
split_text!HTMLHeaderTextSplitter.split_text   s     (($88r+   c                    SSK Jn  U" USSS9  [        R                  " U4SU0UD6nUR	                  5         U R                  UR                  5      $ )a  Fetch text content from a URL and split it into documents.

Args:
    url: The URL to fetch content from.
    timeout: Timeout for the request.
    **kwargs: Additional keyword arguments for the request.

Returns:
    A list of split `Document` objects.

        Each `Document` contains `page_content` holding the extracted text and
        `metadata` that maps the header hierarchy to their corresponding titles.

Raises:
    requests.RequestException: If the HTTP request fails.
r   )validate_safe_urlFT)allow_private
allow_httptimeout))langchain_core._security._ssrf_protectionrU   requestsgetraise_for_statusrR   rQ   )rI   r   rX   kwargsrU   responses         r,   split_text_from_url*HTMLHeaderTextSplitter.split_text_from_url   sL    &	
 	#UtD<<?W??!!#x}}--r+   c                    [        U[        5      (       a$  [        R                  " U5      R	                  SS9nOUR                  5       n[        U R                  U5      5      $ )aa  Split HTML content from a file into a list of `Document` objects.

Args:
    file: A file path or a file-like object containing HTML content.

Returns:
    A list of split `Document` objects.

        Each `Document` contains `page_content` holding the extracted text and
        `metadata` that maps the header hierarchy to their corresponding titles.
zutf-8)encoding)
isinstancer   pathlibPath	read_textreadlist_generate_documents)rI   filehtml_contents      r,   rO   +HTMLHeaderTextSplitter.split_text_from_file   sN     dC  "<<-777IL99;LD,,\:;;r+   c           	   #    ^^#    [         (       d  Sn[        U5      e[        US5      nUR                  =(       d    Un0 m/ mSUU4S jjnU/nU(       Ga;  UR	                  5       n[        UR                  5      nUR                  S [        U5       5       5        [        USS5      n	U	(       d  Md  [        USS9 V
s/ s H  n
[        U
5      R                  5       PM     nn
S	R                  S
 U 5       5      nU(       d  M  [        [        UR                  5      5      nXR                   ;   a  U R"                  (       d  U" 5       nU(       a  Uv    [%        U	SS 5      nTR)                  5        VVVVs/ s H  u  nu  nnnUU:  d  M  UPM     nnnnnU H  nTU	 M     U R*                  U	   nXU4TU'   TR)                  5        VVs0 s H  u  nnUUS   _M     nnn[-        UUS9v   OTR)                  5        VVVs/ s H  u  nu    nnUU:  d  M  UPM     nnnnU H  nTU	 M     U R"                  (       a6  TR)                  5        VVs0 s H  u  nnUUS   _M     nnn[-        UUS9v   OTR/                  U5        U(       a  GM;  U R"                  (       d  U" 5       nU(       a  Uv   gggs  sn
f ! [&         a    Sn GNhf = fs  snnnnf s  snnf s  snnnf s  snnf 7f)a  Private method that performs a DFS traversal over the DOM and yields.

Document objects on-the-fly. This approach maintains the same splitting logic
(headers vs. non-headers, chunking, etc.) while walking the DOM explicitly in
code.

Args:
    html_content: The raw HTML content.

Yields:
    Document objects as they are created.

Raises:
    ImportError: If BeautifulSoup is not installed.
zEUnable to import BeautifulSoup. Please install via `pip install bs4`.html.parserc                    > T(       d  gSR                  S T 5       5      n TR                  5         U R                  5       (       d  gTR                  5        VVs0 s H
  u  pXS   _M     nnn[	        XS9$ s  snnf )z6Finalize the accumulated chunk into a single Document.Nz  
c              3  R   #    U  H  oR                  5       (       d  M  Uv   M     g 7fNstrip).0lines     r,   	<genexpr>UHTMLHeaderTextSplitter._generate_documents.<locals>.finalize_chunk.<locals>.<genexpr>  s     $Tmdzz|TTms   '	'r   page_contentr"   )joinclearrs   itemsr   )
final_textkv
final_metaactive_headerscurrent_chunks       r,   finalize_chunkBHTMLHeaderTextSplitter._generate_documents.<locals>.finalize_chunk
  ss     $Tm$TTJ!##%%.<.B.B.DE.Dda!qT'.DJEII Fs   A8c              3  T   #    U  H  n[        U[        5      (       d  M  Uv   M      g 7frq   )rc   r   )rt   childs     r,   rv   =HTMLHeaderTextSplitter._generate_documents.<locals>.<genexpr>  s      #5%E39O#5s   (	(r5   NFr-    c              3  6   #    U  H  o(       d  M  Uv   M     g 7frq   r#   )rt   elems     r,   rv   r   (  s      H-$4-s   
	r=   i'  r   rx   )returnzDocument | None)_HAS_BS4ImportErrorr   bodypoprh   childrenextendreversedgetattrr4   r   rs   rz   lenparentsrG   rH   r>   
ValueErrorr|   rF   r   append)rI   rk   msgsoupr   r   stacknoder   r3   r   text_elements	node_text	dom_depthdoclevelr~   rJ   lvldheaders_to_removerB   header_namer   header_metaheaders_out_of_scopemetar   r   s                              @@r,   ri   *HTMLHeaderTextSplitter._generate_documents   s      xW  c""\=9yy D
 ;=#%	J 	J 99;DDMM*HLL #+H#5  $-C 1B$RW0X0XuE
  "0X    H- HHID./I &&&//(*C!	!ABLE -;,@,@,B%,B.![aacUlA,B " % -C&s+ - #11#6/8.K{+ 4B3G3G3IJ3I41aq!A$w3IJILL +9*>*>*@(*@,!YaAIPQMA*@ % ( 0C&s+ 0 ++0>0D0D0FG0F1AqtG0FDG"	DII "((3 eD '' "C	  (m( " ! E!% K( Hsy   B:K*>#J7!A3K*J< #K*8K
K
:K*K $K*KK4K*K$&(K*+K*<KK*KK*)rF   rG   rD   rH   N)F)rD   list[tuple[str, str]]rH   boolr   NonerQ   r   r   list[Document])
   )r   r   rX   r>   r]   r   r   r   )rj   zstr | IO[str]r   r   )rk   r   r   zIterator[Document])r$   r%   r&   r'   r(   rK   rR   r_   rO   ri   r*   r#   r+   r,   r9   r9   S   sh    ;@ %*727 "7 
	7<9 (*..!$.58.	.8<$sr+   r9   c                  z    \ rS rSrSr      SS jrSS jrSS jr S     SS jjrSS jr	SS	 jr
SS
 jrSrg)HTMLSectionSplitteria  zTSplitting HTML files based on specified tag and font sizes.

Requires lxml package.
c                    [        U5      U l        [        R                  " [        5      R
                  S-  R                  5       U l        X l        g)a^  Create a new `HTMLSectionSplitter`.

Args:
    headers_to_split_on: List of tuples of headers we want to track mapped to
        (arbitrary) keys for metadata.

        Allowed header values: `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, e.g.:
        `[("h1", "Header 1"), ("h2", "Header 2"]`.
    **kwargs: Additional optional arguments for customizations.

zxsl/converting_to_header.xsltN)	rE   rD   rd   re   __file__parentabsolute	xslt_pathr]   )rI   rD   r]   s      r,   rK   HTMLSectionSplitter.__init__g  s?      $((;#< LL")),KK
(* 	 r+   c                    / / p2U H9  nUR                  UR                  5        UR                  UR                  5        M;     U R                  X#S9n[	        S0 U R
                  D6nUR                  U5      $ )zSplit documents.

Args:
    documents: Iterable of `Document` objects to be split.

Returns:
    A list of split `Document` objects.
)	metadatasr#   )r   ry   r"   create_documentsr   r]   split_documents)rI   	documentstextsr   r   resultstext_splitters          r,   r   #HTMLSectionSplitter.split_documents}  sp     ryCLL))*S\\*  '''C6EE,,W55r+   c                6    U R                  [        U5      5      $ )zeSplit HTML text string.

Args:
    text: HTML text

Returns:
    A list of split `Document` objects.
rN   rP   s     r,   rR   HTMLSectionSplitter.split_text  s     (($88r+   Nc                   U=(       d    0 /[        U5      -  n/ n[        U5       H  u  pVU R                  U5       H  n[        R                  " X5   5      nUR
                   H*  n	UR
                  U	   S:X  d  M  US   UR
                  U	'   M,     0 UEUR
                  En[        UR                  US9n
UR                  U
5        M     M     U$ )zCreate a list of `Document` objects from a list of texts.

Args:
    texts: A list of texts to be split and converted into documents.
    metadatas: Optional list of metadata to associate with each document.

Returns:
    A list of `Document` objects.
#TITLE#Titlerx   )	r   	enumeraterR   copydeepcopyr"   r   ry   r   )rI   r   r   
metadatas_r   irQ   chunkr"   rB   new_docs              r,   r   $HTMLSectionSplitter.create_documents  s     32$U"3
	 'GA.==7 >>C~~c*i7.6w.?s+ * :h9%..9"0B0BXV  ) / ( r+   c                r   [         (       d  Sn[        U5      e[        US5      n[        U R                  R                  5       5      n/ n[        US/UQS9n[        U5       H  u  pxUS:X  a  Sn	Sn
/ nO(UR                  R                  5       n	UR                  n
/ nUR                   HJ  nUS-   [        U5      :  a  XUS-      :X  a    O,[        U[        5      (       d  M9  UR                  U5        ML     S	R!                  U5      R                  5       nU(       d  M  UR                  U	UU
S
.5        M     U$ )a  Split an HTML document into sections based on specified header tags.

This method uses BeautifulSoup to parse the HTML content and divides it into
sections based on headers defined in `headers_to_split_on`. Each section
contains the header text, content under the header, and the tag name.

Args:
    html_doc: The HTML document to be split into sections.

Returns:
    A list of dictionaries representing sections.

        Each dictionary contains:

        * `'header'`: The header text or a default title for the first section.
        * `'content'`: The content under the header.
        * `'tag_name'`: The name of the header tag (e.g., `h1`, `h2`).

Raises:
    ImportError: If BeautifulSoup is not installed.
zzUnable to import BeautifulSoup/PageElement,                     please install with `pip install                     bs4`.rn   r   r5   r   r   h1r=   r   )headerr    tag_name)r   r   r   rh   rD   keysr7   r   rQ   rs   r5   next_elementsr   rc   r   r   rz   )rI   html_docr   r   header_namessectionsheadersr   r   current_headercurrent_header_tagsection_contentelementr    s                 r,   split_html_by_headers)HTMLSectionSplitter.split_html_by_headers  s.   , xC c""X}5D4499;<02 V,Cl,CD"7+IAAv!*%)"-/!'!2!2!4%+[[""$!//q53w<'Gq1u~,Egs++#**73	 0
 hh/557Gw"0#*$6# ,2 r+   c                   [         (       d  Sn[        U5      e[        R                  " SS9n[        R                  " SSSS9n[        R
                  R                  n[        R                  " [        U5      U5      n[        R                  " U R                  U5      n[        R                  " XuS9nU" U5      n	[        U	5      $ )a  Convert specific HTML tags to headers using an XSLT transformation.

This method uses an XSLT file to transform the HTML content, converting
certain tags into headers for easier parsing. If no XSLT path is provided,
the HTML content is returned unchanged.

Args:
    html_content: The HTML content to be transformed.

Returns:
    The transformed HTML content as a string.

Raises:
    ImportError: If the `lxml` library is not installed.
z>Unable to import lxml, please install with `pip install lxml`.T)
no_networkF)resolve_entitiesr   load_dtd)access_control)	_HAS_LXMLr   r   
HTMLParser	XMLParserXSLTAccessControlDENY_ALLparser   r   XSLTr   )
rI   rk   r   html_parserxslt_parseractree	xslt_tree	transformresults
             r,   convert_possible_tags_to_header3HTMLSectionSplitter.convert_possible_tags_to_header  s      yRCc"" &&$7oo"te
 $$--{{8L1;?KK<	JJy<	46{r+   c                    UR                  5       nU R                  U5      nU R                  U5      nU Vs/ s H8  n[        [	        SUS   5      U R
                  [        US   5         US   0S9PM:     sn$ s  snf )zSplit HTML content from a file into a list of `Document` objects.

Args:
    file: A file path or a file-like object containing HTML content.

Returns:
    A list of split `Document` objects.
r   r    r   r   )r"   )getvaluer   r   r   r
   rD   r   )rI   rj   file_contentr   sections        r,   rO   (HTMLSectionSplitter.split_text_from_file  s     }};;LI--l; $

 $ UGI./,,S1D-EF I $

 
	
 

s   ?A9)rD   r]   r   )rD   r   r]   r   r   r   )r   zIterable[Document]r   r   r   rq   )r   	list[str]r   zlist[dict[Any, Any]] | Noner   r   )r   r   r   zlist[dict[str, str | None]])rk   r   r   r   )rj   r   r   r   )r$   r%   r&   r'   r(   rK   r   rR   r   r   r   rO   r*   r#   r+   r,   r   r   a  sg    
2  
	,6&	9 JN+F	4;z#J
r+   r   c                  N   \ rS rSrSrSSSSSSSSSSSSSSSSSS	.                                     SS
 jjrSS jr\      SS j5       rSS jr	\
SS j5       rSS jrSS jrSS jr        SS jr        SS jr\
      SS j5       rSrg)HTMLSemanticPreservingSplitteri1  a  Split HTML content preserving semantic structure.

Splits HTML content by headers into generalized chunks, preserving semantic
structure. If chunks exceed the maximum chunk size, it uses
`RecursiveCharacterTextSplitter` for further splitting.

The splitter preserves full HTML elements and converts links to Markdown-like links.
It can also preserve images, videos, and audio elements by converting them into
Markdown format. Note that some chunks may exceed the maximum size to maintain
semantic integrity.

!!! version-added "Added in `langchain-text-splitters` 0.3.5"

Example:
    ```python
    from langchain_text_splitters.html import HTMLSemanticPreservingSplitter

    def custom_iframe_extractor(iframe_tag):
        ```
        Custom handler function to extract the 'src' attribute from an <iframe> tag.
        Converts the iframe to a Markdown-like link: [iframe:<src>](src).

        Args:
            iframe_tag (bs4.element.Tag): The <iframe> tag to be processed.

        Returns:
            str: A formatted string representing the iframe in Markdown-like format.
        ```
        iframe_src = iframe_tag.get('src', '')
        return f"[iframe:{iframe_src}]({iframe_src})"

    text_splitter = HTMLSemanticPreservingSplitter(
        headers_to_split_on=[("h1", "Header 1"), ("h2", "Header 2")],
        max_chunk_size=500,
        preserve_links=True,
        preserve_images=True,
        custom_handlers={"iframe": custom_iframe_extractor}
    )
    ```
i  r   NFenglishT)max_chunk_sizechunk_overlap
separatorselements_to_preservepreserve_linkspreserve_imagespreserve_videospreserve_audiocustom_handlersstopword_removalstopword_langnormalize_textexternal_metadataallowlist_tagsdenylist_tagspreserve_parent_metadatakeep_separatorc          
        [         (       d  Sn[        U5      e[        U5      U l        X l        U=(       d    / U l        X`l        Xpl        Xl        Xl	        U
=(       d    0 U l
        Xl        Xl        Xl        U=(       d    0 U l        Xl        UU l        UU l        U(       a/  [%        ['        X Vs/ s H  nUS   PM
     sn-   5      5      U l        UU l        U(       a6  U VVs/ s H!  nUU Vs/ s H  nUS   PM
     sn;  d  M  UPM#     snnU l        U(       a  [+        UUUUS9U l        O[+        UUUS9U l        U R                  (       ap  [.        (       d  Sn[        U5      e[0        R2                  " S5        ['        [0        R4                  R6                  R9                  U R                  5      5      U l        ggs  snf s  snf s  snnf )a&  Initialize splitter.

Args:
    headers_to_split_on: HTML headers (e.g., `h1`, `h2`) that define content
        sections.
    max_chunk_size: Maximum size for each chunk, with allowance for exceeding
        this limit to preserve semantics.
    chunk_overlap: Number of characters to overlap between chunks to ensure
        contextual continuity.
    separators: Delimiters used by `RecursiveCharacterTextSplitter` for
        further splitting.
    elements_to_preserve: HTML tags (e.g., `table`, `ul`) to remain
        intact during splitting.
    preserve_links: Converts `a` tags to Markdown links (`[text](url)`).
    preserve_images: Converts `img` tags to Markdown images (`![alt](src)`).
    preserve_videos: Converts `video` tags to Markdown video links
        (`![video](src)`).
    preserve_audio: Converts `audio` tags to Markdown audio links
        (`![audio](src)`).
    custom_handlers: Optional custom handlers for specific HTML tags, allowing
        tailored extraction or processing.
    stopword_removal: Optionally remove stopwords from the text.
    stopword_lang: The language of stopwords to remove.
    normalize_text: Optionally normalize text (e.g., lowercasing, removing
        punctuation).
    external_metadata: Additional metadata to attach to the Document objects.
    allowlist_tags: Only these tags will be retained in the HTML.
    denylist_tags: These tags will be removed from the HTML.
    preserve_parent_metadata: Whether to pass through parent document metadata
        to split documents when calling
        `transform_documents/atransform_documents()`.
    keep_separator: Whether separators should be at the beginning of a chunk, at
        the end, or not at all.

Raises:
    ImportError: If BeautifulSoup or NLTK (when stopword removal is enabled)
        is not installed.
zICould not import BeautifulSoup. Please install it with 'pip install bs4'.r   )r   r  
chunk_sizer   )r  r  r   zACould not import nltk. Please install it with 'pip install nltk'.	stopwordsN)r   r   rC   _headers_to_split_on_max_chunk_size_elements_to_preserve_preserve_links_preserve_images_preserve_videos_preserve_audio_custom_handlers_stopword_removal_stopword_lang_normalize_text_external_metadata_allowlist_tags_preserve_parent_metadata_keep_separatorrh   set_denylist_tagsr   _recursive_splitter	_HAS_NLTKnltkdownloadcorpusr  words
_stopwords)rI   rD   r   r   r   r   r   r  r  r  r  r  r  r  r  r	  r
  r  r  r   r   r3   s                         r,   rK   'HTMLSemanticPreservingSplitter.__init__\  s   x x<  c""$*+>$?!-%9%?R"- / /- / 52!1+-"3"9r-)A&-#'N>Q%R>QFfQi>Q%RRS$D  , )#(C7JK7JVvay7JKK (#D
 'E%-)+	(D$ (F-)+(D$ !!9W  "#&&MM+&!$++"7"7"="=d>Q>Q"RSDO "/ &S L#s$   2G&G1G GGGc                   [        US5      nU R                  U5        U R                  (       a  U R                  U5        U R                  (       d  U R
                  (       a  U R                  U5        U R                  U5      $ )zSplits the provided HTML text into smaller chunks based on the configuration.

Args:
    text: The HTML content to be split.

Returns:
    A list of `Document` objects containing the split content.
rn   )r   _process_mediar  _process_linksr  r   _filter_tags_process_html)rI   rQ   r   s      r,   rR   )HTMLSemanticPreservingSplitter.split_text  se     T=1D!%4#6#6d#!!$''r+   c           	        / nU H|  nU R                  UR                  5      nU R                  (       a<  U Vs/ s H/  n[        UR                  0 UR                  EUR                  ES9PM1     nnUR                  U5        M~     U$ s  snf )zTransform sequence of documents by splitting them.

Args:
    documents: A sequence of `Document` objects to be split.

Returns:
    A sequence of split `Document` objects.
rx   )rR   ry   r  r   r"   r   )rI   r   r]   transformedr   splits	split_docs          r,   transform_documents2HTMLSemanticPreservingSplitter.transform_documents  s     C__S%5%56F-- &,
 &,		 %.%;%;!GCLL!GI4F4F!G &,   v&  s   6Bc                d   U R                   (       aT  [        USS9 HF  nUR                  SS5      nSU SU S3nUR                  S5      nXEl        UR                  U5        MH     U R                  (       aT  [        US	S9 HF  nUR                  SS5      nS
U SU S3nUR                  S5      nXl        UR                  U5        MH     U R                  (       aU  [        USS9 HF  n	U	R                  SS5      n
SU
 SU
 S3nUR                  S5      nXl        U	R                  U5        MH     gg)zProcesses the media elements.

Process elements in the HTML content by wrapping them in a <media-wrapper> tag
and converting them to Markdown format.

Args:
    soup: Parsed HTML content using BeautifulSoup.
imgr   src z![image:]()zmedia-wrappervideoz![video:audioz![audio:N)r  r7   r[   new_tagr0   replace_withr  r  )rI   r   img_tagimg_srcmarkdown_imgwrapper	video_tag	video_srcmarkdown_video	audio_tag	audio_srcmarkdown_audios               r,   r*  -HTMLSemanticPreservingSplitter._process_media  s,      )$U;!++eR0!)'"WIQ?,,7!-$$W- <   +Dw?	%MM%4	#+I;b1!E,,7!/&&w/ @ +Dw?	%MM%4	#+I;b1!E,,7!/&&w/ @  r+   c                    [        U SS9 H^  nUR                  SS5      nUR                  SS9nSU SU S	3nU R                  S
5      nXEl        UR                  [        U5      5        M`     g)zcProcesses the links in the HTML content.

Args:
    soup: Parsed HTML content using BeautifulSoup.
ar   hrefr8  Trr   [r9  r:  zlink-wrapperN)r7   r[   get_textr=  r0   r>  r   )r   a_taga_hrefa_textmarkdown_linkrB  s         r,   r+  -HTMLSemanticPreservingSplitter._process_links   so     $Ds3EYYvr*F^^$^/Fxr&3Mll>2G*N}=> 4r+   c                   U R                   (       a=  [        USS9 H/  nUR                  U R                   ;  d  M  UR                  5         M1     U R                  (       a+  [        XR                  S9 H  nUR                  5         M     gg)z}Filters the HTML content based on the allowlist and denylist tags.

Args:
    soup: Parsed HTML content using BeautifulSoup.
Tr   N)r  r7   r5   	decomposer   )rI   r   r3   s      r,   r,  +HTMLSemanticPreservingSplitter._filter_tags/  sh     %d6884#7#77MMO 7 %d1D1DE F r+   c                r   U R                   (       aN  UR                  5       n[        R                  " SSU5      n[        R                  " SSU5      R	                  5       nU R
                  (       aA  SR                  UR                  5        Vs/ s H  o"U R                  ;  d  M  UPM     sn5      nU$ s  snf )zNormalizes the text by removing extra spaces and newlines.

Args:
    text: The text to be normalized.

Returns:
    The normalized text.
z[^\w\s]r8  \s+r   )	r  lowerresubrs   r  rz   splitr'  )rI   rQ   words      r,   _normalize_and_clean_text8HTMLSemanticPreservingSplitter._normalize_and_clean_text>  s     ::<D66*b$/D66&#t,224D!!88"&**,N,$doo2M,ND  Os   B4%B4c                  ^ ^^	 / n0 n/ n0 nSnSUU 4S jjm[        USS9n              SUU	U 4S jjm	T	" UUUUUU5      u  nnnnnU(       a1  UR                  T R                  USR                  U5      U5      5        U$ )	zProcesses the HTML content using BeautifulSoup and splits it using headers.

Args:
    soup: Parsed HTML content using BeautifulSoup.

Returns:
    A list of `Document` objects containing the split content.
r   c                  > [        SU 5      n U R                  TR                  ;   a  TR                  U R                     " U 5      $ SnU R                  bA  U R                   H0  nT" U5      R	                  5       nU(       a  U(       a  US-  nX-  nM2     OU R
                  (       a  XR
                  -  nTR                  U5      $ )zRecursively extracts and processes the text of an element.

Applies custom handlers where applicable, and ensures correct spacing.

Args:
    element: The HTML element to process.

Returns:
    The processed text of the element.
zTag | NavigableStringr8  r   )r
   r5   r  r   rs   r0   r^  )r   rQ   r   
child_text_get_element_textrI   s       r,   rc  GHTMLSemanticPreservingSplitter._process_html.<locals>._get_element_textb  s     2G<G||t444,,W\\:7CCD||'$--E!25!9!?!?!AJ
&D	 .
 &11$77r+   Fr-   c           	     F  > U  GH  nUR                   TR                   Vs/ s H  owS   PM	     sn;   a  U(       aQ  UR                  TR                  USR	                  U5      U5      5        UR                  5         UR                  5         UR                  SS9n[        TR                  5      UR                      U0nM  UR                   TR                  ;   a'  SU 3n	T" U5      XI'   UR                  U	5        US-  nM  [        USS9n
U
(       a[  T" U
UUUUU5      u  nnnnnSR	                  [        USS95      nU(       a%  TR                  U5      nUR                  U5        GMh  GMk  T" U5      nU(       d  GM}  UR                  U5        GM     UUUUU4$ s  snf )	Nr   r   Trr   
PRESERVED_r=   Fr-   )r5   r  r   _create_documentsrz   r{   rN  rE   r  r   r7   r4   r^  )r   r   current_headerscurrent_contentpreserved_elementsplaceholder_countr   hr   placeholderr   r    rc  _process_elementrI   s               r,   rn  FHTMLSemanticPreservingSplitter._process_html.<locals>._process_element  s     99t/H/H I/H!1/H II&!(( 22 / # 9 2 (--/*002"&--d-";KT667		BK'O YY$"<"<<$./@.A"BK6G6M&3#**;7%*%  .deDH -$%++.-%++.- #&((+<TU+S"T"&*&D&DW&MG+227; # #4D"9"7+227;m  r "! o !Js   Fr   )r   r   r   r   )r   ResultSet[Tag]r   r   rh  r!   ri  r   rj  r!   rk  r>   r   zEtuple[list[Document], dict[str, str], list[str], dict[str, str], int])r7   r   rg  rz   )
rI   r   r   rh  ri  rj  rk  elementsrc  rn  s
   `       @@r,   r-  ,HTMLSemanticPreservingSplitter._process_htmlS  s     %'	*,%'-/!"	8 	88 "$%8F	#F	%F	 ,F	 '	F	
 !/F	  #F	 SF	 F	^ 
	
 &&#HH_-& r+   c                    [         R                  " SSU5      R                  5       n0 UEU R                  En[	        U5      U R
                  ::  a  U R                  X#5      n[        XTS9/$ U R                  X$U5      $ )a:  Creates Document objects from the provided headers, content, and elements.

Args:
    headers: The headers to attach as metadata to the `Document`.
    content: The content of the `Document`.
    preserved_elements: Preserved elements to be reinserted into the content.

Returns:
    A list of `Document` objects.
rX  r   rx   )	rZ  r[  rs   r  r   r  _reinsert_preserved_elementsr   _further_split_chunk)rI   r   r    rj  r"   ry   s         r,   rg  0HTMLSemanticPreservingSplitter._create_documents  s}     &&g.4469g9!8!89w<4///<<L ,JKK((<NOOr+   c                    U R                   R                  U5      n/ nU HR  nU R                  Xc5      nUR                  5       (       d  M+  UR	                  [        UR                  5       US95        MT     U$ )a#  Further splits the content into smaller chunks.

Args:
    content: The content to be split.
    metadata: Metadata to attach to each chunk.
    preserved_elements: Preserved elements to be reinserted into each chunk.

Returns:
    A list of `Document` objects containing the split content.
rx   )r!  rR   rt  rs   r   r   )rI   r    r"   rj  r1  r   r\  split_with_preserveds           r,   ru  3HTMLSemanticPreservingSplitter._further_split_chunk  sx     ))44W=E#'#D#D$  $))++%9%?%?%A!)  r+   c                    [        UR                  5       5       H$  u  p#U R                  X#R                  5       5      n M&     U $ )a  Reinserts preserved elements into the content into their original positions.

Args:
    content: The content where placeholders need to be replaced.
    preserved_elements: Preserved elements to be reinserted.

Returns:
    The content with placeholders replaced by preserved elements.
)r   r|   replacers   )r    rj  rm  preserved_contents       r,   rt  ;HTMLSemanticPreservingSplitter._reinsert_preserved_elements  s=     /77I7O7O7Q.R*Kook3J3J3LMG /Sr+   )r  r  r   r  r  r  r  r  r  r  r  r  r  r  r!  r  r  r'  )&rD   r   r   r>   r   r>   r   list[str] | Noner   r~  r   r   r  r   r  r   r  r   r  z&dict[str, Callable[[Tag], str]] | Noner  r   r  r   r  r   r  zdict[str, str] | Noner	  r~  r
  r~  r  r   r  zbool | Literal['start', 'end']r   r   r   )r   zSequence[Document]r]   r   r   r   )r   r   r   r   )rQ   r   r   r   )r   r   r   r   )r   r!   r    r   rj  r!   r   r   )r    r   r"   zdict[Any, Any]rj  r!   r   r   )r    r   rj  r!   r   r   )r$   r%   r&   r'   r(   rK   rR   r   r3  r*  staticmethodr+  r,  r^  r-  rg  ru  rt  r*   r#   r+   r,   r   r   1  s   'Z #'+15$ % %$BF!&&$37+/*.).9=)rT2rT 	rT
 rT %rT /rT rT rT rT rT @rT rT rT rT  1!rT" )#rT$ (%rT& #''rT( 7)rT* 
+rTh(* +7:	 20B ? ? *ObP%P03PIWP	P0&4JX	< *8	 r+   r   )r3   r   r.   r   r   zResultSet[NavigableString])r3   r   r5   zbool | str | list[str] | Noner.   r   r   rp  )2r(   
__future__r   r   rd   rZ  ior   typingr   r   r   r   r	   r
   rZ   langchain_core._apir   langchain_core.documentsr   r   typing_extensionsr   "langchain_text_splitters.characterr   collections.abcr   r   r   r   bs4.elementr   r#  r"  r   bs4r   r   r   r   r   lxmlr   r   r   r4   r7   r9   r   r   r#   r+   r,   <module>r     sI    "   	    $ F & MFF%I&8HI
)   :	: :  	: +/	3	3 (3 	3
 3K K\M
 M
` w%< w wa  I  H  Is6   #C *C' =C5 C$#C$'C21C25D ?D 