ó
\¬L\c           @` s   d  d l  m Z m Z m Z m Z d d l m Z m Z e rG e Z	 n  d  d l
 Z
 d d l m Z d d
 d „  ƒ  YZ d d d	 „  ƒ  YZ d S(   i    (   t   unicode_literalst   divisiont   absolute_importt   print_functioni   (   t   PY2t   utf8_strN(   t
   fromBase32t   HTMLProcessorc           B` s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C` sF   | |  _  | |  _ | |  _ i  |  _ x | D] } d |  j | <q+ Wd  S(   Nu   used(   t   filest   metadatat   rscnamest   used(   t   selfR   R	   R
   t   name(    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyt   __init__   s    				c         C` s¶  t  d ƒ t j d t j ƒ } g  | j | ƒ D] } t | j d ƒ ƒ ^ q/ } | rœ g  | D]  } | d d k r] | d ^ q] } t t | | ƒ ƒ } n  xM | D]E }	 |	 | k rÔ | |	 t	 d |	 ƒ | |	 <q£ t	 d |	 ƒ | |	 <q£ Wt  d ƒ d }
 t
 | ƒ } g  } xc t | j ƒ  ƒ D]O } | d k s!| | k rEq!n  | j | |
 | !ƒ | j | | ƒ | }
 q!W| j | |
 ƒ d j | ƒ } d  } d  } | |  _ | |  _ | S(	   Nu   Find link anchorss$   <[^<>]+filepos=['"]{0,1}(\d+)[^<>]*>i   u   posi    u   <a id="filepos%d" />u   Insert data into htmlt    (   t   printt   ret   compilet
   IGNORECASEt   finditert   intt   groupt   listt   setR   t   lent   sortedt   keyst   appendt   joint   Nonet   srctextt	   indx_data(   R   t   rawtextR    t   positionMapt   link_patternt   mt	   pos_linkst   et   pos_indxt   positiont   post   lastPost   dataListt   endR   (    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyt   findAnchors   s6    
1-

		c         C` sé  |  j  } |  j } |  j } t d ƒ t j d t j ƒ } | j d | ƒ } t d ƒ t j d d | ƒ } t j d d | ƒ } t d ƒ t j d	 t j ƒ } t j d
 t j ƒ } | j | ƒ } d  } |  _  x· t
 d t | ƒ d ƒ D] } | | }	 x€ | j |	 ƒ D]o }
 t |
 j d ƒ ƒ } | | d } | d  k rOt d | ƒ q	d t | ƒ d } | j | |	 d ƒ }	 q	W|	 | | <qé Wd j | ƒ } d | k rÜ| d d !d t | j d ƒ d ƒ d | d } n  | |  j f S(   Nu   Insert hrefs into htmls4   <a([^>]*?)filepos=['"]{0,1}0*(\d+)['"]{0,1}([^>]*?)>s   <a\1href="#filepos\2"\3>u   Remove empty anchors from htmls   <a\s*/>R   s   <a\s*>\s*</a>u!   Insert image references into htmls
   (<img.*?>)s#   recindex=['"]{0,1}([0-9]+)['"]{0,1}i   i   u>   Error: Referenced image %s was not recognized as a valid images   src="Images/t   "u   Codeci    i   s<   <meta http-equiv="content-type" content="text/html; charset=s   " />(   R   R
   R	   R   R   R   R   t   subt   splitR   t   rangeR   R   R   R   R   R   t   getR   (   R   R   R
   R	   R#   t   image_patternt   image_index_patternt	   srcpiecest   it   tagR$   t   imageNumbert	   imageNamet   replacement(    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyt   insertHREFSA   s8    			



7(   t   __name__t
   __module__R   R-   R;   (    (    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyR      s   			$t   XHTMLK8Processorc           B` s   e  Z d  „  Z d „  Z RS(   c         C` s   | |  _  | |  _ i  |  _ d  S(   N(   R
   t   k8procR   (   R   R
   R?   (    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyR   p   s    		c   5      C` s=  t  j d t  j ƒ } t  j d ƒ } g  } t d ƒ xnt |  j j ƒ  ƒ D]W} |  j j | ƒ } |  j j | ƒ \ } } } }	 }
 } | j	 | ƒ } xí t d t
 | ƒ d ƒ D]Ó } | | } | j d ƒ r® x¤ | j | ƒ D]“ } | j d ƒ } | j d ƒ } |  j j | | ƒ \ } } | d k r?d t | ƒ d } n d t | ƒ d	 | d } | j | | d ƒ } qÝ W| | | <q® q® Wd j | ƒ } | j | ƒ qJ Wt  j d
 t  j ƒ } t  j d ƒ } xt t
 | ƒ ƒ D] } | | } | j	 | ƒ } xÅ t t
 | ƒ ƒ D]± } | | } | j d ƒ rx‚ | j | ƒ D]q } y | j d ƒ } Wn t k
 rod  } n Xd } | |  j j k r™d | d } n  | j | | d ƒ } q=W| | | <qqWd j | ƒ } | | | <qÜWt  j d t  j ƒ } t  j d ƒ } xž t t
 | ƒ ƒ D]Š } | | } | j	 | ƒ } xO t t
 | ƒ ƒ D]; } | | } | j d ƒ rI| j d „  | ƒ | | <qIqIWd j | ƒ } | | | <qWg  } | j d  ƒ g  } | j d  d  d  d  g ƒ t  j d t  j ƒ } t  j d t  j ƒ } t  j d ƒ } t  j d t  j ƒ } t  j d t  j ƒ } t  j d t  j ƒ }  t  j d t  j ƒ }! t  j d t  j ƒ }" t  j d t  j ƒ }# xTt d |  j j ƒ  ƒ D]:} |  j j | ƒ \ }$ }% } } |  j j | ƒ }& | j	 |& ƒ } xß t d t
 | ƒ d ƒ D]Å } | | } | j d ƒ rx– | j | ƒ D]… } t | j d ƒ ƒ }' |  j |' d }( |( d  k	 r¥d t |( ƒ d } d |  j |( <| j | | d ƒ } q4t d |' | f ƒ q4W| | | <qqWd j | ƒ }& | j	 |& ƒ } xÖt d t
 | ƒ d ƒ D]¼} | | } x½ |  j | ƒ D]¬ } t | j d ƒ ƒ }' |  j |' d }( | j ƒ  d d !}) | j ƒ  d }* |( d  k	 r½|) d t |( ƒ |* } d |  j |( <|  j | | d ƒ } q%t d |' | f ƒ q%Wx½ |! j | ƒ D]¬ } t | j d ƒ ƒ }+ |  j |+ d }, | j ƒ  d d !}) | j ƒ  d }* |, d  k rWt d  |+ | f ƒ qå|) d! t |, ƒ |* } |! j | | d ƒ } d |  j |, <qåWxŽ |" j | ƒ D]} } t | j d ƒ ƒ }- |  j j |- ƒ \ }. }/ }0 }1 d" t |0 ƒ d# t |1 ƒ d } |" j | | d ƒ } d |  j |1 <q¥WxŽ |# j | ƒ D]} } t | j d ƒ ƒ }- |  j j |- ƒ \ }. }/ }0 }1 d" t |0 ƒ d# t |1 ƒ d } |# j | | d ƒ } d |  j |1 <q6W| | | <qWd j | ƒ }& | j |& ƒ q§Wt  j d ƒ } t  j d t  j ƒ } x¢t t
 | ƒ ƒ D]Ž} | | } |  j j | \ } } } }	 }
 } | j	 | ƒ } x1t d t
 | ƒ d ƒ D]} | | } | j d ƒ rv	xè | j | ƒ D]× } t | j d ƒ ƒ }- |- d k rh
|- t
 |  j j ƒ k  rh
|  j j |- ƒ \ }. }/ }0 }1 | |- }& |/ d$ k r!
|& } q|
d" t |0 ƒ d# t |1 ƒ d } | j | | d ƒ } d |  j |1 <q¥	t d% | d& |- ƒ q¥	W| | | <qv	qv	Wd j | ƒ } | | | <q	Wt  j d' t  j ƒ }2 t  j d t  j ƒ } xtt t
 | ƒ ƒ D]`} | | } |  j j | \ } } } }	 }
 } |2 j	 | ƒ } xt d t
 | ƒ d ƒ D]é } | | } d( | k rEx½ | j | ƒ D]¬ } t | j d ƒ ƒ }' |  j |' d }( | j ƒ  d d !}) | j ƒ  d }* |( d  k	 r	|) d t |( ƒ |* } d |  j |( <| j | | d ƒ } qqt d) |' | f ƒ qqW| | | <qEqEWd j | ƒ } | | | <që
Wt  j d t  j ƒ } t  j d* ƒ } xPt t
 | ƒ ƒ D]<} | | } |  j j | \ } } } }	 }
 } | j	 | ƒ } xß t d t
 | ƒ d ƒ D]Å } | | } | j d ƒ ràx– | j | ƒ D]… } t | j d ƒ ƒ }' |  j |' d }( |( d  k	 r€d t |( ƒ d } d |  j |( <| j | | d ƒ } qt d |' | f ƒ qW| | | <qàqàWd j | ƒ } | | | <q†Wt  j d ƒ } t  j d+ t  j ƒ }3 x)t t
 | ƒ ƒ D]} | | } |  j j | \ } } } }	 }
 } | j	 | ƒ } x¸ t d t
 | ƒ d ƒ D]ž } | | } | j d, ƒ s…| j d- ƒ r¬| j d. d/ ƒ } | j d0 d1 ƒ } n? | j d2 ƒ sÊ| j d3 ƒ rë|3 j	 | ƒ }4 d j |4 ƒ } n  | | | <qWWd j | ƒ } | | | <qýW|  j j | ƒ |  j j  | ƒ |  j S(4   Ns   (<a.*?href=.*?>)s7   ['"]kindle:pos:fid:([0-9|A-V]+):off:([0-9|A-V]+).*?["']u#   Building proper xhtml for each filei   i   t   <R   R.   t   #s   (<[^>]*\said\s*=[^>]*>)s   \said\s*=['"]([^'"]*)['"]s	    id="aid-s#   (<[^>]*\sdata-AmznPageBreak=[^>]*>)s%   \sdata-AmznPageBreak=['"]([^'"]*)['"]c         S` s   d |  j  d ƒ d S(   Ns    style="page-break-after:i   R.   (   R   (   R$   (    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyt   <lambda>¾   R   s   (<[img\s|image\s][^>]*>)s)   [('"]kindle:embed:([0-9|A-V]+)[^'"]*['")]s	   (<[^>]*>)s/   ['"]kindle:flow:([0-9|A-V]+)\?mime=([^'"]+)['"]s   (url\(.*?\))s6   [('"]kindle:embed:([0-9|A-V]+)\?mime=image/[^\)]*["')]s#   [('"]kindle:embed:([0-9|A-V]+)["')]s-   kindle:flow:([0-9|A-V]+)\?mime=text/css[^\)]*s3   kindle:flow:([0-9|A-V]+)\?mime=image/svg\+xml[^\)]*s   <ims   "../Images/u   useduD   Error: Referenced image %s was not recognized as a valid image in %si    iÿÿÿÿs
   ../Images/uB   Error: Referenced font %s was not recognized as a valid font in %ss	   ../Fonts/s   "../t   /t   inlineu(   warning: ignoring non-existent flow linku    value 0x%xs(   (<[a-zA-Z0-9]+\s[^>]*style\s*=\s*[^>]*>)s   kindle:embedu@   Error: Referenced image %s in style url was not recognized in %ss'   ['"]kindle:embed:([0-9|A-V]+)[^'"]*['"]s   \svalue\s*=\s*['"][^'"]*['"]s   <svgs   <SVGt   preserveaspectratiot   preserveAspectRatiot   viewboxt   viewBoxs   <li s   <LI (!   R   R   R   R   R1   R?   t   getNumberOfPartst   getPartt   getPartInfoR0   R   t
   startswithR   R   t   getIDTagByPosFidR   R/   R   R   t
   IndexErrorR   t   linked_aidst   getNumberOfFlowst   getFlowInfot   getFlowR   R
   R   t   partinfot   flowinfot   replacet   setFlowst   setParts(5   R   t   posfid_patternt   posfid_index_patternt   partsR6   t   partt   partnumt   dirt   filenamet   begR,   t   aidtextR5   t   jR7   R$   t   posfidt   offsett   idtagR:   t   find_tag_with_aid_patternt   within_tag_aid_position_patternt   aidt#   find_tag_with_AmznPageBreak_patternt)   within_tag_AmznPageBreak_position_patternt   flowsRT   t   img_patternt   img_index_patternt   tag_patternt   flow_patternt   url_patternt   url_img_index_patternt   font_index_patternt   url_css_index_patternt   url_svg_image_patternt   ftypet   formatt   flowpartR8   R9   t   osept   csept
   fontNumbert   fontNamet   numt   typt   fmtt   pdirt   fnmt   style_patternt   li_value_patternt	   tagpieces(    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyt
   buildXHTMLu   sž   
$







""
"
$
	"
"

"

"
(   R<   R=   R   Rƒ   (    (    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyR>   n   s   	(    (    (   t
   __future__R    R   R   R   t   compatibility_utilsR   R   t   xrangeR1   R   t
   mobi_utilsR   R   R>   (    (    (    s8   /home/sid/Downloads/KindleUnpack-master/lib/mobi_html.pyt   <module>   s   "	\