U
    \Sh,                     @  sX  d Z ddlm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 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 eeZG dd de	Z dddZ!dddZ"dddZ#dZ$dZ%dZ&e&d Z'e(de& de' dZ)G d d! d!ej*Z+d"d#d$d%d&Z,d"d'd#d(d)d*Z-d"d+d$d,d-Z.dS ).z5Build epub3 files.

Originally derived from epub.py.
    )annotationsN)path)Any
NamedTuple)package_dir)Sphinx)
_epub_base)ENUMConfig)__)logging)copy_asset_file)format_date)make_filenamec                   @  s&   e Zd ZU ded< ded< ded< dS )NavPointstrtextrefuriz	list[Any]childrenN)__name__
__module____qualname____annotations__ r   r   H/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/builders/epub3.pyr      s   
r   Zltrrtl)
horizontalverticalr   r   zvertical-rlzhorizontal-tb)r   r   z<!DOCTYPE html>zU<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">u   :|[A-Z]|_|[a-z]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[𐀀-󯿿]u    \-|\.|[0-9]|·|[̀-ͯ]|[‿-⁀](z)(z)*c                      s   e Zd ZdZdZedZdZe	e
ddZeZeZdZdd	d
dZdd	 fddZddd fddZdddddZdddddZdd	ddZ  ZS )Epub3Builderz
    Builder that outputs epub3 files.

    It creates the metainfo files content.opf, nav.xhtml, toc.ncx, mimetype,
    and META-INF/container.xml. Afterwards, all necessary files are zipped to
    an epub file.
    epubzThe ePub file is in %(outdir)s.FZ	templatesZepub3TNone)returnc                 C  s<   |    |   |   |   |   |   |   dS )z/Create the metainfo files and finally the epub.N)Zget_tocZbuild_mimetypeZbuild_containerZbuild_contentbuild_navigation_docZ	build_tocZ
build_epubselfr   r   r   handle_finishV   s    zEpub3Builder.handle_finishdict[str, Any]c                   s   | j j}t  }t| j j|d< t| j j|d< t	||d< t
	||d< ttddd|d< t| j j|d	< | j j|d
< |S )zaCreate a dictionary with all metadata for the content.opf
        file properly escaped.
        descriptionZcontributorZpage_progression_directionZibook_scroll_axisz%Y-%m-%dT%H:%M:%SZenlanguagedateversionepub_version)configepub_writing_modesupercontent_metadatahtmlescapeepub_descriptionepub_contributorPAGE_PROGRESSION_DIRECTIONSgetIBOOK_SCROLL_AXISr   r-   r.   )r%   writing_modemetadata	__class__r   r   r2   `   s    
zEpub3Builder.content_metadatazset[str])docnamesr"   c                   sJ   t  | | jj}t|| jd< | j| jd< | j| jd< d| jd< d S )NZtheme_writing_modehtml_taguse_meta_charsetTZskip_ua_compatible)	r1   prepare_writingr/   r0   THEME_WRITING_MODESr8   Zglobalcontextr?   r@   )r%   r>   r:   r<   r   r   rA   p   s    zEpub3Builder.prepare_writingzlist[dict[str, Any]]zlist[NavPoint])navnodesr"   c                 C  s6  g }| tddg  d}|D ]
}|d s.q|d dd }|| jkrLq|d | jjkr^qt|d |d g }|d |kr|  |d j | | | q|d |d kr|d7 }|d j | | | q|d |k r"|d t|k r|  q|d }|d j | | | qt	d	q|d jS )
ac  Create the toc navigation structure.

        This method is almost same as build_navpoints method in epub.py.
        This is because the logical navigation structure of epub3 is not
        different from one of epub2.

        The difference from build_navpoints method is templates which are used
        when generating navigation documents.
         r   r   r   #level   z+Should never reach here. It might be a bug.)
appendr   splitZignored_filesr/   epub_tocdepthpopr   lenRuntimeError)r%   rC   ZnavstackrF   nodefileZnavpointr   r   r   build_navlisty   s8    




zEpub3Builder.build_navlist)navlistr"   c                 C  s"   t | jjt | jd |dS )z_Create a dictionary with all metadata for the nav.xhtml file
        properly escaped.
        Ztoc)langZ
toc_localerR   )r3   r4   r/   epub_languageZguide_titles)r%   rR   r   r   r   navigation_doc_metadata   s    z$Epub3Builder.navigation_doc_metadatac                 C  s   t td | jjdkrJ| jj| jj| ddd}| |g }| 	| n| j
}| |}tt| jd| j| | d| jkr| jd dS )z"Write the metainfo file nav.xhtml.zwriting nav.xhtml file...defaultF)Zprune_toctreesZincludehiddenznav.xhtml_tz	nav.xhtmlN)loggerinfor   r/   epub_tocscopeenvZget_and_resolve_doctreeZroot_docZget_refnodesZtoc_add_filesrefnodesrQ   r   r   jointemplate_dirZoutdirrU   filesrI   )r%   Zdoctreer[   rR   r   r   r   r#      s"      

z!Epub3Builder.build_navigation_doc)r   r   r   __doc__namer   epilogZsupported_remote_imagesr   r\   r   r]   DOCTYPEdoctypeHTML_TAGr?   r@   r&   r2   rA   rQ   rU   r#   __classcell__r   r   r<   r   r   D   s   
	*
r   r   r!   )appr"   c                 C  s   | j jdkrd S | jjs&ttd t| jj	sBttd | jj
sXttd | jjsnttd | jjsttd | jjsttd | jjsttd | jjsttd	 | jjsttd
 | jjsttd d S )Nr    zHconf value "epub_language" (or "language") should not be empty for EPUB3z2conf value "epub_uid" should be XML NAME for EPUB3zGconf value "epub_title" (or "html_title") should not be empty for EPUB3z6conf value "epub_author" should not be empty for EPUB3z;conf value "epub_contributor" should not be empty for EPUB3z;conf value "epub_description" should not be empty for EPUB3z9conf value "epub_publisher" should not be empty for EPUB3zIconf value "epub_copyright" (or "copyright")should not be empty for EPUB3z:conf value "epub_identifier" should not be empty for EPUB3z2conf value "version" should not be empty for EPUB3)Zbuilderr`   r/   rT   rW   warningr   _XML_NAME_PATTERNmatchepub_uid
epub_titleepub_authorr6   r5   epub_publisherepub_copyrightepub_identifierr-   rf   r   r   r   validate_config_values   s,    rq   r
   )rf   r/   r"   c              	   C  sx   g }|j D ]b}t|tr(||i f q
z|\}}|||f W q
 tk
rj   ttd| Y q
Y q
X q
||_ dS )z?This converts string styled epub_css_files to tuple styled one.zinvalid css_file: %r, ignoredN)epub_css_files
isinstancer   rI   	ExceptionrW   rg   r   )rf   r/   rr   entryfilenameattrsr   r   r   convert_epub_css_files   s    

rx   r'   c                 C  s  |  t | ddd d | ddd | ddd | d	i d | d
dd d | ddd d | ddd d | ddd d | ddd d | ddd | ddd | ddd | ddd | ddd | dg d | dg d | ddd d | d g d | d!d"d | d#d$d | d%d&d | d'dd | d(d)d | d*d+d | d,d-d d | d.dd | d/dd | d0d1dtd1d2 | jd3td4d5 | d6t d7d$d$d8S )9NZepub_basenamec                 S  s
   t | jS N)r   projectr$   r   r   r   <lambda>       zsetup.<locals>.<lambda>Fr.   g      @r    Z
epub_themeZepub_theme_optionsrk   c                 S  s   | j S ry   )rz   r$   r   r   r   r{     r|   rl   c                 S  s   | j S ry   authorr$   r   r   r   r{     r|   rT   c                 S  s
   | j pdS )Nr)   r*   r$   r   r   r   r{     r|   rm   c                 S  s   | j S ry   r}   r$   r   r   r   r{     r|   rn   c                 S  s   | j S ry   )	copyrightr$   r   r   r   r{     r|   ro   unknownZepub_schemerj   rZ   Z
epub_coverr   Z
epub_guideZepub_pre_filesZepub_post_filesrr   c                 S  s   | j S ry   )Zhtml_css_files)r/   r   r   r   r{     r|   Zepub_exclude_filesrK      Zepub_tocdupTrY   rV   Zepub_fix_imagesZepub_max_image_widthr   Zepub_show_urlsinlineZepub_use_indexc                 S  s   | j S ry   )Zhtml_use_indexr$   r   r   r   r{     r|   r5   r6   r0   r   r   zconfig-initedi   )priorityzbuilder-initedbuiltin)r-   Zparallel_read_safeZparallel_write_safe)Zadd_builderr   Zadd_config_valuer	   connectrx   rq   rp   r   r   r   setup   sJ    

r   )/r_   
__future__r   r3   reosr   typingr   r   Zsphinxr   Zsphinx.applicationr   Zsphinx.buildersr   Zsphinx.configr	   r
   Zsphinx.localer   Zsphinx.utilr   Zsphinx.util.fileutilr   Zsphinx.util.i18nr   Zsphinx.util.osutilr   	getLoggerr   rW   r   r7   r9   rB   rb   rd   Z_xml_name_start_charZ_xml_name_charcompilerh   ZEpubBuilderr   rq   rx   r   r   r   r   r   <module>   sJ   
	'