o
                         @   s   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 deZG dd deZeejG dd deZdS )z0Cloud SDK markdown document renderer base class.    )absolute_import)division)unicode_literalsN)log)resource_printer)range   c                   @   s   e Zd ZdZdddZdS )TableColumnAttributeszMarkdown table column attributes.

  Attributes:
    align: Column alignment, one of {'left', 'center', 'right'}.
    label: Column heading label string.
    width: Minimum column width.
  leftNr   c                 C   s   || _ || _|| _d S Nalignlabelwidthselfr   r   r    r   L/tmp/google-cloud-sdk/lib/googlecloudsdk/core/document_renderers/renderer.py__init__,      
zTableColumnAttributes.__init__)r
   Nr   )__name__
__module____qualname____doc__r   r   r   r   r   r	   #   s    r	   c                   @   s.   e Zd ZdZdddZddd	Zdd
dZdS )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.
  Fc                 C   s   || _ d| _g | _d S )Nr   )boxheadingcolumns)r   r   r   r   r   r   <   r   zTableAttributes.__init__r
    r   c                 C   s,   |r	|  j d7  _ | jt|||d dS )z-Adds the next column attributes to the table.   r   N)r   r   appendr	   r   r   r   r   	AddColumnA   s
   zTableAttributes.AddColumnc              	   C   s   dg}g }| j r|d | js|d |r|d| |r)|d| |r7|dd| d  |d	 t| jD ]*\}}|rL|d |d
|t|jpVd	d|j
 |jrk|d|j qA|rs|d |d d|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={}r   uz	:width={}z:wrap))r   r    r   formatjoin	enumerater   reprr   lstripr   r   )r   marginr   fmtattrindexcolumnr   r   r   GetPrintFormatI   s8   







zTableAttributes.GetPrintFormatN)F)r
   r   r   )r   r   )r   r   r   r   r   r!   r3   r   r   r   r   r   2   s
    
	
r   c                   @   s   e Zd ZdZ					d%ddZedd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zd&ddZdd Zdd Zdd Zdd Zdd Zd'd!d"Zd#d$ ZdS )(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.
  NP   c                 C   sN   d| _ dg| _d| _g | _d | _d| _|ptj| _|| _	|| _
|| _|| _d S )NTgcloudr   )_blank_command_font_indent_lang_levelr   out_out_title_widthcommand_metadatacommand_node)r   r=   titler   rA   rB   r   r   r   r      s   
zRenderer.__init__c                 C      | j S )z/Returns the command split into component names.r8   r   r   r   r   command   s   zRenderer.commandc                 C   
   d| _ dS )zThe last output line is blank.TNr7   rF   r   r   r   Blank      
zRenderer.Blankc                 C   rH   )z4Some non-blank line content was added to the output.FNrI   rF   r   r   r   Content   rK   zRenderer.Contentc                 C   rD   )z.Returns True if the last output line is blank.rI   rF   r   r   r   	HaveBlank   s   zRenderer.HaveBlankc                 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   bufr   r   r   Entities      zRenderer.Entitiesc                 C   rN   )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   rO   r   r   r   Escape   rR   zRenderer.Escapec                 C      dS )z'Finishes all output document rendering.Nr   rF   r   r   r   Finish      zRenderer.Finishc                 C   rT   )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_outr   r   r   Font   rR   zRenderer.Fontc                 C   
   || _ dS )zfSets the document command name.

    Args:
      command: The command split into component names.
    NrE   )r   rG   r   r   r   
SetCommand   s   
zRenderer.SetCommandc                 C   rZ   )zSets the ```...``` code block language.

    Args:
      lang: The language name, None if not in a code block, '' is no explicit
        language specified.
    N)r;   )r   langr   r   r   SetLang   s   
zRenderer.SetLangc                 C   rT   )z$Renders a paragraph separating line.Nr   rF   r   r   r   Line   rV   zRenderer.Linec                 C   s,   |r|rd|v rd ||S |S |r|S dS )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[]())r)   )r   targettextr   r   r   Link   s   
zRenderer.Linkc                 C   rN   )zAdd global flags links to line if any.

    Args:
      line: The text line.

    Returns:
      line with annoted global flag links.
    r   )r   liner   r   r   LinkGlobalFlags   s   	zRenderer.LinkGlobalFlagsr   c                 C   s   | j |d | d  dS )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)r>   write)r   rb   indentr   r   r   	TableLine
  s   zRenderer.TableLinec                 C   s   |    | j| j jd }tdd |D r|nd}t }tj||j	|| j
d|d | ddd	 D ]	}| j||d
 q7|   |    dS )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.
       c                 S   s   g | ]
}d |d v rdqS )rd   Tr   ).0rr   r   r   
<listcomp>  s    z"Renderer.Table.<locals>.<listcomp>r   )r.   r   )r=   re   Nrj   )rg   )r^   r:   r<   rg   anyioStringIOr   Printr3   r@   getvaluesplitrh   rL   )r   r"   rowsrg   r.   rP   rb   r   r   r   Table  s   	zRenderer.Table)NNr5   NNr   )r   )r   r   r   r   r   propertyrG   rJ   rL   rM   rQ   rS   rU   rY   r[   r]   r^   ra   rc   rh   ru   r   r   r   r   r4   p   s0    


	
	r4   )r   
__future__r   r   r   abcro   googlecloudsdk.corer   googlecloudsdk.core.resourcer   six	six.movesr   listBOLDITALICCODEobjectr	   r   add_metaclassABCMetar4   r   r   r   r   <module>   s   
>