§
    �Øj>  ã                   óV   — d Z ddlmZmZmZmZmZmZmZ g d¢Z	d„ Z
d„ Zd„ Zd„ Zdd
„ZdS )uŠ  Canonical text source for chunking: render one LayoutBox to markdown.

This module is the single place where the chunking package calls the
markdown renderers in ``document_layout``.  ``box_to_markdown`` mirrors the
per-box dispatch of ``ParsedDocument.to_markdown`` so that chunk text is the
same markdown the document renderer emits for that box â€” upstream renderer
signature drift is absorbed here and nowhere else.

Documented deviations from ``to_markdown``:

- picture/formula boxes without text are not rendered here at all; the
  chunker represents them with a placeholder (see ``figure_placeholder``).
- formula boxes *with* textlines are rendered via ``text_to_md``.
  ``to_markdown`` drops that text (it only emits the image reference), but
  dropping it from chunks would lose indexable content.
- image files / base64 payloads are never inlined into chunk text; images
  travel in metadata.
é   )Úcreate_list_item_levelsÚfootnote_to_mdÚlist_item_to_mdÚpicture_text_to_mdÚsection_hdr_to_mdÚ
text_to_mdÚtitle_to_md)Úbox_to_markdownr   Úextract_table_headersÚfigure_placeholderÚtable_contentc                 ó   — t          | j        t          ¦  «        r| j        ni }|                     d¦  «        }|€8|                     d¦  «        }|r!d                     d„ |D ¦   «         ¦  «        pd}|�d|fS |                     d¦  «        dfS )ua  Adapter for ``box.table`` â€” the only place chunking reads its schema.

    Returns ``(markdown, html)`` where at most one side is a string,
    mirroring the ``to_markdown`` branching: when the parse ran with HTML
    table rendering, the table dict carries the canonical rendering under
    ``"html"`` and ``markdown`` is ``None``; otherwise ``markdown`` is the
    canonical rendering (possibly ``""`` for a degenerate table) and
    ``html`` is ``None``.

    Routing uses ``is not None`` on purpose: a degenerate table carries
    ``markdown == ""`` and must still route as markdown, never as html.
    ÚhtmlNÚhtml_tablesz

c              3   ól   K  — | ]/}t          |t          ¦  «        ¯|                     d d¦  «        V — Œ0dS )r   Ú N)Ú
isinstanceÚdictÚget)Ú.0Úts     ú�/Users/tedpeters/Documents/Projects/Claude Projects/Misc Tools/notename-sheets/.venv/lib/python3.11/site-packages/pymupdf4llm/helpers/chunking/text_source.pyú	<genexpr>z table_content.<locals>.<genexpr>;   sS   è è € ð ð Ø&'½
À1ÅdÑ8KÔ8KðØ—’�f˜bÑ!Ô!ðð ð ð ð ð ó    Úmarkdown)r   Útabler   r   Újoin)Úboxr   r   Ú	per_tables       r   r   r   '   s´   € õ $ C¤I­tÑ4Ô4Ð<ˆCŒIˆI¸"€EØ�9Š9�VÑÔ€DØ€|ð —I’I˜mÑ,Ô,ˆ	Øð 	Ø—;’;ð ð Ø+4ðñ ô ñ ô ð àð ð ÐØ�TˆzÐØ�9Š9�ZÑ Ô  $Ð&Ð&r   c                 ó~   — | sg S ddl m}  G d„ d|¦  «        } |¦   «         }|                     | ¦  «         |j        S )u  Header cell texts from a table's HTML rendering (D13).

    Header identity comes exclusively from ``<th>`` elements emitted by the
    table engine â€” no local header heuristics.  Returns ``[]`` when the
    rendering carries no ``<th>`` (including all markdown-mode parses).
    é    )Ú
HTMLParserc                   ó0   ‡ — e Zd Zˆ fd„Zd„ Zd„ Zd„ Zˆ xZS )ú+extract_table_headers.<locals>._THCollectorc                 ór   •— t          ¦   «                              ¦   «          d| _        g | _        g | _        d S )Nr!   )ÚsuperÚ__init__Ú_depthÚ_partsÚheaders)ÚselfÚ	__class__s    €r   r'   z4extract_table_headers.<locals>._THCollector.__init__P   s2   ø€ Ý‰GŒG×ÒÑÔÐØˆDŒKØˆDŒKØˆDŒLˆLˆLr   c                 óD   — |dk    r| xj         dz  c_         g | _        d S d S )NÚthé   )r(   r)   )r+   ÚtagÚattrss      r   Úhandle_starttagz;extract_table_headers.<locals>._THCollector.handle_starttagV   s-   € Ø�dŠ{ˆ{Ø�”˜qÑ �”Ø �”��ð ˆ{r   c                 óÐ   — |dk    r]| j         rX| xj         dz  c_         | j                             d                     | j        ¦  «                             ¦   «         ¦  «         d S d S d S )Nr.   r/   r   )r(   r*   Úappendr   r)   Ústrip)r+   r0   s     r   Úhandle_endtagz9extract_table_headers.<locals>._THCollector.handle_endtag[   sb   € Ø�dŠ{ˆ{˜tœ{ˆ{Ø�”˜qÑ �”Ø”×#Ò# B§G¢G¨D¬KÑ$8Ô$8×$>Ò$>Ñ$@Ô$@ÑAÔAÐAÐAÐAð ˆ{ˆ{ˆ{r   c                 óL   — | j         r| j                             |¦  «         d S d S )N)r(   r)   r4   )r+   Údatas     r   Úhandle_dataz7extract_table_headers.<locals>._THCollector.handle_data`   s2   € ØŒ{ð )Ø”×"Ò" 4Ñ(Ô(Ð(Ð(Ð(ð)ð )r   )Ú__name__Ú
__module__Ú__qualname__r'   r2   r6   r9   Ú__classcell__)r,   s   @r   Ú_THCollectorr$   O   sh   ø€ € € € € ð	ð 	ð 	ð 	ð 	ð	!ð 	!ð 	!ð
	Bð 	Bð 	Bð
	)ð 	)ð 	)ð 	)ð 	)ð 	)ð 	)r   r>   )Úhtml.parserr"   Úfeedr*   )r   r"   r>   Ú	collectors       r   r   r   C   su   € ð ð Øˆ	à&Ð&Ð&Ð&Ð&Ð&ð)ð )ð )ð )ð )�zñ )ô )ð )ð* �‘”€IØ‡N‚N�4ÑÔÐØÔÐr   c                 ó   — d„ | D ¦   «         S )u®  Copy textlines deep enough to isolate renderer side effects.

    Some renderers mutate their input in place (``footnote_to_md`` extends
    the first line's span list, ``list_item_to_md`` rewrites/pops spans), so
    rendering the same box twice â€” e.g. ``to_chunks`` followed by
    ``to_markdown`` â€” would duplicate text.  Rendering from a copy keeps
    ``box_to_markdown`` free of side effects on the parsed document.
    c           	      óZ   — g | ](}i |¥d d„ |                      d g ¦  «        D ¦   «         i¥‘Œ)S )Úspansc                 ó,   — g | ]}t          |¦  «        ‘ŒS © )r   )r   Úss     r   ú
<listcomp>z._copy_textlines.<locals>.<listcomp>.<listcomp>s   s   € Ð>Ð>Ð> Q�˜a™œÐ>Ð>Ð>r   )r   )r   Útls     r   rH   z#_copy_textlines.<locals>.<listcomp>r   sU   € ð ð ð àð 	@ˆ2Ð?ˆwÐ>Ð>¨"¯&ª&°¸"Ñ*=Ô*=Ð>Ñ>Ô>Ð?Ð?ðð ð r   rF   )Ú	textliness    r   Ú_copy_textlinesrK   i   s#   € ðð àðñ ô ð r   c                 óŠ   — t          |j        |j        z
  ¦  «        t          |j        |j        z
  ¦  «        }}d| › d|› d|› d�S )z>Stable text placeholder for a figure/formula box without text.z	[Figure fz: Úxú])ÚintÚx1Úx0Úy1Úy0)Ú
fig_numberr   ÚwÚhs       r   r   r   x   sL   € åˆsŒv˜œ‰ÑÔ¥ S¤V¨c¬f¡_Ñ!5Ô!5€q€AØ-�zÐ-Ð- QÐ-Ð-¨Ð-Ð-Ð-Ð-r   NFc                 ó  — |j         }|p| j        }|dv r€t          |¦  «        \  }}|�|S |pd}| j        r|                     dd¦  «        }|                     ¦   «         r|S t          |dd¦  «        }	|	rt          t          |	¦  «        |¬¦  «        S dS t          |dd¦  «        }	|	sdS t          |	¦  «        }	|dk    rt          |	|¬¦  «        S |dk    rt          |	|¬¦  «        S |d	k    rt          |j
        |	¦  «        S |d
k    rt          |j
        |	¦  «        S |dk    r:|€t          | j        ¦  «        }t          |	|                     |d¦  «        ¦  «        S |dk    rt!          |	¦  «        S t          |	|¬¦  «        S )a¦  Render one LayoutBox to markdown, mirroring ``to_markdown`` dispatch.

    Args:
        page: the PageLayout owning the box (``full_ocred`` is honored).
        box: the LayoutBox to render.
        box_idx: index of the box on its page (list-item level lookup).
        list_item_levels: optional precomputed ``create_list_item_levels``
            result for the page; computed on demand when omitted.
        ignore_code: suppress code-block styling (as in ``to_markdown``).

    Returns the markdown string for the box ("" when there is nothing to
    render).  Trailing block separators ("\n\n") are preserved as emitted
    by the renderers; callers strip as needed.
    )r   ztable-fallbackNr   ú`rJ   )Úignore_codeÚpictureÚformulaÚtitlezsection-headerz	list-itemr/   Úfootnote)ÚboxclassÚ
full_ocredr   Úreplacer5   Úgetattrr   rK   r   r	   Úheader_levelr   r   Úboxesr   r   r   )
Úpager   Úbox_idxÚlist_item_levelsrY   Úbtyper   r   Ú
table_textrJ   s
             r   r
   r
   ~   sÍ  € ð ŒL€EØÐ0 ¤€KàÐ+Ð+Ð+Ý& sÑ+Ô+‰ˆ�$ØÐØˆKØ�^ ˆ
ØŒ?ð 	5à#×+Ò+¨C°Ñ4Ô4ˆJØ×ÒÑÔð 	ØÐå˜C ¨dÑ3Ô3ˆ	Øð 	SÝ�o¨iÑ8Ô8ÀkÐRÑRÔRÐRØˆrå˜˜[¨$Ñ/Ô/€IØð ØˆrÝ 	Ñ*Ô*€Ià�	ÒÐÝ! )¸ÐEÑEÔEÐEØ�	ÒÐå˜)°Ð=Ñ=Ô=Ð=Ø�ÒÐÝ˜3Ô+¨YÑ7Ô7Ð7ØÐ Ò Ð Ý  Ô!1°9Ñ=Ô=Ð=Ø�ÒÐØÐ#Ý6°t´zÑBÔBÐÝ˜yÐ*:×*>Ò*>¸wÈÑ*JÔ*JÑKÔKÐKØ�
ÒÐÝ˜iÑ(Ô(Ð(Ý�i¨[Ð9Ñ9Ô9Ð9r   )NF)Ú__doc__Údocument_layoutr   r   r   r   r   r   r	   Ú__all__r   r   rK   r   r
   rF   r   r   ú<module>rl      sÕ   ððð ð&ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ðð ð €ð'ð 'ð 'ð8#ð #ð #ðLð ð ð.ð .ð .ð6:ð 6:ð 6:ð 6:ð 6:ð 6:r   