ó
    Í>j¨  ã                  óÚ   • S r SSKJr  SSKrSSKrSSKJr  SSKJr  SSK	J
r
JrJrJr  SSKJr  SSKJr  SS	KJr  \\\\\4   rS
rSrSS jrSS jrSS jrSS jr\S.       SS jjrg)aN  Before/after extraction review HTML.

One row per table, two columns: the page cropped around the table with its
Set-of-Mark bounding box drawn (``before``), beside the final corrected
markdown (``after``). The tables are supplied already-extracted, so this does
no extraction of its own -- it only renders what the caller passes in.
é    )ÚannotationsN)Úescape)ÚPath)ÚDictÚListÚSequenceÚTuple)Ú	ImageDraw)ÚImage)ÚExtractedTableé–   a.  
body{font-family:-apple-system,Segoe UI,Roboto,sans-serif;margin:24px;color:#1a1a1a;background:#fafafa}
h1{font-size:22px} h2{margin-top:40px;border-bottom:2px solid #ccc;padding-bottom:4px}
h3{margin:28px 0 6px;font-size:15px}
.meta{color:#666;font-size:12px;margin-bottom:6px}
.row{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr);gap:16px;align-items:start;
  border:1px solid #e0e0e0;border-radius:8px;padding:12px;background:#fff;margin-bottom:18px}
.col{min-width:0;overflow-x:auto}
.col h4{margin:0 0 6px;font-size:12px;text-transform:uppercase;letter-spacing:.04em;color:#888}
.col img{max-width:100%;border:1px solid #ddd}
table.t{border-collapse:collapse;font-size:11px;width:100%}
table.t th,table.t td{border:1px solid #ccc;padding:2px 5px;text-align:right;vertical-align:top}
table.t th{background:#f0f0f0;font-weight:600}
.legend{font-size:12px;color:#555;margin:8px 0 20px}
.nomatch{color:#b00;font-style:italic}
.units{color:#0a5;font-weight:600}
@media(max-width:900px){.row{grid-template-columns:1fr}.col img{max-width:none;width:100%}}
c                óV   • SSK Jn  U" [        U 5      USS9n[        [	        USS95      $ )zGRasterize every page of the PDF to a PIL image keyed by 1-indexed page.r   )Úconvert_from_pathÚpng)ÚdpiÚfmté   ©Ústart)Ú	pdf2imager   ÚstrÚdictÚ	enumerate)Úpdfr   r   Úimgss       Ú//home/mande/repo/quber/src/quber/review/html.pyÚrender_pagesr   -   s)   € å+áœS ›X¨3°EÑ:€DÜ”	˜$ aÑ(Ó)Ð)ó    c           
     óP  • U R                   u  p4Uu  pVpx[        XW5      [        XW5      p©[        Xh5      [        Xh5      pËSu  pÞ[        [        SX�-
  5      U-  5      [        [        SX¾-
  5      U-  5      [        [        SX­-   5      U-  5      [        [        SXÎ-   5      U-  5      4nUu  nnnn[        UU5      [        UU5      nn[        UU5      [        UU5      nnU R	                  5       n[
        R                  " U5      R                  UU-  UU-  UU-  UU-  /SSS9  US   US   :”  a  US   US	   :”  a  UR                  U5      OUn[        R                  " 5       nUR                  US
5        [        R                  " UR                  5       5      R                  5       $ )uª  Draw `box` on the page and crop around `crop_region` (both normalized
0..1, top-left), with a small margin so the box is inset and visible.

`box` is the table's final boundary (content_region â€” the corrected end of
the table at its last data row). The crop is taken around the broader
`crop_region` (som_region) so the area just below the box, where footnotes
sit, stays visible and the box is seen to correctly exclude it.)gš™™™™™™?gú~j¼t“ˆ?g        g      ð?)r   éZ   éë   é   )ÚoutlineÚwidthé   r   r   ÚPNG)ÚsizeÚminÚmaxÚintÚcopyr
   ÚDrawÚ	rectangleÚcropÚioÚBytesIOÚsaveÚbase64Ú	b64encodeÚgetvalueÚdecode)ÚimgÚboxÚcrop_regionÚw_pxÚh_pxÚcx0Úcy0Úcx1Úcy1ÚcleftÚcrightÚctopÚcbottomÚmxÚmyÚcrop_boxÚbx0Úby0Úbx1Úby1ÚbleftÚbrightÚbtopÚbbottomÚcanvasr.   Úbufs                              r   Ú
before_b64rP   5   sŒ  € ð —‘�J€DØ$Ñ€CˆcÜ˜“M¤3 s£=ˆ6Ü˜“M¤3 s£=ˆ'Ø�F€BäŒC��U‘ZÓ  4Ñ'Ó(ÜŒC��T‘YÓ $Ñ&Ó'ÜŒC��V‘[Ó! DÑ(Ó)ÜŒC��W‘\Ó" TÑ)Ó*ð	€Hð Ñ€Cˆˆc�3Ü˜˜S“M¤3 s¨C£=ˆ6€EÜ˜˜S“M¤3 s¨C£=ˆ'€DØ�X‰X‹Z€FÜ‡N‚N�6Ó×$Ñ$Ø	�‰�t˜d‘{ F¨T¡M°7¸T±>ÐBÈLÐ`að %ñ ð %-¨Q¡K°(¸1±+Ó$=À(È1Á+ÐPXÐYZÑP[ÓB[ˆ6�;‰;�xÔ Ðag€DÜ
�*Š*‹,€CØ‡I�Iˆc�5ÔÜ×Ò˜CŸL™L›NÓ+×2Ñ2Ó4Ð4r   c           	     ó€  ^• U (       d  g[        S U  5       5      n/ n[        U 5       Hw  u  p4U Vs/ s H  n[        [        U5      5      PM     snS/U[	        U5      -
  -  -   nUS:X  a  SOSmUR                  SSR                  U4S jU 5       5      -   S	-   5        My     S
SR                  U5      -   S-   $ s  snf )Nz<em>(empty)</em>c              3  ó8   #   • U  H  n[        U5      v •  M     g 7f)N)Úlen)Ú.0Úrs     r   Ú	<genexpr>Úgrid_html.<locals>.<genexpr>X   s   é € Ð&¢˜1”�A—�¢ùs   ‚Ú r   ÚthÚtdz<tr>c              3  ó<   >#   • U  H  nS T SU ST S3v •  M     g7f)Ú<Ú>z</N© )rT   ÚcÚtags     €r   rV   rW   ]   s&   øé € Ð$KÂF¸q q¨¨¨Q¨q¨c°°C°5¸Õ%:ÂFùs   ƒz</tr>z<table class="t">z</table>)r)   r   r   r   rS   ÚappendÚjoin)Úcellsr$   ÚrowsÚirU   r_   Úpaddedr`   s          @r   Ú	grid_htmlrg   U   s°   ø€ ÞØ!ÜÑ&¡Ó&Ó&€EØ€DÜ˜%Ö ‰ˆÙ*+Ó,ª! Q”&œ˜Q›–.©!Ñ,°¨t°u¼sÀ1»v±~Ñ/FÑFˆØ˜1“f‰d $ˆØ�‰�F˜RŸW™WÔ$KÁFÓ$KÓKÑKÈgÑUÖVñ !ð  §¡¨£Ñ.°Ñ;Ð;ùò -s   ²B;c                ó€  • / nU R                  5        H™  nUR                  5       nUR                  S5      (       d  M+  UR                  S5      R                  S5       Vs/ s H  o3R                  5       PM     nnU(       a  [	        S U 5       5      (       a  Mˆ  UR                  U5        M›     [        U5      $ s  snf )NÚ|c              3  óB   #   • U  H  n[        U5      1 S k:*  v •  M     g7f)>   Ú Ú-Ú:N)Úset)rT   r_   s     r   rV   Úmd_to_html.<locals>.<genexpr>h   s   é € ÐBºE°qœ˜Q›¢?Ö2ºEùs   ‚)Ú
splitlinesÚstripÚ
startswithÚsplitÚallra   rg   )Úmdrd   Úliner_   rc   s        r   Ú
md_to_htmlrw   a   s•   € Ø€DØ—‘–ˆØ�z‰z‹|ˆØ�‰˜s×#Ñ#ÙØ$(§J¡J¨s£O×$9Ñ$9¸#Ô$>Ó?Ò$>˜q—‘–Ñ$>ˆÐ?Þ”SÑB¹EÓB×BÑBÙØ�‰�EÖñ  ô �T‹?Ðùò	 @s   Á B;)r   c               ó(  • SSS[          S3SS/nU  GHÊ  u  pE[        XB5      nUR                  S[        UR                  5       S35        UR                  S	[        U5       S
35        [        USS9 GHd  u  pxUR                  UR                  5      n	UR                  =(       d    UR                  n
UR                  =(       d    UR                  nU	b  U
(       a  U(       a  [        XšU5      OSn[        UR                  =(       d    S5      nUR                  SUR                   SU SU S35        UR                  (       a  S[        UR                  5       S3OSnUR                  SU SUR                   SUR                  S SUR                    S3	5        U(       a  SU S3OSnUR                  SU S [#        UR$                  5       S!35        GMg     GMÍ     UR                  S"5        UR'                  S#R)                  U5      5        U$ )$zßRender the before/after review HTML for one or more already-extracted PDFs.

``items`` pairs each source PDF with the tables already extracted from it.
One ``<h2>`` section per PDF, one row per table. Returns ``out_path``.
z1<!DOCTYPE html><html><head><meta charset='utf-8'>zD<meta name='viewport' content='width=device-width, initial-scale=1'>z<style>z</style></head><body>ze<h1>Set-of-Mark extraction review &mdash; before (table + bounding box) | after (final markdown)</h1>a  <div class='legend'>Left: the page cropped around the table with its final boundary (content_region, the corrected end of the table) drawn; the crop extends a little below so any footnotes sit visibly outside the box. Right: the final corrected markdown. One row per table.</div>z<h2>z</h2>z<div class='meta'>z tables</div>r   r   Nz
(untitled)z<h3>pz &middot; table z
 &middot; z</h3>z<span class='units'>z</span>z(none)z<div class='meta'>units=z &middot; flavor=z &middot; camelot_accuracy=z.1fz &middot; llm_corrected=z</div>z <img src='data:image/png;base64,z'>z2<span class='nomatch'>no Set-of-Mark region</span>zN<div class='row'><div class='col'><h4>before &mdash; table + bounding box</h4>z<</div><div class='col'><h4>after &mdash; final markdown</h4>z</div></div>z</body></html>rX   )ÚCSSr   ra   r   ÚnamerS   r   ÚgetÚpageÚcontent_regionÚ
som_regionrP   ÚtitleÚunitsÚflavorÚcamelot_accuracyÚllm_correctedrw   ÚmarkdownÚ
write_textrb   )ÚitemsÚout_pathr   Úpartsr   ÚtablesÚimagesÚidxÚftr6   r7   r8   Úbeforer   r€   Úimg_htmls                   r   Úrender_review_htmlr�   n   s  € ð 	<ØNØ
”#�Ð+Ð,Øoð	sð€Eô ‰ˆÜ˜cÓ'ˆØ�‰�tœF 3§8¡8Ó,Ð-¨UÐ3Ô4Ø�‰Ð)¬#¨f«+¨°mÐDÔEä  ¨qÕ1‰GˆCØ—*‘*˜RŸW™WÓ%ˆCð ×#Ñ#×4 r§}¡}ˆCØŸ-™-×<¨2×+<Ñ+<ˆKØ;>¹?ÎsÖWb”Z ¨+Ô6ÐimˆFÜ˜2Ÿ8™8×3 |Ó4ˆEØ�L‰L˜5 §¡ 	Ð)9¸#¸¸jÈÈÈuÐUÔVØHJÏÏÐ*¬6°"·(±(Ó+;Ð*<¸GÑDÐV^ˆEØ�L‰LØ*¨5¨'Ð1BÀ2Ç9Á9À+ð N$Ø$&×$7Ñ$7¸Ð#<Ð<TÐUW×UeÑUeÐTfÐflðnôö ð 3°6°(¸"Ñ=àIð ð
 �L‰LðPØPXÈzð ZIÜISÐTV×T_ÑT_ÓI`ÐHað bð÷ô' 2ñ ð> 
‡L�LÐ!Ô"Ø×Ñ˜Ÿ™ ›Ô'Ø€Or   )r   r   r   r*   ÚreturnzDict[int, Image])r6   r   r7   ÚBoxr8   r‘   r�   r   )rc   zList[List[str]]r�   r   )ru   r   r�   r   )r†   z/Sequence[Tuple[Path, Sequence[ExtractedTable]]]r‡   r   r   r*   r�   r   )Ú__doc__Ú
__future__r   r2   r/   Úhtmlr   Úpathlibr   Útypingr   r   r   r	   ÚPILr
   Ú	PIL.Imager   Úquber.core.extractorsr   Úfloatr‘   ÚDEFAULT_DPIry   r   rP   rg   rw   r�   r^   r   r   Ú<module>rœ      sŒ   ðñõ #ã Û 	Ý Ý ß .Ó .å Ý å 0àˆE�5˜% Ð&Ñ'€Ø€ð€ô(*ô5ô@	<ô
ð" ñ	6Ø:ð6àð6ð 
ð	6ð
 
ö6r   