
                         T   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSK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KrSrSrSrSrSr\" / SQ5      rS r " S S\5      rS rS rS rS r  " S S\RB                  " \RD                  \5      5      r# " S S\#5      r$S r%g)z6The Calliope command help document markdown generator.    )absolute_import)division)unicode_literalsN)base)
usage_text)
properties)
console_io)universe_descriptorN            )gcloudzgcloud alphazgcloud betazgcloud previewc                     [         R                  " SSU 5      n [         R                  " SU 5      nU(       a1  UR                  S5      R	                  5       UR                  S5      -   n U R                  S5      $ )a  Returns a help doc index line for a capsule line.

The capsule line is a formal imperative sentence, preceded by optional
(RELEASE-TRACK) or [TAG] tags, optionally with markdown attributes. The index
line has no tags, is not capitalized and has no period, period.

Args:
  capsule: The capsule line to convert to an index line.

Returns:
  The help doc index line for a capsule line.
z(\*?[\[(][A-Z]+[\])]\*? +)* z([A-Z])([^A-Z].*)   r   .)resubmatchgrouplowerrstrip)capsuler   s     'lib/googlecloudsdk/calliope/markdown.py_GetIndexFromCapsuler   0   s]     FF12w?'
(('
1%
kk!n""$u{{1~5G		    c                   *    \ rS rSrSrS rS rS rSrg)ExampleCommandLineSplitterG   zExample command line splitter.

Attributes:
  max_index: int, The max index to check in line.
  quote_char: str, The current quote char for quotes split across lines.
  quote_index: int, The index of quote_char in line or 0 if in previous line.
c                 R    [         [        -
  [        -
  U l        S U l        SU l        g )Nr   )_SPLIT_SECTION_INDENT_FIRST_INDENT
_max_index_quote_char_quote_indexselfs    r   __init__#ExampleCommandLineSplitter.__init__P   s#    .>DODDr   c                    SnSnSnSnSnSnXpR                   :  a  X   nUS-  nXR                  :X  a  SU l        OU R                  (       a  US:X  a  US-
  nU R                  nOaUS;   a  Xl        Xpl        SnOLUS:X  a  US-  nO@XpR                   :  a1  US:X  a  X   S:X  a  S	nUnOU(       a  SnOUnOUS
;   a  UnOUS:X  a  SnXpR                   :  a  M  Sn	[        n
U(       a	  Un[        n
O.U(       a  UnUS:X  a  Sn	OUS-  nOU(       a  UnOU R                   nXR                  ::  a  SU l        OSU l        [
        [        -
  U
-
  U l         USU U	SU
-  /XS 4$ )a  Splits line into before and after, len(before) < self._max_index.

Args:
  line: str, The line to split.

Returns:
  (before, after)
    The line split into two parts. <before> is a list of strings that forms
    the first line of the split and <after> is a string containing the
    remainder of the line to split. The display width of <before> is
    < self._max_index. <before> contains the separator chars, including a
    newline.
r   NFr    "'\-T),;/|=z\
r0   
)r%   r&   r'   r$   _SUBSEQUENT_INDENTr"   r#   )r)   linepunct_indexquoted_space_indexquoted_space_quotespace_index
space_flagic	separatorindentsplit_indexs               r   _SplitInTwo&ExampleCommandLineSplitter._SplitInTwoU   s    KKJ	A
oo

'a1fa	
	8 1u
#//

?9	Q8 W^JKJK&&+#X*; oo
< IFk!f	&k	s	"	q	kOOk'''dd.7DO+	3<8$|:LLLr   c                     / n[        U5      U R                  :  a?  U R                  U5      u  p1UR                  U5        [        U5      U R                  :  a  M?  UR	                  U5        SR                  U5      $ )zSplits a long example command line by inserting newlines.

Args:
  line: str, The command line to split.

Returns:
  str, The command line with newlines inserted.
r   )lenr%   rE   extendappendjoin)r)   r:   linesbefores       r   Split ExampleCommandLineSplitter.Split   se     E
d)doo
%%%d+lfll6 d)doo
% 
LL775>r   )r%   r&   r'   N)	__name__
__module____qualname____firstlineno____doc__r*   rE   rN   __static_attributes__ r   r   r   r   G   s    
FMPr   r   c                    [         R                  " S[         R                  [         R                  -  S9n[         R                  " S[         R                  [         R                  -  S9nUR	                  U 5      nUR	                  U 5      nU(       d	  U(       d  U $ U(       a  UnOUnU SUR                  S5       nXR                  S5      UR                  S5       nXR                  S5      S n[         R                  " S[         R                  5      n	[         R                  " S[         R                  [         R                  -  5      n
SnS	n U	R	                  X{5      nU
R	                  X{5      nU(       d  U(       d  OU(       a  U(       a  UR                  S5      UR                  S5      :  a8  [        X}R                  S5      5      u  nnXXR                  S5       U-   -  nUnOXXR                  S5       -  nUR                  S5      nOeU(       a'  XXR                  S5       -  nUR                  S5      nO7[        X}R                  S5      5      u  nnXXR                  S5       U-   -  nUnGM*  XlX{S -   -   U-   $ )
a2  Removes line breaks and extra spaces in example commands.

In command implementation, some example commands were manually broken into
multiple lines with or without "". This function removes these line
breaks and let ExampleCommandLineSplitter to split the long commands
centrally.

This function will not change example commands in the following situations:

1. If the command is in a code block, surrounded with ```sh...```.
2. If the values are within a quote (single or double quote).

Args:
  doc: str, help text to process.

Returns:
  Modified help text.
z^## EXAMPLES\n(.+?)(\n+## ))flagsz^## EXAMPLES\n(.+)Nr   z^ *(\$ .*)$z^ *```sh(.+?```)r   r   )r   compileMDOTALLsearchstartendUnifyExampleLine)docexample_sec_until_next_secexample_sec_until_endmatch_example_secmatch_example_sec_to_endselected_matchdoc_before_examplesexample_sectiondoc_after_examplepat_example_linepat_code_blockposresmatch_example_linematch_code_blockexamplenext_poss                    r   NormalizeExampleSectionrq      s[   &  "zz$BDD299,<  **244"))#3 177<299#>	#;J&N-N5n22156,,Q/.2D2DQ2GH/,,Q/12ZZ5::1244"))3CD.	#
#)00F%,,_B$6	0 
			"%7%=%=a%@	@,55a8
 	s%=%=a%@AGKKs%9%9!%<==""1%		S#7#7#:;;c  #c*
33A6gx 
S#;#;A#>?'IIcc3 	4 
od&; ;	<?P	PPr   c                    [         R                  " S[         R                  5      n[         R                  " S[         R                  5      nUR                  X5      nUR                  X5      nU(       d  U(       d  U R	                  5       n[         R                  " S[         R                  5      nUR                  Xa5      nUR                  S5      n	[         R                  " S5      nUR                  SU	5      n	[        U	5      n	SU	-   [        U5      4$ U(       aC  U(       a<  [        UR                  S5      5      [        UR                  S5      5      :  a  Un
OUn
OU(       a  UOUn
U
R                  S5      n	[         R                  " S5      nUR                  SU	5      n	[        U	5      n	U
R                  S5      nSU	-   U4$ )a  Returns the example command line at pos in one single line.

  pos is the starting point of an example (starting with "$ ").
  This function removes "
" and "" and redundant spaces in the example line.
  The resulted example should be in one single line.

  Args:
    example_doc: str, Example section of the help text.
    pos: int, Position to start. pos will be the starting position of an example
      line.

  Returns:
    normalized example command, next starting position to search
  z\$\s+(.+?)(\n +\$\s+)z\$\s+(.+?)(\n\s*\n|\n\+\n)z	\$\s+(.+)r   z\\\n\s*r   z$ )
r   rY   r[   r   r   r   r   !RemoveSpacesLineBreaksFromExamplerH   r^   )example_docrk   pat_match_next_command"pat_match_empty_line_after_commandmatch_next_commandmatch_empty_line_after_commandnew_docpatr   ro   re   rp   s               r   r_   r_      s    ::		 (*zz#RYY($ .33KE#E#K#K$  
$B  "G
**\299
-CIIg#Ekk!nG
**Z
 Cggb'"G/8G'>3w<''<
##A&'#&,,Q/+  6n)n  	+ 
   #'


:#GGB '-g6'"(		!!r   c                     [        U 5      S-
  nUS:  a  X   S:X  a  US-  nUS:  a
  X   S:X  a  M  [        U 5      U-
  S-
  $ )Nr   r   r1   )rH   )rl   indexs     r   _PrecedingBackslashCountr}   -  sN    
c(Q,%
szT)	QJE 	
szT)	SE	A	r   c                    / nU R                  5       n SnU[        U 5      :  Ga  X   nUS;  a6  US:X  a  SnUS:X  a  U(       a	  US   S:X  d  UR                  U5        US-  nGO9US:X  a  UR                  U5        US-  nU[        U 5      :  a9  X   S:w  a1  UR                  X   5        US-  nU[        U 5      :  a
  X   S:w  a  M1  U[        U 5      :  a  UR                  X   5        US-  nOUR                  X   5        US-  nU[        U 5      :  a_  X   S:X  a  [        U5      S	-  S:X  dE  UR                  X   5        US-  nU[        U 5      :  a  X   S:X  d  M1  [        U5      S	-  S:X  d  ME  U[        U 5      :  a  UR                  X   5        US-  nU[        U 5      :  a  GM  S
R	                  U5      $ )a  Returns the example with redundant spaces and line breaks removed.

If a character sequence is quoted (either single or double quote), we will
not touch its value. Single quote is not allowed within single quote even
with a preceding backslash. Double quote is allowed in double quote with
preceding backslash though. If the spaces and line breaks are within quote,
they are not touched.

Args: example, str: Example line to process.
r   r.   r8   r-   r   r0   r/   r   r   )striprH   rJ   r}   rK   )ro   rl   rk   rA   s       r   rs   rs   4  s    	#MMO'	#c'lA
	
d3h33r7c>

1	Qhc	
c	jjm	Qhc#g,7<3#6

7< q #g,7<3#6 
s7|	

7< q	jj	Qhc#g,
,#
":3"?!"Cq"H

7< q	 #g,
,#
":3"?!"Cq"H 
s7|	

7< q; 	c'l> 
r   c                      \ rS rSrSrS r\R                  S 5       r\R                  S 5       r	S r
\S 5       r\S 5       r\S	 5       r\S
 5       rS rS rS rS rS rS rS3S jrS4S jrS4S jrS4S jrS5S jrS5S jrS6S jrS7S jrS4S jrS4S jrS4S jr S4S jr!S8S jr"S4S jr#S4S  jr$ S9S! jr%S" r&S4S# jr'S6S$ jr(S% r)S& r*S3S' jr+S( r,S) r-S* r.S+ r/S, r0S- r1S. r2S/ r3S:S0 jr4S1 r5S2r6g);MarkdownGeneratorid  aZ  Command help markdown document generator base class.

Attributes:
  _buf: Output document stream.
  _capsule: The one line description string.
  _command_name: The dotted command name.
  _command_path: The command path list.
  _doc: The output markdown document string.
  _docstring: The command docstring.
  _file_name: The command path name (used to name documents).
  _final_sections: The list of PrintFinalSections section names.
  _is_hidden: The command is hidden.
  _out: Output writer.
  _printed_sections: The set of already printed sections.
  _release_track: The calliope.base.ReleaseTrack.
c                    Xl         SR                  U R                   5      U l        SU l        SU l        SU l        U R                   (       a  U R                   S   OSU l        [        R                  " 5       U l	        U R                  R                  U l        SU l        SU l        SS/U l        SU l        0 U l        SR                  U R                   5      U l        [%        5       U l        X0l        X l        [%        5       U l        g)zConstructor.

Args:
  command_path: The command path list.
  release_track: The base.ReleaseTrack of the command.
  is_hidden: The command is hidden if True.
r-   Nr   r   EXAMPLESzSEE ALSO_)_command_pathrK   _command_name_subcommands
_subgroups_sort_top_level_args_topioStringIO_bufwrite_out_capsule
_docstring_final_sections_arg_sections	_sections
_file_nameset_global_flags
_is_hidden_release_track_printed_sections)r)   command_pathrelease_track	is_hiddens       r   r*   MarkdownGenerator.__init__v  s     &$"4"45DDDO $D)-););""1%DIDI		DIDMDO&
3DDDNhht112DODO' UDr   c                     g)a  Determines if the given sub command path is valid from this node.

Args:
  sub_command_path: [str], The pieces of the command path.

Returns:
  True, if the given path parts exist under this command or group node.
  False, if the sub path does not lead to a valid command or group.
NrV   r)   sub_command_paths     r   IsValidSubPath MarkdownGenerator.IsValidSubPath  s     	r   c                     g)Returns the command arguments.NrV   r(   s    r   GetArgumentsMarkdownGenerator.GetArguments  s     	r   c                     U(       a  U(       d  gSR                  U5      nSU-   S-   SR                  U5      -   S-   nU(       a  USSR                  U5      -   -  nU$ )a  Creates a link to the command reference from a command example.

If with_args is False and the provided command includes args,
returns None.

Args:
  cmd: [str], a command.
  args: [str], args with the command.
  with_args: bool, whether the example is valid if it has args.

Returns:
  (str) a representation of the command with a link to the reference, plus
  any args. | None, if the command isn't valid.
Nr5   zlink:[r-   ])rK   )r)   cmdargs	with_argsrefcommand_links         r   FormatExampleMarkdownGenerator.FormatExample  sY     I
((3-CS=3&#6<LcCHHTN**lr   c                 
   [        U R                  5      S:X  a  g[        U R                  5      S:X  aK  [        R                  R	                  5        Vs/ s H  oR
                  PM     nnU R                  S   U;   a  ggs  snf )a  Determine if this node should be treated as a "root" of the CLI tree.

The top element is the root, but we also treat any additional release tracks
as a root so that global flags are shown there as well.

Returns:
  True if this node should be treated as a root, False otherwise.
r   Tr   r   F)rH   r   r   ReleaseTrack	AllValuesprefix)r)   ttrackss      r   is_rootMarkdownGenerator.is_root  ss     4!#	T	 A	%"&"3"3"="="?@"?Q"?f@			B	6	) As   B c                 R    [        U R                  =(       d    U R                  5      $ )z-Returns True if this node is a command group.)boolr   r   r(   s    r   is_groupMarkdownGenerator.is_group  s     44#4#455r   c                     U R                   $ )zAReturns whether to sort the top level arguments in markdown docs.)r   r(   s    r   sort_top_level_args%MarkdownGenerator.sort_top_level_args  s     $$$r   c                     [        U R                  5      S:  a*  U R                  S   U R                  R                  :X  a  SnOSn[        U R                  5      US-   :  =(       a    U R                  U   S:H  $ )z-Returns True if this node is a topic command.   r   r   topic)rH   r   r   r   )r)   command_indexs     r   is_topicMarkdownGenerator.is_topic  su     	D1$q!T%8%8%?%??mmDMA$56 	9}-8r   c                 $   [         R                  " U=(       d    S4U R                  U R                  U R                  SR                  U R                  SS 5      SR                  U R                  SS 5      U R                  S.U R                  D6$ )zvExpand command {...} references in text.

Args:
  text: The text chunk to expand.

Returns:
  The expanded help text.
r   r-   Nr   )commandman_nametop_commandparent_commandgrandparent_commandr|   )	r	   
LazyFormatr   r   r   rK   r   r   r   )r)   texts     r   _ExpandHelpText!MarkdownGenerator._ExpandHelpText  s       
	""IIxx 2 23B 78HHT%7%7%<=mm	 ..	 	r   c                     U R                   cS  [        R                  " U R                  5       U R                  U R
                  U R                  5      u  U l         U l        gg)z*Sets self._arg_sections in document order.N)r   r   GetArgSectionsr   r   r   r   r   r(   s    r   _SetArgSections!MarkdownGenerator._SetArgSections  sQ    !/9/H/H




,,
--

"
"	0,d$, "r   c                     [        U5      S::  a  U/ 4$ SnUnU[        U5      ::  a4  US-  nU R                  XU 5      (       d  US-  nOU[        U5      ::  a  M4  USU XS 4$ )zSplits cmd into command and args lists.

The command list part is a valid command and the args list part is the
trailing args.

Args:
  cmd: [str], A command + args list.

Returns:
  (command, args): The command and args lists.
r   N)rH   r   )r)   r   skipr@   s       r   _SplitCommandFromArgs'MarkdownGenerator._SplitCommandFromArgs  s{     3x1}"WnDA
s3x-1fa  !--	Q	 s3x-
 r7CGr   c                     [         R                  [         R                  -   U-   [         R                  -   [         R                  -   $ )zReturns msg with user input markdown.

Args:
  msg: str, The user input string.

Returns:
  The msg string with embedded user input markdown.
)r   MARKDOWN_CODEMARKDOWN_ITALIC)r)   msgs     r   
_UserInputMarkdownGenerator._UserInput  sH     	


	
	 

	 

		r   c                 ,    UR                   (       a  S$ S$ )z'Returns the argument type name for arg.
positionalflag)is_positional)r)   args     r   _ArgTypeNameMarkdownGenerator._ArgTypeName-  s    ,,<8&8r   c                 (    U R                   [        ;   $ )z2Returns True if the command is the gcloud command.)r   _GCLOUD_ROOT_SURFACESr(   s    r   _IsGcloudSurfaceCommand)MarkdownGenerator._IsGcloudSurfaceCommand1  s    !666r   c                     U R                   R                  U5        U R                  SR                  US95        U(       a  U R                  S5        gg)zPrints the section header markdown for name.

Args:
  name: str, The manpage section name.
  sep: boolean, Add trailing newline.
z

## {name}
)namer8   N)r   addr   format)r)   r   seps      r   PrintSectionHeader$MarkdownGenerator.PrintSectionHeader5  sF     	t$II&&D&12

iio r   c                    [         R                  " 5       (       a  gU(       d  U R                  S5        [        R                  n[        R
                  nU R                  R                  5       (       d  U R                  5       (       a.  U U R                   U SU [         R                  " 5        U S3nO-U U R                   U SU [         R                  " 5        U S3nU R                  U5        U R                  S5        g)zPrints the command line information section.

The information section provides disclaimer information on whether a command
is available in a particular universe domain.

Args:
  disable_header: Disable printing the section header if True.
NINFORMATIONz! is supported in universe domain z; however, some of the values used in the help text may not be available. Command examples may not work as-is and may requires changes before execution.z% is not available in universe domain r   UNIVERSE ADDITIONAL INFO)r   IsDefaultUniverser   r   r   r   _commandIsUniverseCompatibler   r   GetUniverseDomainr   PrintSectionIfExists)r)   disable_headercodeem	info_bodys        r   PrintUniverseInformationSection1MarkdownGenerator.PrintUniverseInformationSectionA  s     ##%%
m,D			B}}))++t/K/K/M/MF4%%&tf,MD--/0 5JJ  F4%%&tf -D!=!=!? @AG  	IIi 	89r   c                     U(       d  U R                  S5        U R                  SR                  U R                  [	        U R
                  5      S95        g)zlPrints the command line name section.

Args:
  disable_header: Disable printing the section header if True.
NAMEz{command} - {index}
)r   r|   N)r   r   r   r   r   r   r)   r   s     r   PrintNameSection"MarkdownGenerator.PrintNameSectiong  sI     
f%II&&&&&t}}5 	' 	
r   c           
         U R                   (       a  gU R                  5         [        R                  n[        R                  nU(       d  U R                  S5        U R                  SR                  X R                  S95        U R                  (       a8  U R                  (       a'  U R                  SU-   S-   U-   S-   U-   S-   U-   5        OWU R                  (       a  U R                  SU-   S-   U-   5        O+U R                  (       a  U R                  SU-   S-   U-   5        / nU R                   HD  nU R                  S5        U R                  [        R                  " UR                  S	S	US
95        MF     U R                  (       a8  U R                  SU-   U R                   R#                  5       -   S-   U-   S-   5        U(       a1  U R                  S5        U R                  SR%                  U5      5        U R                  S5        g)zpPrints the command line synopsis section.

Args:
  disable_header: Disable printing the section header if True.
NSYNOPSISz{code}{command}{code})r   r   r-   GROUPz | COMMANDT)markdowntopremainder_usagez [z_WIDE_FLAG ...r   r8   )r   r   r   r   r   r   r   r   r   r   r   r   r   GetArgUsager   r   r   upperrK   )r)   r   r   r   r  sections         r   PrintSynopsisSection&MarkdownGenerator.PrintSynopsisSectionv  s    }}D			B
j)II&&D:L:L&M T__
iib7"R'%/"4y@2EF			
iib9$r)*	
iib7"R'( O%%
iin
ii

 
 ll-	 & 
iir	DIIOO--0@@2EKL
iin
ii)*IIdOr   c                    [         R                  " USSS9nU(       d  gU R                  SR                  USU[        -   -  S95        UR
                  (       a-  U(       a&  U(       d  SR                  U R                  U5      S9nOS	nU R                  XS
9R                  SS5      nU R                  SR                  XeS95        g)z>Prints a positional or flag arg definition list item at depth.T)
definitionr
  N
{usage}{depth}
:usagedepthzh
+
This {arg_type} argument must be specified if any of the other arguments in this group are specified.)arg_typer   r  


+
z
{details}{modal}
)detailsmodal)	r   r  r   r   _SECOND_LINE_OFFSETis_requiredr   GetArgDetailsreplace)r)   r   r  singler  r  r  s          r   _PrintArgDefinition%MarkdownGenerator._PrintArgDefinition  s    ""34$GEII##se.A&AB 	$ 	

 53))#./ 
 e   2::67KGII$++G+IJr   c                    UR                   (       a"  [        UR                  [        R                  S9OUR                  n/ nUR
                  (       d"  UR                  (       d  UR                  (       a  UR
                  (       a  UR                  UR
                  5        UR                  (       a  O[        U5      S:X  d  US   R                  (       a#  UR                  (       a  UR                  S5        OhUR                  (       a5  UR                  (       a  UR                  S5        O4UR                  S5        O"UR                  (       a  UR                  S5        UR                  (       dX  U(       aQ  U R                  SR                  S	U[        -   -  S
R                  U5      5      R!                  SS
5      5        SnUS-  nU H  nUR                  (       a  M  UR"                  (       a5  Sn[        R$                  " U5      nU(       a  UR
                  (       d  UnOSnUR"                  (       a  U R'                  XbUS9  M  U R)                  XbUS9  M     g)z-Prints an arg group definition list at depth.)keyr   r   zThis must be specified.z'Exactly one of these must be specified:z&At most one of these can be specified:z(At least one of these must be specified:z

{0} {1}

r  r  r  NFT)r  r#  )	sort_argssorted	argumentsr   GetArgSortKeyhelpis_mutexr   rJ   disable_default_headingrH   r   r   r   r  rK   r"  r   GetSingleton_PrintArgGroupr$  )r)   r   r  r#  r   headinga	singletons           r   r0   MarkdownGenerator._PrintArgGroup  s    == 	s}}*":":;]] 	
 G
xx3<<3??	sxx 		$	$t9>T!W00??
..2
3<<??
..B
C
..A
B??AB==W
ii

 
 U0017<<3HGFG$
 gqje	
	
++A.	AF	
A6:   ? r   c           	          U R                  SR                  [        R                  " USS9SU[        -   -  S95        U R                  SR                  U R                  U5      S95        g )Nr  Tr
  r  r  
{arghelp}
arghelp)r   r   r   GetPositionalUsager  r!  r)   r   r  s      r   PrintPositionalDefinition+MarkdownGenerator.PrintPositionalDefinition  sf    II##//dC!445 	$ 	
 	IIo$$T-?-?-D$EFr   c           	         U(       d  U R                  S5        U R                  SR                  [        R                  " USS9SU[        -   -  S95        U R                  SR                  U R                  U5      S95        g	)
zPrints a flags definition list item.

Args:
  flag: The flag object to display.
  disable_header: Disable printing the section header if True.
  depth: The indentation depth at which to print arg help text.
r8   z{usage}{depth}
Tr6  r  r  r7  r8  N)r   r   r   GetFlagUsager  r!  )r)   r   r   r  s       r   PrintFlagDefinition%MarkdownGenerator.PrintFlagDefinition  sv     
iioII!!))$>!445 	" 	
 	IIo$$T-?-?-E$FGr   c                 T    U(       d  U R                  USS9  U R                  U5        g)zPrints a flag section.

Args:
  heading: The flag section heading name.
  arg: The flag args / group.
  disable_header: Disable printing the section header if True.
Fr   N)r   r0  )r)   r1  r   r   s       r   PrintFlagSection"MarkdownGenerator.PrintFlagSection  s(     
g51r   c           	         U R                   (       a  gU R                  5         U R                   H(  nU R                  UR                  UR
                  US9  M*     U R                  (       a  U(       d7  U R                  SR                  U R                  R                  5       5      SS9  U R                  SR                  SR                  [        U R                  5      5      U R                  5      5        gg)zqPrints the positionals and flags sections.

Args:
  disable_header: Disable printing the section header if True.
Nr   z{} WIDE FLAGSFrC  zN
These flags are available to all commands: {}.

Run *$ {} help* for details.
z, )r   r   r   rD  r1  r   r   r   r   r   r  r   rK   r)  r)   r   r  s      r    PrintPositionalsAndFlagsSections2MarkdownGenerator.PrintPositionalsAndFlagsSections  s     }} %%

//7<<   &
 ""499??#455 	  	
 ii//5viit1123TYY0 r   c                 ^    U R                   (       a  U R                  SU R                   US9  gg)zzPrints the subgroup section if there are subgroups.

Args:
  disable_header: Disable printing the section header if True.
r  rG  N)r   PrintCommandSectionr  s     r   PrintSubGroups MarkdownGenerator.PrintSubGroups-  s.     

4??>   r   c                     U R                   (       aJ  U R                  (       a  U R                  SU R                   SUS9  gU R                  SU R                   US9  gg)z~Prints the subcommand section if there are subcommands.

Args:
  disable_header: Disable printing the section header if True.
TOPICT)r   r   r	  rG  N)r   r   rL  r  s     r   PrintSubCommands"MarkdownGenerator.PrintSubCommands8  s`     	  )	 	! 	
 	  t(( 	! 	
 r   Nc                 N   XR                   ;   a  gU R                  R                  X5      nU(       d  g[        U5      (       a  U" 5       nOUnU(       d  U R	                  U5        U R                  SR                  [        R                  " U5      R                  5       S95        g)zPrint a section name if it exists.

Args:
  name: str, The manpage section name.
  default: str, Default help_stuff if section name is not defined.
  disable_header: Disable printing the section header if True.
Nz
{message}
)message)
r   r   getcallabler   r   r   textwrapdedentr   )r)   r   defaultr   
help_stuffhelp_messages         r   r   &MarkdownGenerator.PrintSectionIfExistsK  s     %%%##D2J
\ll
d#IIX__\%B%H%H%JKr   c                     [        U R                  SS/-   5      n[        U R                  5       H0  nUR	                  5       (       d  M  X2;  d  M!  U R                  X1S9  M2     g)zPrint extra sections not in excluded_sections.

Extra sections are sections that have not been printed yet.
PrintSectionIfExists() skips sections that have already been printed.

Args:
  disable_header: Disable printing the section header if True.
NOTESr   rG  N)r   r   r)  r   isupperr   )r)   r   excluded_sectionsr  s       r   PrintExtraSections$MarkdownGenerator.PrintExtraSectionsb  s[     )CDD $..)			w?!!'!I *r   c                 f    U R                    H  nU R                  X!S9  M     U R                  US9  g)ziPrint the final sections in order.

Args:
  disable_header: Disable printing the section header if True.
rG  N)r   r   PrintNotesSectionrH  s      r   PrintFinalSections$MarkdownGenerator.PrintFinalSectionsr  s6     ''
G (.9r   c                 
   Sn[        [        R                  " U5      5       Hd  u  pgU R                  (       d  UR                  (       a  M)  USR                  SR                  U R                  U/-   5      UUR                  S9-  nMf     U(       as  U(       d  U R                  US-   5        U(       a  U R                  S5        O-U R                  SR                  U R                  U5      S95        U R                  U5        g	g	)
zPrints a group or command section.

Args:
  name: str, The section name singular form.
  subcommands: dict, The subcommand dict.
  is_topic: bool, True if this is a TOPIC subsection.
  disable_header: Disable printing the section header if True.
r   z
*link:{ref}[{cmd}]*::

{txt}
r5   )r   r   txtSz#The supplementary help topics are:
z{cmd} is one of the following:
)r   N)r)  six	iteritemsr   r   r   rK   r   	help_textr   r   r   )r)   r   subcommandsr   r   content
subcommand	help_infos           r   rL  %MarkdownGenerator.PrintCommandSection|  s     G!'k(B!C
		 3 3 3 	7>>++zl:;## ? 
 	
	 "D s
+			89		.55$//$:O5P	
 ii r   c                 8    U R                   R                  S5      $ )z,Returns the explicit NOTES section contents.r^  )r   rU  r(   s    r   GetNotesMarkdownGenerator.GetNotes  s    >>g&&r   c                     U R                  5       nU(       a5  U(       d  U R                  S5        U(       a  U R                  US-   5        ggg)zjPrints the NOTES section if needed.

Args:
  disable_header: Disable printing the section header if True.
r^  r  N)rs  r   r   )r)   r   notess      r   rd  #MarkdownGenerator.PrintNotesSection  s@     MMOE(			%&.! 
 r   c           	          [        USS5      (       a/  [        SR                  U R                  [        US5      5      5      e[        R
                  " XS9$ )z4Returns the detailed help message for the given arg.detailed_helpNzA{}: Use add_argument(help=...) instead of detailed_help="""{}""".r  )getattr
ValueErrorr   r   r   r!  r;  s      r   r!  MarkdownGenerator.GetArgDetails  sM    sOT**
M6$$$gc?&CD  ##C55r   c           	         U R                  U5      n[        U5      n[        R                  " S[        [
        -
  [        -
  -  [        R                  5      nSnSn UR                  X5      nU(       d  ObXAX5R                  S5       [        5       R                  XR                  S5      UR                  S5       5      -   -  nUR                  S5      nM{  U(       a  XAUS -   nU$ )zExpand {...} references in doc.z^ *(\$ .{%d,})$r   r   r   N)r   rq   r   rY   r"   r$   r#   rZ   r\   r]   r   rN   r^   )r)   r`   rz   rk   repr   s         r   _ExpandFormatReferences)MarkdownGenerator._ExpandFormatReferences  s    


s
#C
!#
&C **f}4FGC C
C
jj"e	{{1~&)C)E)K)K
kk!nuyy|
,*  c IIaLc  cd)OcJr   c                 \    SR                  U5      SR                  U R                  5      :g  $ )Nr   )rK   r   )r)   r   s     r   _IsNotThisCommand#MarkdownGenerator._IsNotThisCommand  s&     88C=CHHT%7%7888r   c                    SnSn UR                  X5      nU(       d  OU R                  UR                  S5      R                  S5      5      u  pxU R	                  XxUS9n	U R                  U5      (       a   U	(       a  XQXFR                  S5       U	-   -  nOXQXFR                  S5       -  nXQUR                  S5      UR                  S5       -  nUR                  S5      nM  U(       a  XQUS -   nU$ )a  Build a representation of a doc, finding all command examples.

Finds examples of both inline commands and commands on their own line.

Args:
  doc: str, the doc to find examples in.
  pat: the compiled regexp pattern to match against (the "command" match
    group).
  with_args: bool, whether the examples are valid if they also have args.

Returns:
  (str) The final representation of the doc.
r   r   r   r-   r   r^   N)r\   r   r   splitr   r  r]   r^   )
r)   r`   rz   r   rk   r~  r   r   r   lnks
             r   _LinkMarkdownMarkdownGenerator._LinkMarkdown  s     C
C
jj"e,,U[[-C-I-I#-NOicsI>c				$	$3Y/0366 	39-..	9%		%(899cIIec  cd)OcJr   c                 \    SR                  [        R                  " U R                  5      S9$ )zRegex to search for inline command examples enclosed in ` or *.

Contains a 'command' group and an 'end' group which will be used
by the regexp search later.

Returns:
  (str) the regex pattern, including a format string for the 'top'
  command.
zy(?<!\n\n)(?<!\*\(ALPHA\)\* )(?<!\*\(BETA\)\* )(?<!\*\(PREVIEW\)\* )([`*])(?P<command>{top}( [a-z][-a-z0-9]*)*)(?P<end>\1)r  r   r   escaper   r(   s    r   InlineCommandExamplePattern-MarkdownGenerator.InlineCommandExamplePattern  s/    	BBH&		$))$ CI C
r   c                     U R                   (       d  U$ [        R                  " U R                  5       5      nU R	                  XSS9nU$ )z-Add ([`*])command ...\1 link markdown to doc.Fr  )r   r   rY   r  r  r)   r`   rz   s      r   _AddCommandLinkMarkdown)MarkdownGenerator._AddCommandLinkMarkdown  sA    j
**T557
8C


S

7CJr   c                 \    SR                  [        R                  " U R                  5      S9$ )zRegex to search for command examples starting with '$ '.

Contains a 'command' group and an 'end' group which will be used
by the regexp search later.

Returns:
  (str) the regex pattern, including a format string for the 'top'
  command.
zU\$ (?P<end>(?P<command>{top}((?: (?!(example|my|sample)-)[a-z][-a-z0-9]*)*))).?[ `\n]r  r  r(   s    r   CommandLineExamplePattern+MarkdownGenerator.CommandLineExamplePattern  s%    &	((.299TYY3G(Hr   c                     U R                   (       d  U$ [        R                  " U R                  5       5      nU R	                  XSS9nU$ )z'Add $ command ... link markdown to doc.Tr  )r   r   rY   r  r  r  s      r   _AddCommandLineLinkMarkdown-MarkdownGenerator._AddCommandLineLinkMarkdown'  sA    j
**T335
6C


S

6CJr   c                    U R                   (       d  U$ [        R                  " SU R                  -   S-   5      nSnSn UR	                  X5      nU(       d  OlUR                  S5      R                  SS5      nUR                  SS5      nS	U-   S
-   U-   S-   nXAX5R                  S5       U-   -  nUR                  S5      nM  U(       a  XAUS -   nU$ )z/Add <top> ...(1) man page link markdown to doc.z(\*?(z(?:[-_ a-z])*)\*?)\(1\)r   r   r   r   r-   r5   z*link:r   z]*r   N)	r   r   rY   r   r\   r   r"  r]   r^   )	r)   r`   rz   rk   r~  r   r   r   r  s	            r   _AddManPageLinkMarkdown)MarkdownGenerator._AddManPageLinkMarkdown/  s    j
**X		),FF
GC
C
C
jj"eKKN""3,cKKS!csNS 3&-c	{{1~&,,cIIaLc  cd)OcJr   c                    [         R                  " S5      nSnSnUR                  U5       H  n[         R                  " SUR	                  S5      5      (       a!  U R                  UR	                  S5      5      nOUR	                  S5      nXAX5R                  S5       U-   -  nUR                  S5      nM     U(       a  XAUS -   nU$ )z>Change ``.*[[:alnum:]]{2,}.*'' quotes => _UserInput(*) in doc.z[^`](``([^`']*)'')r   r   z\w\wr   r   N)r   rY   finditerr\   r   r   r]   r^   )r)   r`   rz   rk   r~  r   quoted_strings          r   _FixAirQuotesMarkdown'MarkdownGenerator._FixAirQuotesMarkdownC  s     ***
+C
C
Cc"	7EKKN	+	+A7A	{{1~&66cIIaLc # cd)OcJr   c                     [         R                  " 5       (       + =(       a@    [        U R                  [        5      (       + =(       a    U R                  R                  5       $ N)r   r   
isinstancer   dictr   r(   s    r   _IsUniverseCompatible'MarkdownGenerator._IsUniverseCompatibleY  sA    ((** 	14==$//	1MM..0r   c                     U R                  5       (       a+  [        R                  " S[        R                  " 5       U5      nU$ )z-Replace static GDU Links with Universe Links.zcloud.google.com)r  r   r   r
   GetUniverseDocumentDomainr)   r`   s     r   !_ReplaceGDULinksWithUniverseLinks3MarkdownGenerator._ReplaceGDULinksWithUniverseLinks`  s:    
 !!##FF


7
7
9
c Jr   c                    Uc  U R                   R                  5       nU R                  U5      nU R                  U5      nU R	                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU$ )aG  Applies edits to a copy of the generated markdown in doc.

The sub-edit method call order might be significant. This method allows
the combined edits to be tested without relying on the order.

Args:
  doc: The markdown document string to edit, None for the output buffer.

Returns:
  An edited copy of the generated markdown.
)r   getvaluer  r  r  r  r  r  r  s     r   EditMarkdownGenerator.Editn  s     {II c

&
&s
+C

*
*3
/C

&
&s
+C

&
&s
+C

$
$S
)C

0
0
5CJr   c                    U R                  SR                  U R                  R                  5       5      5        U R	                  5         U R                  5         U R                  5         U R                  S5        U R                  S5        U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5       $ )zyGenerates markdown for the command, group or topic, into a string.

Returns:
  An edited copy of the generated markdown.
z	# {0}(1)
DESCRIPTIONr   )r   r   r   r  r   r  r  r   rI  rM  rQ  ra  re  r  r(   s    r   GenerateMarkdownGenerator.Generate  s     	IIl!!$//"7"7"9:;((*m,j)))+99;r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )T)F)r   F)r   )Fr   )NF)FFr  )7rP   rQ   rR   rS   rT   r*   abcabstractmethodr   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r  r  r$  r0  r<  r@  rD  rI  rM  rQ  r   ra  re  rL  rs  rd  r!  r  r  r  r  r  r  r  r  r  r  r  r  r  rU   rV   r   r   r   r   d  sc   "#6 
	 
	 	 	.  " 6 6 % %  (2"97
$:L+ZK(0@dGH$
:	
&.J : ?DB'"6.9
B(.(,,r   r   c                   J   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	rU =r$ )
CommandMarkdownGeneratori  a'  Command help markdown document generator.

Attributes:
  _command: The CommandCommon instance for command.
  _root_command: The root CLI command instance.
  _subcommands: The dict of subcommand help indexed by subcommand name.
  _subgroups: The dict of subgroup help indexed by subcommand name.
c                   > Xl         UR                  5         UR                  5       U l        [        [
        U ]  UR                  5       UR                  5       UR                  5       5        U R                   R                  U l        U R                   R                  U l        U R                  U R                  5        U R                  R!                  SS5      U R                  S'   U R                  R#                  [%        U R                   S0 5      5        UR'                  5       U l        UR+                  5       U l        UR.                  R0                  U l        g)zConstructor.

Args:
  command: A calliope._CommandCommon instance. Help is extracted from this
    calliope command, group or topic.
r  r   descriptionry  N)r   LoadAllSubElements_TopCLIElement_root_commandsuperr  r*   GetPathr   IsHidden
short_helpr   	long_helpr   _ExtractSectionsFromDocstringr   rU  updaterz  GetSubCommandHelpsr   GetSubGroupHelpsr   air(  r   )r)   r   	__class__s     r   r*   !CommandMarkdownGenerator.__init__  s     M  //1D	
"D27//173C3C3E MM,,DMmm--DO&&t7$(NN$6$6}b$IDNN=!NN'$--"EF224D..0DO '

 4 4Dr   c                     U(       a"  US   (       d  USS nU(       a  US   (       d  M  U(       a"  US   (       d  USS nU(       a  US   (       d  M  U(       a  SR                  U5      U R                  U'   gg)zvSets section name help composed of lines.

Args:
  name: The section name.
  lines: The list of lines in the section.
r   r   Nr   r8   )rK   r   )r)   r   rL   s      r   _SetSectionHelp(CommandMarkdownGenerator._SetSectionHelp  sg     aABie a b	CRje b		!YYu-dnnT r   c                 @   Sn/ n[         R                  " U5      R                  5       R                  5        HS  n[	        U5      S:  a0  UR                  S5      (       a  U R                  X#5        USS n/ nMB  UR                  U5        MU     U R                  X#5        g)z1Extracts section help from the command docstring.r     z## r   N)rW  rX  r   
splitlinesrH   
startswithr  rJ   )r)   	docstringr   rL   r:   s        r   r  6CommandMarkdownGenerator._ExtractSectionsFromDocstring  s    DE	*002==?	TaDOOE22T)ABxT @ 	%r   c                 8    U R                   R                  U5      $ )zCReturns True if the given sub command path is valid from this node.)r  r   r   s     r   r   'CommandMarkdownGenerator.IsValidSubPath  s    ,,-=>>r   c                 B    U R                   R                  R                  $ )r   )r   r  r*  r(   s    r   r   %CommandMarkdownGenerator.GetArguments  s    ==%%%r   c                 j    U R                   R                  U R                  R                  S5      5      $ )z?Returns the explicit and auto-generated NOTES section contents.r^  )r   GetNotesHelpSectionr   rU  r(   s    r   rs  !CommandMarkdownGenerator.GetNotes  s&    ==,,T^^-?-?-HIIr   )r   r   r   r  r   r   r   )rP   rQ   rR   rS   rT   r*   r  r  r   r   rs  rU   __classcell__)r  s   @r   r  r    s-    5.. &?&J Jr   r  c                 4    [        U 5      R                  5       $ )zGenerates and returns the help markdown document for command.

Args:
  command: The CommandCommon command instance.

Returns:
  The markdown document string.
)r  r  )r   s    r   Markdownr    s     
"'	*	3	3	55r   )&rT   
__future__r   r   r   r  r   r   rW  googlecloudsdk.callioper   r   googlecloudsdk.corer   googlecloudsdk.core.consoler	   'googlecloudsdk.core.universe_descriptorr
   rj  r"   r#   r$   r9   r  	frozensetr   r   objectr   rq   r_   r}   rs   with_metaclassABCMetar   r  r  rV   r   r   <module>r     s     = &  ' 
 	 	  ( . * 2 G 
 
  ! #  .d dNDQN5"p-`r**3;;? rjIJ0 IJX	6r   