ó
öÀ„\c           @   sÁ  d  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 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 e r#d d l m Z m Z m Z m Z m Z d d l  m! Z! d d l" m# Z# d d l m$ Z$ n  e j% e& ƒ Z' e d d d d g ƒ Z( i d d 6d d 6Z) i d d 6d d 6Z* i d d 6d d 6Z+ d Z, d Z- d e
 j. f d „  ƒ  YZ/ d „  Z0 d  „  Z1 d! S("   sä   
    sphinx.builders.epub3
    ~~~~~~~~~~~~~~~~~~~~~

    Build epub3 files.
    Originally derived from epub.py.

    :copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iÿÿÿÿ(   t
   namedtuple(   t   path(   t   string_types(   t   package_dir(   t
   _epub_base(   t   string_classest   ENUM(   t   __(   t   loggingt   xmlname_checker(   t   copy_asset_file(   t   format_date(   t   make_filename(   t   Anyt   Dictt   Iterablet   Listt   Tuple(   t   nodes(   t   Sphinx(   t   Configt   NavPointt   textt   refurit   childrent   ltrt
   horizontalt   rtlt   verticals   vertical-rls   horizontal-tbs   <!DOCTYPE html>uU   <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">t   Epub3Builderc           B   sŒ   e  Z d  Z d Z e d ƒ Z e Z e j	 e
 d d ƒ Z e Z e Z e Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   sÙ   
    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.
    t   epubs   The ePub file is in %(outdir)s.t	   templatest   epub3c         C   s”   |  j  ƒ  |  j ƒ  |  j |  j d ƒ |  j |  j d ƒ |  j |  j d ƒ |  j |  j d ƒ |  j |  j d ƒ |  j |  j |  j	 j
 d ƒ d S(   s/   Create the metainfo files and finally the epub.t   mimetypes   META-INF/container.xmls   content.opfs	   nav.xhtmls   toc.ncxs   .epubN(   t   validate_config_valuet   get_toct   build_mimetypet   outdirt   build_containert   build_contentt   build_navigation_doct	   build_toct
   build_epubt   configt   epub_basename(   t   self(    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyt   handle_finishO   s    

c         C   s‚  |  j  j j s% t j t d ƒ ƒ n  t ƒ  j |  j  j j ƒ sV t j t d ƒ ƒ n  |  j  j j	 s{ t j t d ƒ ƒ n  |  j  j j
 s  t j t d ƒ ƒ n  |  j  j j sÅ t j t d ƒ ƒ n  |  j  j j sê t j t d ƒ ƒ n  |  j  j j st j t d ƒ ƒ n  |  j  j j s4t j t d ƒ ƒ n  |  j  j j sYt j t d	 ƒ ƒ n  |  j  j j s~t j t d
 ƒ ƒ n  d  S(   NsH   conf value "epub_language" (or "language") should not be empty for EPUB3s2   conf value "epub_uid" should be XML NAME for EPUB3sG   conf value "epub_title" (or "html_title") should not be empty for EPUB3s6   conf value "epub_author" should not be empty for EPUB3s;   conf value "epub_contributor" should not be empty for EPUB3s;   conf value "epub_description" should not be empty for EPUB3s9   conf value "epub_publisher" should not be empty for EPUB3sI   conf value "epub_copyright" (or "copyright")should not be empty for EPUB3s:   conf value "epub_identifier" should not be empty for EPUB3s2   conf value "version" should not be empty for EPUB3(   t   appR+   t   epub_languaget   loggert   warningR   R	   t   matcht   epub_uidt
   epub_titlet   epub_authort   epub_contributort   epub_descriptiont   epub_publishert   epub_copyrightt   epub_identifiert   version(   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR"   [   s(    c         C   s¿   |  j  j } t t |  ƒ j ƒ  } |  j |  j  j ƒ | d <|  j |  j  j ƒ | d <t j	 | ƒ | d <t
 j	 | ƒ | d <|  j t d ƒ ƒ | d <|  j |  j  j ƒ | d <|  j  j | d <| S(	   sa   Create a dictionary with all metadata for the content.opf
        file properly escaped.
        t   descriptiont   contributort   page_progression_directiont   ibook_scroll_axiss   %Y-%m-%dT%H:%M:%SZt   dateR<   t   epub_version(   R+   t   epub_writing_modet   superR   t   content_metadatat   escR8   R7   t   PAGE_PROGRESSION_DIRECTIONSt   gett   IBOOK_SCROLL_AXISR   R<   RB   (   R-   t   writing_modet   metadata(    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyRE      s    c         C   si   t  t |  ƒ j | ƒ |  j j } t j | ƒ |  j d <|  j |  j d <|  j	 |  j d <t
 |  j d <d  S(   Nt   theme_writing_modet   html_tagt   use_meta_charsett   skip_ua_compatible(   RD   R   t   prepare_writingR+   RC   t   THEME_WRITING_MODESRH   t   globalcontextRM   RN   t   True(   R-   t   docnamesRJ   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyRP      s    c         C   s¤  g  } | j  t d d g  ƒ ƒ d } xq| D]i} | d sB q, n  | d j d ƒ d } | |  j k rn q, n  | d |  j j k rŠ q, n  t | d | d g  ƒ } | d | k râ | j ƒ  | d j j  | ƒ | j  | ƒ q, | d | d k r$| d 7} | d j j  | ƒ | j  | ƒ q, | d | k  r‰x$ | d t | ƒ k  rZ| j ƒ  q7W| d } | d j j  | ƒ | j  | ƒ q, t	 d	 ƒ ‚ q, W| d j S(
   sc  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.
        t    i    R   R   t   #t   leveliÿÿÿÿi   s+   Should never reach here. It might be a bug.(
   t   appendR   t   splitt   ignored_filesR+   t   epub_tocdeptht   popR   t   lent   RuntimeError(   R-   t   navnodest   navstackRW   t   nodet   filet   navpoint(    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyt   build_navlistš   s8    



c         C   sG   i  } |  j  |  j j ƒ | d <|  j  |  j d ƒ | d <| | d <| S(   s_   Create a dictionary with all metadata for the nav.xhtml file
        properly escaped.
        t   langt   toct
   toc_localet   navlist(   RF   R+   R0   t   guide_titles(   R-   Rh   RK   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyt   navigation_doc_metadataÅ   s
    
c         C   sã   t  j t d ƒ | ƒ |  j j d k rq |  j j |  j j |  d t d t ƒ} |  j	 | g  ƒ } |  j
 | ƒ n	 |  j } |  j | ƒ } t t j |  j d ƒ t j | | ƒ |  j | ƒ ƒ | |  j k rß |  j j | ƒ n  d S(   s"   Write the metainfo file nav.xhtml.s   writing %s file...t   defaultt   prune_toctreest   includehiddens   nav.xhtml_tN(   R1   t   infoR   R+   t   epub_tocscopet   envt   get_and_resolve_doctreet
   master_doct   Falset   get_refnodest   toc_add_filest   refnodesRd   R
   R   t   joint   template_dirRj   t   filesRX   (   R-   R%   t   outnamet   doctreeRv   Rh   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR(   Ð   s    		(   t   __name__t
   __module__t   __doc__t   nameR   t   epilogRs   t   supported_remote_imagesR   Rw   R   Rx   t   DOCTYPEt   doctypet   HTML_TAGRM   RS   RN   R.   R"   RE   RP   Rd   Rj   R(   (    (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR   =   s   		$		
	+	c         C   sœ   g  } x† | j  D]{ } t | t ƒ r; | j | i  f ƒ q y# | \ } } | j | | f ƒ Wq t k
 rŠ t j t d ƒ | ƒ q q Xq W| | _  d S(   s?   This converts string styled epub_css_files to tuple styled one.s   invalid css_file: %r, ignoredN(   t   epub_css_filest
   isinstanceR   RX   t	   ExceptionR1   R2   R   (   R/   R+   R…   t   entryt   filenamet   attrs(    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyt   convert_epub_css_filesè   s    c         C   st  |  j  t ƒ |  j d d „  d  ƒ |  j d d d ƒ |  j d d d ƒ |  j d i  d ƒ |  j d d	 „  d ƒ |  j d
 d „  d ƒ |  j d d „  d ƒ |  j d d „  d ƒ |  j d d „  d ƒ |  j d d d ƒ |  j d d d ƒ |  j d d d ƒ |  j d d4 d ƒ |  j d d5 d ƒ |  j d g  d ƒ |  j d g  d ƒ |  j d d „  d ƒ |  j d g  d ƒ |  j d d d ƒ |  j d  t d ƒ |  j d! d" d ƒ |  j d# t d ƒ |  j d$ d% d ƒ |  j d& d' d ƒ |  j d( d) „  d ƒ |  j d* d d t ƒ |  j d+ d d t ƒ |  j d, d- d t d- d. ƒ ƒ |  j d/ t	 ƒ i d0 d1 6t d2 6t d3 6S(6   NR,   c         S   s   t  |  j ƒ S(   N(   R   t   project(   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyt   <lambda>ÿ   s    RB   g      @R   t
   epub_themet   epub_theme_optionsR5   c         S   s   |  j  S(   N(   t
   html_title(   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR     s    R6   c         S   s   |  j  S(   N(   t   author(   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR     s    R0   c         S   s   |  j  p d S(   Nt   en(   t   language(   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR     s    R9   c         S   s   |  j  S(   N(   R‘   (   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR     s    R:   c         S   s   |  j  S(   N(   t	   copyright(   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR     s    R;   t   unknownt   epub_schemeR4   Rp   t
   epub_covert
   epub_guidet   epub_pre_filest   epub_post_filesR…   c         S   s   |  j  S(   N(   t   html_css_files(   R+   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR     s    t   epub_exclude_filesR[   i   t   epub_tocdupRo   Rk   t   epub_fix_imagest   epub_max_image_widthi    t   epub_show_urlst   inlinet   epub_use_indexc         S   s   |  j  S(   N(   t   html_use_index(   R-   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyR     s    R8   R7   RC   R   R   s   config-initedt   builtinR<   t   parallel_read_safet   parallel_write_safe(    (    (
   t   add_builderR   t   add_config_valuet   NoneRS   Rs   R   R   t   connectR‹   (   R/   (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyt   setupú   sF    N(2   R~   t   collectionsR    t   osR   t   sixR   t   sphinxR   t   sphinx.buildersR   t   sphinx.configR   R   t   sphinx.localeR   t   sphinx.utilR   R	   t   sphinx.util.fileutilR
   t   sphinx.util.i18nR   t   sphinx.util.osutilR   Rs   t   typingR   R   R   R   R   t   docutilsR   t   sphinx.applicationR   R   t	   getLoggerR|   R1   R   RG   RI   RQ   R‚   R„   t   EpubBuilderR   R‹   R«   (    (    (    s4   lib/python2.7/site-packages/sphinx/builders/epub3.pyt   <module>   s@   (


«	