B
    .Kc%              
   @   s  d Z ddlZddlZddlmZ ddl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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%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l0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 e%7e8Z9ej:eddZ;G dd deZ<eee
e=e=e=e=e=e=e=f  dddZ>eee=ef ddd Z?dS )!zTexinfo builder.    N)path)AnyDictIterableListOptionalTupleUnion)nodes)OptionParser)
FileOutput)Node)addnodespackage_dir)Sphinx)Builder)Config)ImageAdapter)NoUri)___)loggingprogress_messagestatus_iterator)	darkgreen)new_document)copy_asset_file)inline_all_toctrees)SEP	ensuredirmake_filename_from_project)TexinfoTranslatorTexinfoWriterZ	templatestexinfoc               @   s  e Zd ZdZdZdZedZejdkr2eed7 ZdddgZ	e
Zd	d
ddZeeee f d
ddZd$eee edddZd%eeee edddZd	d
ddZed	dddZeeee ejdddZd	d
ddZed	dd d!Zd	d
d"d#Zd	S )&TexinfoBuilderz=
    Builds Texinfo output to create Info documentation.
    r#   z$The Texinfo files are in %(outdir)s.posixzl
Run 'make' in that directory to run these through makeinfo
(use 'make info' here to do that automatically).z	image/pngz
image/jpegz	image/gifN)returnc             C   s   g | _ g | _d S )N)docnamesdocument_data)self r*   6lib/python3.7/site-packages/sphinx/builders/texinfo.pyinit0   s    zTexinfoBuilder.initc             C   s   dS )Nzall documentsr*   )r)   r*   r*   r+   get_outdated_docs4   s    z TexinfoBuilder.get_outdated_docs)docnametypr&   c             C   s"   || j krt||nd| S d S )N%)r'   r   )r)   r.   r/   r*   r*   r+   get_target_uri7   s    
zTexinfoBuilder.get_target_uri)from_tor/   r&   c             C   s   |  ||S )N)r1   )r)   r2   r3   r/   r*   r*   r+   get_relative_uri=   s    zTexinfoBuilder.get_relative_uric             C   s   dd | j jD }|s(ttd d S g | _xl|D ]d}|d }|| jjkr^ttd| q4| j	| |
td r|d d }| j	||d f q4W d S )	Nc             S   s   g | ]}t |qS r*   )list).0xr*   r*   r+   
<listcomp>B   s    z5TexinfoBuilder.init_document_data.<locals>.<listcomp>zGno "texinfo_documents" config value found; no documents will be writtenr   z?"texinfo_documents" config value references unknown document %sindex   )configtexinfo_documentsloggerwarningr   titlesenvZall_docsr(   appendendswithr   )r)   Zpreliminary_document_dataentryr.   r*   r*   r+   init_document_dataA   s    

z!TexinfoBuilder.init_document_data)ignoredr&   c             G   s  |    x| jD ]}|d d \}}}}|d7 }d } }}	t|dkr^|dd \}}}	d}
t|dkrv|d }
tt| j|dd}ttd	| " | j	j
pg }| j||
|d
}W d Q R X ttd | | t| }t , tjdtd t| jj|fdd }W d Q R X ||_||_|d d d |_| j	j|_|pNd|_|	pZd|_|pfd|_||_||_||| | |d d  W d Q R X qW d S )N   z.texi       Fzutf-8)Zdestination_pathencodingzprocessing %s)
appendicesZwritingignore)categoryT)defaultsZ
componentsZread_config_filesr:   z.info) rE   r(   lenr   r   joinoutdirr   r   r<   texinfo_appendicesassemble_doctreeZpost_process_imagesr"   warningscatch_warningsfilterwarningsDeprecationWarningr   rA   settingsZget_default_valuesauthortitleZtexinfo_filenametexinfo_elementsZtexinfo_dir_entryZtexinfo_dir_categoryZtexinfo_dir_descriptionr.   writecopy_image_files)r)   rF   rD   r.   
targetnamer[   rZ   ZdirentrydescriptionrN   toctree_onlyZdestinationrL   ZdoctreeZ	docwriterrY   r*   r*   r+   r]   T   sH    


zTexinfoBuilder.write)	indexfilera   rL   r&   c             C   s  t |g| | _tjt|d dd | j|}||d< |rtd}t	 }|t
dd7 }||7 }x|tjD ]}||7 }qtW |}t| | j||t|g}||d< x*|D ]"}	| j|	}
|	|
d< ||
 qW td ttd | j|||  x|tjD ]}|d	 }	|d
 }t||g}xX| jD ]N\}}|	|r2|ttd |t|| |td P q2W || qW |S )N T)nonlr.   z<texinfo output>z<Set title in conf.py>rH   zresolving references...Z
refdocnameZrefsectnamez (in ))setr'   r>   infor   rA   Zget_doctreer   r
   sectionr[   findallr   Ztoctreer   rB   r   Zresolve_referencesZpending_xrefZemphasisr@   
startswithZTextr   Zreplace_self)r)   rb   ra   rL   ZtreeZnew_treeZnew_sectZnodeZ	largetreer.   ZappendixZpendingnodeZsectnameZnewnodesZsubdirr[   r*   r*   r+   rT   }   sF    


zTexinfoBuilder.assemble_doctreec             C   s   |    d S )N)copy_support_files)r)   r*   r*   r+   finish   s    zTexinfoBuilder.finish)r_   r&   c             C   s   | j rt| jjj}xt| j tddt| j | jj|dD ]}| j | }y:t	
| j|d }t| tt	
| j|t	
|| W q: tk
r } z ttdt	
| j|| W d d }~X Y q:X q:W d S )Nzcopying images... Zbrown)stringify_funcz-figureszcannot copy image file %r: %s)Zimagesr   apprA   Zget_original_image_urir   r   rP   	verbosityr   rQ   rR   r   r   Zsrcdir	Exceptionr>   r?   )r)   r_   rm   srcdestZimagedirerrr*   r*   r+   r^      s    

zTexinfoBuilder.copy_image_filesc          
   C   sx   y@t td* tjddd ttjtd| j	 W d Q R X W n2 t
k
rr } zttd| W d d }~X Y nX d S )Nzcopying Texinfo support filesz	Makefile T)rd   ZMakefilezerror writing file Makefile: %s)r   r   r>   rg   r   osr   rQ   template_dirrR   OSErrorr?   )r)   rs   r*   r*   r+   rk      s    $z!TexinfoBuilder.copy_support_files)N)N)__name__
__module____qualname____doc__nameformatr   epilogrt   Zsupported_image_typesr!   Zdefault_translator_classr,   r	   strr   r-   r   r1   r4   rE   r   r]   boolr
   ZdocumentrT   rl   r^   rk   r*   r*   r*   r+   r$       s&   
)*r$   )r<   r&   c             C   s$   t | j}| j|| j| j|ddfgS )z, Better default texinfo_documents settings. zOne line description of projectZMiscellaneous)r    ZprojectZroot_docrZ   )r<   filenamer*   r*   r+   default_texinfo_documents   s    
r   )rn   r&   c             C   s|   |  t | dtd | dg d | di d | dddtg | ddd | d	dd | d
dd ddddS )Nr=   FrS   r\   Ztexinfo_domain_indicesTZtexinfo_show_urlsZfootnoteZtexinfo_no_detailmenuZtexinfo_cross_referencesZbuiltin)versionZparallel_read_safeZparallel_write_safe)Zadd_builderr$   Zadd_config_valuer   r5   )rn   r*   r*   r+   setup   s    
r   )@rz   rt   rU   r   typingr   r   r   r   r   r   r	   Zdocutilsr
   Zdocutils.frontendr   Zdocutils.ior   Zdocutils.nodesr   Zsphinxr   r   Zsphinx.applicationr   Zsphinx.buildersr   Zsphinx.configr   Z!sphinx.environment.adapters.assetr   Zsphinx.errorsr   Zsphinx.localer   r   Zsphinx.utilr   r   r   Zsphinx.util.consoler   Zsphinx.util.docutilsr   Zsphinx.util.fileutilr   Zsphinx.util.nodesr   Zsphinx.util.osutilr   r   r    Zsphinx.writers.texinfor!   r"   Z	getLoggerrw   r>   rQ   ru   r$   r~   r   r   r*   r*   r*   r+   <module>   s8   $
 $&