o
    1                     @   sV   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 G dd dej	Z
d	S )
z*Cloud SDK markdown document text renderer.    )absolute_import)division)unicode_literals)console_attr)rendererc                       s   e Zd ZdZdZdZG dd deZ fddZdd	 Z	d)ddZ
dd Zdd Zdd Zd*ddZdd Zdd Zd+ddZdd Zdd  Zd,d#d$Zd%d& Zd-d'd(Z  ZS ).TextRenderera  Renders markdown to text.

  Attributes:
    _attr: console_attr.ConsoleAttr object.
    _bullet: List of bullet characters indexed by list level modulo #bullets.
    _csi_char: The first control sequence indicator character or None if control
      sequences are not supported.
    _fill: The number of characters in the current output line.
    _ignore_width: True if the next output word should ignore _width.
    _indent: List of left indentations in characters indexed by _level.
    _level: The section or list level counting from 0.
        c                   @   s   e Zd ZdZdd ZdS )zTextRenderer.IndentzSecond line indent stack.c                 C   s   t j| _| j| _d S )N)r   INDENTindentsecond_line_indentself r   Q/tmp/google-cloud-sdk/lib/googlecloudsdk/core/document_renderers/text_renderer.py__init__-   s   zTextRenderer.Indent.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   Indent*   s    r   c                    sl   t t| j|i | t | _| j | _| j | _	| j	r%| j	d | _	d| _
d| _|  g| _d| _d S )Nr   F)superr   r   r   GetConsoleAttr_attr
GetBullets_bulletGetControlSequenceIndicator	_csi_char_fill_ignore_widthr   _indent_level)r   argskwargs	__class__r   r   r   1   s   

zTextRenderer.__init__c                 C   s.   d| _ | jr| jd |   d| _dS dS )z/Flushes the current collection of Fill() lines.F
r   N)r   r   _outwriteContentr   r   r   r   _Flush=   s   
zTextRenderer._Flushr   Nc                 C   s  | j |k rq| j |k ro| j }|  j d7  _ | j t| jkr$| j|   | j| j| | j| j  _| j dkrM| j| j| j| jkrM| j| j   jd7  _| j| j  j| j| j  _|durh| j| j   j|8  _| j |k s
dS dS || _ |dur| j| j  j| | j| j  _dS dS )a  Sets the markdown list level and indentations.

    Args:
      level: int, The desired markdown list level.
      indent: int, The new indentation.
      second_line_indent: int, The second line indentation. This is subtracted
        from the prevailing indent to decrease the indentation of the next input
        line for this effect:
            SECOND LINE INDENT ON THE NEXT LINE
               PREVAILING INDENT
               ON SUBSEQUENT LINES
       N)r!   lenr    appendr   r   r   )r   levelr   r   
prev_levelr   r   r   
_SetIndentE   s.   




zTextRenderer._SetIndentc                 C   sB   | j | j j| j | _| jd| j | d  |   d| _dS )zXDisplays line as an indented example.

    Args:
      line: The example line text.
     r&   r   N)r    r!   r   r
   r   r'   r(   r)   )r   liner   r   r   Exampleo   s   
zTextRenderer.Examplec                 C   s   |    | D ]c}| js | j| j jd | _| jd| j  | j	|}| j| d | j
krK| jsK| jd | j| j j| _| jd| j  nd| _| jr^|  jd7  _| jd |  j|7  _| j| qdS )zAdds a line to the output, splitting to stay within the output width.

    This is close to textwrap.wrap() except that control sequence characters
    don't count in the width computation.

    Args:
      line: The text line.
    r+   r1   r&   FN)Blanksplitr   r    r!   r   r'   r(   r   DisplayWidth_widthr   )r   r2   wordwidthr   r   r   Fillz   s"   	zTextRenderer.Fillc                 C   s   |    | j| jd dS )z'Finishes all output document rendering.outN)r*   Fontr'   r   r   r   r   Finish   s   zTextRenderer.Finishc                 C   sr   |du rd| _ nd|> }|  j |N  _ | j dtj> dtj> B @ }| j dtj> @ }| jj||d}|r7|| |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 to this stream if not None.

    Returns:
      The font embellishment string.
    Nr   r+   )bolditalic)_fontr   BOLDCODEITALICr   GetFontCoder(   )r   attrr<   maskr?   r@   coder   r   r   r=      s   
zTextRenderer.Fontc                 C   s   |dkr| drdS |   |   | j| jd |dkr(| jd|d   | j| tj| | tj d  |dkrF| jd |   d| _	g | _
dS )	zsRenders a heading.

    Args:
      level: The heading level counting from 1.
      heading: The heading text.
    r+   z(1)Nr;   r	   z  r&   r   )endswithr*   Liner=   r'   r(   r   rB   r4   r!   _rows)r   r.   headingr   r   r   Heading   s"   

zTextRenderer.Headingc                 C   s,   |    |  s|   | jd dS dS )z$Renders a paragraph separating line.r&   N)r*   	HaveBlankr4   r'   r(   r   r   r   r   rJ      s
   zTextRenderer.LineFc                 C   s   |    |s|| _dS |r| | dS |durB|r4| j|ddd | jd| j| j | d  dS | j|ddd |   dS |dkrHd	nd}| j||d	d | jd| j| j | j|d t	| j    | j| j
d | _d
| _dS )zRenders a bullet or definition list item.

    Args:
      level: The list nesting level, 0 if not currently in a list.
      definition: Bullet list if None, definition list item otherwise.
      end: End of list if True.
    Nr      )r   r   r1   r&   r+   r   r	   T)r*   r!   r0   r'   r(   r    r   rJ   r   r,   r   r   r   )r   r.   
definitionendr   r   r   r   List   s(   

zTextRenderer.Listc                 C   s:   |t |k r|| }|dkr	 |S |d7 }|t |k s|S )zSkip space characters starting at line[index].

    Args:
      line: The string.
      index: The starting index in string.

    Returns:
      The index in line after spaces or len(line) at end of string.
    r1   r+   )r,   )r   r2   indexcr   r   r   
_SkipSpace   s   
zTextRenderer._SkipSpacec                 C   s$   | j ||d }|sd}|| S )zSkip the control sequence at line[index].

    Args:
      line: The string.
      index: The starting index in string.

    Returns:
      The index in line after the control sequence or len(line) at end of
      string.
    Nr+   )r   GetControlSequenceLen)r   r2   rS   nr   r   r   _SkipControlSequence   s   z!TextRenderer._SkipControlSequence[()]c                 C   sx   d}|t |k r:|| }|d7 }||v r|d7 }n||v r)|d8 }|dkr(	 |S n|| jkr4| ||}|t |k s|S )aJ  Skip a [...] nested bracket group starting at line[index].

    Args:
      line: The string.
      index: The starting index in string.
      open_chars: The open nesting characters.
      close_chars: The close nesting characters.

    Returns:
      The index in line after the nesting group or len(line) at end of string.
    r   r+   )r,   r   rX   )r   r2   rS   
open_charsclose_charsnestrT   r   r   r   	_SkipNest  s    

zTextRenderer._SkipNestc           
      C   s   d}|rrdD ]i}| |\}}}| j|}	|t| |	 | jks)|dkr[|dkr[|dkr<|| j t| |	 | jkr<q|dkrH| j| d}||kr[|| j }| jdd|   | j||  |t||	 7 }|}|} |s|S )aA  Splits a wide SYNOPSIS section group string to self._out.

    Args:
      group: The wide group string to split.
      indent: The prevailing left indent.
      running_width: The width of the self._out line in progress.

    Returns:
      The running_width after the group has been split and written to self._out.
    r1   )z | z : r1   ,r_   r&   )	partitionr   r6   r,   r7   SPLIT_INDENTr'   r(   )
r   groupr   running_widthprev_delimiter	delimiterpart_	remainderwr   r   r   _SplitWideSynopsisGroup*  s<   

z$TextRenderer._SplitWideSynopsisGroupc                 C   s  g }|  |d}|}|t|k rl|| }|dkrK|}|  ||}|t|d kr?|| dkr?||d  dkr?|  ||d }n'||||  |}n|dv rV| ||}n|| jkrb| ||}n|d7 }|t|k s|t|k r{|||d  | jd jd }|}	| j	d|	  || j
7 }|D ]8}
| j|
d }|	| | jkr|}	| j	dd|	   |	| | jkr| |
||	}	q| j	d|
  |	|7 }	q| j	d dS )	ac  Renders NAME and SYNOPSIS lines as a second line indent.

    Collapses adjacent spaces to one space, deletes trailing space, and doesn't
    split top-level nested [...] or (...) groups. Also detects and does not
    count terminal control sequences.

    Args:
      line: The NAME or SYNOPSIS text.
      is_synopsis: if it is the synopsis section
    r   r1   r+   |rY   Nr&   z

)rU   r,   r-   r^   r   rX   r    r   r'   r(   r
   r   r6   r7   rj   )r   r2   is_synopsisgroupsibegrT   rQ   r   rc   rb   ri   r   r   r   SynopsisP  sJ   ,


zTextRenderer.Synopsis)r   N)NN)NF)rY   rZ   )F)r   r   r   r   r
   ra   objectr   r   r*   r0   r3   r:   r>   r=   rM   rJ   rR   rU   rX   r^   rj   rp   __classcell__r   r   r$   r   r      s(    
*

 
&r   N)r   
__future__r   r   r   googlecloudsdk.core.consoler   &googlecloudsdk.core.document_renderersr   Rendererr   r   r   r   r   <module>   s   