3 E\@sdZdZddlZddlZddlmZmZddlmZm Z Gddde Z Gdd d e Z Gd d d e Z Gd d d e Z Gddde ZGddde ZGdddeZGddde ZGddde ZGddde ZGdddejZdS)z& Transforms for resolving references. ZreStructuredTextN)nodesutils)TransformError Transformc@seZdZdZdZddZdS)PropagateTargetsa} Propagate empty internal targets to the next element. Given the following nodes:: This is a test. PropagateTargets propagates the ids and names of the internal targets preceding the paragraph to the paragraph itself:: This is a test. icCs|xt|jjtjD]`}t|jtjs|jds|jds|jdrFqt|dksZt d|j dd}|dk ot|tj  rt|tj  st|tjr|dj |d|d j |d t|d si|_t|d si|_x&|dD]}||jj|<||j|<qWx|d D]}||j|<q W|jjt|d i|jjt|d i|dd|d<g|d<g|d <|jj|qWdS) Nrefidrefurirefnamerz&error: block-level target has childrenT)Zascendidsnamesexpect_referenced_by_nameexpect_referenced_by_id)documenttraversertarget isinstanceparentZ TextElementhasattrlenAssertionError next_nodeZ InvisibleZ Targetableextendr r r updategetattr note_refid)selfrridnamerx/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/docutils/docutils/transforms/references.pyapply*s<      zPropagateTargets.applyN)__name__ __module__ __qualname____doc__default_priorityr rrrrrsrc@seZdZdZdZddZdS)AnonymousHyperlinksa Link anonymous references to targets. Given:: internal external Corresponding references are linked via "refid" or resolved via "refuri":: text external ic Csdg}g}x*|jjtjD]}|jdr|j|qWx*|jjtjD]}|jdrD|j|qDWt|t|kr|jjj dt|t|f}|jj |}x@|D]8}tj |j |j |d}|jj |}|j ||j|qWdSx~t||D]p\}} d| _x`| jdr| d|d<d|_Pq| ds:|jj| d} q| dd|d<|jj|PqWqWdS) N anonymousz]Anonymous hyperlink mismatch: %s references but %s targets. See "backrefs" attribute for IDs.)rrr rr)rrr referencegetappendrrreportererrorset_id problematic rawsource add_backref replace_selfzip referencedrresolvedr r) rZanonymous_refsZanonymous_targetsnodemsgmsgidrefprbprbidrrrrr rsD           zAnonymousHyperlinks.applyN)r!r"r#r$r%r rrrrr&Xsr&c@sDeZdZdZdZddZddZddZd d Zd d Z d dZ dS)IndirectHyperlinksaN a) Indirect external references:: indirect external The "refuri" attribute is migrated back to all indirect targets from the final direct target (i.e. a target not referring to another indirect target):: indirect external Once the attribute is migrated, the preexisting "refname" attribute is dropped. b) Indirect internal references:: indirect internal Targets which indirectly refer to an internal target become one-hop indirect (their "refid" attributes are directly set to the internal target's "id"). References which indirectly refer to an internal target become direct internal references:: indirect internal icCs0x*|jjD]}|js|j||j|q WdS)N)rZindirect_targetsr5resolve_indirect_targetresolve_indirect_references)rrrrrr s zIndirectHyperlinks.applycCsP|jd}|dkr|d}n>|jjj|}|sZx&|jjjD]}||r:Pq:W|j|dS|jj|}|j|dt|t j r|j r|j drt |dr|j |dSd|_|j||`|j dr|d|d<d|kr|d=nT|j dr|d|d<|jj|n.|dr(||d<|jj|n|j|dS|dk rF|d=d|_ dS)Nr r)rmultiply_indirectr(rr )r*rnameids transformerunknown_reference_resolversnonexistent_indirect_targetr note_referenced_byrrrr5rcircular_indirect_referencer?r=r)rrr Z reftarget_idresolver_functionZ reftargetrrrr=sH                z*IndirectHyperlinks.resolve_indirect_targetcCs.|d|jjkr|j|dn |j|ddS)Nr z>which is a duplicate, and cannot be used as a unique referencezwhich does not exist)rr@indirect_target_error)rrrrrrCsz.IndirectHyperlinks.nonexistent_indirect_targetcCs|j|ddS)Nzforming a circular reference)rG)rrrrrrE sz.IndirectHyperlinks.circular_indirect_referencec Cs d}g}|dr d|dd}x&|dD]}|j|jjj|gq*Wx&|dD]}|j|jjj|gqRW|dr|d|dd7}|jjjd||d|f|d }|jj|}xFtj |D]8} t j | j | j |d } |jj| } |j | | j| qWd |_dS) Nr z"%s" rr z (id="%s")z7Indirect hyperlink target %s refers to target "%s", %s.r ) base_node)rr()rrrefnamesr*refidsr,r-r.rZuniqrr/r0r1r2r5) rrZ explanationnamingreflistrrr7r8r9r:r;rrrrG s*   z(IndirectHyperlinks.indirect_target_errorc Cs:|jdrd}|jj}n|jdr,d}d}ndS||}x||dD]p}|jjj|g}|rf|j|dxJ|D]B}|jrxql|d=|||<|r||d|_t|tj rl|j |qlWqBWx~|dD]r}|jj j|g}|r|j|dxL|D]D}|jrq|d=|||<|r||d|_t|tj r|j |qWqWdS) Nrrr )rr r(r )r) rrrrJr*rDr5rrrr>rK) rrZattnameZ call_methodZattvalrrMr9rrrrr>$sH         z.IndirectHyperlinks.resolve_indirect_referencesN) r!r"r#r$r%r r=rCrErGr>rrrrr<s2-r<c@seZdZdZdZddZdS)ExternalTargetsa Given:: direct external The "refname" attribute is replaced by the direct "refuri" attribute:: direct external icCsx|jjtjD]r}|jdr|d}xZ|dD]N}|jjj|g}|rT|j|dx(|D] }|jrfqZ|d=||d<d|_qZWq0WqWdS)Nrr )rr r() rrrrrrJr*rDr5)rrrrrMr9rrrr `s   zExternalTargets.applyN)r!r"r#r$r%r rrrrrNLsrNc@s eZdZdZddZddZdS)InternalTargetsicCs>x8|jjtjD]&}|jd r|jd r|j|qWdS)Nrr)rrrrrresolve_reference_ids)rrrrrr tszInternalTargets.applycCsrxl|dD]`}|jjj|}|jjj|g}|r<|j|dx,|D]$}|jrNqB|r`|d=||d<d|_qBWq WdS)a Given:: direct internal The "refname" attribute is replaced by "refid" linking to the target's "id":: direct internal r )rr rr(N)rr@r*rJrDr5)rrrrrMr9rrrrPys  z%InternalTargets.resolve_reference_idsN)r!r"r#r%r rPrrrrrOpsrOc @s`eZdZdZdZdZdddddd d d d d g ZddZddZddZ ddZ ddZ ddZ dS) Footnotesa Assign numbers to autonumbered footnotes, and resolve links to footnotes, citations, and their references. Given the following ``document`` as input:: A labeled autonumbered footnote referece: An unlabeled autonumbered footnote referece: Unlabeled autonumbered footnote. Labeled autonumbered footnote. Auto-numbered footnotes have attribute ``auto="1"`` and no label. Auto-numbered footnote_references have no reference text (they're empty elements). When resolving the numbering, a ``label`` element is added to the beginning of the ``footnote``, and reference text to the ``footnote_reference``. The transformed result will be:: A labeled autonumbered footnote referece: 2 An unlabeled autonumbered footnote referece: 1