B
    \.                 @   s  d Z ddlZddl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mZmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZmZm Z m!Z!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. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; e<dZ=e7>e?Z@G dd dejAZBG dd deBZCG dd deBZDG dd deZEdd ZFG d d! d!eZGG d"d# d#eGZHG d$d% d%eGZIG d&d' d'eJZKd(d) ZLd*d+ ZMd,d- ZNdS ).z
    sphinx.io
    ~~~~~~~~~

    Input/Output files

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    N)	Publisher)	FileInput
NullOutput)Parser)
standalone)
StringListstring2lines)UnfilteredWriter)	text_type	iteritems)AnyUnion)RemovedInSphinx30Warning)__)ApplySourceWorkaroundExtraTranslatableNodesCitationReferencesDefaultSubstitutionsMoveModuleTargetsHandleCodeBlocksSortIdsAutoNumberingAutoIndexUpgraderFilterSystemMessagesUnreferencedFootnotesDetectorSphinxSmartQuotesDoctreeReadEventManpageLink)SphinxTransformer)RefOnlyBulletListTransform)PreserveTranslatableMessagesLocaleRemoveTranslatableInline)SphinxDomainsSubstitutionDefinitionsRemover)logging)LoggingReporter)UIDTransformz:\w+:.*?c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SphinxBaseReaderzm
    A base class of readers for Sphinx.

    This replaces reporter by Sphinx's on generating document.
    c             O   s    |j | _ tjj| f|| d S )N)envr   Reader__init__)selfappargskwargs r0   (lib/python3.7/site-packages/sphinx/io.pyr+   B   s    zSphinxBaseReader.__init__c             C   s   t j| | j S )N)r   r*   get_transforms
transforms)r,   r0   r0   r1   r2   G   s    zSphinxBaseReader.get_transformsc             C   s:   t j| }t||_|j| j |j}t	||_|S )zgCreates a new document object which having a special reporter object good
        for logging.
        )
r   r*   new_documentr   ZtransformerZset_environmentr)   reporterr&   Zfrom_reporter)r,   documentr5   r0   r0   r1   r4   K   s    
zSphinxBaseReader.new_documentN)__name__
__module____qualname____doc__r+   r2   r4   r0   r0   r0   r1   r(   ;   s   r(   c               @   sF   e Zd ZdZeeeeee	e
eeeeeeeeeeeeeegZdd ZdS )SphinxStandaloneReaderz-
    A basic document reader for Sphinx.
    c             O   s*   | j |j  | _ tj| |f|| d S )N)r3   registryr2   r(   r+   )r,   r-   r.   r/   r0   r0   r1   r+   j   s    zSphinxStandaloneReader.__init__N)r7   r8   r9   r:   r   r   r    r!   r   r   r   r   r   r   r   r"   r   r   r   r   r   r#   r$   r   r'   r3   r+   r0   r0   r0   r1   r;   ]   s   r;   c               @   sF   e Zd ZdZeeeeee	e
eeeeeeeegZdd Zedd ZdS )SphinxI18nReadera  
    A document reader for i18n.

    This returns the source line number of original text as current source line number
    to let users know where the error happened.
    Because the translated texts are partial and they don't have correct line numbers.
    c             C   s   t jdtdd dS )z/Stores the source line number of original text.z9SphinxI18nReader.set_lineno_for_reporter() is deprecated.   )
stacklevelN)warningswarnr   )r,   linenor0   r0   r1   set_lineno_for_reporter   s    z(SphinxI18nReader.set_lineno_for_reporterc             C   s   t jdtdd dS )Nz$SphinxI18nReader.line is deprecated.r>   )r?   r   )r@   rA   r   )r,   r0   r0   r1   line   s    
zSphinxI18nReader.lineN)r7   r8   r9   r:   r   r   r   r   r   r   r   r   r"   r   r   r   r   r   r$   r3   rC   propertyrD   r0   r0   r0   r1   r=   p   s   r=   c               @   s   e Zd ZdZdZdd ZdS )SphinxDummyWriterz0Dummy writer module used for generating doctree.)Zhtmlc             C   s   d S )Nr0   )r,   r0   r0   r1   	translate   s    zSphinxDummyWriter.translateN)r7   r8   r9   r:   	supportedrG   r0   r0   r0   r1   rF      s   rF   c             O   s   | S )z.Bypass source object as is to cheat Publisher.r0   )sourcer.   r/   r0   r0   r1   SphinxDummySourceClass   s    rJ   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )SphinxBaseFileInputzA base class of SphinxFileInput.

    It supports to replace unknown Unicode characters to '?'. And it also emits
    Sphinx events :event:`source-read` on reading.
    c             O   s8   || _ || _td| j d|d< tj| f|| d S )NsphinxZerror_handler)r-   r)   codecsregister_errorwarn_and_replacer   r+   )r,   r-   r)   r.   kwdsr0   r0   r1   r+      s
    zSphinxBaseFileInput.__init__c             C   s   t |tr|S || jdS )NrL   )
isinstancer
   decodeencoding)r,   datar0   r0   r1   rR      s    
zSphinxBaseFileInput.decodec             C   s,   t | }|g}| jd| jj| |d S )zeReads the contents from file.

        After reading, it emits Sphinx event ``source-read``.
        zsource-readr   )r   readr-   emitr)   docname)r,   rT   argr0   r0   r1   rU      s    
zSphinxBaseFileInput.readc             C   s   |j dd|j}|j d|j}|dkr4t|j }|j dd|jd }tjtd|j |d |j d |j |j|j	  d |j |j	|  | j
j|fd d	|j	fS )
z6Custom decoding error handler that warns and replaces.   
r      z5undecodable source characters, replacing with "?": %rs   >>>s   <<<)location?)objectrfindstartfindlencountloggerZwarningr   endr)   rW   )r,   errorZ	linestartZlineendrB   r0   r0   r1   rO      s    

,z$SphinxBaseFileInput.warn_and_replaceN)r7   r8   r9   r:   r+   rR   rU   rO   r0   r0   r0   r1   rK      s
   rK   c               @   s   e Zd ZdZdZdS )SphinxFileInputzA basic FileInput for Sphinx.)*N)r7   r8   r9   r:   rH   r0   r0   r0   r1   rg      s   rg   c               @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )SphinxRSTFileInputa  A reST FileInput for Sphinx.

    This FileInput automatically prepends and appends text by :confval:`rst_prolog` and
    :confval:`rst_epilog`.

    .. important::

       This FileInput uses an instance of ``StringList`` as a return value of ``read()``
       method to indicate original source filename and line numbers after prepending and
       appending.
       For that reason, ``sphinx.parsers.RSTParser`` should be used with this to parse
       a content correctly.
    )restructuredtextc             C   sp   |  |}|r&||ddd |d7 }x,t| D ]\}}||| |d| q4W ||| d ddd d S )N z<generated>r   r[   z<rst_prolog>)count_docinfo_linesinsert	enumerate
splitlines)r,   textZprologZdocinforB   rD   r0   r0   r1   prepend_prolog   s    
z!SphinxRSTFileInput.prepend_prologc             C   s:   | ddd x&t| D ]\}}| |d| qW d S )Nrk   z<generated>r   z<rst_epilog>)appendrn   ro   )r,   rp   ZepilogrB   rD   r0   r0   r1   append_epilog   s    z SphinxRSTFileInput.append_epilogc             C   s~   t | }t|dd}t }x$t|D ]\}}||| j| q&W | jjj	r^| 
|| jjj	 | jjjrz| || jjj |S )NT)Zconvert_whitespace)rK   rU   r   r   rn   rr   source_pathr)   configZ
rst_prologrq   Z
rst_epilogrs   )r,   ZinputstringlinescontentrB   rD   r0   r0   r1   rU      s    


zSphinxRSTFileInput.readc             C   s<   t |dkrdS x"t|jD ]\}}t|sP qW |S d S )Nr   )rb   rn   rT   
docinfo_rematch)r,   rw   rB   rD   r0   r0   r1   rl     s    
z&SphinxRSTFileInput.count_docinfo_linesN)	r7   r8   r9   r:   rH   rq   rs   rU   rl   r0   r0   r0   r1   ri      s   ri   c               @   s   e Zd ZdS )FiletypeNotFoundErrorN)r7   r8   r9   r0   r0   r0   r1   rz     s   rz   c             C   s0   x*t | D ]\}}||r
|p"dS q
W td S )Nrj   )r   endswithrz   )source_suffixfilenamesuffixfiletyper0   r0   r1   get_filetype  s    
r   c       	      C   s   t | jj|}| j|}t| }|| |d||jjd}| j| |}|jj	dkrd|j
dkrdtj
|_
t||t tt d}|ddd |d|jd ||| |  |jS )z(Parse a document and convert to doctree.N)rI   rt   rS   ZCommonMarkParserr0   )readerparserwriterZsource_classZdestinationrj   )r   ru   r|   r<   Zget_source_inputr;   Zsource_encodingZcreate_source_parser	__class__r7   Zsettings_spec	RSTParserr   rF   rJ   r   Zset_componentsZprocess_programmatic_settingsZsettingsZ
set_sourceZpublishr6   )	r-   r)   r}   r   Zinput_classr   rI   r   Zpubr0   r0   r1   read_doc%  s$    

r   c             C   s$   | j t | j t ddddS )NZbuiltinT)versionZparallel_read_safeZparallel_write_safe)r<   Zadd_source_inputrg   ri   )r-   r0   r0   r1   setupB  s
    r   )Or:   rM   rer@   Zdocutils.corer   Zdocutils.ior   r   Zdocutils.parsers.rstr   r   Zdocutils.readersr   Zdocutils.statemachiner   r   Zdocutils.writersr	   Zsixr
   r   typingr   r   Zsphinx.deprecationr   Zsphinx.localer   Zsphinx.transformsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z%sphinx.transforms.compact_bullet_listr   Zsphinx.transforms.i18nr    r!   r"   Zsphinx.transforms.referencesr#   r$   Zsphinx.utilr%   Zsphinx.util.docutilsr&   Zsphinx.versioningr'   compilerx   Z	getLoggerr7   rd   r*   r(   r;   r=   rF   rJ   rK   rg   ri   	Exceptionrz   r   r   r   r0   r0   r0   r1   <module>
   sF   @

"
5?
