
    '                         S SK Jr  S SKJr  SrSrSrSr " S S\5      r " S	 S
5      r	 " S S5      r
 " S S\
5      r " S S\5      r " S S\
5      rg)    )
HTMLParser)zip_longest)codea)spanir   r   )r   b)pnoteullic                   \   ^  \ rS rSrSrU 4S jrS rU 4S jrU 4S jrS r	S r
S	 rS
rU =r$ )DocStringParser   z
A simple HTML parser.  Focused on converting the subset of HTML
that appears in the documentation strings of the JSON models into
simple ReST format.
c                 <   > S U l         Xl        [        TU ]  5         g N)treedocsuper__init__)selfr   	__class__s     6lib/third_party/botocore/docs/bcdoc/docstringparser.pyr   DocStringParser.__init__   s    	    c                 d    [         R                  " U 5        [        U R                  5      U l        g r   )r   resetHTMLTreer   r   r   s    r   r   DocStringParser.reset"   s     TXX&	r   c                    > [         TU ]  U5        U R                  R                  5         [	        U R
                  5      U l        g r   )r   feedr   writer   r   )r   datar   s     r   r"   DocStringParser.feed&   s-    T		TXX&	r   c                    > [         TU ]  5         U R                  R                  5         [	        U R
                  5      U l        g r   )r   closer   r#   r   r   )r   r   s    r   r'   DocStringParser.close+   s*    		TXX&	r   c                 6    U R                   R                  XS9  g )N)attrsr   add_tag)r   tagr*   s      r   handle_starttagDocStringParser.handle_starttag1   s    		#+r   c                 8    U R                   R                  USS9  g )NF)is_startr+   )r   r-   s     r   handle_endtagDocStringParser.handle_endtag4   s    		#.r   c                 :    U R                   R                  U5        g r   )r   add_datar   r$   s     r   handle_dataDocStringParser.handle_data7   s    		4 r   )r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r"   r'   r.   r2   r7   __static_attributes____classcell__r   s   @r   r   r      s0    
''
',/! !r   r   c                   :    \ rS rSrSrS rS
S jrS rS rS r	S	r
g)r   ;   z
A tree which handles HTML nodes. Designed to work with a python HTML parser,
meaning that the current_node will be the most recently opened tag. When
a tag is closed, the current_node moves up to the parent node.
c                 ^    Xl         [        5       U l        U R                  U l        / U l        g r   )r   StemNodeheadcurrent_nodeunhandled_tagsr   r   s     r   r   HTMLTree.__init__B   s$    J	 II r   Nc                    U R                  X5      (       d  U R                  R                  U5        g U(       a-  [        X5      nU R                  R                  U5        X@l        g U R                  R                  U l        g r   )_doc_has_handlerrG   appendTagNoderF   	add_childparent)r   r-   r*   r1   nodes        r   r,   HTMLTree.add_tagH   se    $$S33&&s+3&D''- $ $ 1 1 8 8Dr   c                 f    U(       a  SU-  nOSU-  n[        U R                  R                  U5      $ )Nstart_%send_%s)hasattrr   style)r   r-   r1   handler_names       r   rK   HTMLTree._doc_has_handlerT   s-    %+L#c>Ltxx~~|44r   c                 L    U R                   R                  [        U5      5        g r   )rF   rN   DataNoder6   s     r   r5   HTMLTree.add_data\   s    ##HTN3r   c                 N    U R                   R                  U R                  5        g r   )rE   r#   r   r   s    r   r#   HTMLTree.write_   s    		!r   )rF   r   rE   rG   )NT)r9   r:   r;   r<   r=   r   r,   rK   r5   r#   r>    r   r   r   r   ;   s     !
954"r   r   c                   $    \ rS rSrSS jrS rSrg)Nodec   Nc                     Xl         g r   rO   )r   rO   s     r   r   Node.__init__d   s    r   c                     [         er   )NotImplementedErrorrH   s     r   r#   
Node.writeg   s    !!r   rc   r   )r9   r:   r;   r<   r   r#   r>   r^   r   r   r`   r`   c   s    "r   r`   c                   b   ^  \ rS rSrSU 4S jjrS rS rS rS rS r	S r
S	 rS
 rS rSrU =r$ )rD   k   c                 2   > [         TU ]  U5        / U l        g r   )r   r   children)r   rO   r   s     r   r   StemNode.__init__l   s     r   c                 F    Xl         U R                  R                  U5        g r   )rO   rk   rL   r   childs     r   rN   StemNode.add_childp   s    U#r   c                 F    U R                  5         U R                  U5        g r   )collapse_whitespace_write_childrenrH   s     r   r#   StemNode.writet   s      "S!r   c                     [        U R                  U R                  SS  5       HA  u  p#[        U[        5      (       a  Ub  UR	                  X5        M0  UR	                  U5        MC     g )N   )r   rk   
isinstancerM   r#   )r   r   ro   
next_childs       r   rs   StemNode._write_childrenx   sP    !,T]]DMM!"<M!NE%))j.DC,C 	 "Or   c                 :    [        S U R                   5       5      $ )Nc              3   @   #    U  H  oR                  5       v   M     g 7fr   )is_whitespace.0ro   s     r   	<genexpr>)StemNode.is_whitespace.<locals>.<genexpr>   s     DmU&&((ms   )allrk   r   s    r   r|   StemNode.is_whitespace   s    DdmmDDDr   c                 b    U R                   =(       a    U R                   S   R                  5       $ )Nr   )rk   startswith_whitespacer   s    r   r   StemNode.startswith_whitespace   s#    }}Iq!1!G!G!IIr   c                 b    U R                   =(       a    U R                   S   R                  5       $ N)rk   endswith_whitespacer   s    r   r   StemNode.endswith_whitespace   s#    }}Hr!2!F!F!HHr   c                 Z   U R                   (       ak  U R                   S   R                  5       (       aI  U R                   SS  U l         U R                   (       a$  U R                   S   R                  5       (       a  MI  U R                   (       a  U R                   S   R                  5         g g )Nr   rv   )rk   r|   lstripr   s    r   r   StemNode.lstrip   ss    mma 0 > > @ @ MM!"-DM mma 0 > > @ @==MM!##% r   c                 Z   U R                   (       ak  U R                   S   R                  5       (       aI  U R                   S S U l         U R                   (       a$  U R                   S   R                  5       (       a  MI  U R                   (       a  U R                   S   R                  5         g g r   )rk   r|   rstripr   s    r   r   StemNode.rstrip   ss    mmb 1 ? ? A A MM#2.DM mmb 1 ? ? A A==MM"$$& r   c                     U R                  5         U R                  5         U R                   H  nUR                  5         M     g)aN  Remove collapsible white-space from HTML.

HTML in docstrings often contains extraneous white-space around tags,
for readability. Browsers would collapse this white-space before
rendering. If not removed before conversion to RST where white-space is
part of the syntax, for example for indentation, it can result in
incorrect output.
N)r   r   rk   rr   rn   s     r   rr   StemNode.collapse_whitespace   s0     	]]E%%' #r   )rk   r   )r9   r:   r;   r<   r   rN   r#   rs   r|   r   r   r   r   rr   r>   r?   r@   s   @r   rD   rD   k   s=    $"!EJI&'( (r   rD   c                   R   ^  \ rS rSrSrS
U 4S jjrS rSS jrS rS r	S r
S	rU =r$ )rM      zH
A generic Tag node. It will verify that handlers exist before writing.
c                 <   > [         TU ]  U5        X l        Xl        g r   )r   r   r*   r-   )r   r-   r*   rO   r   s       r   r   TagNode.__init__   s     
r   c                 :    [        S U R                   5       5      $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )rw   rM   r}   s     r   r   +TagNode._has_nested_tags.<locals>.<genexpr>   s     I=%:eW--=s   )anyrk   r   s    r   _has_nested_tagsTagNode._has_nested_tags   s    I4==IIIr   c                    U R                   [        ;   =(       a    U R                  5       n[        U R                  [
        5      =(       a7    U R                  R                   [        ;   =(       a    U R                   [        ;   nU(       d  U(       a  U R                  U5        g U R                  U5        U R                  U5        U R                  X5        g r   )r-   OMIT_SELF_TAGSr   rw   rO   rM   PRIORITY_PARENT_TAGSOMIT_NESTED_TAGSrs   _write_start
_write_end)r   r   rx   prioritize_nested_tagsprioritize_parent_tags        r   r#   TagNode.write   s    HH&B4+@+@+B 	 t{{G, -#77-,, 	
 "%:  %#S!(r   c                    U R                   [        ;   a   U R                  5         U R                  5         [	        U R
                  SS U R
                  SS 5       HZ  u  p[        U[        5      (       d  M  UR                  5       (       d  M3  UR                  5       (       d  MJ  UR                  5         M\     [	        U R
                  SS U R
                  SS 5       HZ  u  p#[        U[        5      (       d  M  UR                  5       (       d  M3  UR                  5       (       d  MJ  UR                  5         M\     U R
                   H  nUR                  5         M     g)aQ  Remove collapsible white-space.

All tags collapse internal whitespace. Block-display HTML tags also
strip all leading and trailing whitespace.

Approximately follows the specification used in browsers:
https://www.w3.org/TR/css-text-3/#white-space-rules
https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace
Nr   rv   )r-   HTML_BLOCK_DISPLAY_TAGSr   r   ziprk   rw   rZ   r   r   rr   )r   prevcurnxtro   s        r   rr   TagNode.collapse_whitespace   s     88..KKMKKM T]]3B/qr1BCID4**,,..--//

 D DMM#2.ab0ABHC3))++----//

 C ]]E%%' #r   c                     SU R                   -  n[        UR                  U5      (       a'  [        UR                  U5      " U R                  5        g g )NrS   )r-   rU   rV   getattrr*   )r   r   rW   s      r   r   TagNode._write_start   s=    !DHH,399l++CII|,TZZ8 ,r   c                     SU R                   -  n[        UR                  U5      (       a?  US:X  a  [        UR                  U5      " U5        g [        UR                  U5      " 5         g g )NrT   end_a)r-   rU   rV   r   )r   r   rx   rW   s       r   r   TagNode._write_end   sR    $((*399l++w&		<0<		<02 ,r   )r*   r-   )NNr   )r9   r:   r;   r<   r=   r   r   r#   rr   r   r   r>   r?   r@   s   @r   rM   rM      s-    
J)" (D9
3 3r   rM   c                   j   ^  \ rS rSrSrSU 4S jjr\S 5       rS rS r	S r
S rS	 rS
 rS rSrU =r$ )rZ      z(
A Node that contains only string data.
c           	        > [         TU ]  U5        [        U[        5      (       d  [	        S[        U5      -  5      eSU l        SU l        SU l        US:X  a  g UR                  5       (       a  Xl        g [        S [        U5       5       5      n[        U5      [        S [        [        U5      5       5       5      -
  nUS U U l        XS  U l        XU U l        g )Nz Expecting string type, %s given. c              3   X   #    U  H   u  pUR                  5       (       a  M  Uv   M"     g 7fr   isspacer~   idxchs      r   r   $DataNode.__init__.<locals>.<genexpr>  s      
.GCbjjlCC   *	*c              3   X   #    U  H   u  pUR                  5       (       a  M  Uv   M"     g 7fr   r   r   s      r   r   r     s       *
8GC

CC8r   )r   r   rw   str
ValueErrortype_leading_whitespace_trailing_whitespace_stripped_datar   next	enumeratelenreversed)r   r$   rO   first_non_spacelast_non_spacer   s        r   r   DataNode.__init__   s     $$$?$t*LMM#% $&! 2:<<>>(,% 
(
 
 TT *
($8*
 &
 
 $((8#9 $($9!">Br   c                 N    U R                    U R                   U R                   3$ r   r   r   r   r   s    r   r$   DataNode.data  s1     ''()<)<(=(()+	
r   c                 x    U R                   S:H  =(       a%    U R                  S:g  =(       d    U R                  S:g  $ Nr   )r   r   r   r   s    r   r|   DataNode.is_whitespace  s8    ""b( 
$$*Md.G.G2.M	
r   c                 x    U R                   S:g  =(       d%    U R                  S:H  =(       a    U R                  S:g  $ r   r   r   s    r   r   DataNode.startswith_whitespace  s8    ''2- 
2%I$*C*Cr*I	
r   c                 x    U R                   S:g  =(       d%    U R                  S:H  =(       a    U R                  S:g  $ r   )r   r   r   r   s    r   r   DataNode.endswith_whitespace  s8    ((B. 
2%H$*B*Bb*H	
r   c                 v    U R                   S:w  a  SU l         g U R                  S:X  a  U R                  5         g g r   )r   r   r   r   s    r   r   DataNode.lstrip$  s4    ##r)')D$  B&KKM 'r   c                 v    U R                   S:w  a  SU l         g U R                  S:X  a  U R                  5         g g r   )r   r   r   r   s    r   r   DataNode.rstrip*  s4    $$*(*D%  B&KKM 'r   c                     g)z4Noop, ``DataNode.write`` always collapses whitespaceNr^   r   s    r   rr   DataNode.collapse_whitespace0  s    r   c                     UR                  U R                  R                  5       5      nU R                   SR	                  U5       U R
                   3nUS:w  a  UR                  U5        g g )N r   )translate_wordsr   splitr   joinr   r7   )r   r   wordsstr_datas       r   r#   DataNode.write4  sj    ##D$7$7$=$=$?@''(%(9(()+ 	 r>OOH% r   r   r   )r9   r:   r;   r<   r=   r   propertyr$   r|   r   r   r   r   rr   r#   r>   r?   r@   s   @r   rZ   rZ      sJ    C, 
 






& &r   rZ   N)html.parserr   	itertoolsr   r   r   r   r   r   r   r`   rD   rM   rZ   r^   r   r   <module>r      sp    # !$ - 3 "!j "!J%" %"P" "5(t 5(pM3h M3`H&t H&r   