o
    €Ï=  ã                   @   sZ   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„ dej	ƒZ
dS )	z*Cloud SDK markdown document HTML renderer.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Úrendererc                       sÂ   e Zd ZdZdZddddœZdZ‡ fdd	„Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd.dd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd/d(d)„Zd0d*d+„Zd,d-„ Z‡  ZS )1Ú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                    sh   t t| ƒj|i |¤Ž tƒ | _d| _d| _d| _d| _d| _	d| _
dg| _d| _|  ¡  | j d¡ d S )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__© úQ/tmp/google-cloud-sdk/lib/googlecloudsdk/core/document_renderers/html_renderer.pyr   2   s   zHTMLRenderer.__init__c                 C   s4   | j  d¡ | jr| j  d| j ¡ | j  d¡ dS )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    r%   r%   r&   r   _   s   zHTMLRenderer._Titlec                 C   sL   d| _ | jr$d| _| jrd| _| j d¡ d| _| j d¡ |  ¡  dS dS )z/Flushes the current collection of Fill() lines.Fz</code>
r   Ú
N)r   r   r   r   r   r   ÚContentr(   r%   r%   r&   Ú_Flusht   s   ùzHTMLRenderer._Flushc                 C   sx   dd„ }t  d|¡}|r| d¡}| d¡ dd¡}||ƒ}|}d}	 || jvr0| j |¡ |S |d	7 }d
j||d}q#)z&Returns a unique document id for name.c                 S   s   t  dd| ¡S )z=Returns singlularized name if name is 'COMMANDS' or 'GROUPS'.z(COMMAND|GROUP)S$z\1©ÚreÚsub)Únamer%   r%   r&   Ú$SingularizeCommandAndGroupDocumentID   s   zHHTMLRenderer.GetDocumentID.<locals>.SingularizeCommandAndGroupDocumentIDzY(-- |\[)*(<[^>]*>)*(?P<anchor>-[-_a-z0-9\[\]]+|[_A-Za-z.0-9 ][-_A-Za-z.0-9 ]*|[-.0-9]+).*Úanchorú ú-r   Té   z{name}-{number})r/   Únumber)r-   ÚmatchÚgroupÚstripÚreplacer   ÚaddÚformat)r    r/   r0   ÚmÚattemptr5   r%   r%   r&   ÚGetDocumentID€   s$   
ú

ûzHTMLRenderer.GetDocumentIDc                 C   s   t  dd|¡}| dd¡S )zýEscapes 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;)r-   r.   r9   )r    ÚbufÚescr%   r%   r&   ÚEntities²   s   zHTMLRenderer.Entitiesc                    s   d  ‡ fdd„|D ƒ¡S )zÏEscapes 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   s    | ]
}ˆ j  ||¡V  qd S )N)Ú_ESCAPEÚget)Ú.0Úcr(   r%   r&   Ú	<genexpr>Ì   s   € z&HTMLRenderer.Escape.<locals>.<genexpr>)Újoin)r    r?   r%   r(   r&   ÚEscapeÁ   s   zHTMLRenderer.Escapec                 C   st   |   ¡  | jsd| _d| _| j d¡ t|ƒ}| ¡ }|t|ƒ8 }| j d| j|  ¡ | j |¡ | j d¡ dS )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Úindentr%   r%   r&   ÚExampleÎ   s   zHTMLRenderer.Examplec                 C   s®   | j rd| _ | j d¡ |  ¡  | jrd| _|  |¡}| ¡ D ]5}t|ƒ}| j| | j	kr7| j d¡ d| _n| jrG|  jd7  _| j d¡ |  j|7  _| j |¡ qdS )zpAdds a line to the output, splitting to stay within the output width.

    Args:
      line: The text line.
    Fz<p>
r)   r   r4   r2   N)
r   r   r   rJ   r   ÚLinkGlobalFlagsÚsplitrK   r   Ú_width)r    rM   ÚwordÚnr%   r%   r&   ÚFillà   s$   
÷zHTMLRenderer.Fillc                 C   s<   | j | jd |  d¡ | jr| j | j¡ | j d¡ dS )z'Finishes all output document rendering.©Úoutr   z
</dl>
</body>
</html>
N)ÚFontr   ÚListr   r   r(   r%   r%   r&   ÚFinishø   s
   
zHTMLRenderer.FinishNc                 C   sì   g }|du r3t jt jt jfD ]#}d|> }| j|@ r1|  j|N  _t| j| ƒD ]	}| d| ¡ q'qn1d|> }|  j|N  _| j|@ rS| j| D ]	}| d| ¡ qHnt| j| ƒD ]	}| d| ¡ qZd |¡}|rt|rt| 	|d ¡ |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.
    Nr4   z</%s>z<%s>r   r)   )
r   ÚBOLDÚITALICÚCODEÚ_fontÚreversedÚ	_FONT_TAGÚappendrG   r   )r    ÚattrrW   ÚtagsÚmaskÚtagÚembellishmentr%   r%   r&   rX      s,   
€û
ÿ
zHTMLRenderer.Fontc                 C   s4   d| _ |d7 }|dkrd}| j d|||f ¡ dS )zyRenders an HTML heading.

    Args:
      level: The heading level counting from 1.
      heading: The heading text.
    ú</dd>
rI   é	   z1
<dt><h%d>%s</h%d></dt>
<dd class="sectionbody">
N)r   r   r   ©r    ÚlevelÚheadingr%   r%   r&   Ú_Heading!  s   ÿÿzHTMLRenderer._Headingc                 C   st   |dkr|  d¡rdS |  ¡  | j| jd |  d¡ | jr%| j | j¡ |  ||¡ d| _|  d¡r8d| _	dS dS )zsRenders a heading.

    Args:
      level: The heading level counting from 1.
      heading: The heading text.
    r4   z(1)NrV   r   Tz WIDE FLAGS)
Úendswithr+   rX   r   rY   r   r   rl   r   r   ri   r%   r%   r&   ÚHeading1  s   


ÿzHTMLRenderer.Headingc                 C   s&   |   ¡  |  ¡ s|  ¡  d| _dS dS )z$Renders a paragraph separating line.TN)r+   Ú	HaveBlankrJ   r   r(   r%   r%   r&   ÚLineD  s
   
þzHTMLRenderer.Linec                 C   s–   d|v s	|  d¡rdj||p|dS d|v s| d¡r$dj||p!|dS |s,| dd	¡}d
}| |¡r<|dt|ƒ … }| dd¡d }dj||dS )z¶Renders 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>ú/r2   z/helpNÚ_z.html)Ú
startswithr;   rm   r9   rK   )r    rr   rs   Útailr%   r%   r&   ÚLinkK  s   
ÿÿ
zHTMLRenderer.Linkc                 C   s   t  dd|¡S )zŠAdd 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>r,   )r    rM   r%   r%   r&   rP   e  s   	zHTMLRenderer.LinkGlobalFlagsFc                 C   sì  |   ¡  | jr%| j|kr%| j | j| j ¡ |  jd8  _| jr%| j|ks|s)|s+dS |dur³| j|k rc|  jd7  _| jt| jƒkrI| j d¡ d| j| j< | jr\d| _| j d¡ n"| j d¡ nd| j| j v rx| j d	¡ d| j| j< n| j d
¡ |r| j dj|  	|¡|d¡ dS | jdkr¥d| j| j v r¥| j d¡ dS | j d¡ d| j| j< dS | j|k rè|  jd7  _| jt| jƒkrÍ| j d¡ d| j| j< | j d| j
|d t| j
ƒ   d ¡ n| j d¡ | j d¡ dS )zÀRenders 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.
    r4   Nr   z</dd>
</dl>
Fz<dl class="notopmargin">
z<dl>
Údtz</dt>
rg   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   rK   ra   r   r;   r>   Ú_BULLET)r    rj   r|   Úendr%   r%   r&   rY   p  s^   þ
ýÿ
ÿþÿzHTMLRenderer.Listc                 C   s(  | j  d¡ t dd|¡}t dd|¡}t dd|¡}t dd|¡}|r+t d	d
|¡}| jd  ¡ }t dj|ddj|d|¡}d}t |¡}|rŒ| 	¡ }|dv r_|d7 }|dkr^d| }n%|dv rn|d8 }|sm|d7 }n|dkr„|dkr„|r„|d dkr„d| 	¡  }| j  |¡ |sJ| j  d¡ dS )zÙRenders 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[(r4   z<span>z)]z</span>r2   ú|z&</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;z
</span></dt></dl>
N)
r   r   r-   r.   ÚcommandÚupperr;   ÚcollectionsÚdequeÚpopleft)r    rM   Úis_synopsisr   ÚnestÚcharsrE   r%   r%   r&   ÚSynopsis¯  sX   	ÿÿýý

ý
€€ ózHTMLRenderer.Synopsisc           	   	   C   sà   | j  d¡ |jr'| j  d¡ |jD ]}| j  d |jpd¡¡ q| j  d¡ |D ]>}| j  d¡ t|ƒD ]+\}}d}d}|t|jƒk rU|j| }|j}|j	rUd |j	¡}| j  d |||¡¡ q5| j  d¡ q)| j  d	¡ d
S )z¶Renders 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   rk   Úcolumnsr;   ÚlabelÚ	enumeraterK   ÚalignÚwidth)	r    ÚtableÚrowsÚcolumnÚrowÚindexÚcolrŽ   r   r%   r%   r&   ÚTableò  s&   

zHTMLRenderer.Table)NN)NF)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r}   rB   r`   r   r   r+   r>   rA   rH   rO   rU   rZ   rX   rl   rn   rp   ry   rP   rY   r‰   r–   Ú__classcell__r%   r%   r#   r&   r      s.    -2
!

?Cr   )rš   Ú
__future__r   r   r   rƒ   r-   Ú&googlecloudsdk.core.document_renderersr   ÚRendererr   r%   r%   r%   r&   Ú<module>   s   