o
    Zh0hD                  	   @   s  d Z ddlZddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
Z
ddlZddlZd ZZi ddd	d
ddddddddddddddddddddddddddddddZg dZddlmZmZmZmZmZ ddlmZmZ dd  ZG d!d" d"ZG d#d$ d$ZG d%d& d&Zejddddddedfd'd(Zdd)l m!Z! dd*l"m#Z#m$Z$m%Z%m&Z& dd+l'm(Z(m)Z) dS ),z~The Awesome Document Factory.

The public API is what is accessible from this "root" packages without
importing sub-modules.

    N)datetime)getctimegetmtime)Path)urljoinz65.1stylesheets
media_typeprintattachmentspdf_identifierpdf_variantpdf_version	pdf_formsuncompressed_pdfFcustom_metadatapresentational_hintssrgboptimize_imagesjpeg_qualitydpi
full_fontshintingcache)	CSSDEFAULT_OPTIONSHTMLVERSION
AttachmentDocumentPage__version__default_url_fetcher   )fetchr!   path2url
ensure_urlurl_is_absolute)LOGGERPROGRESS_LOGGERc                 C   s>   t t| dd}|dur|dd }|rt||S |S )zlReturn the base URL for the document.

    See https://www.w3.org/TR/html5/urls.html#document-base-url

    baseNhref )nextitergetstripr   )html_documentfallback_base_urlfirst_base_elementr*    r3   I/var/www/html/rh/venv/lib/python3.10/site-packages/weasyprint/__init__.py_find_base_urlc   s   
r5   c                	   @   s^   e Zd ZdZdddddddedf	ddZdddZd	d
 Zdd ZdddZ			dddZ
dS )r   a  HTML document parsed by tinyhtml5.

    You can just create an instance with a positional argument:
    ``doc = HTML(something)``
    The class will try to guess if the input is a filename, an absolute URL,
    or a :term:`file object`.

    Alternatively, use **one** named argument so that no guessing is involved:

    :type filename: str or pathlib.Path
    :param filename:
        A filename, relative to the current directory, or absolute.
    :param str url:
        An absolute, fully qualified URL.
    :type file_obj: :term:`file object`
    :param file_obj:
        Any object with a ``read`` method.
    :param str string:
        A string of HTML source.

    Specifying multiple inputs is an error:
    ``HTML(filename="foo.html", url="localhost://bar.html")``
    will raise a :obj:`TypeError`.

    You can also pass optional named arguments:

    :param str encoding:
        Force the source character encoding.
    :type base_url: str or pathlib.Path
    :param base_url:
        The base used to resolve relative URLs (e.g. in
        ``<img src="../foo.png">``). If not provided, try to use the input
        filename, URL, or ``name`` attribute of
        :term:`file objects <file object>`.
    :type url_fetcher: :term:`callable`
    :param url_fetcher:
        A function or other callable with the same signature as
        :func:`default_url_fetcher` called to fetch external resources such as
        stylesheets and images. (See :ref:`URL Fetchers`.)
    :param str media_type:
        The media type to use for ``@media``. Defaults to ``'print'``.
        **Note:** In some cases like ``HTML(string=foo)`` relative URLs will be
        invalid if ``base_url`` is not provided.

    Nr	   c
                 C   s   t d|p|p|pt|dd t|trt|}t|||||||}
|
6\}}}}t|tr7tj|dd}
nddi}|d urC||d< |d urK||d< tj|fi |}
W d    n1 s^w   Y  t	|
|| _
|| _|	| _tjj|
d d	| _| jj| _d S )
Nz'Step 1 - Fetching and parsing HTML - %snamezHTML stringF)namespace_html_elementsr7   transport_encodingoverride_encoding)content_language)r(   infogetattr
isinstancer   str_select_source	tinyhtml5parser5   base_urlurl_fetcherr   
cssselect2ElementWrapperfrom_html_rootwrapper_elementetree_element)selfguessfilenameurlfile_objstringencodingrB   rC   r   resultsource_typesourceprotocol_encodingkwargsr3   r3   r4   __init__   s8   



zHTML.__init__Fc                 C   s   |rt tgS t gS N)HTML5_UA_STYLESHEETHTML5_UA_FORM_STYLESHEET)rI   formsr3   r3   r4   _ua_stylesheets   s   zHTML._ua_stylesheetsc                 C   s
   t  gS rV   )HTML5_UA_COUNTER_STYLEcopyrI   r3   r3   r4   _ua_counter_style   s   
zHTML._ua_counter_stylec                 C   s   t gS rV   )HTML5_PH_STYLESHEETr]   r3   r3   r4   _ph_stylesheets   s   zHTML._ph_stylesheetsc                 K   sH   t |t t D ]}td| qt }|| |}t| |||S )a  Lay out and paginate the document, but do not (yet) export it.

        This returns a :class:`document.Document` object which provides
        access to individual pages and various meta-data.
        See :meth:`write_pdf` to get a PDF directly.

        :type font_config: :class:`text.fonts.FontConfiguration`
        :param font_config:
            A font configuration handling ``@font-face`` rules.
        :type counter_style: :class:`css.counters.CounterStyle`
        :param counter_style:
            A dictionary storing ``@counter-style`` rules.
        :param options:
            The ``options`` parameter includes by default the
            :data:`DEFAULT_OPTIONS` values.
        :returns: A :class:`document.Document` object.

        zUnknown rendering option: %s.)setr   r'   warningr\   updater   _render)rI   font_configcounter_styleoptionsunknownnew_optionsr3   r3   r4   render   s   
zHTML.renderr"   c                 K   s<   t  }|| |}| j||fi |j|||fi |S )aE  Render the document to a PDF file.

        This is a shortcut for calling :meth:`render`, then
        :meth:`Document.write_pdf() <document.Document.write_pdf>`.

        :type target:
            :class:`str`, :class:`pathlib.Path` or :term:`file object`
        :param target:
            A filename where the PDF file is generated, a file object, or
            :obj:`None`.
        :param float zoom:
            The zoom factor in PDF units per CSS units.  **Warning**:
            All CSS units are affected, including physical units like
            ``cm`` and named sizes like ``A4``.  For values other than
            1, the physical CSS units will thus be "wrong".
        :type finisher: :term:`callable`
        :param finisher:
            A finisher function or callable that accepts the document and a
            :class:`pydyf.PDF` object as parameters. Can be passed to perform
            post-processing on the PDF right before the trailer is written.
        :type font_config: :class:`text.fonts.FontConfiguration`
        :param font_config:
            A font configuration handling ``@font-face`` rules.
        :type counter_style: :class:`css.counters.CounterStyle`
        :param counter_style:
            A dictionary storing ``@counter-style`` rules.
        :param options:
            The ``options`` parameter includes by default the
            :data:`DEFAULT_OPTIONS` values.
        :returns:
            The PDF as :obj:`bytes` if ``target`` is not provided or
            :obj:`None`, otherwise :obj:`None` (the PDF is written to
            ``target``).

        )r   r\   rc   rj   	write_pdf)rI   targetzoomfinisherre   rf   rg   ri   r3   r3   r4   rk      s   %
zHTML.write_pdf)F)NN)Nr"   NNN)__name__
__module____qualname____doc__r!   rU   rZ   r^   r`   rj   rk   r3   r3   r3   r4   r   q   s    -


r   c                   @   s6   e Zd ZdZdddddddeddddddfddZdS )r   a#  CSS stylesheet parsed by tinycss2.

    An instance is created in the same way as :class:`HTML`, with the same
    arguments.

    An additional argument called ``font_config`` must be provided to handle
    ``@font-face`` rules. The same ``text.fonts.FontConfiguration`` object
    must be used for different ``CSS`` objects applied to the same document.

    ``CSS`` objects have no public attributes or methods. They are only meant
    to be used in the :meth:`HTML.write_pdf` and :meth:`HTML.render` methods
    of :class:`HTML` objects.

    NFr	   c              
   C   s   t d|p|pt|dd t||||||||	d}|)\}}}}|dkr)| }t|tr4t|}n
tj	|||d\}}W d    n1 sHw   Y  || _
|pUt | _|d u r]g n|| _|d u rfi n|}t|
|||| j| j|| d S )Nz&Step 2 - Fetching and parsing CSS - %sr6   z
CSS stringrB   rC   check_css_mime_typerM   )environment_encodingrS   )r(   r;   r<   r?   readr=   r>   tinycss2parse_stylesheetparse_stylesheet_bytesrB   rD   Matchermatcher
page_rulespreprocess_stylesheet)rI   rJ   rK   rL   rM   rN   rO   rB   rC   _check_mime_typer   re   rf   r{   r|   rP   rQ   rR   rS   
stylesheetr3   r3   r4   rU     s8   



zCSS.__init__ro   rp   rq   rr   r!   rU   r3   r3   r3   r4   r     s    r   c                   @   s2   e Zd ZdZddddddedddddfddZdS )r   a  File attachment for a PDF document.

    An instance is created in the same way as :class:`HTML`, except that the
    HTML specific arguments (``encoding`` and ``media_type``) are not
    supported.

    :param str name:
        The name of the attachment to be included in the PDF document.
        May be :obj:`None`.
    :param str description:
        A description of the attachment to be included in the PDF document.
        May be :obj:`None`.
    :type created: :obj:`datetime.datetime`
    :param created:
        Creation date and time. Default is current date and time.
    :type modified: :obj:`datetime.datetime`
    :param modified:
        Modification date and time. Default is current date and time.
    :param str relationship:
        A string that represents the relationship between the attachment and
        the PDF it is embedded in. Default is 'Unspecified', other common
        values are defined in ISO-32000-2:2020, 7.11.3.

    NUnspecifiedc              	   C   s   t |||||||d| _|| _|	| _|| _d | _|
d u r*|r&tt|}
nt	 }
|d u r<|r8tt
|}nt	 }|
| _|| _d S )N)rB   rC   )r?   rR   r6   descriptionrelationshipmd5r   fromtimestampr   nowr   createdmodified)rI   rJ   rK   rL   rM   rN   rB   rC   r6   r   r   r   r   r3   r3   r4   rU   T  s$   
zAttachment.__init__r   r3   r3   r3   r4   r   ;  s    r   c                 c   st   |dur	t |}dd | ||||fD }t|dkr)d|p!d}	td|	 | durkt| dr5d	}
nt| tr=d
}
n	t| rDd}
nd
}
td|||d|
| i}|}|V  W d   dS 1 sdw   Y  dS |dur|du rwt	|}t
|d}d	||dfV  W d   dS 1 sw   Y  dS |durt||Z}|r|d dkrtd|d |d  dd|dfV  n.|d}|du r|d|}d|v rd|d ||fV  nd	|d	 ||fV  W d   dS W d   dS W d   dS 1 sw   Y  dS |dur*|du r!t|dd}|r!|ds!t |}d	||dfV  dS |dus1J d||dfV  dS )zCIf only one input is given, return it with normalized ``base_url``.Nc                 S   s   g | ]}|d ur|qS rV   r3   ).0paramr3   r3   r4   
<listcomp>v  s
    z"_select_source.<locals>.<listcomp>r"   z, nothingz!Expected exactly one source, got rv   rM   rK   rL   rs   rb	mime_typeztext/cssz%Unsupported stylesheet type %s for %sredirected_urlrN   r+   rO   r6   <r3   )r%   lenjoin	TypeErrorhasattrr=   r   r&   r?   r$   openr#   r'   errorr.   r<   
startswith)rJ   rK   rL   rM   rN   rB   rC   rt   selected_paramsrR   type_rP   proto_encodingr6   r3   r3   r4   r?   n  sz   

""


"

r?   )r}   )r[   rW   rX   r_   )r   r   )*rr   
contextlibr   os.pathr   r   pathlibr   urllib.parser   rD   rw   r@   r   r    r   __all__urlsr#   r!   r$   r%   r&   loggerr'   r(   r5   r   r   r   contextmanagerr?   cssr}   htmlr[   rW   rX   r_   documentr   r   r3   r3   r3   r4   <module>   sz    0	
 .3?