
    FeME                       d Z ddlmZ ddlZddlZddlmZmZm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mZ dd
lm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m Z  ddl!m"Z"m#Z# er&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-m.Z. ddl/m0Z0  ejb                  e2      Z3h dZ4 G d de      Z5 G d de      Z6 G d de5      Z7dCdZ8 G d d e5      Z9 G d! d"e5      Z: G d# d$e5      Z; G d% d&e5      Z<e
jz                  e
j|                  e
j~                  ej                  e
j                  d'ZB G d( d)e5      ZC G d* d+e5      ZD G d, d-e5      ZE G d. d/e5      ZF G d0 d1e5      ZG G d2 d3e5      ZH G d4 d5e      ZI G d6 d7ee5      ZJ G d8 d9e5      ZK G d: d;e5      ZL G d< d=e5      ZM G d> d?e5      ZNdDd@ZOdEdAZPdFdBZQy)Gz:Docutils transforms used by Sphinx when reading documents.    )annotationsN)TYPE_CHECKINGAnycast)nodes)	TransformTransformer)ContentsFilter)SmartQuotes)normalize_language_tag)
smartchars)addnodes)___)logging)new_document)format_date)apply_source_workaroundis_smartquotable)	Generator)NodeText)Sphinx)Config)StandardDomain)BuildEnvironment>   todayreleaseversiontranslation progressc                  F    e Zd ZdZedd       Zedd       Zedd       Zy)	SphinxTransformzA base class of Transforms.

    Compared with ``docutils.transforms.Transform``, this class improves accessibility to
    Sphinx APIs.
    c                .    | j                   j                  S )z)Reference to the :class:`.Sphinx` object.)envappselfs    </usr/lib/python3/dist-packages/sphinx/transforms/__init__.pyr%   zSphinxTransform.app3   s     xx||    c                B    | j                   j                  j                  S )z3Reference to the :class:`.BuildEnvironment` object.)documentsettingsr$   r&   s    r(   r$   zSphinxTransform.env8   s     }}%%)))r)   c                .    | j                   j                  S )z)Reference to the :class:`.Config` object.)r$   configr&   s    r(   r.   zSphinxTransform.config=   s     xxr)   N)returnr   )r/   r   )r/   r   )__name__
__module____qualname____doc__propertyr%   r$   r.    r)   r(   r"   r"   ,   sC       * *  r)   r"   c                  F     e Zd ZU dZded<   dZded<   d	dZd
 fdZ xZS )SphinxTransformerz#
    A transformer for Sphinx.
    nodes.documentr+   NzBuildEnvironment | Noner$   c                    || _         y N)r$   )r'   r$   s     r(   set_environmentz!SphinxTransformer.set_environmentK   s	    r)   c                   t        | j                  t        j                        r`t        | j                  j                  d      s1| j
                  r%| j
                  | j                  j                  _        t        |           y 	 t        d      }| j
                  r| j
                  |j                  _        || j                  z  }|| _        t        |           | j                  d   | _        y # | j                  d   | _        w xY w)Nr$    r   )	
isinstancer+   r   hasattrr,   r$   superapply_transformsr   )r'   r+   	__class__s     r(   rA   z"SphinxTransformer.apply_transformsN   s    dmmU^^44==1159dhh-1XX&&*G$&1'+88,0HHH%%)DMM) ((* $a 0a 0s   AC2 2D)r$   r   r/   None)r/   rC   )	r0   r1   r2   r3   __annotations__r$   r;   rA   __classcell__rB   s   @r(   r7   r7   C   s*     #'C	 '1 1r)   r7   c                      e Zd ZdZdZddZy)DefaultSubstitutionszL
    Replace some substitutions if they aren't defined in the document.
       c                   t         t        | j                  j                        z
  }| j                  j	                  t
        j                        D ]  }|d   }||v s|dk(  rt        | j                        }n| j                  |   }|dk(  rD|sBt        | j                  j                  xs t        d      | j                  j                        }|j                  t        j                  |              y )Nrefnamer    r   z	%b %d, %Y)language)default_substitutionssetr+   substitution_defsfindallr   substitution_reference_calculate_translation_progressr.   r   	today_fmtr   rL   replace_selfr   )r'   kwargs	to_handlerefrK   texts         r(   applyzDefaultSubstitutions.applyh   s    )C0O0O,PP	==(()E)EF 	3C)nG)#44:4==ID;;w/Dg%d&t{{'<'<'N+040D0DFD  D!12	3r)   NrU   r   r/   rC   r0   r1   r2   r3   default_priorityrY   r5   r)   r(   rH   rH   a   s     3r)   rH   c                    	 | d   }|d   }|d   }|dk  rt        d      S ||z  dS # t         $ r t        d      cY S w xY w)Ntranslation_progressz)could not calculate translation progress!total
translatedr   zno translated elements!z.2%)KeyErrorr   )r+   r^   r_   r`   s       r(   rR   rR   z   si    >'(>? !)E%l3Jz*++5 %&  ><==>s   ( ??c                      e Zd ZdZdZddZy)MoveModuleTargetszv
    Move module targets that are the first thing in a section to the section
    title.

    XXX Python specific
    rI   c                r   t        | j                  j                  t        j                              D ]  }|d   s	d|v st        |j                        t        j                  u s4|j                  j                  |      dk(  sS|d   |j                  d   dd |j                  j                  |        y )Nidsismod   r   )
listr+   rP   r   targettypeparentsectionindexremover'   rU   nodes      r(   rY   zMoveModuleTargets.apply   s    ..u||<= 	)D;4%6 KK%%d+q0*.u+E"1Q'""4(	)r)   NrZ   r[   r5   r)   r(   rc   rc      s     )r)   rc   c                      e Zd ZdZdZddZy)HandleCodeBlocksz5
    Several code block related transformations.
    rI   c                    | j                   j                  t        j                        D ]:  }t	        d |j
                  D              s |j                  |j
                         < y )Nc              3  P   K   | ]  }t        |t        j                           y wr:   )r>   r   doctest_block).0childs     r(   	<genexpr>z)HandleCodeBlocks.apply.<locals>.<genexpr>   s!      %e:eU%8%89 %s   $&)r+   rP   r   block_quoteallchildrenrT   ro   s      r(   rY   zHandleCodeBlocks.apply   sQ    MM))%*;*;< 	1D %mm% %!!$--0	1r)   NrZ   r[   r5   r)   r(   rr   rr      s     1r)   rr   c                      e Zd ZdZdZddZy)AutoNumberingzO
    Register IDs of tables, figures and literal_blocks to assign numbers.
    rI   c                "   | j                   j                  d   }| j                  j                  t        j
                        D ]J  }|j                  |      s|j                  |      '|d   g k(  s0| j                  j                  |       L y )Nstdre   )	r$   domainsr+   rP   r   Elementis_enumerable_nodeget_numfig_titlenote_implicit_target)r'   rU   domainrp   s       r(   rY   zAutoNumbering.apply   su    !%!1!1%!8MM))%--8 	9D))$/++D1=K2%2248		9r)   NrZ   r[   r5   r)   r(   r}   r}      s     9r)   r}   c                      e Zd ZdZdZddZy)SortIdszA
    Sort section IDs so that the "id[0-9]+" one comes last.
    i  c                    | j                   j                  t        j                        D ]A  }t	        |d         dkD  s|d   d   j                  d      s-|d   dd  |d   d   gz   |d<   C y )Nre      r   id)r+   rP   r   rl   len
startswithro   s      r(   rY   zSortIds.apply   so    MM))%--8 	AD4;!#UA(A(A$(G"5k!"oeQ0@@U	Ar)   NrZ   r[   r5   r)   r(   r   r      s     Ar)   r   )zliteral-blockzdoctest-blockrawrm   imagec                      e Zd ZdZdZddZy)ApplySourceWorkaroundz0
    Update source and rawsource attributes
    
   c                    | j                   j                         D ]G  }t        |t        j                  t        j
                  t        j                  f      s=t        |       I y r:   )r+   rP   r>   r   TextElementr   topicr   ro   s      r(   rY   zApplySourceWorkaround.apply   sE    MM))+ 	.D$!2!2EKK MN'-	.r)   NrZ   r[   r5   r)   r(   r   r      s     .r)   r   c                      e Zd ZdZdZddZy)AutoIndexUpgraderz[
    Detect old style (4 column based indices) and automatically upgrade to new style.
    rI   c                F   | j                   j                  t        j                        D ]u  }d|v st	        d |d   D              st        d      |d   z  }t        j                  ||       t        |d         D ]  \  }}t        |      dk(  s|dz   |d   |<   ! w y )Nentriesc              3  8   K   | ]  }t        |      d k(    yw)   N)r   )rv   entrys     r(   rx   z*AutoIndexUpgrader.apply.<locals>.<genexpr>   s     (VUUq(Vs   zG4 column based index found. It might be a bug of extensions you use: %r)locationr   r:   )
r+   rP   r   rm   anyr   loggerwarning	enumerater   )r'   rU   rp   msgir   s         r(   rY   zAutoIndexUpgrader.apply   s    MM))(..9 	=DD S(Vd9o(V%V G HJNy/ZsT2 )$y/ : =HAu5zQ-2W_Y*=	=r)   NrZ   r[   r5   r)   r(   r   r      s     =r)   r   c                      e Zd ZdZdZddZy)ExtraTranslatableNodesz!
    Make nodes translatable
    r   c                    | j                   j                  }t        j                         D cg c]  \  }}||v s| c}}sy dfd}| j                  j                  |      D ]  }d|d<   	 y c c}}w )Nc                .    t        | t                    S r:   )r>   tuple)rp   target_nodess    r(   is_translatable_nodez:ExtraTranslatableNodes.apply.<locals>.is_translatable_node  s    dE,$788r)   Ttranslatable)rp   r   r/   bool)r.   gettext_additional_targetsTRANSLATABLE_NODESitemsr+   rP   )r'   rU   targetskvr   rp   r   s          @r(   rY   zExtraTranslatableNodes.apply   sq    ++88&8&>&>&@QdaALQ	9 MM))*>? 	(D#'D 	( Rs
   A3A3NrZ   r[   r5   r)   r(   r   r      s     
(r)   r   c                      e Zd ZdZdZddZy)UnreferencedFootnotesDetectorz9
    Detect unreferenced footnotes and emit warnings
       c                   | j                   j                  D ]S  d   g k(  rd   d   | j                   j                  vs+t        j	                  t        d      d   d   dd       U | j                   j                  D ]M  t        fd| j                   j                  D              r,t        j	                  t        d      dd       O y )	Nnamesr   z Footnote [%s] is not referenced.rW   footnote)rj   subtyper   c              3  4   K   | ]  }|d    d    k(    yw)autoNr5   )rv   rW   rp   s     r(   rx   z6UnreferencedFootnotesDetector.apply.<locals>.<genexpr>  s     ^ss6{d6l2^s   zFootnote [#] is not referenced.)	r+   	footnotesfootnote_refsr   r   r   autofootnotesr   autofootnote_refsro   s     @r(   rY   z#UnreferencedFootnotesDetector.apply  s    MM++ 	.DG}"gq!)D)DDr"DEtG}UVGW$):(,  .	. MM// 	.D^dmm>]>]^^r"CD$):(,  .	.r)   NrZ   r[   r5   r)   r(   r   r     s     .r)   r   c                      e Zd ZdZdZddZy)DoctestTransformz.Set "doctest" style to each doctest_block node  c                    | j                   j                  t        j                        D ]  }|d   j	                  d        y )Nclassesdoctest)r+   rP   r   ru   appendro   s      r(   rY   zDoctestTransform.apply(  s8    MM))%*=*=> 	.DO""9-	.r)   NrZ   r[   r5   r)   r(   r   r   $  s    8.r)   r   c                      e Zd ZdZdZddZy)FilterSystemMessagesz&Filter system messages from a doctree.  c                6   | j                   j                  rdnd}t        | j                  j	                  t
        j                              D ]J  }|d   |k  st        j                  d|j                                |j                  j                  |       L y )Nrg      levelz%s [filtered system message])r.   keep_warningsrh   r+   rP   r   system_messager   debugastextrk   rn   )r'   rU   filterlevelrp   s       r(   rY   zFilterSystemMessages.apply1  sq    ;;44a!..u/C/CDE 	)DG}{*;T[[]K""4(	)r)   NrZ   r[   r5   r)   r(   r   r   -  s    0)r)   r   c                  0    e Zd ZdZej
                  ZddZy)SphinxContentsFilterzx
    Used with BuildEnvironment.add_toc_from() to discard cross-file links
    within table-of-contents link nodes.
    c                "    t         j                  r:   )r   SkipNode)r'   rp   s     r(   visit_imagez SphinxContentsFilter.visit_image@  s    nnr)   N)rp   znodes.imager/   rC   )r0   r1   r2   r3   r
    ignore_node_but_process_childrenvisit_pending_xrefr   r5   r)   r(   r   r   9  s     (HHr)   r   c                  8     e Zd ZdZdZd fdZddZddZ xZS )	SphinxSmartQuoteszr
    Customized SmartQuotes to avoid transform for some extra node types.

    refs: sphinx.parsers.RSTParser
    i  c                z    | j                         sy | j                  j                  | _        t        |           y r:   )is_availabler.   smartquotes_actionr@   rY   )r'   rU   rB   s     r(   rY   zSphinxSmartQuotes.applyL  s/      " #'++"@"@r)   c                   | j                   j                  j                  dg       }| j                   j                  j                  dg       }| j                  j                  j
                  du ry| j                   j                  du ry| j                  j                  j                  |v ry| j                   j                  |v ry| j                  j                  d   }t        d t        |      D              S )Nbuilders	languagesFlanguage_codec              3  @   K   | ]  }|t         j                  v   y wr:   )r   quotes)rv   tags     r(   rx   z1SphinxSmartQuotes.is_available.<locals>.<genexpr>h  s#      
 :$$$
s   )r.   smartquotes_excludesgetr+   r,   smart_quotessmartquotesr%   buildernamerL   r$   r   r   )r'   r   r   rL   s       r(   r   zSphinxSmartQuotes.is_availableU  s    ;;3377
BGKK4488bI	==!!..%7;;""e+88  H,;;9, 88$$_5 
-h7
 
 	
r)   c              #     K   |D ]H  }t        |      r't        j                  ddt        |            }d|f 5d|j	                         f J y w)Nz(?<=\x00)([-\\\'".`])z\\\1plainliteral)r   resubstrr   )r'   txtnodestxtnoderX   s       r(   
get_tokenszSphinxSmartQuotes.get_tokensm  sT        	2G(vv6WNtm#  !111	2s   AArZ   )r/   r   )r   z
list[Text]r/   z&Generator[tuple[str, str], None, None])	r0   r1   r2   r3   r\   rY   r   r   rE   rF   s   @r(   r   r   D  s    
 
0
2r)   r   c                      e Zd ZdZdZddZy)DoctreeReadEventz!Emit :event:`doctree-read` event.ip  c                P    | j                   j                  d| j                         y )Nzdoctree-read)r%   emitr+   )r'   rU   s     r(   rY   zDoctreeReadEvent.apply~  s    ndmm4r)   NrZ   r[   r5   r)   r(   r   r   z  s    +5r)   r   c                      e Zd ZdZdZddZy)ManpageLinkz&Find manpage section numbers and namesr   c           
        | j                   j                  t        j                        D ]  }dj	                  |j
                  D cg c]'  }t        |t        j                        rt        |      ) c}      }d}||dd}t        j                  ||      }|r|j                         }|j                  j                  |        y c c}w )N z7^(?P<path>(?P<page>.+)[\(\.](?P<section>[1-9]\w*)?\)?)$r=   )pathpagerl   )r+   rP   r   manpagejoinr{   r>   r   r   r   r   match	groupdict
attributesupdate)r'   rU   rp   xr   patterninfors           r(   rY   zManpageLink.apply  s    MM))(*:*:; 
	)Dhh  >1#-a#< !$A  > ?GPG##!#D '*A{{}OO""4(
	) >s   ,C
NrZ   r[   r5   r)   r(   r   r     s    0)r)   r   c                      e Zd ZdZdZddZy)GlossarySorterz.Sort glossaries that have the ``sorted`` flag.r   c                    | j                   j                  t        j                        D ]6  }|d   s	t	        t
        j                  |d         }t        |d       |d d  8 y )Nsortedr   c                    t        j                  dt        t        j                  |       d   j                         j                               S )NNFDr   )unicodedata	normalizer   r   termr   lower)items    r(   <lambda>z&GlossarySorter.apply.<locals>.<lambda>  s:    [%:%:UZZ.q188:@@B&D r)   key)r+   rP   r   glossaryr   r   definition_listr
  )r'   rU   r  r  s       r(   rY   zGlossarySorter.apply  s\    --h.?.?@ 	H!"&u'<'<hqk"J%+#D&"	r)   NrZ   r[   r5   r)   r(   r  r    s    8 	r)   r  c                      e Zd ZdZdZddZy)%ReorderConsecutiveTargetAndIndexNodesa  Index nodes interspersed between target nodes prevent other
    Transformations from combining those target nodes,
    e.g. ``PropagateTargets``.  This transformation reorders them:

    Given the following ``document`` as input::

        <document>
            <target ids="id1" ...>
            <index entries="...1...">
            <target ids="id2" ...>
            <target ids="id3" ...>
            <index entries="...2...">
            <target ids="id4" ...>

    The transformed result will be::

        <document>
            <index entries="...1...">
            <index entries="...2...">
            <target ids="id1" ...>
            <target ids="id2" ...>
            <target ids="id3" ...>
            <target ids="id4" ...>
       c                v    | j                   j                  t        j                        D ]  }t	        |        y r:   )r+   rP   r   ri   _reorder_index_target_nodes)r'   rU   ri   s      r(   rY   z+ReorderConsecutiveTargetAndIndexNodes.apply  s,    mm++ELL9 	0F'/	0r)   NrZ   r[   r5   r)   r(   r  r    s    4 0r)   r  c                   g }| j                  dd      D ]?  }t        |t        j                  t        j
                  f      r|j                  |       ? n t        |      dk  ry|d   j                  }||d   j                  k(  rU|j                  |d         }|j                  |d         }|t        |      z   dz
  |k(  rt        |t        	      |||dz    yyy)
zSort target and index nodes.

    Find all consecutive target and index nodes starting from ``start_node``,
    and move all index nodes to before the first target node.
    FT)descendsiblingsrg   Nr   r   r  )rP   r>   r   ri   r   rm   r   r   rk   r
  	_sort_key)
start_nodenodes_to_reorderrp   rk   	first_idxlast_idxs         r(   r  r    s     =?
 ""54"@ dU\\8>>:;##D)	 q a ''F!"%,,,LL!1!!45	<< 0 45s+,,q0H<-34D)-TF9X\* = -r)   c                    t        | t        j                        ryt        | t        j                        rydt        |       }t        |      )Nr   r   z+_sort_key called with unexpected node type )r>   r   rm   r   ri   rj   
ValueError)rp   r   s     r(   r   r     s>    $'$%7T
~
FC
S/r)   c                   | j                  t               | j                  t               | j                  t               | j                  t               | j                  t
               | j                  t               | j                  t               | j                  t               | j                  t               | j                  t               | j                  t               | j                  t               | j                  t               | j                  t               | j                  t               | j                  t                ddddS )NbuiltinT)r   parallel_read_safeparallel_write_safe)add_transformr   r   rH   rc   rr   r   r   r}   r   r   r   r   r   r   r  r  )r%   s    r(   setupr,    s   +,,-*+'(&'g&'m$'(*+34'(&'k"n%;< "# r)   )r+   r8   r/   r   )r!  znodes.targetr/   rC   )rp   z
nodes.Noder/   int)r%   r   r/   zdict[str, Any])Rr3   
__future__r   r   r  typingr   r   r   docutilsr   docutils.transformsr   r	   docutils.transforms.partsr
   docutils.transforms.universalr   docutils.utilsr   docutils.utils.smartquotesr   sphinxr   sphinx.localer   r   sphinx.utilr   sphinx.util.docutilsr   sphinx.util.i18nr   sphinx.util.nodesr   r   collections.abcr   docutils.nodesr   r   sphinx.applicationr   sphinx.configr   sphinx.domains.stdr   sphinx.environmentr   	getLoggerr0   r   rM   r"   r7   rH   rR   rc   rr   r}   r   literal_blockru   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r,  r5   r)   r(   <module>rD     s   @ " 	  + +  6 4 5 1 1    - ( G)))$13 
		8	$ i .1 1<3? 32
') )21 109O 9 	Ao 	A ((((99^^[[ 	.O 	.= ="(_ (&.O ... .	)? 	)> 32_ 32l5 5)/ )$_ $0O 0DU8r)   