a
    ¥Q•h•"  ã                   @   sN   d Z ddlZddlmZ G dd„ dƒZG dd„ dƒZd	d
„ ZG dd„ dƒZdS )zðHandle target-counter, target-counters and target-text.

The TargetCollector is a structure providing required targets' counter_values
and stuff needed to build pending targets later, when the layout of all
targeted anchors has been done.

é    Né   )ÚLOGGERc                   @   s   e Zd ZdZddd„ZdS )ÚTargetLookupItemz‡Item controlling pending targets and page based target counters.

    Collected in the TargetCollector's ``target_lookup_items``.

    Úpendingc                 C   s"   || _ d | _i | _d | _i | _d S )N)ÚstateÚ
target_boxÚparse_again_functionsÚpage_maker_indexÚcached_page_counter_values)Úselfr   © r   úO/var/www/sistema_ama/venv/lib/python3.9/site-packages/weasyprint/css/targets.pyÚ__init__   s
    zTargetLookupItem.__init__N)r   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdd„ ZdS )ÚCounterLookupItemzmItem controlling page based counters.

    Collected in the TargetCollector's ``counter_lookup_items``.

    c                 C   s(   || _ || _|| _d | _d| _i | _d S )NF)Úparse_againÚmissing_countersÚmissing_target_countersr	   r   r
   )r   r   r   r   r   r   r   r   -   s    zCounterLookupItem.__init__Nr   r   r   r   r   r   '   s   r   c                 C   sV   | d dkr*| d   d¡r*| d dd… S | d dkrR| d d dkrR| d d S dS )z)Get anchor name from string or uri token.r   Ústringé   ú#NÚurlÚinternal)Ú
startswith)Úanchor_tokenr   r   r   Úanchor_name_from_token?   s    r   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚTargetCollectorz@Collector of HTML targets used by CSS content with ``target-*``.c                 C   s   i | _ i | _d| _d| _d S )NTF)Útarget_lookup_itemsÚcounter_lookup_itemsÚ
collectingÚhad_pending_targets)r   r   r   r   r   J   s    zTargetCollector.__init__c                 C   s<   t |tƒr8| j |¡dur(t d|¡ n| j |tƒ ¡ dS )z7Create a TargetLookupItem for the given `anchor_name``.NzAnchor defined twice: %r)Ú
isinstanceÚstrr!   Úgetr   ÚwarningÚ
setdefaultr   )r   Úanchor_namer   r   r   Úcollect_anchorZ   s    
ÿzTargetCollector.collect_anchorc                 C   sV   t |ƒ}| j |tdƒ¡}|jdkr<d| _|j ||f|¡ |jdkrRt 	d|¡ |S )zîGet a TargetLookupItem corresponding to ``anchor_token``.

        If it is already filled by a previous anchor-element, the status is
        'up-to-date'. Otherwise, it is 'pending', we must parse the whole
        tree again.

        Ú	undefinedr   Tz7Content discarded: target points to undefined anchor %r)
r   r!   r'   r   r   r$   r   r)   r   Úerror)r   r   Z
source_boxÚ	css_tokenr   r*   Úitemr   r   r   Úlookup_targetc   s    ÿ
ÿ
þzTargetCollector.lookup_targetc                 C   sH   | j  |¡}|rD|jdkrDd|_||_|jdu rDdd„ | ¡ D ƒ|_dS )z¦Store a target called ``anchor_name``.

        If there is a pending TargetLookupItem, it is updated. Only previously
        collected anchors are stored.

        r   ú
up-to-dateNc                 S   s   i | ]\}}||  ¡ “qS r   )Úcopy)Ú.0ÚkeyÚvaluer   r   r   Ú
<dictcomp>‰   s   z0TargetCollector.store_target.<locals>.<dictcomp>)r!   r'   r   r   Úcached_counter_valuesÚitems)r   r*   Ztarget_counter_valuesr   r/   r   r   r   Ústore_target{   s    
þzTargetCollector.store_targetc                 C   sD   | j s
dS |s|r@|jdu r"||_t|||ƒ}| j ||f|¡ dS )a$  Collect missing (probably page-based) counters during formatting.

        The ``missing_counters`` are re-used during pagination.

        The ``missing_link`` attribute added to the parent_box is required to
        connect the paginated boxes to their originating ``parent_box``.

        N)r#   Úmissing_linkr   r"   r)   )r   Ú
parent_boxr.   Zparse_again_functionr   r   Zcounter_lookup_itemr   r   r   Úcollect_missing_counters�   s    
þÿz(TargetCollector.collect_missing_countersc                 C   s<   | j r2| j ¡ D ]}|j ¡ D ]
}|ƒ  qqd| _ d| _dS )z Check pending targets if needed.FN)r$   r!   Úvaluesr   r#   )r   r/   Úfunctionr   r   r   Úcheck_pending_targets¦   s    
z%TargetCollector.check_pending_targetsc                 C   sÞ   | j r
dS | j |¡}|rÚ|jdkrÚ||_|j|krÚt |¡|_| j 	¡ D ]Ž\\}}}|dkr`qJ|j
 |¡}|du rvqJ|jdu sŽ|jt|ƒkr–d|_qJ|D ]<}	| |	¡}
|
durš||j d }d|d< | |j¡  qJqšqJdS )z…Store target's current ``page_maker_index`` and page counter values.

        Eventually update associated targeting boxes.

        Nr1   ÚcontentTéÿÿÿÿÚcontent_changed)r#   r!   r'   r   r	   r
   r2   Údeepcopyr"   r8   r   Úlenr   r   )r   r*   Úpage_counter_valuesr	   Ú
page_makerr/   Ú_r.   r   Úcounter_nameÚcounter_valueÚremake_stater   r   r   Úcache_target_page_counters°   s<    
ÿÿ
ÿ
ÿz*TargetCollector.cache_target_page_countersN)r   r   r   r   r   r+   r0   r9   r<   r?   rK   r   r   r   r   r    G   s   	
r    )r   r2   Úloggerr   r   r   r   r    r   r   r   r   Ú<module>   s   