� K�g�D����dZddlmZddlZddlmcmZddlmZm Z ddl m Z ddl m Z erddl mZdd�Zdd�ZGd�de j��ZGd�de��ZGd�de��ZGd�de��ZdS)z� Tree processors manipulate the tree created by block processors. They can even create an entirely new `ElementTree` object. This is an excellent place for creating summaries, adding collected references, or last minute adjustments. �)� annotationsN)� TYPE_CHECKING�Any�)�util)�inlinepatterns)�Markdown�mdr �kwargsr�return�util.Registry[Treeprocessor]c ��tj��}|�t|��dd��|�t |��dd��|�t |��dd��|S)z3 Build the default `treeprocessors` for Markdown. �inline��prettify� �unescaper)r�Registry�register�InlineProcessor�PrettifyTreeprocessor�UnescapeTreeprocessor)r r �treeprocessorss �g/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/markdown/treeprocessors.py�build_treeprocessorsr'su���]�_�_�N����O�B�/�/��2�>�>�>����1�"�5�5�z�2�F�F�F����1�"�5�5�z�1�E�E�E� ���s�object�boolc�d�t|tj��st|t��SdS)z_ Return `True` if object is a string but not an [`AtomicString`][markdown.util.AtomicString]. F)� isinstancer� AtomicString�str)rs r�isStringr$0s,�� �a��*� +� +�"��!�S�!�!�!� �5rc��eZdZdZdd�ZdS) � Treeprocessora  `Treeprocessor`s are run on the `ElementTree` object before serialization. Each `Treeprocessor` implements a `run` method that takes a pointer to an `Element` and modifies it as necessary. `Treeprocessors` must extend `markdown.Treeprocessor`. �root� etree.Elementr �etree.Element | Nonec��dS)a Subclasses of `Treeprocessor` should implement a `run` method, which takes a root `Element`. This method can return another `Element` object, and the existing root `Element` will be replaced, or it can modify the current tree and return `None`. N�)�selfr's r�runzTreeprocessor.runAs �� �rN)r'r(r r))�__name__� __module__� __qualname__�__doc__r-r+rrr&r&7s2�������� � � � � � rr&c�p�eZdZdZd.d�Zd/d �Zd0d�Zd1d�Zd2d3d�Zd4d5d�Z d4d6d�Z d2d7d$�Z d8d(�Z d9d:d-�Z d)S);rzL A `Treeprocessor` that traverses a tree, applying inline patterns. r r c��tj|_tj|_dt |j��zt |j��z|_tj|_||_ |j |_ g|_ dS)N�) r�INLINE_PLACEHOLDER_PREFIX�$_InlineProcessor__placeholder_prefix�ETX�$_InlineProcessor__placeholder_suffix�len�$_InlineProcessor__placeholder_length�INLINE_PLACEHOLDER_RE� _InlineProcessor__placeholder_rer �inlinePatterns� ancestors)r,r s r�__init__zInlineProcessor.__init__Psk��$(�$B��!�$(�H��!�$%��D�,E�(F�(F�$F�(+�D�,E�(F�(F�%G��!� $� :������ �/���$&����r�typer#r �tuple[str, str]c�V�dt|j��z}tj|z}||fS)z Generate a placeholder z%04d)r9� stashed_nodesr�INLINE_PLACEHOLDER)r,r@�id�hashs r�__makePlaceholderz!InlineProcessor.__makePlaceholderZs-�� �c�$�,�-�-� -���&��+���R�x�r�data�index�int�tuple[str | None, int]c��|j�||��}|r)|�d��|���fSd|dzfS)a Extract id from data string, start from index. Arguments: data: String. index: Index, from which we start search. Returns: Placeholder id and string index, after the found placeholder. rN)r<�search�group�end)r,rHrI�ms r�__findPlaceholderz!InlineProcessor.__findPlaceholder`sO�� � !� (� (��u� 5� 5�� � #��7�7�1�:�:�q�u�u�w�w�&� &�����?� "r�node�etree.Element | strc�J�|�|��\}}||j|<|S)z Add node to stash. )�!_InlineProcessor__makePlaceholderrC)r,rRr@� placeholderrEs r� __stashNodezInlineProcessor.__stashNoders,���0�0��6�6�� �R�!%���2���rr� patternIndexc���t|tj��sPd}t|j��}||kr4|�|j||||��\}}}|s|dz }||k�4|S)a Process string with inline patterns and replace it with placeholders. Arguments: data: A line of Markdown text. patternIndex: The index of the `inlinePattern` to start with. Returns: String with placeholders. rr)r!rr"r9r=�_InlineProcessor__applyPattern)r,rHrX� startIndex�count�matcheds r�__handleInlinezInlineProcessor.__handleInlinexs����$�� 1�2�2� &��J���+�,�,�E���&�&�,0�,?�,?��'� �5�t�\�:�-�-�)��g�z��&� �A�%�L� ��&�&� � rTr(�subnode�isTextr�Nonec�8�|r|j}d|_n|j}d|_|�|||��}|s*||ur&t|���|��dz}nd}|���|D]}|�||d���dS)a' Process placeholders in `Element.text` or `Element.tail` of Elements popped from `self.stashed_nodes`. Arguments: node: Parent node. subnode: Processing node. isText: Boolean variable, True - it's text, False - it's a tail. Nrr)�text�tail�%_InlineProcessor__processPlaceholders�listrI�reverse�insert)r,rRr_r`rc� childResult�pos�newChilds r�__processElementTextz$InlineProcessor.__processElementText�s��� � ��<�D��G�L�L��<�D��G�L��0�0��w��G�G� �� �$�g�-�-��t�*�*�"�"�7�+�+�a�/�C�C��C�������#� *� *�H� �K�K��X�a�[� )� )� )� )� *� *r� str | None�parent�%list[tuple[etree.Element, list[str]]]c���� �d ��� fd� }g� d}|�r�|�|j|��}|dk�rT|�||��\}}||jv�r|j�|��} |dkr|||�} || ��t | t ��s�| gt| ��zD]o} | jr0| j� ��r|� | | d��| j r/| j � ��r|� | | ���pn|| ��|}��%|}� � | |j d d �f��nr|t|j��z} |||| ���| }nE||d �} t |tj��rtj| ��} || ��d }|���� S) a� Process string with placeholders and generate `ElementTree` tree. Arguments: data: String with placeholders instead of `ElementTree` elements. parent: Element, which contains processing inline data. isText: Boolean variable, True - it's text, False - it's a tail. Returns: List with `ElementTree` elements with applied inline patterns. rcrmr rac�(��|r��rF�ddjr�ddxj|z c_dS|�dd_dS�s"�jr�xj|z c_dS|�_dS�jr�xj|z c_dS|�_dSdS)N�����r)rdrc)rcr`rn�results ���r�linkTextz7InlineProcessor.__processPlaceholders.<locals>.linkText�s����� +��+��b�z�!�}�)�2��r� �1� �*�*�d�2�*�*�*�*�-1��r� �1� �*�*�*�� +��{�+�� � �t�+� � � � �&*�� � � ��{�+�� � �t�+� � � � �&*�� � � � +� +rrrrFN�)rcrmr ra)�findr6�!_InlineProcessor__findPlaceholderrC�getr!r#rfrd�strip�$_InlineProcessor__processElementTextrc�appendr>r9rr")r,rHrnr`rt� strartIndexrIrE� phEndIndexrRrc�childrOrss `` @r�__processPlaceholdersz%InlineProcessor.__processPlaceholders�sB�����$ +� +� +� +� +� +� +� +�"��� ��( ��I�I�d�7��E�E�E���{�{�!%�!7�!7��e�!D�!D���J���+�+�+��-�1�1�"�5�5�D��q�y�y�#�K��$5�6�� ������%�d�C�0�0� !�&*�V�d�4�j�j�%8�L�L�E�$�z�&�#(�:�#3�#3�#5�#5�!&�$(�$=�$=�(,�e�U�%&�%&�%&� %�z�L�#(�:�#3�#3�#5�#5�!L�$(�$=�$=�e�U�$K�$K�$K��L�!������&0� � �",�K��M�M�4������):�";�<�<�<�<� �#�d�&?�"@�"@�@�C��H�T�+�c�/�2�3�3�3�"%�K�K��K�L�L�)���d�D�$5�6�6�3��,�T�2�2�D���������Q�( �T� r�pattern�inlinepatterns.Patternr[�tuple[str, bool, int]c��t|tj��}|jD]$}|���|jvr|ddfcS�%|rgd}|����||��D];}|�||��\}} } | �| �||� d��z }d}�;n9|���� ||d���}|d|�} |s|ddfS|s?|�|��}|� d��} |� d��} |�|d| fSt|t��s�t|j tj��s�|gt!|��zD]�} t#|��s�| j rm|j�| j�����|�| j |dz��| _ |j���| jr |�| j|��| _��|�||�����} |r*d�|d| �| || d���ddfSd�| |�d��| |���d��ddfS) a� Check if the line fits the pattern, create the necessary elements, add it to `stashed_nodes`. Arguments: data: The text to be processed. pattern: The pattern to be checked. patternIndex: Index of current pattern. startIndex: String index, from which we start searching. Returns: String with placeholders instead of `ElementTree` elements. FrNTrz{}{}{}z{}{}{}{}rr)r!rr�ANCESTOR_EXCLUDES�lowerr>�getCompiledRegExp�finditer� handleMatchrO�match�startr#rcrr"rfr$r{�tag�_InlineProcessor__handleInline�poprd�_InlineProcessor__stashNoder@�formatrN�groups)r,r�rHrXr[� new_style�excluder�rRr�rO�leftDatar~rVs r�__applyPatternzInlineProcessor.__applyPattern�s���*�w��(F�G�G� ��0� &� &�G��}�}���$�.�0�0��U�A�~�%�%�%�1� � )��E�!�2�2�4�4�=�=�d�J�O�O� � ��#*�#6�#6�u�d�#C�#C� ��e�S��=�C�K��%�)�)�A�,�,�.�J� �E�����-�-�/�/�5�5�d�:�;�;�6G�H�H�E��K�Z�K�(�H�� "����>� !�� ��&�&�u�-�-�D��K�K��N�N�E��)�)�A�,�,�C� �<���s�?� "��$��$�$� ��d�i��):�;�;� �"�V�d�4�j�j�0� � �E�#�D�>�>� � �:�1� �N�1�1�%�)�/�/�2C�2C�D�D�D�)-�)<�)<� %� �L�1�,<�*�*�E�J�!�N�.�.�0�0�0� �:��)-�)<�)<� %� �L�*�*�E�J���&�&�t�W�\�\�^�^�<�<� � � O��?�?�4����<�#.��S�T�T� �<�<�=A�1�E� E��$�$�X�%*�[�[��^�^�%0�%�,�,�.�.��2D�F�F�GK�Q�O� Orr)�parents� list[str]c���g}|�J|�,|�|j�����|j�|��}|�J|���|�|��dS)zBuild the ancestor list.N)r{r�r�� parent_maprxrg�extend)r,rnr�r>s r�__build_ancestorsz!InlineProcessor.__build_ancestorsMs~��� �� ��!�� � ���!1�!1�!3�!3�4�4�4��_�(�(��0�0�F�� � ���������y�!�!�!�!�!rN�treer>�list[str] | Nonec�P�i|_|�gn |dd�}d�|���D��|_||fg}|�re|���\}}||_|�||j��g}|D�]�}|jr�t|jtj ��s�|j� |j � ����|j} d|_|� |�| ��|��} | D]} ||j| d<�|| z }|� || f��|j���|jr�|�|j��} t!jd��} d|_|� | | d��}| jr | j|_t%|���|��dz}|���|D].}||j|d<|�||d���/t-|��r.||j|<|� ||jdd�f�����|D]8\}} t/| ��D]#\}}|d}|�||���$�9|��e|S)a�Apply inline patterns to a parsed Markdown tree. Iterate over `Element`, find elements with inline tag, apply inline patterns and append newly created Elements to tree. To avoid further processing of string with inline patterns, instead of normal string, use subclass [`AtomicString`][markdown.util.AtomicString]: node.text = markdown.util.AtomicString("This will not be processed.") Arguments: tree: `Element` object, representing Markdown tree. ancestors: List of parent tag names that precede the tree node (if needed). Returns: An element tree object with applied inline patterns. Nc��i|] }|D]}||��� Sr+r+)�.0�p�cs r� <dictcomp>z'InlineProcessor.run.<locals>.<dictcomp>os'��@�@�@�A�a�@�@��1�a�@�@�@�@rr�dFr)rC�iterr�r�r>�!_InlineProcessor__build_ancestorsrcr!rr"r{r�r�rer�rd�etree�ElementrfrIrgrhr9� enumerate)r,r�r>� tree_parents�stack� currElementr�� insertQueuer~rc�lst�itemrd�dumby� tailResultrjrk�element�i�objs rr-zInlineProcessor.runWs���$>@���'�.�r�r�I�a�a�a�L� �@�@������@�@�@���� �%�&���) 0�#(�9�9�;�;� �K��$�D�N� � "� "�;��� ?� ?� ?��K�$� =� =���:� )�j��J�� 1�'�'� )��N�)�)�%�)�/�/�*;�*;�<�<�<� �:�D�!%�E�J��4�4��+�+�D�1�1�5���C�!$�9�9��38����Q��0�0��S�L�E��&�&��s�|�4�4�4��N�&�&�(�(�(��:� =��.�.�u�z�:�:�D�!�M�#�.�.�E�!%�E�J�!%�!;�!;�D�%��!O�!O�J��z�0�%*�Z�� ��{�+�+�1�1�%�8�8�1�<�C��&�&�(�(�(�$.�=�=��7B����� �4�#�*�*�3��� �<�<�<�<��u�:�:�=�-8�D�O�E�*��L�L�%������):�!;�<�<�<�� +� 0� 0� ���'��n�n�0�0�F�A�s�"�1�v�H��N�N�1�h�/�/�/�/�0�O�) 0�T� r)r r )r@r#r rA)rHr#rIrJr rK)rRrSr@r#r r#)r)rHr#rXrJr r#)T)rRr(r_r(r`rr ra)rHrmrnr(r`rr ro) r�r�rHr#rXrJr[rJr r�)rnr)r�r�r ra�N)r�r(r>r�r r()r.r/r0r1r?rUrwr�r�rzrerZr�r-r+rrrrKs"��������'�'�'�'����� #�#�#�#�$���� �����.*�*�*�*�*�B� O�O�O�O�O�l� NO�NO�NO�NO�NO�`"�"�"�"�E�E�E�E�E�E�Errc�"�eZdZdZd d�Zd d�Zd S) rz' Add line breaks to the html document. �elemr(r rac���d}|j�|j��r�|jdvr�|jr|j���s;t |��r,|j�|dj��r||_|D]6}|j�|j��r|�|���7|jr|j���s ||_dSdS)z8 Recursively add line breaks to `ElementTree` children. � )�code�prerN)r �is_block_levelr�rcryr9�_prettifyETreerd)r,r�r��es rr�z$PrettifyTreeprocessor._prettifyETree�s��� �� �7� !� !�$�(� +� +� +����0O�0O��I� �T�Y�_�_�%6�%6� ��D� � � �&*�g�&<�&<�T�!�W�[�&I�&I� ��� �� +� +���7�)�)�!�%�0�0�+��'�'��*�*�*���y� �� ��� 1� 1� ��D�I�I�I� � rr'c���|�|��|�d��}|D]9}|jr|j���sd|_�*d|jz|_�:|�d��}|D]s}t |��rb|djdkrQ|d}t |��s:|j�3tj|j� ��dz��|_�tdS)z7 Add line breaks to `Element` object and its children. �brr�z %sr�rr�N) r�r�rdryr9r�rcrr"�rstrip)r,r'�brsr��presr�r�s rr-zPrettifyTreeprocessor.run�s��� ���D�!�!�!��i�i��o�o��� +� +�B��7� +�"�'�-�-�/�/� +����� �2�7�*�����y�y������ M� M�C��3�x�x� M�C��F�J�&�0�0��1�v���4�y�y�M�T�Y�%:� $� 1�$�)�2B�2B�2D�2D�t�2K� L� L�D�I��  M� MrN)r�r(r ra�r'r(r ra)r.r/r0r1r�r-r+rrrr�sH������1�1� � � � �M�M�M�M�M�Mrrc��eZdZdZejd�ejej ����Z dd�Z dd �Z dd �Z dS)rz Restore escaped chars z {}(\d+){}rP� re.Match[str]r r#c�`�tt|�d������S)Nr)�chrrJrN)r,rPs r� _unescapezUnescapeTreeprocessor._unescape�s ���3�q�w�w�q�z�z�?�?�#�#�#rrcc�B�|j�|j|��Sr�)�RE�subr�)r,rcs rrzUnescapeTreeprocessor.unescape�s���w�{�{�4�>�4�0�0�0rr'r(rac�h�|���D]�}|jr*|jdks|�|j��|_|jr|�|j��|_|���D].\}}|�||�|�����/��dS)z/ Loop over all elements and unescape all text. r�N)r�rcr�rrd�items�set)r,r'r��key�values rr-zUnescapeTreeprocessor.run�s����I�I�K�K� 4� 4�D��y� 5���V�!3�!3� �M�M�$�)�4�4�� ��y� 5� �M�M�$�)�4�4�� �"�j�j�l�l� 4� 4� ��U�����d�m�m�E�2�2�3�3�3�3� 4� 4� 4rN)rPr�r r#)rcr#r r#r�)r.r/r0r1�re�compiler�r�STXr7r�r�rr-r+rrrr�s{������!�!� ���L�'�'���$�(�;�;� <� <�B�$�$�$�$�1�1�1�1� 4� 4� 4� 4� 4� 4rr)r r r rr r )rrr r)r1� __future__rr��xml.etree.ElementTreer�� ElementTree�typingrrrurr�markdownr rr$� Processorr&rrrr+rr�<module>r�s���(��#�"�"�"�"�"� � � � �%�%�%�%�%�%�%�%�%�%�%�%�%�%�%�%�%��������������"�!�!�!�!�!�!��������� � � � � �D�N� � � �(Q�Q�Q�Q�Q�m�Q�Q�Q�h $M�$M�$M�$M�$M�M�$M�$M�$M�N4�4�4�4�4�M�4�4�4�4�4r
Memory