a
    ¡˜ëjÀ  ã                   @  s¢   d Z ddlmZ ddlZddlmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ erdddlmZ d	d
dœdd„ZG dd„ dƒZG dd„ dƒZG dd„ dƒZdS )z�Provides operations related to the HTML table stored in `.metadata.text_as_html`.

Used during partitioning as well as chunking.
é    )ÚannotationsN)ÚTYPE_CHECKINGÚIteratorÚSequenceÚcast)Úetree)Úfragment_fromstring)Úlazyproperty)ÚHtmlElementúSequence[Sequence[str]]Ústr)ÚmatrixÚreturnc                   sB   dddœ‡ fdd„}dddœdd	„‰ | r>d
d  || ƒ¡› d�S dS )a¡  Form an HTML table from "rows" and "columns" of `matrix`.

    Character overhead is minimized:
    - No whitespace padding is added for human readability
    - No newlines ("
") are added
    - No `<thead>`, `<tbody>`, or `<tfoot>` elements are used; we can't tell where those might be
      semantically appropriate anyway so at best they would consume unnecessary space and at worst
      would be misleading.
    r   úIterator[str])Úrows_of_cell_strsr   c                 3  s,   | D ]"}|sqdd  ˆ |ƒ¡› d�V  qd S )Nz<tr>Ú z</tr>©Újoin)r   Úrow_cell_strs©Úiter_tds© ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/common/html_table.pyÚiter_trs   s    z.htmlify_matrix_of_cell_texts.<locals>.iter_trszSequence[str])r   r   c                 s  sL   | D ]B}t  |¡}d | d¡¡}d | ¡ ¡}|r@d|› d�ndV  qd S )Nz<br/>Ú
ú z<td>z</td>ú<td/>)ÚhtmlÚescaper   Úsplit)r   ÚsZ	cell_textr   r   r   r   &   s
    
z.htmlify_matrix_of_cell_texts.<locals>.iter_tdsz<table>r   z</table>r   )r   r   r   r   r   Úhtmlify_matrix_of_cell_texts   s    r!   c                   @  sd   e Zd ZdZddœdd„Zedd dœdd	„ƒZedd
œdd„ƒZdd
œdd„Z	edd
œdd„ƒZ
dS )Ú	HtmlTablezA `<table>` element.r
   )Útablec                 C  s
   || _ d S ©N)Ú_table)Úselfr#   r   r   r   Ú__init__7   s    zHtmlTable.__init__r   )Ú	html_textr   c                 C  s–   t |ƒ}| d¡}|stdƒ‚|d }| d¡}|D ]}| ¡  q4| ¡ D ]B}|j ¡  |jdkrhd|_|jr€d 	|j 
¡ ¡|_|jrJd |_qJ| |ƒS )Nz//tablez)`html_text` contains no `<table>` elementr   z.//thead | .//tbody | .//tfootÚthÚtdr   )r   ÚxpathÚ
ValueErrorZdrop_tagÚiterÚattribÚclearÚtagÚtextr   r   Útail)Úclsr(   ÚrootZtablesr#   Znoise_elementsÚer   r   r   Úfrom_html_text:   s"    




zHtmlTable.from_html_text©r   c                 C  s   t j| jtd�S )a˜  The HTML-fragment for this `<table>` element, all on one line.

        Like: `<table><tr><td>foo</td></tr><tr><td>bar</td></tr></table>`

        The HTML contains no human-readability whitespace, attributes, or `<thead>`, `<tbody>`, or
        `<tfoot>` tags. It is made as compact as possible to maximize the semantic content in a
        given space. This is particularly important for chunking.
        ©Úencoding)r   Útostringr%   r   ©r&   r   r   r   r   ]   s    
zHtmlTable.htmlzIterator[HtmlRow]c                 c  s&   dd„ t d| j d¡ƒD ƒE d H  d S )Nc                 s  s   | ]}t |ƒV  qd S r$   )ÚHtmlRow)Ú.0Útrr   r   r   Ú	<genexpr>j   ó    z&HtmlTable.iter_rows.<locals>.<genexpr>zlist[HtmlElement]z./tr)r   r%   r+   r;   r   r   r   Ú	iter_rowsi   s    zHtmlTable.iter_rowsc                 C  s   d  | j ¡ ¡}d  | ¡ ¡S )z-The clean, concatenated, text for this table.r   )r   r%   Úitertextr   )r&   Z
table_textr   r   r   r1   l   s    zHtmlTable.textN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   Úclassmethodr6   r	   r   rA   r1   r   r   r   r   r"   4   s   "r"   c                   @  s^   e Zd ZdZddœdd„Zeddœdd	„ƒZd
dœdd„Zddœdd„Zeddœdd„ƒZ	dS )r<   zA `<tr>` element.r
   )r>   c                 C  s
   || _ d S r$   )Ú_tr)r&   r>   r   r   r   r'   w   s    zHtmlRow.__init__r   r7   c                 C  s   t j| jtd�S )z*Like  "<tr><td>foo</td><td>bar</td></tr>".r8   )r   r:   rH   r   r;   r   r   r   r   z   s    zHtmlRow.htmlzIterator[HtmlCell]c                 c  s   | j D ]}t|ƒV  qd S r$   )rH   ÚHtmlCell©r&   r*   r   r   r   Ú
iter_cells   s    
zHtmlRow.iter_cellsr   c                 c  s,   | j D ] }|j }du rq|s q|V  qdS )zŸGenerate contents of each cell of this row as a separate string.

        A cell that is empty or contains only whitespace does not generate a string.
        N)rH   r1   )r&   r*   r1   r   r   r   Úiter_cell_textsƒ   s    
zHtmlRow.iter_cell_textsÚintc                 C  s   t d |  ¡ ¡ƒS )zDLength of the normalized text, as it would appear in `element.text`.r   )Úlenr   rL   r;   r   r   r   Útext_len�   s    zHtmlRow.text_lenN)
rC   rD   rE   rF   r'   r	   r   rK   rL   rO   r   r   r   r   r<   t   s   r<   c                   @  sB   e Zd ZdZddœdd„Zeddœdd	„ƒZeddœd
d„ƒZdS )rI   zA `<td>` element.r
   )r*   c                 C  s
   || _ d S r$   )Ú_tdrJ   r   r   r   r'   ˜   s    zHtmlCell.__init__r   r7   c                 C  s   | j rtj| jtd�S dS )zLike  "<td>foo bar baz</td>".r8   r   )r1   r   r:   rP   r   r;   r   r   r   r   ›   s    zHtmlCell.htmlc                 C  s&   | j j }du rdS d | ¡  ¡ ¡S )z6Text inside `<td>` element, empty string when no text.Nr   r   )rP   r1   r   Ústripr   )r&   r1   r   r   r   r1       s    zHtmlCell.textN)rC   rD   rE   rF   r'   r	   r   r1   r   r   r   r   rI   •   s   rI   )rF   Ú
__future__r   r   Útypingr   r   r   r   Úlxmlr   Z	lxml.htmlr   Zunstructured.utilsr	   r
   r!   r"   r<   rI   r   r   r   r   Ú<module>   s    @!