B
    .Kc[              "   @   s4  d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
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 erddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& e'e(Z)e*dej+Z,e,Z-G dd dZ.ee/dddZ0dee1e/dddZ2eddddZ3ej4ej5ej6ej7fZ8ee9dddZ:ee9dddZ;ej5ej6ej<ej=fZ>ej?fZ@ejAfZBeeeee/f  d d!d"ZCee	e/ dd#d$ZDee	e1 dd%d&ZEdeeee d'd(d)ZFee	e dd*d+ZGeeeeed, f  d d-d.ZHeeee/d/d0d1ZIee/dd2d3ZJe/ee9e/e/f d4d5d6ZKd7d8d9d:d;d<gZLe/e/eee/e/e/e/e	e/ f  d=d>d?ZMd@e
e/ e/ejNeee/ ejNdAdBdCZOe/e/dDdEdFZPe*dGZQe*dHZRdIdJdKdLdMdNdOdOdPdJdQdRdMdSdTdNdNdUdVdWdVdMdSdNdXdPdYdVdZdXd[d\dUd]!ZSd^d_d`dadbdcZTddeejNe/e	e/ e/dfdgdhZUejVe/e	e didjdkZWdd@e/e/e/eeee f e	e/ ejXdldmdnZYeeddodpdqZZee1eddrdsdtZ[eeddudvdwZ\ej]ej^ej_ej?ej=ej`ejafZbee9ddxdyZcedzdd{d|d}Zdeed~ddZeeeej_fdS )z3Docutils node-related utility functions for Sphinx.    N)
TYPE_CHECKINGAnyCallableIterableListOptionalSetTupleTypeUnion)nodes)ElementNode)	Directive)Inliner)
StringList)addnodes)__)logging)Builder)
IndexEntry)BuildEnvironment)Tagsz^(.+?)\s*(?<!\x00)<([^<]*?)>$c               @   sF   e Zd ZdZee eddddZeedddZ	eedd	d
Z
dS )NodeMatchera"  A helper class for Node.findall().

    It checks that the given node is an instance of the specified node-classes and
    has the specified node-attributes.

    For example, following example searches ``reference`` node having ``refdomain``
    and ``reftype`` attributes::

        matcher = NodeMatcher(nodes.reference, refdomain='std', reftype='citation')
        doctree.findall(matcher)
        # => [<reference ...>, <reference ...>, ...]

    A special value ``typing.Any`` matches any kind of node-attributes.  For example,
    following example searches ``reference`` node having ``refdomain`` attributes::

        from typing import Any
        matcher = NodeMatcher(nodes.reference, refdomain=Any)
        doctree.findall(matcher)
        # => [<reference ...>, <reference ...>, ...]
    N)node_classesattrsreturnc             O   s   || _ || _d S )N)classesr   )selfr   r    r   0lib/python3.7/site-packages/sphinx/util/nodes.py__init__6   s    zNodeMatcher.__init__)noder   c             C   s   yr| j rt|| j sdS | jrpt|tjs.dS x@| j D ]2\}}||krNdS |tkrZq:q:|||kr:dS q:W dS  tk
r   dS X d S )NFT)	r   
isinstancer   r   r   itemsr   get	Exception)r   r"   keyvaluer   r   r    match:   s     zNodeMatcher.matchc             C   s
   |  |S )N)r)   )r   r"   r   r   r    __call__P   s    zNodeMatcher.__call__)__name__
__module____qualname____doc__r
   r   r   r!   boolr)   r*   r   r   r   r    r       s   r   )r"   r   c             C   s   d | j| jjS )z
    Return full module dotted path like: 'docutils.nodes.paragraph'

    :param nodes.Node node: target node
    :return: full module dotted path
    z{}.{})formatr,   	__class__r+   )r"   r   r   r    get_full_module_nameT   s    r2   P   )r"   lengthr   c             C   sR   y|    }W n tk
r,   t| }Y nX |rNt||krN|d| d }|S )a  
    return DOM XML representation of the specified node like:
    '<paragraph translatable="False"><inline classes="versionmodified">New in version...'

    :param nodes.Node node: target node
    :param int length:
       length of return value to be striped. if false-value is specified, repr_domxml
       returns full of DOM XML representation.
    :return: DOM XML representation
    Nz...)ZasdomZtoxmlr&   strlen)r"   r4   textr   r   r    repr_domxml^   s    r8   c             C   s4  t | tjrN| jsNtdt| t|  | j}|j	| _	|j
d | _
|  | _nt | tjrj| j	sj| jj	| _	t | tjr| j	d krtdt| t|  | jj	| jj
 | _	| _
t | tjr| j	d krtdt| t|  | jj	| jj
 | _	| _
t | tjrTtdt| t|  x@tt| jtjD ]&}tdt|  d| j| _q*W t | tjr| j	d krtdt| t|  | jj	| jj
 | _	| _
t | tjr| j	d krt| | _	| js|  | _| j	r| jrd S t | tjtj
tjtjfr0tdt| t|  t| p"d| _	d	| _
d S d S )
Nz7[i18n] PATCH: %r to have source, line and rawsource: %s   z)[i18n] PATCH: %r to have source, line: %sz#[i18n] PATCH: %r to have source: %sz&[i18n] PATCH: %r to have rawsource: %sz	\s*:\s*%s z,[i18n] PATCH: %r to have source and line: %sr   )r#   r   
classifier	rawsourceloggerdebugr2   r8   parentsourcelineastextimagetitletermreversedlistfindallresubescapeZtopicliteral_blockget_node_sourceZrubric
field_name)r"   Zdefinition_list_itemr;   r   r   r    apply_source_workaroundr   sV    


rO   c             C   s4   t | tjr,t | jdd gd tjr,dS dS d S )Nr   r   TF)r#   r   pendingdetailsr%   r   meta)r"   r   r   r    is_pending_meta   s    rS   c             C   s:  t | tjrdS t | tjr4| ds0| dr4dS t | tjrLd| krLdS t | tjr| jszt	
dt| t|  dS t | trd| krt	
dt| t|  dS | ddst	
dt| t|  dS t | tjr| jd d	krt	
d
t| t|  dS dS t| s t | tjr$dS t | tjr6dS dS )NTtranslatablealtFz)[i18n] SKIP %r because no node.source: %szO[i18n] SKIP %r because node is in IGNORED_NODES and no node['translatable']: %sz3[i18n] SKIP %r because not node['translatable']: %sr   Zorphanz&[i18n] SKIP %r because orphan node: %s)r#   r   rT   r   rC   r%   ZInlineZTextElementr@   r=   r>   r2   r8   IGNORED_NODESrN   childrenrS   rR   docutils_meta)r"   r   r   r    is_translatable   s:     rY   )doctreer   c             c   s  x
|  tD ]}t|tjr<x| D ]}||fV  q(W qt|trZ|j}|s| }nt|t	j
r|dr~||d fV  |drd|d  }qd}n`t|tr|j}nNt|t	jrt|r|jd d j}n(t|tjr|d }n|jd	d
 }|r||fV  qW dS )z3Extract translatable messages from a document tree.rU   rT   z.. image:: %sZurir:   r   r   content
 N)rH   rY   r#   r   rT   Zextract_original_messagesLITERAL_TYPE_NODESr<   rB   r   rC   r%   META_TYPE_NODESZ
rawcontentrP   rS   rQ   rX   replacestrip)rZ   r"   msgr   r   r    extract_messages   s0    





rc   c             C   s"   xt | D ]}|jr
|jS q
W d S )N)traverse_parentr@   )r"   pnoder   r   r    rM     s    
rM   c             C   s"   xt | D ]}|jr
|jS q
W d S )N)rd   rA   )r"   re   r   r   r    get_node_line&  s    
rf   )r"   clsr   c             c   s,   x&| r&|d kst | |r| V  | j} qW d S )N)r#   r?   )r"   rg   r   r   r    rd   -  s    rd   c             C   s*   | j | }|dkr"| j |d  S d S d S )Nr   r9   )r?   index)r"   posr   r   r    get_prev_node4  s    rj   r   c             c   sJ   t tjdd}x6| |D ](}d|kr0|d }n|d }||fV  qW dS )z6Traverse translatable index node from a document tree.F)ZinlineZraw_entriesentriesN)r   r   rh   rH   )rZ   Zmatcherr"   rk   r   r   r    traverse_translatable_index<  s    
rl   )stater[   r"   r   c             C   sJ   | j j}| j j}g | j _d| j _z| j|d|ddS || j _|| j _X dS )zVersion of state.nested_parse() that allows titles and does not require
    titles to have the same decoration as the calling document.

    This is useful when the parsed content comes from a completely different
    context, such as docstrings.
    r   r9   )Zmatch_titlesN)memoZtitle_stylesZsection_levelZnested_parse)rm   r[   r"   Zsurrounding_title_stylesZsurrounding_section_levelr   r   r    nested_parse_with_titlesG  s    ro   c             C   sT   |   } x| tjD ]}d|d< qW x$t| tjD ]}|j| q8W |  S )z&Like node.astext(), but ignore images.r:   rU   )	deepcopyrH   r   rC   rG   rawr?   removerB   )r"   Zimgrq   r   r   r    clean_astextZ  s    rs   )r7   r   c             C   s.   t | }|r$d|d|dfS d| | fS )z3Split role content into title and target, if given.Tr9      F)explicit_title_rer)   group)r7   r)   r   r   r    split_explicit_titled  s    
rw   singlepairdoubleZtripleZseeZseealso)entrytargetidr   c             C   sP  ddl m} g }|  } | }d}| dr>d}| dd   } x
|D ]P}| |d rF| t|d d   }|| d | }|d	|||d f P qFW xtD ]L}| |d r| t|d d   }|d
krd	}|||||d f P qW xZ|dD ]L}| }d}|dr,d}|dd   }|s4q|d|||d f qW |S )Nr   )pairindextypesr:   !mainr9   :z; ry   rz   ,rx   )	Zsphinx.domains.pythonr}   ra   
startswithlstripr6   append
indextypessplit)r{   r|   r}   ZindexentriesZoentryr   typer(   r   r   r    process_index_entryq  s>    

r   r   )builder
docnamesetdocnametree	colorfunc	traversedr   c          
   C   s  |  }xt|tjD ]}g }tt|d }x|D ]}	|	|kr8yH||	 tj	||	d dd t
| ||	| j|	||}
||	 W n( tk
r   tjtd|	|d Y q8X tj|	d}|
j|_x$|tjD ]}d|kr|	|d< qW || q8W |j|| qW |S )	zwInline all toctrees in the *tree*.

    Record all docnames in *docnameset*, and output docnames with *colorfunc*.
    includefilesr]   T)Znonlz+toctree contains ref to nonexisting file %r)location)r   r   )rp   rG   rH   r   Ztoctreemapr5   r   r=   infoinline_all_toctreesenvZget_doctreeaddr&   warningr   Zstart_of_filerW   r   sectionr?   r`   )r   r   r   r   r   r   ZtoctreenodeZnewnodesr   ZincludefileZsubtreeZsofZsectionnoder   r   r    r     s0    



r   )stringr   c             C   sX   |  t}| t}td|ddd}tdd	|
 }td|}t|S )a  Convert `string` into an identifier and return it.

    This function is a modified version of ``docutils.nodes.make_id()`` of
    docutils-0.16.

    Changes:

    * Allow to use capital alphabet characters
    * Allow to use dots (".") and underscores ("_") for an identifier
      without a leading character.

    # Author: David Goodger <goodger@python.org>
    # Maintainer: docutils-develop@lists.sourceforge.net
    # Copyright: This module has been placed in the public domain.
    ZNFKDasciiignore-r]   r:   )	translate_non_id_translate_digraphs_non_id_translateunicodedata	normalizeencodedecode_non_id_charsrJ   joinr   _non_id_at_endsr5   )r   idr   r   r    _make_id  s    

r   z[^a-zA-Z0-9._]+z^[-0-9._]+|-+$odhiltbcfknpyzgjseqr)!   i  i'  i1  iB  ig  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i%  i4  i5  i6  i7  i<  i?  i@  iG  iI  iK  iM  iO  ZszZaeZoeZdbZqp)      iS  i8  i9  r:   r   )r   documentprefixrE   r   c             C   s   d}|r|d }n|j jpdd }|rD|rDt|| }||kr\d}n|r\t|}|dkr\d}x$|dksp||jkr|| | }q^W |S )z>Generate an appropriate node_id for given *prefix* and *term*.Nz-%sr   z%sr:   )ZsettingsZ	id_prefixr   ZidsZnew_serialno)r   r   r   rE   Znode_idZidformatr   r   r    make_id  s    
r   )r"   	conditionr   c             C   s4   x.| D ]"}t |tjr|d|kr|S qW dS dS )zBPick matched pending_xref_condition node up from the pending_xref.r   N)r#   r   Zpending_xref_conditionr%   )r"   r   Zsubnoder   r   r    find_pending_xref_condition  s
    
r   )r   fromdocname	todocnamer|   childrD   r   c             C   sl   t jdddd}||kr&|r&||d< n.|rD| ||d | |d< n| |||d< |r`||d< ||7 }|S )z$Shortcut to create a reference node.r:   T)ZinternalZrefid#ZrefuriZreftitle)r   	referenceZget_relative_uri)r   r   r   r|   r   rD   r"   r   r   r    make_refnode(  s    
r   )	directiver"   r   c             C   s   | j | j\|_|_d S )N)Zstate_machineget_source_and_linelinenor@   rA   )r   r"   r   r   r    set_source_info;  s    r   )inlinerr   r"   r   c             C   s   | j |\|_|_d S )N)Zreporterr   r@   rA   )r   r   r"   r   r   r    set_role_source_info@  s    r   )srcdstr   c             C   s   t | |_t| |_d S )N)rM   r@   rf   rA   )r   r   r   r   r    copy_source_infoD  s    
r   c             C   sN   x4t | jD ]&}t|trdS |dddkrdS qW t| dddkrJdS dS )z0Check whether the node is smart-quotable or not.FZsupport_smartquotesNT)rd   r?   r#   NON_SMARTQUOTABLE_PARENT_NODESr%   getattr)r"   re   r   r   r    is_smartquotableT  s    
r   r   )r   tagsr   c             C   s   x|  tjD ]}y||d }W nJ tk
rn } z,tjtd||d ||j	pZt
  W dd}~X Y qX |r||j	pt
  q|t
  qW dS )z0Filter ``only`` nodes which do not match *tags*.exprz8exception while evaluating only directive expression: %s)r   N)rH   r   ZonlyZeval_conditionr&   r=   r   r   Zreplace_selfrW   r   comment)r   r   r"   reterrr   r   r    process_only_nodesb  s    &r   )r   r   c             C   s2   | j | jf| j}t| tjr.| j|_| j|_|S )zmonkey-patch Element.copy to copy the rawsource and line
    for docutils-0.16 or older versions.

    refs: https://sourceforge.net/p/docutils/patches/165/
    )r1   r<   Z
attributesr#   r   r   r@   rA   )r   Znewnoder   r   r    	_new_copyv  s
    r   )r3   )N)r:   N)N)gr.   rI   r   typingr   r   r   r   r   r   r   r	   r
   r   Zdocutilsr   Zdocutils.nodesr   r   Zdocutils.parsers.rstr   Zdocutils.parsers.rst.statesr   Zdocutils.statemachiner   Zsphinxr   Zsphinx.localer   Zsphinx.utilr   Zsphinx.buildersr   Zsphinx.domainr   Zsphinx.environmentr   Zsphinx.util.tagsr   Z	getLoggerr+   r=   compileDOTALLru   Zcaption_ref_rer   r5   r2   intr8   rO   Z	InvisiblerL   Zdoctest_blockZversionmodifiedrV   r/   rS   rY   Z
math_blockrq   r^   rC   ZIMAGE_TYPE_NODESrR   r_   rc   rM   rf   rd   rj   rl   ro   rs   rw   r   r   r   r   r   r   r   r   r   r   Zpending_xrefr   r   r   r   r   r   ZFixedTextElementliteralZmathZproblematicZnot_smartquotabler   r   r   r   copyr   r   r   r    <module>   s   0
4
?.# 
	$&
 

