o
    Ni-                     @  sh  d Z ddlmZ ddl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
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 e	rhd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/d0d'd(Z0d1d+d,Z1d2d.d/Z2dS )3z5Build epub3 files.

Originally derived from epub.py.
    )annotationsN)path)TYPE_CHECKINGAny
NamedTuple)package_dir)
_epub_base)ENUMConfig)__)logging)copy_asset_file)make_filename)Set)Sphinx)ExtensionMetadatac                   @  s&   e Zd ZU ded< ded< ded< dS )NavPointstrtextrefurilist[NavPoint]childrenN)__name__
__module____qualname____annotations__ r   r   I/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/builders/epub3.pyr       s   
 r   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Zd fddZd fddZd ddZd!ddZ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.F	templatesepub3TreturnNonec                 C  s<   |    |   |   |   |   |   |   dS )z/Create the metainfo files and finally the epub.N)get_tocbuild_mimetypebuild_containerbuild_contentbuild_navigation_doc	build_toc
build_epubselfr   r   r   handle_finish\   s   zEpub3Builder.handle_finishdict[str, Any]c                   s   | j j}td }durtt|}nt }t  }t	
| j j|d< t	
| j j|d< t||d< t||d< t	
td||d< t	
| j j|d	< | j j|d
< |S )zaCreate a dictionary with all metadata for the content.opf
        file properly escaped.
        SOURCE_DATE_EPOCHNdescriptioncontributorpage_progression_directionibook_scroll_axisz%Y-%m-%dT%H:%M:%SZdateversionepub_version)configepub_writing_modeosgetenvtimegmtimeintsupercontent_metadatahtmlescapeepub_descriptionepub_contributorPAGE_PROGRESSION_DIRECTIONSgetIBOOK_SCROLL_AXISstrftimer:   r;   )r1   writing_modesource_date_epoch
time_tuplemetadata	__class__r   r   rD   f   s   
zEpub3Builder.content_metadatadocnamesSet[str]c                   sJ   t  | | jj}t|| jd< | j| jd< | j| jd< d| jd< d S )Ntheme_writing_modehtml_taguse_meta_charsetTskip_ua_compatible)	rC   prepare_writingr<   r=   THEME_WRITING_MODESrJ   globalcontextrV   rW   )r1   rS   rM   rQ   r   r   rY   }   s   zEpub3Builder.prepare_writingnavnodeslist[dict[str, Any]]r   c                 C  sB  g }| tddg  d}|D ]}|d sq|d dd }|| jv r%q|d | jjkr.qt|d |d g }|d |krP|  |d j | | | q|d |d krj|d7 }|d j | | | q|d |k r|d t|k r|  |d t|k sx|d }|d j | | | qd	}t	||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   splitignored_filesr<   epub_tocdepthpopr   lenRuntimeError)r1   r\   navstackr`   nodefilenavpointunreachabler   r   r   build_navlist   s<   


zEpub3Builder.build_navlistnavlistc                 C  s"   t | jjt | jd |dS )z_Create a dictionary with all metadata for the nav.xhtml file
        properly escaped.
        toc)lang
toc_localerp   )rE   rF   r<   epub_languageguide_titles)r1   rp   r   r   r   navigation_doc_metadata   s   z$Epub3Builder.navigation_doc_metadatac                 C  s   t td | jjdkr%| jj| jj| ddd}| |g }| 	| n| j
}| |}tt| jd| j| |dd d| jvrK| jd d	S d	S )
z"Write the metainfo file nav.xhtml.zwriting nav.xhtml file...defaultF)prune_toctreesincludehiddenznav.xhtml.jinjaT)contextforcez	nav.xhtmlN)loggerinfor   r<   epub_tocscopeenvget_and_resolve_doctreeroot_docget_refnodestoc_add_filesrefnodesro   r   r   jointemplate_diroutdirrv   filesrc   )r1   doctreer   rp   r   r   r   r-      s$   

z!Epub3Builder.build_navigation_doc)r'   r(   )r'   r3   )rS   rT   r'   r(   )r\   r]   r'   r   )rp   r   r'   r3   )r   r   r   __doc__namer   epilogsupported_remote_imagesr   r   r   r   DOCTYPEdoctypeHTML_TAGrV   rW   r2   rD   rY   ro   rv   r-   __classcell__r   r   rQ   r   r#   I   s    


	
+
r#   appr   r'   r(   c                 C  s   | j jdkrd S | jjsttd t| jj	s!ttd | jj
s,ttd | jjs7ttd | jjsBttd | jjsMttd | jjsXttd | jjscttd	 | jjsnttd
 | jjs{ttd d S 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)builderr   r<   rt   r|   warningr   _XML_NAME_PATTERNmatchepub_uid
epub_titleepub_authorrH   rG   epub_publisherepub_copyrightepub_identifierr:   r   r   r   r   validate_config_values   sN   r   r<   r
   c              	   C  sp   g }|j D ]-}t|tr||i f qz|\}}|||f W q ty2   ttd| Y qw ||_ dS )z9Convert string styled epub_css_files to tuple styled one.zinvalid css_file: %r, ignoredN)epub_css_files
isinstancer   rc   	Exceptionr|   r   r   )r   r<   r   entryfilenameattrsr   r   r   convert_epub_css_files  s   


r   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 | d0dd | d1d2dtd2d3 | jd4td5d6 | d7t d8d$d$d9S ):Nepub_basenamec                 S  s
   t | jS N)r   projectr0   r   r   r   <lambda>!     
 zsetup.<locals>.<lambda>r^   r;   g      @r$   
epub_themeepub_theme_optionsr   c                 S     | j S r   )r   r0   r   r   r   r   %      r   c                 S  r   r   authorr0   r   r   r   r   &  r   rt   c                 S  s
   | j pdS )Nen)languager0   r   r   r   r   '  r   r   c                 S  r   r   r   r0   r   r   r   r   (  r   r   c                 S  r   r   )	copyrightr0   r   r   r   r   )  r   r   unknownepub_schemer   r   
epub_coverr   
epub_guideepub_pre_filesepub_post_filesr   c                 S  r   r   )html_css_files)r<   r   r   r   r   1  r   epub_exclude_filesrf      epub_tocdupTr~   rw   epub_fix_imagesFepub_max_image_widthr   epub_show_urlsinlineepub_use_indexc                 S  r   r   )html_use_indexr0   r   r   r   r   9  r   rG   rH   r=   r    r!   zconfig-initedi   )priorityzbuilder-initedbuiltin)r:   parallel_read_safeparallel_write_safe)add_builderr#   add_config_valuer	   connectr   r   r   r   r   r   setup  sJ   
r   )r   r   r'   r(   )r   r   r<   r
   r'   r(   )r   r   r'   r   )3r   
__future__r   rE   r>   rer@   r   typingr   r   r   sphinxr   sphinx.buildersr   sphinx.configr	   r
   sphinx.localer   sphinx.utilr   sphinx.util.fileutilr   sphinx.util.osutilr   collections.abcr   sphinx.applicationr   sphinx.util.typingr   	getLoggerr   r|   r   rI   rK   rZ   r   r   _xml_name_start_char_xml_name_charcompiler   EpubBuilderr#   r   r   r   r   r   r   r   <module>   sV    
 

7