gdSc@s dZddlZddlZddlZddlZddlZddlZddlmZyddlm Z Wn;e k ryddl Z Wqe k rdZ qXnXddl m Z ddlmZddlmZddlmZmZmZdd lmZdd lmZd Zd Zd ZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+idd6dd6Z,i d d!6d"d#6d$d%6d&d'6d(d)6d*d+6d*d,6d-d.6d/d06Z-d4Z.ej/d1Z0d2efd3YZ1dS(5s sphinx.builders.epub ~~~~~~~~~~~~~~~~~~~~ Build epub files. Originally derived from qthelp.py. :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. iN(tpath(tImage(tnodes(taddnodes(tStandaloneHTMLBuilder(t ensuredirtcopyfiletEEXIST(tsphinx_smarty_pants(tbrownsapplication/epub+zipu u %(title)s %(navpoints)s u%(indent)s %(indent)s %(indent)s %(text)s %(indent)s %(indent)s %(indent)s s s navPoint%du  %(lang)s %(title)s %(author)s %(publisher)s %(copyright)s %(id)s %(date)s %(files)s %(spine)s %(guide)s u- uepub-cover.htmluX u! uB u toctree-l%du [%(uri)s]u#%du Footnotesu link-targetuTable of ContentsttocuCovertcoversapplication/xhtml+xmls.htmlstext/csss.csss image/pngs.pngs image/gifs.gifs image/svg+xmls.svgs image/jpegs.jpgs.jpegsapplication/x-font-otfs.otfsapplication/x-font-ttfs.ttfs ([^#:]*#)(.*)t EpubBuildercBs=eZdZdZeZddddgZeZeZ dZ dZ idZ d Z d Zd Zd Zd ZdZddZdZdZdZdZdZdd#d#dZdZdZdZdZdZedZ dZ!dZ"d Z#d!Z$d"Z%RS($s 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. tepubs image/svg+xmls image/pngs image/gifs image/jpegcCs,tj|d|_d|_d|_dS(Ns.htmli(Rtinitt out_suffixt playorderttocid(tself((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRs   cCs|jj|jjfS(N(tconfigt epub_themetepub_theme_options(R((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytget_theme_configscCs<|j|}|s8d|jjd}|||/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytmake_ids  cCs^|jdd}|jdd}|jdd}|jdd}|jd d }|S( s?Replace all characters not allowed in text an attribute values.t&s&ts>t"s"s's'(treplace(RR((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytescs cCst|tjr|jdr|d}|jdsg|jdsg|jdsg|jdrk|S|jjd}xtddd D]Z}t||kr|j i|d 6|j |d6t |j |j d 6PqqWn&x#|j D]}|j||}qW|S( s>Collect section titles, their depth in the toc and the refuri.trefurishttp://shttps://sirc:smailto:tclassesiiitlevelttext(t isinstanceRt referencethas_keyt startswithtparentt attributestranget_toctree_templatetappendR#tssptastexttchildrent get_refnodes(RtdoctreetresultR$R%R&telem((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyR4s !    cCs|jj|jj|dtdt}|j|g|_tj |jj}|r|d7}x&|jD]}||d|d/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytget_tocs cCs|jdidd6|j|jjdd6t|j|jj|jjjd6x]t|jj D]I\}}|jdidd6|j|d6t|j|d6qpWxT|jj D]F\}}|j idd6|j|d6t|j|d6qWdS(sFAdd the master_doc, pre and post files to a list of refnodes. iiR&s.htmlR$R'N( tinsertR#RR<R1RttitlesR2treversedtepub_pre_filestepub_post_filesR0(RR?tfileR'((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRAs  '  cCs||jddS(s;Return a href/id attribute with colons replaced by hyphens.t:t-(R"(Rtprefixtfragment((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt fix_fragmentscCsx|jtjD]}d|krltj|d}|rl|j|jd|jd|d/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytfix_idss . !  tinlinecCsd}d}d}|dkr+dS|dkrh|jtjd}||\}}d} nxr|jtjD]^} | jd d } | jd s| jd s| jd r{| | jkr{| jj| d} |dkr>t i| d6} tj | | } | dj t | jj | | q|dkrt| }| d7} |||}| jj | ||||| }|j |||dd|d<|j|dd|d7}qq{q{WdS(s+Add visible link targets for external linkscSs6tjd}|jtj||j||S(s.Create a footnote_reference node with childrens[#]_(Rtfootnote_referenceR0tTexttnote_autofootnote_ref(tdoctlabelt footnote_ref((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytmake_footnote_ref0s cSsktj|}tj}|jtj||j||jdtjd||j||S(s$Create a footnote node with childreniRQ(Rtfootnotet paragraphR0R_RERbtnote_autofootnote(RaRbturiRetpara((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt make_footnote7s   cSs|jtj}|r?|d}|j|jj|dfSxe|jtjD]Q}t|jdkrR|jdjt krR|j|jj|dfSqRW|jtj d}tj}|j tj t |j |||j|dfS(sfFind or create a spot to place footnotes. The function returns the tuple (parent, index).iii( RSRReR,tindextrubrictlenR3R2t_footnotes_rubric_nametdocumentR0R_(RXtfnstfnRYRatrub((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt footnote_spotAs !  tnoNReiiR$RQshttp:shttps:sftp:R]RhR%RRRP(RSRRoR)RR+R2R,Rkt_link_target_templateR]R0t_css_link_target_classREt_footnote_label_templatet add_backref(RRXt show_urlsRdRjRsRatfn_spottfn_idxtnrRYRhtidxtlinkRbRcRe((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytadd_visible_links-s:     !    cCs6|j||j||jjtj|||S(sWrite one document file. This method is overwritten in order to fix fragment identifiers and to add visible external links. (R\RRtepub_show_urlsRt write_doc(RtdocnameR5((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRrs c Cs!x|D]\}}x|D]\}\}}xft|D]X\}\}} tj| } | r9||j| jd| jdf||/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt fix_genindex|s2cCstj|d}|tkS(s=Does the filename extension indicate a vector graphic format?i(Rtsplitextt_vector_graphics_extensions(Rtfilenametext((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytis_vector_graphicssc Cs5ttj|jdx|j|jdtt|jD]}|j|}y"tj tj|j |}Wnt k r2|j |s|j dtj|j |fny2ttj|j |tj|jd|Wq>t tfk r+}|j dtj|j ||fq>Xq>nX|jjr`|jd kr`|j}q`n|jjdkr|j\}}|jj}||kr|||}|j||ftj}qny#|jtj|jd|Wq>t tfk r,}|j dtj|j ||fq>Xq>WdS( sCopy images using the PIL. The method tries to read and write the files with the PIL, converting the format and resizing the image if necessary/possible. t_imagesscopying images... s-cannot read image file %r: copying it insteadscannot copy image file %r: %stPiscannot write image file %r: %sN(R(RRtjointoutdirtstatus_iteratortimagesR RmRtopentsrcdirtIOErrorRtwarnRtOSErrorRtepub_fix_imagestmodetconverttepub_max_image_widthtsizetresizetBICUBICtsave( Rtsrctdesttimgterrtwidththeighttnwtnh((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytcopy_image_files_pils> "   !   !# cCsq|jrm|jjs!|jjrWtsJ|jdtt|jqj|j qmtt|jndS(s{Copy image files to destination directory. This overwritten method can use the PIL to convert image files. s#PIL not found - copying image filesN( RRRRRRtsuperR tcopy_image_filesR(R((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRs   s page.htmlcCsC|jdr#|j|dntj||||||dS(sCreate a rendered page. This method is overwritten for genindex pages in order to fix href link attributes. tgenindextgenindexentriesN(R+RRt handle_page(Rtpagenametaddctxt templatenamet outfilenamet event_arg((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRscCsw|j|j|jd|j|jd|j|jd|j|jd|j|j|jjddS(s/Create the metainfo files and finally the epub.tmimetypesMETA-INF/container.xmls content.opfstoc.ncxs.epubN( RDtbuild_mimetypeRtbuild_containert build_contentt build_toct build_epubRt epub_basename(R((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt handle_finishs  cCsU|jd|tjtj||dd}z|jtWd|jXdS(s!Write the metainfo file mimetype.swriting %s file...twsutf-8N(tinfotcodecsRRRtwritet_mimetype_templatetclose(RRtoutnametf((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRs !cCs|jd|tj||}ytjtj|Wn(tk rg}|jtkrhqhnXt j tj||dd}z|j t Wd|j XdS(s0Write the metainfo file META-INF/cointainer.xml.swriting %s file...Rsutf-8N(RRRtostmkdirR@RterrnoRRRRt_container_templateR(RRRRqRR((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRs !cCs i}|j|jj|d<|j|jj|d<|j|jj|d<|j|jj|d<|j|jj|d<|j|jj|d<|j|jj|d<|j|jj |d<|jt j d |d <||d <||d <||d <|S(saCreate a dictionary with all metadata for the content.opf file properly escaped. ttitletauthortuidtlangt publishert copyrighttschemeRs%Y-%m-%dtdatetfilestspinetguide( R#Rt epub_titlet epub_authortepub_uidt epub_languagetepub_publishertepub_copyrightt epub_schemetepub_identifierttimetstrftime(RRRRtmetadata((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytcontent_metadatas   cCs*|jd||jtjs3|tj7}nt|}g}g|_ddddd|jjdg|jj|_ xtj |D]\}}}x|D]}t j |||} | |j krqnt j | d} | tkr| d#kr|jd | qqn| jtjd } |jti|j| d 6|j|j| d6|jt| d6|jj| qWqWg} xn|jD]c} d| dkrqn| d|j krqn| jti|j|j| dd6qWxF|jD];} | jti|j|j| d|jd6qW|jddr| jti|j|jd|jd6nt}d"}|jjr3|jj\}}|jtjd }|jd}|jdd|d}|| ti|j|j|d6||}|r3| jdti|j|jt d6t |jkrt j t d} |jjt |jti|jt d 6|j|jt d6|jt| d6ni|j|d6|jj!d6}|j"t j t d||q3ng}t#}t#}|jj$rx|jj$D]\}}}|j%dd}||jkr|jj|n|dkrt&}n|dkrt&}n|jt'i|j|d6|j|d6|j|d6q^Wn|rV|rV|jt'idd6t(dd6|jt d6n|r|jr|jt'idd6t(dd6|j|jddd6ndj |}dj | } dj |}t)j*t j ||d d!}z$|j+||j,|| |Wd"|j-Xd"S($sWrite the metainfo file content.opf It contains bibliographic data, a file list and the spine (the reading order). swriting %s file...s .buildinfoRs content.opfstoc.ncxsMETA-INF/container.xmls.epubis.jss.xmls!unknown mimetype for %s, ignoringR:threfRt media_typet#R$tidrefit use_indexR Rs s iR timageRR ttypeRhRsutf-8N(s.jss.xml(.RtendswithRtsepRmRRRtepub_exclude_filest ignored_filestwalkRRRt _media_typesRR"R0t_file_templateR#RR?t_spine_templatetdomain_indicesRtget_builder_configt_content_templatetNonet epub_covertrfindt_cover_templateREt_coverpage_nametprojectRR>t epub_guidetsplitR=t_guide_templatet _guide_titlesRRRRR(RRRtolent projectfilestroottdirsRRqRRRRCRt content_tmplt html_tmplRtmpostcpostctxRtauto_add_covert auto_add_tocRRhRRJR((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyR s        & - ( (  #            #! cCsd|r|jd7_n|jd7_t||d<|jt|j|d<|j|d/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt new_navpoints cCs/|jdd}|jd|dj|S(skInsert nested navpoints for given node. The node and subnav are already rendered to text. s ii(trsplitRER(RRYtsubnavtnlist((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt insert_subnavsc Csg}g}d}d }x|D]}}|ds5qn|djdd}||jkraqn|d|jjkr}qn|d|kr|j|j||n|d|dkr'|j|g}|d7}|r |jjr |j|j||tn|j|j||noxS|d|kr|dj |}|j }|j |d||d<|d8}q*W|j|j|||}qWxO|dkrdj |}|j }|j |d||d<|d8}qWdj |S( sCreate the toc navigation structure. Subelements of a node are nested inside the navpoint. For nested nodes the parent node is reinserted in the subnav. iR'R$RiR&s iN( RRRRt epub_tocdepthR0Rt epub_tocdupR=RtpopR( RRtnavstacktnavlistR&tlastnodeRYRJR((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pytbuild_navpointssD       cCs>i}|jj|d<|jj|d<||d<||d<|S(s]Create a dictionary with all metadata for the toc.ncx file properly escaped. RRR&t navpoints(RRR(RR&RR((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt toc_metadatas   cCs|jd||jjdkrl|jj|jj|dtdt}|j|g}|j|n |j }|j |}t d|j D}t ||jj }tjtj||dd}z!|jt|j||Wd|jXdS( s Write the metainfo file toc.ncx.swriting %s file...tdefaultR8R9css|]}|dVqdS(R&N((t.0RC((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pys sRsutf-8N(RRt epub_tocscopeRR;R<R=R4RAR?RtmaxtminRRRRRRt _toc_templateRR(RRRR5R?RR&R((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRs !!cCs|jd|dddg|j}tjtj||dtj}|jtj|ddtjxi|D]a}tj||}t j d krt |t r|j d }qn|j||tjqtW|jd S( s~Write the epub file. It is a zip file with the mimetype file stored uncompressed as the first entry. swriting %s file...sMETA-INF/container.xmls content.opfstoc.ncxRRiisutf-8N(ii(RRtzipfiletZipFileRRt ZIP_DEFLATEDRt ZIP_STOREDtsyst version_infoR(tunicodetencodeR(RRRRR RJtfp((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyRs     N(&t__name__t __module__t__doc__RR=t copysourcetsupported_image_typestadd_permalinksR>tembeddedRRRR#R4RDRAROR\RRRRRRRRRRRRRRRRRRR(((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyR sF          E   &    z + (s.svg(2R0RtreR)RRR%RtPILRt ImportErrorRtdocutilsRtsphinxRtsphinx.builders.htmlRtsphinx.util.osutilRRRtsphinx.util.smartypantsRR1tsphinx.util.consoleR RRR$RR RRRRRRRR/RuRwRnRvRRRtcompileRTR (((s>/usr/local/lib/python2.7/site-packages/sphinx/builders/epub.pyt sj