
    1                     n    S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr   " S S\R                  5      r
g	)
z*Cloud SDK markdown document text renderer.    )absolute_import)division)unicode_literals)console_attr)rendererc                      ^  \ rS rSrSrSrSr " S S\5      rU 4S jr	S r
SS	 jrS
 rS rS rSS jrS rS rSS jrS rS rSS jrS rSS jrSrU =r$ )TextRenderer   a
  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                       \ rS rSrSrS rSrg)TextRenderer.Indent*   zSecond line indent stack.c                 P    [         R                  U l        U R                  U l        g )N)r	   INDENTindentsecond_line_indentselfs    ;lib/googlecloudsdk/core/document_renderers/text_renderer.py__init__TextRenderer.Indent.__init__-   s     ''dk $d    r   r   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__ r   r   Indentr   *   s
    #,r   r"   c                 ~  > [         [        U ]
  " U0 UD6  [        R                  " 5       U l        U R
                  R                  5       U l        U R
                  R                  5       U l	        U R                  (       a  U R                  S   U l	        SU l
        SU l        U R                  5       /U l        SU l        g )Nr   F)superr	   r   r   GetConsoleAttr_attr
GetBullets_bulletGetControlSequenceIndicator	_csi_char_fill_ignore_widthr"   _indent_level)r   argskwargs	__class__s      r   r   TextRenderer.__init__1   s    	,&77,,.DJ::((*DLZZ;;=DN~~~~a(dnDJDKKM?DLDKr   c                     SU l         U R                  (       a3  U R                  R                  S5        U R	                  5         SU l        gg)z/Flushes the current collection of Fill() lines.F
r   N)r,   r+   _outwriteContentr   s    r   _FlushTextRenderer._Flush=   s6    Dzz
iiood
llndj r   c                    U R                   U:  Ga  U R                   U:  Ga  U R                   nU =R                   S-  sl         U R                   [        U R                  5      :  a)  U R                  R                  U R	                  5       5        U R                  U   R
                  U-   U R                  U R                      l        U R                   S:  a`  U R                  U   R                  U R                  U   R
                  :X  a,  U R                  U R                      =R
                  S-  sl        U R                  U R                      R
                  U R                  U R                      l        Ub,  U R                  U R                      =R                  U-  sl        U R                   U:  a  GM  ggXl         UbC  U R                  U R                      R                  U-   U R                  U R                      l        gg)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   
_SetIndent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%( k		' LL%88;MM 	T[[!( 
(r   c                     U R                   U R                     R                  U R                  -   U l        U R
                  R                  SU R                  -  U-   S-   5        U R                  5         SU l        g)zLDisplays line as an indented example.

Args:
  line: The example line text.
 r4   r   N)r-   r.   r   r   r+   r5   r6   r7   )r   lines     r   ExampleTextRenderer.Exampleo   sZ     dkk*11DKK?DJIIOOC$**$t+d23LLNDJr   c                 ~   U R                  5         UR                  5        GH  nU R                  (       dS  U R                  U R                     R
                  S-
  U l        U R                  R                  SU R                  -  5        U R                  R                  U5      nU R                  U-   S-   U R                  :  a}  U R                  (       dl  U R                  R                  S5        U R                  U R                     R
                  U l        U R                  R                  SU R                  -  5        OHSU l        U R                  (       a0  U =R                  S-  sl        U R                  R                  S5        U =R                  U-  sl        U R                  R                  U5        GM     g)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;   rC   r4   FN)Blanksplitr+   r-   r.   r   r5   r6   r&   DisplayWidth_widthr,   )r   rD   wordwidths       r   FillTextRenderer.Fillz   s    	JJL

ZZ\\$++.559
		djj()jj%%d+e	e	a	4;;	.t7I7I		\\$++.55
		djj()"::
**/*
))//#

jjEj
iiood r   c                 V    U R                  5         U R                  U R                  S9  g)z'Finishes all output document rendering.outN)r8   Fontr5   r   s    r   FinishTextRenderer.Finish   s    KKMII$))Ir   c                 Z   Uc  SU l         OSU-  nU =R                   U-  sl         U R                   S[        R                  -  S[        R                  -  -  -  nU R                   S[        R                  -  -  nU R
                  R                  XES9nU(       a  UR                  U5        U$ )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   r;   )bolditalic)_fontr   BOLDCODEITALICr&   GetFontCoder6   )r   attrrR   maskrW   rX   codes          r   rS   TextRenderer.Font   s     |dj$Yd
jjDj::!x}},hmm1CDEDZZ1/0F::!!t!;D
	iioKr   c                 8   US:X  a  UR                  S5      (       a  gU R                  5         U R                  5         U R                  U R                  S9  US:  a!  U R                  R                  SUS-
  -  5        U R                  R                  U R                  [        R                  5      U-   U R                  [        R                  5      -   S-   5        US:X  a  U R                  R                  S5        U R                  5         SU l	        / U l
        g)	zcRenders a heading.

Args:
  level: The heading level counting from 1.
  heading: The heading text.
r;   z(1)NrQ   r   z  r4   r   )endswithr8   LinerS   r5   r6   r   rZ   rH   r.   _rows)r   r>   headings      r   HeadingTextRenderer.Heading   s     zg&&u--KKMIIKII$))Iqy
iioodeai()IIOODIIhmm,w6IIhmm,-/34 5z
iioodJJLDKDJr   c                     U R                  5         U R                  5       (       d,  U R                  5         U R                  R	                  S5        gg)z$Renders a paragraph separating line.r4   N)r8   	HaveBlankrH   r5   r6   r   s    r   rd   TextRenderer.Line   s6    KKM>>
jjl
iiood r   c                    U R                  5         U(       d  Xl        gU(       a  U R                  U5        gUbv  U(       aM  U R                  USSS9  U R                  R	                  SU R
                  U   R                  -  U-   S-   5        gU R                  USSS9  U R                  5         gUS:  a  S	OSnU R                  XS	S9  U R                  R	                  SU R
                  U   R                  -  U R                  US-
  [        U R                  5      -     -   5        U R
                  U   R                  S-   U l        S
U l        g)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   rC   r4   r;   r   r   T)r8   r.   r@   r5   r6   r-   r   rd   r(   r<   r   r+   r,   )r   r>   
definitionendr   s        r   ListTextRenderer.List   s    	KKMk	
ooe			aA>		$,,u%888:EL	N 	aA>		 AIq1f
ooeqoA
iioocDLL/BBBllEAIT\\1B#BCD E<<&--1djdr   c                 j    U[        U5      :  a#  X   nUS:w  a   U$ US-  nU[        U5      :  a  M#  U$ )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.
rC   r;   )r<   )r   rD   indexcs       r   
_SkipSpaceTextRenderer._SkipSpace   sF     #d)

+a	
cL qje	 #d)

 Lr   c                 V    U R                   R                  XS 5      nU(       d  SnX#-   $ )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   rD   rs   ns       r   _SkipControlSequence!TextRenderer._SkipControlSequence   s,     	

((f6A
a9r   c                     SnU[        U5      :  aY  X   nUS-  nXc;   a  US-  nO4Xd;   a  US-  nUS::  a   U$ O X`R                  :X  a  U R                  X5      nU[        U5      :  a  MY  U$ )a*  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*   rz   )r   rD   rs   
open_charsclose_charsnestrt   s          r   	_SkipNestTextRenderer._SkipNest  s     D
#d)

+aqje	
		19
 L	 ))$6 #d)
 Lr   c                 p   SnU(       Ga+  S GH  nUR                  U5      u  pgnU R                  R                  U5      n	U[        U5      -   U	-   U R                  :  d  US:w  a  US:X  a  US:w  a.  UU R
                  -   [        U5      -   U	-   U R                  :  a  M  US:X  a  U R                  R                  U5        SnX2:w  a/  X R
                  -   nU R                  R                  SSU-  -   5        U R                  R                  XF-   5        U[        U5      U	-   -  nUnUn  O   U(       a  GM+  U$ )a%  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.
rC   )z | z : rC   ,r   r4   )	partitionr&   rJ   r<   rK   SPLIT_INDENTr5   r6   )
r   groupr   running_widthprev_delimiter	delimiterpart_	remainderws
             r   _SplitWideSynopsisGroup$TextRenderer._SplitWideSynopsisGroup*  s7    N
/)"__Y7JJ##D)S0014Dc!i3&6#6#'#4#4$5#&~#6$7 $%$% *.#5
 s"IIOON+ N$"%6%66MIIOOD3#667		-.^,q00"+ 0 %0 r   c                    / nU R                  US5      nUnU[        U5      :  a  X   nUS:X  ac  UnU R                  X5      nU[        U5      S-
  ::  a(  X   S:X  a   XS-      S:X  a  U R                  XS-   5      nOTUR                  XU 5        UnO>US;   a  U R                  X5      nO&X`R                  :X  a  U R                  X5      nOUS-  nU[        U5      :  a  M  U[        U5      :  a  UR                  XS 5        U R                  S   R                  S-
  nUn	U R                  R                  SU	-  5        XR                  -  nU H  n
U R                  R                  U
5      S-   nX-   U R                  :  aI  Un	U R                  R                  SSU	-  -   5        X-   U R                  :  a  U R                  XU	5      n	M|  U R                  R                  SU
-   5        X-  n	M     U R                  R                  S5        g)	aG  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   rC   r;   |[(Nr4   z

)ru   r<   r=   r   r*   rz   r-   r   r5   r6   r   r&   rJ   rK   r   )r   rD   is_synopsisgroupsibegrt   ro   r   r   r   r   s               r   SynopsisTextRenderer.SynopsisP  s    Fa A
C
c$i-
'a	
cOOD$TQDGsNtE{c7IoodE*!
--
&#9NN4#%%d.	Q c$i-  SYmmDJ \\!_##a'FMIIOOC-'(
kkF
**
!
!%
(1
,a

	+		s]223$++-66],-

iioocEk"m  	IIOOFr   )	r&   r(   r*   r+   rY   r,   r-   r.   re   )r   N)NN)NF)r   z)])F)r   r   r   r   r   r   r   objectr"   r   r8   r@   rE   rN   rT   rS   rg   rd   rp   ru   rz   r   r   r   r    __classcell__)r1   s   @r   r	   r	      st     &,,v ,
(OT	6
.. @" 4$L3 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   