B
    u|b\#                 @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	d>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d Zdd ZeddZeddZeddZeddZed dZed!dZed"dZed#dZed$d%Zed&dZed'd(Zed)dZ ed*dZ!ed+dZ"ed,dZ#ed-dZ$ed.dZ%ed/dZ&ed0dZ'ed1dZ(ed2dZ)ed3dZ*ed4dZ+ed5dZ,ed6dZ-ed7dZ.ed8dZ/ed9d%Z0ed:d%Z1ed;dZ2ed<dZ3ed=dZ4dS )AzY
Functions to aid writing python scripts that process the pandoc
AST serialized as JSON.
    Nc                s   t dr*tj| d t fdd n| d  t|t	
  }y"t   t	jd  d  W n( tk
r   t	jd  d	  Y nX |r|d
| 7 }t j |S )a  Generate filename based on content

    The function ensures that the (temporary) directory exists, so that the
    file can be written.

    By default, the directory won't be cleaned up,
    so a filter can use the directory as a cache and
    decide not to regenerate if there's no change.

    In case the user preferres the files to be temporary files,
    an environment variable `PANDOCFILTER_CLEANUP` can be set to
    any non-empty value such as `1` to
    make sure the directory is created in a temporary location and removed
    after finishing the filter. In this case there's no caching and files
    will be regenerated each time the filter is run.

    Example:
        filename = get_filename4code("myfilter", code)
    ZPANDOCFILTER_CLEANUP)prefixc                  s
   t  S )N)shutilrmtree )imagedirr   P/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/pandocfilters.py<lambda>.       z#get_filename4code.<locals>.<lambda>z-imageszCreated directory 
zCould not create directory "z"
.)osgetenvtempfilemkdtempatexitregisterhashlibsha1encodesysgetfilesystemencoding	hexdigestmkdirstderrwriteOSErrorpathjoin)modulecontentextfnr   )r   r   get_filename4code   s    

r"   c             C   s:   g }x,| D ]$\}}||kr |}q
| ||g q
W ||fS )z&get value from the keyvalues (options))append)kvkeyvaluereskvr   r   r   	get_value;   s    r*   c             C   s6   g }d}t | d\}}|dk	r,t|g}d}|||fS )a  get caption from the keyvalues (options)

    Example:
      if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        caption, typef, keyvals = get_caption(keyvals)
        ...
        return Para([Image([ident, [], keyvals], caption, [filename, typef])])
     captionNzfig:)r*   Str)r$   r,   Ztypefr&   r'   r   r   r   get_captionE   s    

r.   c             K   s"   y||  S  t k
r   |S X dS )zget the extension for the result, needs a default and some specialisations

    Example:
      filetype = get_extension(format, "png", html="svg", latex="eps")
    N)KeyError)formatdefaultZ
alternatesr   r   r   get_extensionY   s    r2   c          	      s   t | trg }x| D ]}t |trd|kr |d d|krB|d nd}|dkrj|t|  qt |trx6|D ]}|t|  qzW q|t|  q|t|  qW |S t | tr fdd|  D S | S dS )a  Walk a tree, applying an action to every object.
    Returns a modified tree.  An action is a function of the form
    `action(key, value, format, meta)`, where:

    * `key` is the type of the pandoc object (e.g. 'Str', 'Para') `value` is
    * the contents of the object (e.g. a string for 'Str', a list of
      inline elements for 'Para')
    * `format` is the target output format (as supplied by the
      `format` argument of `walk`)
    * `meta` is the document's metadata

    The return of an action is either:

    * `None`: this means that the object should remain unchanged
    * a pandoc object: this will replace the original object
    * a list of pandoc objects: these will replace the original object; the
      list is merged with the neighbors of the orignal objects (spliced into
      the list the original object belongs to); returning an empty list deletes
      the object
    tcNc                s    i | ]\}}t | |qS r   )walk).0r(   r)   )actionr0   metar   r   
<dictcomp>   s    zwalk.<locals>.<dictcomp>)
isinstancelistdictr#   r5   items)xr7   r0   r8   arrayitemr'   zr   )r7   r0   r8   r   r5   g   s"    




r5   c             C   s   t | g dS )zALike `toJSONFilters`, but takes a single action as argument.
    N)toJSONFilters)r7   r   r   r   toJSONFilter   s    rC   c             C   sz   yt jtjjdd}W n$ tk
r:   tdtj}Y nX | }t	tj
dkr^tj
d }nd}tjt| || dS )aY  Generate a JSON-to-JSON filter from stdin to stdout

    The filter:

    * reads a JSON-formatted pandoc document from stdin
    * transforms it by walking the tree and performing the actions
    * returns a new JSON-formatted pandoc document to stdout

    The argument `actions` is a list of functions of the form
    `action(key, value, format, meta)`, as described in more
    detail under `walk`.

    This function calls `applyJSONFilters`, with the `format`
    argument provided by the first command-line argument,
    if present.  (Pandoc sets this by default when calling
    filters.)
    zutf-8)encoding   r+   N)ioTextIOWrapperr   stdinbufferAttributeErrorcodecs	getreaderreadlenargvstdoutr   applyJSONFilters)actionsZinput_streamsourcer0   r   r   r   rB      s    rB   r+   c             C   s`   t |}d|kr|d }n|d r2|d d }ni }|}x| D ]}t||||}q@W t |S )a  Walk through JSON structure and apply filters

    This:

    * reads a JSON-formatted pandoc document from a source string
    * transforms it by walking the tree and performing the actions
    * returns a new JSON-formatted pandoc document as a string

    The `actions` argument is a list of functions (see `walk`
    for a full description).

    The argument `source` is a string encoded JSON object.

    The argument `format` is a string describing the output format.

    Returns a the new JSON-formatted pandoc document.
    r8   r   ZunMeta)jsonloadsr5   dumps)rR   rS   r0   docr8   Zalteredr7   r   r   r   rQ      s    


rQ   c                s(   g   fdd}t | |di  d S )z^Walks the tree x and returns concatenated string content,
    leaving out all formatting.
    c                s   | dkr  | nj| dkr,  |d  nR| dkrD  |d  n:| dkrX  d n&| dkrl  d n| dkr~  d d S )	N)r-   Z
MetaStringCoderE   Math	LineBreak 	SoftBreakSpace)r#   )r%   valr0   r8   )resultr   r   go   s    zstringify.<locals>.gor+   )r5   r   )r>   r`   r   )r_   r   	stringify   s    ra   c                s<    pi    dd}  dg } fdd D }|||gS )zJReturns an attribute list, constructed from the
    dictionary attrs.
    idr+   classesc                s(   g | ] }|d kr|dkr| | gqS )rc   rb   r   )r6   r>   )attrsr   r   
<listcomp>   s    zattributes.<locals>.<listcomp>)get)rd   identrc   Zkeyvalsr   )rd   r   
attributes   s
    rh   c                s    fdd}|S )Nc                 sf   t | }|kr0t d t d t| dkr>g }nt | dkrT| d }nt| } |dS )Nz	 expects z arguments, but given r   rE   )r3   r4   )rN   
ValueErrorstrr;   )argsZlenargsxs)eltTypenumargsr   r   fun   s    
zelt.<locals>.funr   )rm   rn   ro   r   )rm   rn   r   elt   s    rp   PlainrE   Para	CodeBlock   RawBlock
BlockQuoteOrderedList
BulletListDefinitionListHeader   HorizontalRuleTable   DivNullr-   EmphStrong	StrikeoutSuperscript	Subscript	SmallCapsQuotedCiterX   r]   rZ   rY   	RawInlineLinkImageNoter\   Span)N)N)r+   )5__doc__rK   r   rF   rT   r   r   r   r   r   r"   r*   r.   r2   r5   rC   rB   rQ   ra   rh   rp   rq   rr   rs   ru   rv   rw   rx   ry   rz   r|   r}   r   r   r-   r   r   r   r   r   r   r   r   rX   r]   rZ   rY   r   r   r   r   r\   r   r   r   r   r   <module>   sh   
#

*!
"






























