o
    i\#                     @   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 )@zY
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	
  }zt   t	jd  d  W n tyI   t	jd  d	  Y nw |rR|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)
    PANDOCFILTER_CLEANUP)prefixc                      s
   t  S N)shutilrmtree imagedirr   H/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/pandocfilters.py<lambda>.   s   
 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   s6   g }| D ]\}}||kr|}q| ||g q||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u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/   typefr)   r*   r   r   r
   get_captionE   s   


r2   c                 K   s"   z||  W S  t y   | Y S w )zget the extension for the result, needs a default and some specialisations

    Example:
      filetype = get_extension(format, "png", html="svg", latex="eps")
    )KeyError)formatdefault
alternatesr   r   r
   get_extensionY   s
   
r7   c              	      s   t | trag }| D ]U}t |trTd|v rT |d d|v r |d nd}|du r4|t|  q	t |trI|D ]}|t|  q;q	|t|  q	|t|  q	|S t | trs fdd|  D S | 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,   actionr4   metar   r
   
<dictcomp>   s     zwalk.<locals>.<dictcomp>)
isinstancelistdictr&   r:   items)xr=   r4   r>   arrayitemr*   zr   r<   r
   r:   g   s&   


r:   c                 C   s   t | g dS )zALike `toJSONFilters`, but takes a single action as argument.
    N)toJSONFilters)r=   r   r   r
   toJSONFilter   s   rI   c                 C   sx   zt jtjjdd}W n ty   tdtj}Y nw | }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)actionsinput_streamsourcer4   r   r   r
   rH      s   rH   r.   c                 C   s\   t |}d|v r|d }n|d r|d d }ni }|}| D ]	}t||||}q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.
    r>   r   unMeta)jsonloadsr:   dumps)rX   rZ   r4   docr>   alteredr=   r   r   r
   rW      s   


rW   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v r  | d S | dkr  |d  d S | dkr%  |d  d S | dkr0  d d S | dkr;  d d S | dkrF  d d S d S )	N)r0   
MetaStringCoderK   Math	LineBreak 	SoftBreakSpacer%   )r(   valr4   r>   resultr   r
   go   s   zstringify.<locals>.gor.   )r:   r   )rD   rk   r   ri   r
   	stringify   s   
rl   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 )rn   rm   r   )r;   rD   attrsr   r
   
<listcomp>   s   ( zattributes.<locals>.<listcomp>)get)rp   identrn   keyvalsr   ro   r
   
attributes   s
   
ru   c                    s    fdd}|S )Nc                     sf   t | }|krt d t d t| dkrg }nt | dkr*| d }nt| } |dS )Nz	 expects z arguments, but given r   rK   )r8   r9   )rT   
ValueErrorstrrA   )argslenargsxseltTypenumargsr   r
   fun   s   

zelt.<locals>.funr   )r|   r}   r~   r   r{   r
   elt   s   r   PlainrK   Para	CodeBlock   RawBlock
BlockQuoteOrderedList
BulletListDefinitionListHeader   HorizontalRuleTable   DivNullr0   EmphStrong	StrikeoutSuperscript	Subscript	SmallCapsQuotedCiterb   rg   rd   rc   	RawInlineLinkImageNoterf   Spanr   )r.   )5__doc__rQ   r   rL   r\   r   r   r   r   r   r$   r-   r2   r7   r:   rI   rH   rW   rl   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   r   r   r   r   r   r   r   r   rb   rg   rd   rc   r   r   r   r   rf   r   r   r   r   r
   <module>   sj   

#
*
!"






























