§
    �Øj?  ã                  ó@   — d dl mZ 	 d dlZddlmZ dd„Zdd„Zdd„ZdS )é    )ÚannotationsNé   )Úhtml_documentÚreturnútuple[int, int, list, list]c           	     ó‚  ‡— t          | ¦  «        }t          ¦   «         }dŠt          | ¦  «        D ]‚\  }}d}|D ]x}||f|v r|dz  }||f|v °t          |j        ¦  «        D ]6}t          |j        ¦  «        D ]}|                     ||z   ||z   f¦  «         Œ Œ7||j        z  }t          ‰|¦  «        ŠŒyŒƒˆfd„t          |¦  «        D ¦   «         }	ˆfd„t          |¦  «        D ¦   «         }
t          ¦   «         }t          | ¦  «        D ]´\  }}d}|D ]ª}||f|v r|dz  }||f|v °|‰k    r n�|j        �t          |j        ¦  «        nd|	|         |<   |j
        |
|         |<   t          |j        ¦  «        D ]:}t          |j        ¦  «        D ]#}|s|r|                     ||z   ||z   f¦  «         Œ$Œ;||j        z  }Œ«Œµ|‰|	|
fS )aÏ  Derive ``(row_count, col_count, cells, extract)`` from a placement grid.

    ``placements`` is a ``Table.placements`` row-major grid of ``SpanCell``
    colspan/rowspan cells. ``row_count`` is the ``<tr>`` count; ``col_count`` the
    column extent after resolving spans (the HTML column count). ``cells`` and
    ``extract`` are the ``row_count x col_count`` post-span bbox and plain-text
    matrices, ``None`` where a span covers a slot or the grid has a gap.r   r   c                ó   •— g | ]}d g‰z  ‘Œ	S ©N© ©Ú.0Ú_Ú	col_counts     €úŸ/Users/tedpeters/Documents/Projects/Claude Projects/Misc Tools/notename-sheets/.venv/lib/python3.11/site-packages/pymupdf4llm/helpers/table_html/reconstruct.pyú
<listcomp>z,_placement_grid_matrices.<locals>.<listcomp>&   ó    ø€ Ð>Ð>Ð>¨�$�˜)Ñ#Ð>Ð>Ð>ó    c                ó   •— g | ]}d g‰z  ‘Œ	S r
   r   r   s     €r   r   z,_placement_grid_matrices.<locals>.<listcomp>'   r   r   N)ÚlenÚsetÚ	enumerateÚrangeÚrowspanÚcolspanÚaddÚmaxÚbboxÚlistÚtext)Ú
placementsÚ	row_countÚoccupiedÚrow_idxÚrowÚcol_idxÚcellÚdrÚdcÚ	bbox_gridÚ	text_gridÚcoveredr   s               @r   Ú_placement_grid_matricesr,      sm  ø€ õ �J‘”€Iõ ‰uŒu€HØ€IÝ! *Ñ-Ô-ð 	0ð 	0‰ˆ�ØˆØð 	0ð 	0ˆDØ˜GÐ$¨Ð0Ð0Ø˜1‘�ð ˜GÐ$¨Ð0Ð0å˜DœLÑ)Ô)ð ?ð ?�Ý ¤Ñ-Ô-ð ?ð ?�BØ—L’L '¨B¡,°¸"±Ð!=Ñ>Ô>Ð>Ð>ð?à�t”|Ñ#ˆGÝ˜I wÑ/Ô/ˆIˆIð	0ð ?Ð>Ð>Ð>­U°9Ñ-=Ô-=Ð>Ñ>Ô>€IØ>Ð>Ð>Ð>­U°9Ñ-=Ô-=Ð>Ñ>Ô>€IÝ‰eŒe€GÝ! *Ñ-Ô-ð $ð $‰ˆ�ØˆØð 	$ð 	$ˆDØ˜GÐ$¨Ð/Ð/Ø˜1‘�ð ˜GÐ$¨Ð/Ð/à˜)Ò#Ð#Ø�Ø=A¼YÐ=R­$¨t¬y©/¬/¨/ÐX\ˆI�gÔ˜wÑ'Ø*.¬)ˆI�gÔ˜wÑ'Ý˜DœLÑ)Ô)ð Bð B�Ý ¤Ñ-Ô-ð Bð B�BØð B˜Rð BØŸš W¨r¡\°7¸R±<Ð$@ÑAÔAÐAøðBð �t”|Ñ#ˆGˆGøØ�i ¨IÐ5Ð5r   c                ó¤  — t          | t          j        ¦  «         }|rt          j        | ¦  «        n| }	 ||         }|                     ¦   «          |                     ddd¬¦  «        }d„ t          |dd¦  «        pg D ¦   «         }|r|                     ¦   «          n# |r|                     ¦   «          w w xY wt          d„ |D ¦   «         ¦  «        S )a°  Reconstruct the tables on one PDF page and return them as an HTML string.

    pdf        : PDF file path (str/Path) or an already-open pymupdf.Document.
    page_index : 0-based page number.
    returns    : concatenated <table>...</table> HTML (empty string if no tables).

    When ``pdf`` is an already-open Document, the target page is derotated in
    place via ``page.remove_rotation()`` (mutates the caller-owned page).
    T©Ú
use_layoutÚunionÚrefinec                ó6   — g | ]}|                      ¦   «         ‘ŒS r   )Úto_html)r   Útabs     r   r   zto_html.<locals>.<listcomp>J   s    € ÐOÐOÐO C�#—+’+‘-”-ÐOÐOÐOr   ÚtablesNc                ó   — g | ]}d |i‘ŒS )Úhtmlr   )r   Úhs     r   r   zto_html.<locals>.<listcomp>N   s   € Ð6Ð6Ð6¨!˜6 1˜+Ð6Ð6Ð6r   )	Ú
isinstanceÚpymupdfÚDocumentÚopenÚremove_rotationÚfind_tablesÚgetattrÚcloser   )ÚpdfÚ
page_indexÚowns_docÚdocÚpageÚtfr5   s          r   r3   r3   :   så   € õ ˜c¥7Ô#3Ñ4Ô4Ð4€HØ'Ð
0�'Œ,�sÑ
Ô
Ð
¨S€CðØ�:ŒˆØ×ÒÑÔÐØ×Ò¨°TÀ$ÐÑGÔGˆØOÐO­G°B¸À$Ñ,GÔ,GÐ,MÈ2ÐOÑOÔOˆàð 	Ø�IŠI‰KŒKˆKøøð ð 	Ø�IŠI‰KŒKˆKˆKð	øøøåÐ6Ð6¨vÐ6Ñ6Ô6Ñ7Ô7Ð7s   µAB ÂB6rE   úpymupdf.Pageú4list[tuple[pymupdf.Rect, str, int, int, list, list]]c           	     ó   — |                       ddd¬¦  «        }g }t          |dd¦  «        pg D ]_}t          |j        ¦  «        \  }}}}|                     t          j        |j        ¦  «        |                     ¦   «         ||||f¦  «         Œ`|S )a«  Reconstruct one already-open page's tables as payload tuples.

    Returns one ``(bbox, html, rows, cols, cells, extract)`` tuple per table, in
    reading order, for the markdown/JSON renderers to drive table emission,
    reading order and body-text exclusion:

    * ``bbox``    -- ``tab.bbox`` (a grid-ref table keeps its reported layout box);
    * ``html``    -- ``tab.to_html()``;
    * ``rows``/``cols``/``cells``/``extract`` -- the reconstructed grid the
      ``html`` shows (see :func:`_placement_grid_matrices`): ``extract`` is the
      per-cell plain-text matrix (``None`` for span-covered slots / grid gaps)
      and ``cells`` the matching post-span bbox matrix, both ``rows x cols``.

    The caller must remove page rotation before calling. Not thread-safe on a
    shared Page: core caches word/vector extraction as attributes on the given
    ``page``, so concurrent calls must each use their own ``pymupdf.Page``.
    Tr.   r5   N)	r>   r?   r,   r    Úappendr:   ÚRectr   r3   )rE   rF   Úresultr4   r!   r   ÚcellsÚextracts           r   Úpage_html_tablesrO   Q   s¨   € ð$ 
×	Ò	 T°¸dÐ	Ñ	CÔ	C€BØ€FÝ˜˜H dÑ+Ô+Ð1¨rð 
ð 
ˆÝ/GÈÌÑ/WÔ/WÑ,ˆ	�9˜e WØ�Šå”˜SœXÑ&Ô&Ø—’‘”ØØØØðñ		
ô 		
ð 		
ð 		
ð €Mr   )r   r   )r   )rE   rG   r   rH   )Ú
__future__r   r:   Úcorer   r,   r3   rO   r   r   r   ú<module>rR      s€   ðØ "Ð "Ð "Ð "Ð "Ð "ðð €€€Ø Ð Ð Ð Ð Ð ð*6ð *6ð *6ð *6ðZ8ð 8ð 8ð 8ð. ð  ð  ð  ð  ð  r   