
                            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 ddl	m
Z
 ddlZddlmZ  e ed	            \  ZZZ G d
 de      Z G d de      Z ej*                  ej,                         G d de             Zy)z0Cloud SDK markdown document renderer base class.    )absolute_import)division)unicode_literalsN)log)resource_printer)range   c                       e Zd ZdZddZy)TableColumnAttributeszMarkdown table column attributes.

  Attributes:
    align: Column alignment, one of {'left', 'center', 'right'}.
    label: Column heading label string.
    width: Minimum column width.
  Nc                 .    || _         || _        || _        y Nalignlabelwidthselfr   r   r   s       6lib/googlecloudsdk/core/document_renderers/renderer.py__init__zTableColumnAttributes.__init__,   s    DJDJDJ    )leftNr   )__name__
__module____qualname____doc__r    r   r   r   r   #   s    r   r   c                   (    e Zd ZdZddZddZddZy)	TableAttributeszMarkdown table attributes.

  Attributes:
    box: True if table and rows framed by box.
    columns: The list of column attributes.
    heading: The number of non-empty headings.
    margin: Extra margin to handle post-processing indent.
  c                 .    || _         d| _        g | _        y )Nr   )boxheadingcolumns)r   r    s     r   r   zTableAttributes.__init__<   s    DHDLDLr   c                     |r| xj                   dz  c_         | j                  j                  t        |||             y)z-Adds the next column attributes to the table.   r   N)r!   r"   appendr   r   s       r   	AddColumnzTableAttributes.AddColumnA   s1    
llalLLEeDr   c           
      V   dg}g }| j                   r|j                  d       | j                  s|j                  d       |r |j                  dj                  |             |r |j                  dj                  |             |r&|j                  ddj	                  |      z   dz          |j                  d	       t        | j                        D ]  \  }}|r|j                  d       |j                  d
j                  |t        |j                  xs d      j                  d      |j                               |j                  sx|j                  dj                  |j                                |r|j                  d       |j                  d       dj	                  |      S )zConstructs and returns a resource_printer print format.

    Args:
      margin: Right hand side padding when one or more columns are wrapped.
      width: The table width.

    Returns:
      The resource printer format string.
    tabler    z
no-headingz	margin={}zwidth={}[,](z[{}]:label={}:align={} uz	:width={}z:wrap))r    r%   r!   formatjoin	enumerater"   reprr   lstripr   r   )r   marginr   fmtattrindexcolumns          r   GetPrintFormatzTableAttributes.GetPrintFormatI   sG    )CDxx
kk%<<
kk,
kk+$$V,-
kk*##E*+	jjsxx~%+,JJsO"4<<0v	

3	jj
"
)
)T&,,,"-44S96<<
 


;%%fll34 1 	jjJJsO773<r   N)F)r   r-   r   )r   r   )r   r   r   r   r   r&   r:   r   r   r   r   r   2   s    
$r   r   c                       e Zd ZdZ	 	 	 	 	 ddZed        Zd Zd Zd Z	d Z
d	 Zd
 ZddZd Zd Zd Zd Zd ZddZd Zy)Renderera}  Markdown renderer base class.

  The member functions provide an abstract document model that matches markdown
  entities to output document renderings.

  Attributes:
    _blank: True if the output already contains a blank line. Used to avoid
      sequences of 2 or more blank lines in the output.
    _command: The command split into component names.
    _font: The font attribute bitmask.
    _indent: List of left indentations in characters indexed by _level.
    _lang: ```lang\n...\n``` code block language. None if not in code block, ''
      if in code block with no explicit lang specified.
    _level: The section or list level counting from 0.
    _out: The output stream.
    _title: The document title.
    _width: The output width in characters.
    command_metadata: Optional metadata of command.
    command_node: The command object that the document is being rendered for.
  Nc                     d| _         dg| _        d| _        g | _        d | _        d| _        |xs t        j                  | _        || _	        || _
        || _        || _        y )NTgcloudr   )_blank_command_font_indent_lang_levelr   out_out_title_widthcommand_metadatacommand_node)r   rE   titler   rI   rJ   s         r   r   zRenderer.__init__   s]     DKJDMDJDLDJDKswwDIDKDK,D$Dr   c                     | j                   S )z/Returns the command split into component names.r@   r   s    r   commandzRenderer.command   s     ==r   c                     d| _         y)zThe last output line is blank.TNr?   rN   s    r   BlankzRenderer.Blank   s	    DKr   c                     d| _         y)z4Some non-blank line content was added to the output.FNrQ   rN   s    r   ContentzRenderer.Content   s	    DKr   c                     | j                   S )z.Returns True if the last output line is blank.rQ   rN   s    r   	HaveBlankzRenderer.HaveBlank   s    ;;r   c                     |S )zConverts special characters to their entity tags.

    This is applied after font embellishments.

    Args:
      buf: The normal text that may contain special characters.

    Returns:
      The escaped string.
    r   r   bufs     r   EntitieszRenderer.Entities   	     Jr   c                     |S )zEscapes special characters in normal text.

    This is applied before font embellishments.

    Args:
      buf: The normal text that may contain special characters.

    Returns:
      The escaped string.
    r   rX   s     r   EscapezRenderer.Escape   r[   r   c                      y)z'Finishes all output document rendering.Nr   rN   s    r   FinishzRenderer.Finish   s    r   c                      y)a  Returns the font embellishment string for attr.

    Args:
      unused_attr: None to reset to the default font, otherwise one of BOLD,
        ITALIC, or CODE.
      unused_out: Writes tags line to this stream if not None.

    Returns:
      The font embellishment string.
    r-   r   )r   unused_attr
unused_outs      r   FontzRenderer.Font   s     r   c                     || _         y)zfSets the document command name.

    Args:
      command: The command split into component names.
    NrM   )r   rO   s     r   
SetCommandzRenderer.SetCommand   s     DMr   c                     || _         y)zSets the ```...``` code block language.

    Args:
      lang: The language name, None if not in a code block, '' is no explicit
        language specified.
    N)rC   )r   langs     r   SetLangzRenderer.SetLang   s     DJr   c                      y)z$Renders a paragraph separating line.Nr   rN   s    r   LinezRenderer.Line   s    r   c                 D    |r|rd|v rdj                  ||      S |S |r|S y)zRenders 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://z	{0} ({1})z[]())r0   )r   targettexts      r   LinkzRenderer.Link   s3     	EVO!!$//kmr   c                     |S )zAdd global flags links to line if any.

    Args:
      line: The text line.

    Returns:
      line with annoted global flag links.
    r   )r   lines     r   LinkGlobalFlagszRenderer.LinkGlobalFlags   s	     Kr   c                 L    | j                   j                  |dz  |z   dz          y)zAdds an indented table line to the output.

    Args:
      line: The line to add. A newline will be added.
      indent: The number of characters to indent the table.
     
N)rF   write)r   rp   indents      r   	TableLinezRenderer.TableLine
  s"     	IIOOFSL4'$./r   c                    | j                          | j                  | j                     j                  dz   }t	        |D cg c]  }d|d   v sd c}      r|nd}t        j                         }t        j                  ||j                  || j                        |       |j                         j                  d      d	d D ]  }| j                  ||
        | j                          | j                          y	c c}w )zRenders a table.

    Nested tables are not supported.

    Args:
      table: A TableAttributes object.
      rows: A list of rows where each row is a list of column strings.
       rs   Tr   )r5   r   )rE   rt   N)rv   )rj   rB   rD   rv   anyioStringIOr   Printr:   rH   getvaluesplitrw   rT   )r   r(   rowsrv   rr5   rY   rp   s           r   TablezRenderer.Table  s     	IIK\\$++&--1F$?$Q#2,D$?@VaF
++-Ce""&"D# $$T*3B/
nnT&n) 0LLNIIK @s    C>C>)NNP   NNr   )r   )r   r   r   r   r   propertyrO   rR   rT   rV   rZ   r]   r_   rc   re   rh   rj   rn   rq   rw   r   r   r   r   r<   r<   p   sw    . %(  	&	0r   r<   )r   
__future__r   r   r   abcr|   googlecloudsdk.corer   googlecloudsdk.core.resourcer   six	six.movesr   listBOLDITALICCODEobjectr   r   add_metaclassABCMetar<   r   r   r   <module>r      s     7 &  ' 
 	 # 9 
  %(^ fdF ;f ;| 3;;uv u  ur   