
    -Ph1                       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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 ddlmZ e	r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j!        e"          Z# G 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   Z+ ej,        de* d e+ d!          Z- G d" d#ej.                  Z/d.d(Z0d/d+Z1d0d-Z2dS )1z5Build epub3 files.

Originally derived from epub.py.
    )annotationsN)TYPE_CHECKING
NamedTuple)package_dir)
_epub_base)ENUM)__)logging)copy_asset_file)make_filename)Set)Any)Sphinx)Config)ExtensionMetadatac                  .    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__     U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx/builders/epub3.pyr   r   "   s0         IIIKKKr   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]|[À-Ö]|[Ø-ö]|[ø-˿]|[Ͱ-ͽ]|[Ϳ-῿]|[‌-‍]|[⁰-↏]|[Ⰰ-⿯]|[、-퟿]|[豈-﷏]|[ﷰ-�]|[𐀀-󯿿]z2\-|\.|[0-9]|\u00b7|[\u0300-\u036f]|[\u203f-\u2040](z)(z)*c                       e Zd ZdZdZ ed          ZdZ ej	        dd          Z
eZeZdZdd
Zd fdZd fdZddZddZddZ xZS )Epub3BuilderzBuilder 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                   |                                   |                                  |                                  |                                  |                                  |                                  |                                  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selfs    r   handle_finishzEpub3Builder.handle_finish]   s}    !!###r   dict[str, Any]c                   | j         j        }t          j        d          x}"t	          j        t          |                    }nt	          j                    }t                                                      }t          j
        | j         j                  |d<   t          j
        | j         j                  |d<   t                              |          |d<   t                              |          |d<   t          j
        t	          j        d|                    |d<   t          j
        | 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@   )r5   writing_modesource_date_epoch
time_tuplemetadata	__class__s        r   rI   zEpub3Builder.content_metadatag   s    {4!#+>!?!??LS):%;%;<<JJJ77++--"&+dk.J"K"K"&+dk.J"K"K1L1P1P2
 2
-. ):(=(=l(K(K$%;t}5I:'V'VWW"k$+*=>>#';#; r   docnamesSet[str]c                    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)	rH   prepare_writingrA   rB   THEME_WRITING_MODESrO   globalcontextr[   r\   )r5   rW   rR   rV   s      r   r^   zEpub3Builder.prepare_writing~   su    ))){43F3J3J<3X3X/0)-:&151F-.37/000r   navnodeslist[dict[str, Any]]r   c                   t          ddg           g}d}|D ]}|d         s|d                             d          d         }|| j        v r7|d         | j        j        k    rNt          |d         |d         g           }|d         |k    rJ|                                 |d         j                            |           |                    |           |d         |dz   k    r<|dz  }|d         j                            |           |                    |           |d         |k     r|d         t          |          k     r-|                                 |d         t          |          k     -|d         }|d         j                            |           |                    |           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.)
r   splitignored_filesrA   epub_tocdepthpopr   appendlenRuntimeError)r5   ra   navstackrf   nodefilenavpointunreachables           r   build_navlistzEpub3Builder.build_navlist   s    %-RR$8$8#9 	0 	0D< >'',,Q/Dt)))G}t{888Vd8nbAAHG}%%%,,X666))))g%!)++
%,,X666))))g&&7mc(mm33LLNNN 7mc(mm33W%,,X666))))K";///{##r   navlistc                    t          j        | j        j                  t          j        | j        d                   |dS )z_Create a dictionary with all metadata for the nav.xhtml file
        properly escaped.
        toc)lang
toc_localerv   )rJ   rK   rA   epub_languageguide_titles)r5   rv   s     r   navigation_doc_metadataz$Epub3Builder.navigation_doc_metadata   s=    
 K 9::+d&7&>??
 
 	
r   c                   t                               t          d                     | j        j        dk    rT| j                            | j        j        | dd          }|                     |g           }| 	                    |           n| j
        }|                     |          }t          | j        dz  | j        |                     |          d           d| j        vr| 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	   rA   epub_tocscopeenvget_and_resolve_doctreeroot_docget_refnodestoc_add_filesrefnodesru   r   template_diroutdirr}   filesrm   )r5   doctreer   rv   s       r   r1   z!Epub3Builder.build_navigation_doc   s   B233444;$	11h66$d5PU 7  G (("55Hx(((( }H$$X.. 11K0099		
 	
 	
 	
 dj((Jk***** )(r   )r*   r+   )r*   r7   )rW   rX   r*   r+   )ra   rb   r*   r   )rv   r   r*   r7   )r   r   r   __doc__namer	   epilogsupported_remote_imagesr   joinpathr   DOCTYPEdoctypeHTML_TAGr[   r\   r6   rI   r^   ru   r}   r1   __classcell__)rV   s   @r   r&   r&   K   s          DR122F#';'W==LGH        .8 8 8 8 8 8($ ($ ($ ($T
 
 
 
+ + + + + + + +r   r&   appr   r*   r+   c                Z   | 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        s't                              t          d                     | j        j        s't                              t          d                     | j        j        s't                              t          d                     | j        j        s't                              t          d                     | j        j        s't                              t          d	                     | j        j        s't                              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   rA   r{   r   warningr	   _XML_NAME_PATTERNmatchepub_uid
epub_titleepub_authorrM   rL   epub_publisherepub_copyrightepub_identifierr?   r   s    r   validate_config_valuesr      s   
{6!! :# 
0 	
 	
 	
 ""3:#677 QrNOOPPP:  
0 	
 	
 	
 :! UrRSSTTT:& 
LMM	
 	
 	
 :& 
LMM	
 	
 	
 :$ XrUVVWWW:$ 
0 	
 	
 	
 :% YrVWWXXX: QrNOOPPPPPQ Qr   rA   r   c                0   g }|j         D ]}t          |t                    r|                    |i f           /	 |\  }}|                    ||f           M# t          $ r+ t
                              t          d          |           Y w xY w||_         dS )z9Convert string styled epub_css_files to tuple styled one.zinvalid css_file: %r, ignoredN)epub_css_files
isinstancer   rm   	Exceptionr   r   r	   )r   rA   r   entryfilenameattrss         r   convert_epub_css_filesr     s    79N& 	 	eS!! 	!!5"+...."'%%%x&78888   r"ABBEJJJ +Fs   A2BBr   c           	     
   |                      t                     |                     dd dt          t          h                     |                     dddt          t
          h                     |                     dddt          t          h                     |                     d	i dt          t          h                     |                     d
d dt          t          h                     |                     dd dt          t          h                     |                     dd dt          t          h                     |                     dd dt          t          h                     |                     dd dt          t          h                     |                     dddt          t          h                     |                     dddt          t          h                     |                     dddt          t          h                     |                     dddt          t          t          h                     |                     dddt          t          t          h                     |                     dg dt          t          t          h                     |                     dg dt          t          t          h                     |                     dd dt          t          t          h                     |                     d g dt          t          t          h                     |                     d!d"dt          t          h                     |                     d#d$dt          t          h                     |                     d%d&dt          t          h                     |                     d'd(dt          t          h                     |                     d)d*dt          t          h                     |                     d+d,dt          t          h                     |                     d-d. dt          t          h                     |                     d/ddt          t          h                     |                     d0ddt          t          h                     |                     d1d2dt          d2d3                     |                     d4t          d56           |                     d7t                     d8d$d$d9S ):Nepub_basenamec                *    t          | j                  S N)r   projectr4   s    r   <lambda>zsetup.<locals>.<lambda>#  s    ]4<00 r   rd   )typesr@   g      @r'   
epub_themeepub_theme_optionsr   c                    | j         S r   )r   r4   s    r   r   zsetup.<locals>.<lambda>-  s    4< r   r   c                    | j         S r   authorr4   s    r   r   zsetup.<locals>.<lambda>0  s    DK r   r{   c                    | j         pdS )Nen)languager4   s    r   r   zsetup.<locals>.<lambda>4  s    T]*d r   r   c                    | j         S r   r   r4   s    r   r   zsetup.<locals>.<lambda>9  s    t{ r   r   c                    | j         S r   )	copyrightr4   s    r   r   zsetup.<locals>.<lambda><  s    t~ r   r   unknownepub_schemer   r   
epub_coverr   
epub_guideepub_pre_filesepub_post_filesr   c                    | j         S r   )html_css_files)rA   s    r   r   zsetup.<locals>.<lambda>G  s	    v, r   epub_exclude_filesrk      epub_tocdupTr   r   epub_fix_imagesFepub_max_image_widthr   epub_show_urlsinlineepub_use_indexc                    | j         S r   )html_use_indexr4   s    r   r   zsetup.<locals>.<lambda>V  s	    T( r   rL   rM   rB   r"   r#   zconfig-initedi   )priorityzbuilder-initedbuiltin)r?   parallel_read_safeparallel_write_safe)add_builderr&   add_config_value	frozensetr   floatdictlisttuplerG   boolr   connectr   r   r   s    r   setupr     s_   OOL!!! 00
	     V9eW+=+=     vvYu=M=MNNN-r6D6ARARSSS//y#?O?O     //y#?O?O     **	     22F)SEBRBR     55vYPSuEUEU     *IvYPSuEUEUVVV	6C5AQAQRRRYYu=M=MNNNr5	4-8P8PQQQr5	4-8P8PQQQ)2uItUm<T<TUUU*BYe}=U=UVVV,,u&&	     b%y$/G/G     !U)SE:J:JKKKe9dV;L;LMMM)U)SEBRBRSSS*E5	4&@Q@QRRR/EC5AQAQRRR)8V9cUCSCSTTT((	     +YiQTPUFVFVWWW+YiQTPUFVFVWWW\6lJ9W9W    
 KK!7#KFFFKK "8999 "#  r   )r   r   r*   r+   )r   r   rA   r   r*   r+   )r   r   r*   r   )3r   
__future__r   rJ   rC   os.pathrerE   typingr   r   sphinxr   sphinx.buildersr   sphinx.configr   sphinx.localer	   sphinx.utilr
   sphinx.util.fileutilr   sphinx.util.osutilr   collections.abcr   r   sphinx.applicationr   r   sphinx.util.typingr   	getLoggerr   r   r   rN   rP   r_   r   r   _xml_name_start_char_xml_name_charcompiler   EpubBuilderr&   r   r   r   r   r   r   <module>r      s   
 # " " " " "  				  				  , , , , , , , ,       & & & & & &                   0 0 0 0 0 0 , , , , , , 5######))))))$$$$$$444444		8	$	$    z      
   
 !  
  1 	/  PP  BJM#7MM>MMMNN G+ G+ G+ G+ G+:) G+ G+ G+T4Q 4Q 4Q 4Qn+ + + +"K K K K K Kr   