o $ar @sdZddlZddlZddlZddlmZddlmZmZmZm Z m Z m Z ddl m Z mZmZddlmZddlmZmZddlmZdd lmZdd lmZmZdd lmZdd lmZm Z dd l!m"Z"ddl#m$Z$ddl%m&Z&m'Z'zddl(m)Z)Wn e*ydZ)Ynwe+e,Z-dZ.dZ/dZ0dZ1dZ2dZ3dddZ4dddddddd d!d"d# Z5d$Z6e7d%Z8Gd&d'd'e Z9Gd(d)d)e Z:Gd*d+d+e Z;Gd,d-d-e Ze>Z?Gd4d5d5eZ@dS)7z sphinx.builders._epub_base ~~~~~~~~~~~~~~~~~~~~~~~~~~ Base class of epub2/epub3 builders. :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. N)path)AnyDictList NamedTupleSetTuple) ZIP_DEFLATED ZIP_STOREDZipFile)nodes)ElementNode) smartquotes)addnodes) BuildInfoStandaloneHTMLBuilder)__)loggingstatus_iterator)copy_asset_file) format_date)copyfile ensuredir)Imagezepub-cover.xhtmlz toctree-l%dz [%(uri)s]z#%d Footnotesz link-targetzTable of ContentsCover)toccoverzapplication/xhtml+xmlztext/css image/png image/gif image/svg+xml image/jpegzapplication/x-font-otfzapplication/x-font-ttfzapplication/font-woff) .xhtmlz.cssz.pngz.gif.svgz.jpgz.jpegz.otfz.ttfz.woff)r$z ([^#:]*#)(.*)c@&eZdZUeed<eed<eed<dS) ManifestItemhrefid media_typeN__name__ __module__ __qualname__str__annotations__r0r0d?Z9dYdAe!ddfdBdCZ:de"e!e'ffdDdEZ;dVdFdGZfdKdLZ?de&e"e!e'fde&e>fdMdNZ@dIe=dOe&e>de"e!e'ffdPdQZAdVdRdSZBdVdTdUZCZDS)[ EpubBuilderz Builder that outputs epub files. It creates the metainfo files container.opf, toc.ncx, mimetype, and META-INF/container.xml. Afterwards, all necessary files are zipped to an epub file. F)r!rr r"TrENcs@td|_d|_d|_d|_i|_|dd|_g|_ dS)Nr#r use_indexepub) superinit out_suffix link_suffixr=tocidid_cacheget_builder_configrMrefnodesself __class__r0r1rPs  zEpubBuilder.initcCst|j|jddgS)NhtmlrN)rconfigtagsrWr0r0r1create_build_infoszEpubBuilder.create_build_infocCs|jj|jjfSN)r\ epub_themeepub_theme_optionsrWr0r0r1get_theme_configszEpubBuilder.get_theme_confignamecCs.|j|}|sd|jd}||j|<|S)zReturn a unique id for name.zepub-%drN)rTgetenv new_serialno)rXrcr(r0r0r1make_ids  zEpubBuilder.make_iddoctreeresultc Cst|tjrS|drS|d}|ds#|ds#|ds#|dr%|S|jjd}tddd D]}|j||vrP| |t |t t | d |Sq1|St|tjrd|D]}|||}q[|S) z>Collect section titles, their depth in the toc and the refuri.r?zhttp://zhttps://zirc:zmailto:classesrlevelr?r>) isinstancer referencerd startswithparent attributesrangetoctree_templateappendr[escapesspastextr get_refnodes)rXrhrir?rjrnelemr0r0r1rzs0  zEpubBuilder.get_refnodesr cCsJt}|D]}|d|vrtjtd|ddddq||dqdS)Nr?zduplicated ToC entry found: %srNduplicated_toc_entryr8subtype)setloggerwarningradd)rXr appearednoder0r0r1check_refnodess zEpubBuilder.check_refnodescCsl|jj|jj|ddd}||g|_t|jj}|r.|d7}|jD] }||d|d<q#||jdS)zwGet the total table of contents, containing the root_doc and pre and post files not managed by sphinx. FTprune_toctrees includehidden/r?N) reget_and_resolve_doctreer\root_docrzrVrdirname toc_add_files)rXrh master_diritemr0r0r1get_tocs  zEpubBuilder.get_tocrVc Cs|ddt|jj|jtt|jj|jj dt |jj D]\}}|ddt|tt|dq%|jj D]\}}| dt|tt|dq@dS)zDAdd the root_doc, pre and post files to a list of refnodes. rrmN)insertr[rwr\rrQrxretitlesryreversedepub_pre_filesepub_post_filesrv)rXrVfiler>r0r0r1rs(    zEpubBuilder.toc_add_filesprefixfragmentcCs||ddS)z;Return a href/id attribute with colons replaced by hyphens.:-)rG)rXrrr0r0r1 fix_fragmentszEpubBuilder.fix_fragmenttreecsdtddffdd }|tjD],}d|vr/j|d}|r/|d|d|d<d |vr=d |d |d <q|tjD]}|||j d d }t |tjrZ||qD|t j D]}||qadS) zReplace colons with hyphens in href and id attributes. Some readers crash because they interpret the part as a transport protocol specification. rrENcs<g}|dD]}d|}||vr||q||d<dS)zUpdate IDs of given *node*.idsrLN)rrv)rnew_idsnode_idnew_idrWr0r1update_node_ids    z+EpubBuilder.fix_ids..update_node_idr?rrefidrLT)ascend) r traverser rp refuri_rematchrgrouptarget next_noderordesc_signature)rXrrrpmrrrr0rWr1fix_ids s$    zEpubBuilder.fix_idsinline show_urlscCsdtjdtdtjfdd}dtjdtdtdtjfdd}d tjdtttffd d }|d kr0d S|dkrFt| tjd}||\}}d} t| tj D]} | dd} | dse| dse| dr| | vr| j| d} |dkr|jd| i} t| | } | d|j| j| | qN|dkrt| }| d7} |||}| j| ||||| }||||dd|d<||dd|d7}qNd S)z+Add visible link targets for external linksdoclabelrEcSs(td}|t||||S)z.Create a footnote_reference node with childrenz[#]_)r footnote_referencervTextnote_autofootnote_ref)rr footnote_refr0r0r1make_footnote_ref-s  z8EpubBuilder.add_visible_links..make_footnote_refr:cSsNt|}t}|t||||dtd||||S)z$Create a footnote node with childrenrrL)r footnote paragraphrvrrrnote_autofootnote)rrr:rparar0r0r1 make_footnote4s   z4EpubBuilder.add_visible_links..make_footnotercSst|tj}|r|d}|j|j|dfS|tjD]}t|dkr:|t kr:|j|j|dfSqt|tj d}t}| t t | ||||dfS)zfFind or create a spot to place footnotes. The function returns the tuple (parent, index).rlrr) listrr rrrindexrubriclenryFOOTNOTES_RUBRIC_NAMEdocumentrvr)rfnsfnrrrubr0r0r1 footnote_spot>s z4EpubBuilder.add_visible_links..footnote_spotnoNrrrr?rLzhttp:zhttps:zftp:rrjrr)r rr.rrrr rArrrprdrqryrrrlink_target_templaterrvcss_link_target_classrFOOTNOTE_LABEL_TEMPLATE add_backref)rXrrrrrrfn_spotfn_idxnrrr:idxlinkrrrr0r0r1add_visible_links*sB        zEpubBuilder.add_visible_linksdocnamecs,|||||jjt||dS)zWrite one document file. This method is overwritten in order to fix fragment identifiers and to add visible external links. N)rrr\epub_show_urlsrO write_doc)rXrrhrYr0r1rns zEpubBuilder.write_docc Cs|D][\}}|D]T\}\}}}t|D]\}\} } |j| } | r1| || d| df||<q|D]'\} } t| D]\}\} } |j| } | rZ| || d| df| |<qr?)r=rSr;)rXrrnrLr0r0r1 new_navpointcs zEpubBuilder.new_navpointcCs|g}|tddddgd}d}|D]}|dsq|ddd}||jvr)q|d|jjkr2q|d|krP|||}||d j|||nf|d|d kr|d 7}|ro|jj ro|d j|||d |||}|d j|||n3|d|kr|dt |kr||dt |ks|d}|||}|d j|||n|}q|djS) zCreate the toc navigation structure. Subelements of a node are nested inside the navpoint. For nested nodes the parent node is reinserted in the subnav. dummyrrLNr>r?r-rnrlrF) rvr;r?r5r\ epub_tocdepthrMpopr@ epub_tocdupr)rXr navstackrnlastnoderrr<r0r0r1build_navpointslsF           zEpubBuilder.build_navpoints navpointscCs6i}|jj|d<t|jj|d<||d<||d<|S)z]Create a dictionary with all metadata for the toc.ncx file properly escaped. rr9rnrU)r\r!r[rwr)rXrnrUr'r0r0r1 toc_metadatas  zEpubBuilder.toc_metadatacCsttd|jjdkr%|jj|jj|ddd}||g}| |n|j }| || |}t dd|j D}t||jj}tt|jd|j|||dS) z Write the metainfo file toc.ncx.zwriting toc.ncx file...defaultFrcss|]}|dVqdS)rnNr0).0rr0r0r1 sz(EpubBuilder.build_toc..z toc.ncx_tN)rrrr\ epub_tocscopererrrzrrVrrTmaxminrOrrrrrrV)rXrhrVrUrnr0r0r1 build_tocs        zEpubBuilder.build_toccCs|jjd}ttd|t|j|}t|dt 8}| t|jddt dD]}| t|j||t q*|j D]}| t|j||t qsh