
    1                     n    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ej                        Z
y	)
z*Cloud SDK markdown document text renderer.    )absolute_import)division)unicode_literals)console_attr)rendererc                        e Zd ZdZdZdZ G d de      Z fdZd Z	ddZ
d	 Zd
 Zd ZddZd Zd ZddZd Zd ZddZd ZddZ x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                       e Zd ZdZd Zy)TextRenderer.IndentzSecond line indent stack.c                 P    t         j                  | _        | j                  | _        y )N)r	   INDENTindentsecond_line_indentselfs    ;lib/googlecloudsdk/core/document_renderers/text_renderer.py__init__zTextRenderer.Indent.__init__-   s     ''dk $d    N)__name__
__module____qualname____doc__r    r   r   Indentr   *   s
    #,r   r   c                 p   t        t        | 
  |i | t        j                         | _        | j
                  j                         | _        | j
                  j                         | _	        | j                  r| j                  d   | _	        d| _
        d| _        | j                         g| _        d| _        y )Nr   F)superr	   r   r   GetConsoleAttr_attr
GetBullets_bulletGetControlSequenceIndicator	_csi_char_fill_ignore_widthr   _indent_level)r   argskwargs	__class__s      r   r   zTextRenderer.__init__1   s    	,&77,,.DJ::((*DLZZ;;=DN~~~~a(dnDJDKKM?DLDKr   c                     d| _         | j                  r3| j                  j                  d       | j	                          d| _        yy)z/Flushes the current collection of Fill() lines.F
r   N)r&   r%   _outwriteContentr   s    r   _FlushzTextRenderer._Flush=   s6    Dzz
iiood
llndj r   c                     | j                   |k  r| j                   |k  r| j                   }| xj                   dz  c_         | j                   t        | j                        k\  r)| j                  j                  | j	                                | j                  |   j
                  |z   | j                  | j                      _        | j                   dkD  r_| j                  |   j                  | j                  |   j
                  k(  r,| j                  | j                      xj
                  dz  c_        | j                  | j                      j
                  | j                  | j                      _        |,| j                  | j                      xj                  |z  c_        | j                   |k  ryy|| _         |C| j                  | j                      j                  |z   | j                  | j                      _        yy)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_levels        r   
_SetIndentzTextRenderer._SetIndentE   s    {{U KK%[[
q;;#dll++
,,

dkkm
,LL$++f4 	T[[!(KK!OLL$77LL$++, ,,t{{
#
*
*a
/
*LL%,, 	T[[!4)
,,t{{
#
6
6:L
L
6# KK%( dk		' LL%88;MM 	T[[!( 
(r   c                     | j                   | j                     j                  | j                  z   | _        | j
                  j                  d| j                  z  |z   dz          | j                          d| _        y)zXDisplays line as an indented example.

    Args:
      line: The example line text.
     r-   r   N)r'   r(   r   r   r%   r.   r/   r0   )r   lines     r   ExamplezTextRenderer.Exampleo   sZ     dkk*11DKK?DJIIOOC$**$t+d23LLNDJr   c                 Z   | j                          |j                         D ]  }| j                  sS| j                  | j                     j
                  dz
  | _        | j                  j                  d| j                  z         | j                  j                  |      }| j                  |z   dz   | j                  k\  rx| j                  sl| j                  j                  d       | j                  | j                     j
                  | _        | j                  j                  d| j                  z         nCd| _        | j                  r0| xj                  dz  c_        | j                  j                  d       | xj                  |z  c_        | j                  j                  |        y)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.
    r3   r:   r-   FN)Blanksplitr%   r'   r(   r   r.   r/   r    DisplayWidth_widthr&   )r   r;   wordwidths       r   FillzTextRenderer.Fillz   s    	JJL

ZZ\\$++.559
		djj()jj%%d+e	e	a	4;;	.t7I7I		\\$++.55
		djj()"::
**/*
))//#

jjEj
iiood r   c                 \    | j                          | j                  | j                         y)z'Finishes all output document rendering.outN)r1   Fontr.   r   s    r   FinishzTextRenderer.Finish   s    KKMII$))Ir   c                 V   |d| _         nd|z  }| xj                   |z  c_         | j                   dt        j                  z  dt        j                  z  z  z  }| j                   dt        j                  z  z  }| j
                  j                  ||      }|r|j                  |       |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.
    r   r3   )bolditalic)_fontr   BOLDCODEITALICr    GetFontCoder/   )r   attrrG   maskrK   rL   codes          r   rH   zTextRenderer.Font   s     |dj$Yd
jjDj::!x}},hmm1CDEDZZ1/0F::!!tF!;D
	iioKr   c                 .   |dk(  r|j                  d      ry| j                          | j                          | j                  | j                         |dkD  r!| j                  j                  d|dz
  z         | j                  j                  | j                  t        j                        |z   | j                  t        j                        z   dz          |dk(  r| j                  j                  d       | j                          d| _	        g | _
        y)	zsRenders a heading.

    Args:
      level: The heading level counting from 1.
      heading: The heading text.
    r3   z(1)NrF   r   z  r-   r   )endswithr1   LinerH   r.   r/   r   rN   r>   r(   _rows)r   r6   headings      r   HeadingzTextRenderer.Heading   s     zg&&u-KKMIIKII$))Iqy
iioodeai()IIOODIIhmm,w6IIhmm,-/34 5z
iioodJJLDKDJr   c                     | j                          | j                         s,| j                          | j                  j	                  d       yy)z$Renders a paragraph separating line.r-   N)r1   	HaveBlankr>   r.   r/   r   s    r   rW   zTextRenderer.Line   s3    KKM>>
jjl
iiood r   c                    | j                          |s|| _        y|r| j                  |       y|w|rP| j                  |dd       | j                  j	                  d| j
                  |   j                  z  |z   dz          y| j                  |dd       | j                          y|dkD  rd	nd}| j                  ||d	       | j                  j	                  d| j
                  |   j                  z  | j                  |dz
  t        | j                        z     z          | j
                  |   j                  dz   | _        d
| _        y)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   r:   r-   r3   r   r   T)r1   r(   r8   r.   r/   r'   r   rW   r"   r4   r   r%   r&   )r   r6   
definitionendr   s        r   ListzTextRenderer.List   s    	KKMdk	
ooe			aA>		$,,u%888:EL	N 	aA>		 AIq1f
ooeFqoA
iioocDLL/BBBllEAIT\\1B#BCD E<<&--1djdr   c                 d    |t        |      k  r!||   }|dk7  r	 |S |dz  }|t        |      k  r!|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.
    r:   r3   )r4   )r   r;   indexcs       r   
_SkipSpacezTextRenderer._SkipSpace   sH     #d)

u+a	
cL qje	 #d)

 Lr   c                 P    | j                   j                  ||d       }|sd}||z   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.
    Nr3   )r    GetControlSequenceLen)r   r;   rc   ns       r   _SkipControlSequencez!TextRenderer._SkipControlSequence   s0     	

((ef6A
a19r   c                     d}|t        |      k  rU||   }|dz  }||v r|dz  }n2||v r|dz  }|dk  r$	 |S || j                  k(  r| j                  ||      }|t        |      k  rU|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   r3   )r4   r$   ri   )r   r;   rc   
open_charsclose_charsnestrd   s          r   	_SkipNestzTextRenderer._SkipNest  s     D
#d)

u+aqje	
j		19
 L ))$6 #d)
 Lr   c                 P   d}|r dD ]  }|j                  |      \  }}}| j                  j                  |      }	|t        |      z   |	z   | j                  k\  s
|dk7  r|dk(  r|dk7  r,|| j
                  z   t        |      z   |	z   | j                  k\  r|dk(  r| j                  j                  |       d}||k7  r0|| j
                  z   }| j                  j                  dd|z  z          | j                  j                  ||z          |t        |      |	z   z  }|}|} n |r |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.
    r:   )z | z : r:   ,rp   r-   )	partitionr    r@   r4   rA   SPLIT_INDENTr.   r/   )
r   groupr   running_widthprev_delimiter	delimiterpart_	remainderws
             r   _SplitWideSynopsisGroupz$TextRenderer._SplitWideSynopsisGroup*  s=    N
/)"__Y7aJJ##D)S0014Dc!i3&6#6#'#4#4$5#&~#6$7 $%$% *.#5
 s"IIOON+ Nf$"T%6%66MIIOOD3#667		-.^,q00"+ 0 0 r   c                    g }| j                  |d      }|}|t        |      k  r||   }|dk(  re|}| j                  ||      }|t        |      dz
  k  r)||   dk(  r!||dz      dk(  r| j                  ||dz         }nU|j                  |||        |}n>|dv r| j                  ||      }n'|| j                  k(  r| j                  ||      }n|dz  }|t        |      k  r|t        |      k  r|j                  ||d        | j                  d   j                  dz
  }|}	| j                  j                  d|	z         || j                  z  }|D ]  }
| j                  j                  |
      dz   }|	|z   | j                  k\  rI|}	| j                  j                  dd|	z  z          |	|z   | j                  k\  r| j                  |
||	      }	|| j                  j                  d|
z          |	|z  }	 | j                  j                  d       y)	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   r:   r3   |[(Nr-   z

)re   r4   r5   rn   r$   ri   r'   r   r.   r/   r   r    r@   rA   r{   )r   r;   is_synopsisgroupsibegrd   r`   r   rt   rs   rz   s               r   SynopsiszTextRenderer.SynopsisP  s    Fa A
C
c$i-
q'a	
cOOD!$TQDGsNtAE{c7IoodAE*!
--S
&#9NN4#%%dA.	Q c$i-  SYmmDJ \\!_##a'FMIIOOC-'(
dkkF
**
!
!%
(1
,a
!
	+		s]223A$++-66V],-

iioocEk"qm  	IIOOFr   )r   N)NN)NF)r~   z)])F)r   r   r   r   r   rr   objectr   r   r1   r8   r<   rD   rI   rH   rZ   rW   ra   re   ri   rn   r{   r   __classcell__)r+   s   @r   r	   r	      so     &,,v ,
(OT	6
.. @" 4$L3r   r	   N)r   
__future__r   r   r   googlecloudsdk.core.consoler   &googlecloudsdk.core.document_renderersr   Rendererr	   r   r   r   <module>r      s-     1 &  ' 4 ;i8$$ ir   