
    fn#                        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	d2dZ
d2dZd Zd Zd Zd Zd	 Zd3d
Zd Zd Z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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      Z0 ed.d      Z1 ed/d      Z2 ed0d      Z3 ed1d      Z4y)4zY
Functions to aid writing python scripts that process the pandoc
AST serialized as JSON.
    Nc                 T   t        j                  d      r/t        j                  |       t	        j
                  fd       n| dz   t        j                  |j                  t        j                                     j                         }	 t        j                  d       t        j                  j                  dz   dz          |r|d|z   z  }t         j                   j#                  |      S # t        $ r( t        j                  j                  d	z   d
z          Y Zw xY w)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                  .    t        j                         S N)shutilrmtree)imagedirs   H/var/www/cvtools/html/venv/lib/python3.12/site-packages/pandocfilters.py<lambda>z#get_filename4code.<locals>.<lambda>.   s    h 7     z-imagesT)exist_okzCreated directory 
zCould not create directory "z"
.)osgetenvtempfilemkdtempatexitregisterhashlibsha1encodesysgetfilesystemencoding	hexdigestmakedirsstderrwriteOSErrorpathjoin)modulecontentextfnr
   s       @r   get_filename4coder'      s    ( 
yy'(##6278I%	gnnS%>%>%@A	B	L	L	NBL
Ht,

-84?@ 
cCi77<<"%%	  L

7(BUJKLs   <C6 6.D'&D'c                 X    g }| D ]   \  }}||k(  r|}|j                  ||g       " ||fS )z&get value from the keyvalues (options)append)kvkeyvaluereskvs         r   	get_valuer1   ;   sB    
C 18EJJ1v	
 #:r   c                 R    g }d}t        | d      \  }}|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])])
     captionzfig:)r1   Str)r+   r4   typefr-   r.   s        r   get_captionr7   E   sA     GE2z*JE3u:,E3r   c                 0    	 ||    S # t         $ r |cY S w xY 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
alternatess      r   get_extensionr=   Y   s&    &!! s    c                 >   t        | t              rg }| D ]  }t        |t              rd|v r ||d   d|v r|d   nd||      }||j                  t	        ||||             Nt        |t              r%|D ]  }|j                  t	        ||||             ! |j                  t	        ||||             |j                  t	        ||||              |S t        | t              r0| j                         D 	ci c]  \  }}	|t	        |	|||       c}	}S | S c c}	}w )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cN)
isinstancelistdictr*   walkitems)
xactionr:   metaarrayitemr.   zr/   r0   s
             r   rD   rD   g   s   * !T 	?D$%#+T#Y*-+T#Y4O;LLdFFD!ABT*  DT!VVT%BCD LLc664!@AT$=>	? 	At	=>WWYGTQ466400GG Hs   :Dc                     t        | g       y)zALike `toJSONFilters`, but takes a single action as argument.
    N)toJSONFilters)rG   s    r   toJSONFilterrN      s     6(r   c                    	 t        j                  t        j                  j                  d      }|j                         }t        t        j                        dkD  rt        j                  d   }nd}t        j                  j                  t        | ||             y# t
        $ r,  t        j                  d      t        j                        }Y w xY w)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   r3   N)ioTextIOWrapperr   stdinbufferAttributeErrorcodecs	getreaderreadlenargvstdoutr   applyJSONFilters)actionsinput_streamsourcer:   s       r   rM   rM      s    $<''		(8(87K  F
388}q!JJ%gvv>?  < 1v''0;<s   /B 2CCc                     t        j                  |      }d|v r|d   }n|d   r	|d   d   }ni }|}| D ]  }t        ||||      } t        j                  |      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.
    rH   r   unMeta)jsonloadsrD   dumps)r^   r`   r:   docrH   alteredrG   s          r   r]   r]      sq    & **V
C}6{	Q1vhG 6w56 ::gr   c                 P    g fd}t        | |di        dj                        S )z^Walks the tree x and returns concatenated string content,
    leaving out all formatting.
    c                 $   | dv rj                  |       y | dk(  rj                  |d          y | dk(  rj                  |d          y | dk(  rj                  d       y | dk(  rj                  d       y | dk(  rj                  d       y y )	N)r5   
MetaStringCoderQ   Math	LineBreak 	SoftBreakSpacer)   )r,   valr:   rH   results       r   gozstringify.<locals>.go   s    ''MM#F]MM#a&!F]MM#a&!KMM#KMM#G^MM# r   r3   )rD   r"   )rF   rs   rr   s     @r   	stringifyrt      s-     F 	BB776?r   c                     | xs i } | j                  dd      }| j                  dg       }| D cg c]  }|dk7  s	|dk7  s|| |   g }}|||gS c c}w )zJReturns an attribute list, constructed from the
    dictionary attrs.
    idr3   classes)get)attrsidentrw   rF   keyvalss        r   
attributesr|      sg     KREIIdBEii	2&G&+NY1958}NGN7G$$ Os   
AA 	Ac                       fd}|S )Nc                      t        |       }|k7  r)t        dz   t              z   dz   t        |      z         dk(  rg }nt        |       dk(  r| d   }nt        |       }|dS )Nz	 expects z arguments, but given r   rQ   )r?   r@   )rZ   
ValueErrorstrrB   )argslenargsxseltTypenumargss      r   funzelt.<locals>.fun   s|    d)gW{2S\A568;GE F Fa<BY!^aBdB2&&r    )r   r   r   s   `` r   eltr      s    ' Jr   PlainrQ   Para	CodeBlock   RawBlock
BlockQuoteOrderedList
BulletListDefinitionListHeader   HorizontalRuleTable   DivNullr5   EmphStrong	StrikeoutSuperscript	Subscript	SmallCapsQuotedCiterk   rp   rm   rl   	RawInlineLinkImageNotero   Spanr   )r3   )5__doc__rW   r   rR   rc   r   r   r   r   r   r'   r1   r7   r=   rD   rN   rM   r]   rt   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r5   r   r   r   r   r   r   r   r   rk   rp   rm   rl   r   r   r   r   ro   r   r   r   r   <module>r      s  

   	  	 
   !&F(	(T@BD0%" 	GQ
61~Q	z1q!
-#q!
%q)	Xq	%q)GQ	%m
61~ 
%m
61~	Xq	Q	-#Q	Q		Xq	
61~
61~GQQ	
61~Q	
61~GQ
61~Q	
61~r   