
    =                     r    d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ  G d dej                        Z
y)	z*Cloud SDK markdown document HTML renderer.    )absolute_import)division)unicode_literalsN)rendererc                        e Zd ZdZdZddddZdZ fdZd	 Zd
 Z	d Z
d Zd Zd Zd Zd ZddZd Zd Zd Zd Zd ZddZddZd Z xZS )HTMLRenderera\  Renders markdown to HTML.

  Attributes:
    _BULLET: A list of bullet type names indexed by list level modulo #bullets.
    _ESCAPE: Character element code name dict indexed by input character.
    _FONT_TAG: A list of font embellishment tag names indexed by font attribute.
    _document_ids: The set of html ids unique in the current document.
    _example: True if currently rendering an example.
    _fill: The number of characters in the current output line.
    _heading: A string of HTML tags that closes out a heading section.
    _level: The section or list level counting from 0.
    _paragraph: True if the output already contains a paragraph tag. Used to
      avoid sequences of 2 or more paragraph tags in the output.
    _pop: A list of list element closing tag names indexed by _level.
    _section: Section heading but no section body yet.
  )disccirclesquarez&amp;z&lt;z&gt;)&<>)code)r   varr   c                    t        t        | 
  |i | t               | _        d| _        d| _        d| _        d| _        d| _	        d| _
        dg| _        d| _        | j                          | j                  j                  d       y )NFr    a  <!--
        THIS DOC IS GENERATED.  DO NOT EDIT.
  -->
<style>
  dd {
    margin-bottom: 1ex;
  }
  li {
    margin-top: 1ex; margin-bottom: 1ex;
  }
  .flag {
    white-space: nowrap;
  }
  .hangingindent {
    padding-left: 1.5em;
    text-indent: -1.5em;
  }
  .normalfont {
    font-weight: normal;
  }
  .notopmargin {
    margin-top: 0em;
  }
  .sectionbody {
    margin-top: .2em;
  }
</style>
</head>
<body>
<dl>
)superr   __init__set_document_ids_example_fill_global_flags_heading_level
_paragraph_pop_section_Title_outwrite)selfargskwargs	__class__s      ;lib/googlecloudsdk/core/document_renderers/html_renderer.pyr   zHTMLRenderer.__init__2   sz    	,&77DDMDJDDMDKDODIDMKKMIIOO      c                     | j                   j                  d       | j                  r(| j                   j                  d| j                  z         | j                   j                  d       y)zRenders an HTML document title.z<html>
<head>
z<title>%s</title>
z<style>
  code { color: green; }
</style>
<script>
  window.onload = function() {
    if (parent.navigation.navigate) {
      parent.navigation.navigate(document.location.href);
    }
  }
</script>
N)r!   r"   _titler#   s    r'   r    zHTMLRenderer._Title_   sO    IIOO   {{
iioo+dkk9:IIOO  r(   c                     d| _         | j                  rhd| _        | j                  r"d| _        | j                  j                  d       d| _        | j                  j                  d       | j                          yy)z/Flushes the current collection of Fill() lines.Fz</code>
r   
N)r   r   r   r   r!   r"   Contentr+   s    r'   _FlushzHTMLRenderer._Flusht   sZ    DOzzdm			$dj
iiood
lln r(   c                 >   d }t        j                  d|      }|r|j                  d      }|j                  d      j	                  dd      } ||      }|}d}	 || j
                  vr| j
                  j                  |       |S |dz  }dj                  ||	      }D)
z&Returns a unique document id for name.c                 0    t        j                  dd|       S )z=Returns singlularized name if name is 'COMMANDS' or 'GROUPS'.z(COMMAND|GROUP)S$z\1resub)names    r'   $SingularizeCommandAndGroupDocumentIDzHHTMLRenderer.GetDocumentID.<locals>.SingularizeCommandAndGroupDocumentID   s     VV(%66r(   zY(-- |\[)*(<[^>]*>)*(?P<anchor>-[-_a-z0-9\[\]]+|[_A-Za-z.0-9 ][-_A-Za-z.0-9 ]*|[-.0-9]+).*anchor -r      z{name}-{number})r5   number)r3   matchgroupstripreplacer   addformat)r#   r5   r6   mattemptr;   s         r'   GetDocumentIDzHTMLRenderer.GetDocumentID   s     7 		
 		A 	WWXd::c?""3,D/5D GF
	**	*w'kf!((d6(Bg r(   c                 T    t        j                  dd|      }|j                  dd      S )zEscapes special characters to their entity tags.

    This is applied after font embellishments.

    Args:
      buf: Normal text that may contain special characters.

    Returns:
      The string with special characters converted to entity tags.
    z(``[^`]*)''z\1&acute;&acute;z...z&hellip;)r3   r4   r?   )r#   bufescs      r'   EntitieszHTMLRenderer.Entities   s(     && 3S
9C;;uj))r(   c                 8     dj                   fd|D              S )zEscapes special characters in normal text.

    This is applied before font embellishments.

    Args:
      buf: Normal text that may contain special characters.

    Returns:
      The escaped string.
    r   c              3   V   K   | ]   }j                   j                  ||       " y w)N)_ESCAPEget).0cr#   s     r'   	<genexpr>z&HTMLRenderer.Escape.<locals>.<genexpr>   s#     73a4<<##Aq)3s   &))join)r#   rF   s   ` r'   EscapezHTMLRenderer.Escape   s     7773777r(   c                    | j                          | j                  s)d| _        d| _        | j                  j	                  d       t        |      }|j                         }|t        |      z  }| j                  j	                  d| j                  |z   z         | j                  j	                  |       | j                  j	                  d       y)zSDisplays line as an indented example.

    Args:
      line: The example line.
    T   z
<p><code>
z&nbsp;z<br>
N)Blankr   r   r!   r"   lenlstrip)r#   lineindents      r'   ExamplezHTMLRenderer.Example   s     	JJL==dmdj
iioom$YF;;=D
c$iFIIOOH

V 345IIOODIIOOHr(   c                 ^   | j                   r"d| _         | j                  j                  d       | j                          | j                  rd| _        | j                  |      }|j                         D ]  }t        |      }| j                  |z   | j                  k\  r#| j                  j                  d       d| _        n<| j                  r0| xj                  dz  c_        | j                  j                  d       | xj                  |z  c_        | j                  j                  |        y)zpAdds a line to the output, splitting to stay within the output width.

    Args:
      line: The text line.
    Fz<p>
r-   r   r:   r8   N)
r   r!   r"   rT   r   LinkGlobalFlagssplitrU   r   _width)r#   rW   wordns       r'   FillzHTMLRenderer.Fill   s     do
iioogJJL d!!$'d


d)a	a4;;	&		
::

a
		
jjAoj
iiood r(   c                     | j                  | j                         | j                  d       | j                  r%| j                  j	                  | j                         | j                  j	                  d       y)z'Finishes all output document rendering.outr   z
</dl>
</body>
</html>
N)Fontr!   Listr   r"   r+   s    r'   FinishzHTMLRenderer.Finish   sK    II$))IIIaL}}
iioodmm$IIOO12r(   c                    g }|t         j                  t         j                  t         j                  fD ]]  }d|z  }| j                  |z  s| xj                  |z  c_        t        | j                  |         D ]  }|j                  d|z          _ nd|z  }| xj                  |z  c_        | j                  |z  r)| j                  |   D ]  }|j                  d|z          n1t        | j                  |         D ]  }|j                  d|z          dj                  |      }|r|r|j                  |dz          |S )a-  Returns the font embellishment string for attr.

    Args:
      attr: None to reset to the default font, otherwise one of renderer.BOLD,
        renderer.ITALIC, or renderer.CODE.
      out: Writes tags line to this stream if not None.

    Returns:
      The font embellishment HTML tag string.
    r:   z</%s>z<%s>r   r-   )
r   BOLDITALICCODE_fontreversed	_FONT_TAGappendrP   r"   )r#   attrrc   tagsmasktagembellishments          r'   rd   zHTMLRenderer.Font   s    D|==(//8==A$Dy::
**
*dnnT23cKK#& 4	 B $Yd
jjDj	d	>>$'C
++fsl
# ( DNN401C
++gm
$ 2GGDMM
}	ii$%r(   c                 l    d| _         |dz  }|dkD  rd}| j                  j                  d|||fz         y)zyRenders an HTML heading.

    Args:
      level: The heading level counting from 1.
      heading: The heading text.
    </dd>
rS   	   z1
<dt><h%d>%s</h%d></dt>
<dd class="sectionbody">
N)r   r!   r"   r#   levelheadings      r'   _HeadingzHTMLRenderer._Heading!  sB     DM	QJEqyeIIOO>'5
!	"r(   c                 r   |dk(  r|j                  d      ry| j                          | j                  | j                         | j	                  d       | j
                  r%| j                  j                  | j
                         | j                  ||       d| _        |j                  d      rd| _	        yy)zsRenders a heading.

    Args:
      level: The heading level counting from 1.
      heading: The heading text.
    r:   z(1)Nrb   r   Tz WIDE FLAGS)
endswithr/   rd   r!   re   r   r"   rz   r   r   rw   s      r'   HeadingzHTMLRenderer.Heading1  s     zg&&u-KKMII$))IIIaL}}
iioodmm$MM%!DM&d 'r(   c                 t    | j                          | j                         s| j                          d| _        yy)z$Renders a paragraph separating line.TN)r/   	HaveBlankrT   r   r+   s    r'   LinezHTMLRenderer.LineD  s*    KKM>>
jjldo r(   c                 n   d|v s|j                  d      rdj                  ||xs |      S d|v s|j                  d      rdj                  ||xs |      S |s|j                  dd	      }d
}|j                  |      r|dt	        |        }|j                  dd      dz   }dj                  ||      S )zRenders an anchor.

    Args:
      target: The link target URL.
      text: The text to be displayed instead of the link.

    Returns:
      The rendered link anchor and text.
    :zwww.z)<a href="{target}" target=_top>{text}</a>)targettext#z..z<a href="{target}">{text}</a>/r8   z/helpN_z.html)
startswithrA   r|   r?   rU   )r#   r   r   tails       r'   LinkzHTMLRenderer.LinkK  s     f}))&18??dnf @   f}-,33dnf 4   ^^C%dDtlT
#f^^C%/F*11d1KKr(   c                 0    t        j                  dd|      S )zAdd global flags links to line if any.

    Args:
      line: The text line.

    Returns:
      line with annoted global flag links.
    z(--[-a-z]+)z<a href="/#\1">\1</a>r2   )r#   rW   s     r'   r[   zHTMLRenderer.LinkGlobalFlagse  s     66.":DAAr(   c                    | j                          | j                  rr| j                  |kD  rc| j                  j                  | j                  | j                            | xj                  dz  c_        | j                  r| j                  |kD  rc|s|sy|| j                  |k  r| xj                  dz  c_        | j                  t        | j                        k\  r| j                  j                  d       d| j                  | j                  <   | j                  r#d| _        | j                  j                  d       n| j                  j                  d       nkd| j                  | j                     v r5| j                  j                  d	       d| j                  | j                  <   n| j                  j                  d
       |r<| j                  j                  dj                  | j                  |      |             y| j                  dkD  r7d| j                  | j                     v r| j                  j                  d       y| j                  j                  d       d| j                  | j                  <   y| j                  |k  r| xj                  dz  c_        | j                  t        | j                        k\  r| j                  j                  d       d| j                  | j                  <   | j                  j                  d| j                  |dz
  t        | j                        z     z   dz          n| j                  j                  d       | j                  j                  d       y)zRenders a bullet or definition list item.

    Args:
      level: The list nesting level.
      definition: Bullet list if None, definition list otherwise.
      end: End of list if True.
    r:   Nr   z</dd>
</dl>
Fz<dl class="notopmargin">
z<dl>
dtz</dt>
ru   zN<dt id="{document_id}"><span class="normalfont">{definition}</span></dt>
<dd>
)document_id
definitionz<dd>
z<dt><span class="normalfont">
z</span></dt>
</dl>
z</li>
</ul>
z<ul style="list-style-type:z">
z</li>
z<li>
)r/   r   r!   r"   r   rU   rn   r   rA   rD   _BULLET)r#   rx   r   ends       r'   re   zHTMLRenderer.Listp  sx    	KKM
++$++-
iioodii,-
kkQk ++$++- % 
u	q;;#dii.(
))

2
!1		$++==$-
))//6
7
))//(
#499T[[))			"!1		$++			"			//5v ..z:% 06 0	
 ;;?ttyy'==		!		9:!8		$++ 
u	q;;#dii.(
))

2
!1		$++		)llEAIT\\)::;<	
 				"
iioohr(   c                 (   | j                   j                  d       t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }|rt        j                  d	d
|      }| j                  d   j                         }t        j                  dj                  |      dj                  |      |      }d}t        j                  |      }|rz|j                         }|dv r|dz  }|dk(  r>d|z   }n8|dv r|dz  }|s-|dz  }n'|dk(  r"|dk(  r|r|d   dk(  rd|j                         z   }| j                   j                  |       |rz| j                   j                  d       y)zRenders NAME and SYNOPSIS lines as a hanging indent.

    Does not split top-level [...] or (...) groups.

    Args:
      line: The NAME or SYNOPSIS section text.
      is_synopsis: if it is the synopsis section
    zL<dl class="notopmargin"><dt class="hangingindent"><span class="normalfont">
z!(<code>)([-a-z0-9\[\]]+)(</code>)z\1<a href="#\2">\2</a>\3zhref="#--no-z	href="#--z0([^[=]\[*<code><var>)([_A-Z0-9]+)(</var></code>)z=(<code><a href="#[-a-z0-9\[\]]+">[-a-z0-9\[\]]+(<[^>]*>|\S)*)z<span class="flag">\1</span>z(,)z\1</span><span class="flag">r   z>{root}_WIDE_FLAG )rootz3><a href="#{root}-WIDE-FLAGS">{root}_WIDE_FLAG</a> z[(r:   z<span>z)]z</span>r8   |z&</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;z
</span></dt></dl>
N)
r!   r"   r3   r4   commandupperrA   collectionsdequepopleft)r#   rW   is_synopsisr   nestcharsrN   s          r'   SynopsiszHTMLRenderer.Synopsis  s    	IIOO	& 66,.I4D 66.+t4D 66;#D 66L'D VVE:DAd <<?  "D66###.=DD$DOD Dd#E

--/a	
d	19l!9	
y.!19ceaC4u}}F
iiooa  	IIOO+,r(   c           	      :   | j                   j                  d       |j                  r| j                   j                  d       |j                  D ]:  }| j                   j                  dj	                  |j
                  xs d             < | j                   j                  d       |D ]  }| j                   j                  d       t        |      D ]  \  }}d}d}|t        |j                        k  rB|j                  |   }|j                  }|j                  rdj	                  |j                        }| j                   j                  dj	                  |||              | j                   j                  d        | j                   j                  d	       y
)zRenders a table.

    Nested tables are not supported.

    Args:
      table: renderer.TableAttributes object.
      rows: A list of rows, each row is a list of column strings.
    z
<blockquote>
<table>
z<tr>
z<th>{}</th>
r   z</tr>
leftz width={}emz<td align={}{}>{}</td>
z</table>
</blockquote>
N)
r!   r"   ry   columnsrA   label	enumeraterU   alignwidth)	r#   tablerowscolumnrowindexcolr   r   s	            r'   TablezHTMLRenderer.Table  s0    	IIOO/0 }}
iioohMM&		..v||/ArBC "
iiooi  
iiooh!#*%3u}}%%=='&,,%\\!((6E		299%LM ' iiooi   	IIOO/0r(   )NN)NF)F)__name__
__module____qualname____doc__r   rK   rm   r   r    r/   rD   rH   rQ   rY   r`   rf   rd   rz   r}   r   r   r[   re   r   r   __classcell__)r&   s   @r'   r   r      s    " )'V4'5)+Z*
0Cd*8$03B  &L4	B= ~A-F'1r(   r   )r   
__future__r   r   r   r   r3   &googlecloudsdk.core.document_renderersr   Rendererr    r(   r'   <module>r      s0     1 &  '  	 ;}18$$ }1r(   