
    ^hn#                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	S2S jr
S2S jrS rS rS rS rS	 rS3S
 jrS rS rS r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r\" SS5      r \" SS5      r!\" SS5      r"\" S S5      r#\" S!S5      r$\" S"S5      r%\" S#S5      r&\" S$S5      r'\" S%S5      r(\" S&S5      r)\" S'S5      r*\" S(S5      r+\" S)S5      r,\" S*S5      r-\" S+S5      r.\" S,S5      r/\" S-S5      r0\" S.S5      r1\" S/S5      r2\" S0S5      r3\" S1S5      r4g)4zY
Functions to aid writing python scripts that process the pandoc
AST serialized as JSON.
    Nc                 l  ^ [         R                  " S5      (       a/  [        R                  " U S9m[        R
                  " U4S j5        OU S-   m[        R                  " UR                  [        R                  " 5       5      5      R                  5       n [         R                  " TSS9  [        R                  R                  ST-   S-   5        U(       a  USU-   -  n[         R                   R#                  TU5      $ ! [         a(    [        R                  R                  S	T-   S
-   5         N`f = f)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                  0   > [         R                  " T 5      $ N)shutilrmtree)imagedirs   G/home/james-whalen/.local/lib/python3.13/site-packages/pandocfilters.py<lambda>#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   :D /D32D3c                 X    / nU  H   u  pEXA:X  a  UnM  UR                  XE/5        M"     X#4$ )z&get value from the keyvalues (options)append)kvkeyvaluereskvs         r   	get_valuer2   ;   s5    
C8EJJv	 
 :r   c                 P    / nSn[        U S5      u  p4Ub  [        U5      /nSnXU4$ )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:)r2   Str)r,   r5   typefr.   r/   s        r   get_captionr8   E   s=     GE2z*JEu:,3r   c                 0     X    $ ! [          a    Us $ f = f)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                 l   [        U [        5      (       a  / nU  H  n[        U[        5      (       a  SU;   a  U" US   SU;   a  US   OSX#5      nUc  UR                  [	        XQX#5      5        MV  [        U[        5      (       a&  U H  nUR                  [	        XqX#5      5        M      M  UR                  [	        XaX#5      5        M  UR                  [	        XQX#5      5        M     U$ [        U [        5      (       a0  U R                  5        VV	s0 s H  u  pU[	        XX#5      _M     sn	n$ U $ s  sn	nf )aF  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/   zr0   r1   s
             r   rE   rE   g   s   * !TD$%%#+T#Y*-+T#Y4O;LLdF!ABT** T!V%BC ! LLc6!@AT$=>  	At		=>WWYGYTQ4600YGG Hs   D0c                     [        U /5        g)zALike `toJSONFilters`, but takes a single action as argument.
    N)toJSONFilters)rH   s    r   toJSONFilterrO      s     6(r   c                     [         R                  " [        R                  R                  SS9nUR                  5       n[        [        R                  5      S:  a  [        R                  S   nOSn[        R                  R                  [        XU5      5        g! [
         a-    [        R                  " S5      " [        R                  5      n Nf = f)a)  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   r4   N)ioTextIOWrapperr   stdinbufferAttributeErrorcodecs	getreaderreadlenargvstdoutr    applyJSONFilters)actionsinput_streamsourcer;   s       r   rN   rN      s    $<''		(8(87K  F
388}q!JJ%gv>?  < ''0;<s   -B 4CCc                     [         R                  " U5      nSU;   a  US   nOUS   (       a	  US   S   nO0 nUnU  H  n[        XVX$5      nM     [         R                  " U5      $ )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.
rI   r   unMeta)jsonloadsrE   dumps)r_   ra   r;   docrI   alteredrH   s          r   r^   r^      sh    & **V
C}6{	Q1vhGw5  ::gr   c                 P   ^ / mU4S jn[        XS0 5        SR                  T5      $ )zVWalks the tree x and returns concatenated string content,
leaving out all formatting.
c                 2  > U S;   a  TR                  U5        g U S:X  a  TR                  US   5        g U S:X  a  TR                  US   5        g U S:X  a  TR                  S5        g U S:X  a  TR                  S5        g U S:X  a  TR                  S5        g g )	N)r6   
MetaStringCoderR   Math	LineBreak 	SoftBreakSpacer*   )r-   valr;   rI   results       r   gostringify.<locals>.go   s    ''MM#F]MM#a&!F]MM#a&!KMM#KMM#G^MM# r   r4   )rE   r#   )rG   rt   rs   s     @r   	stringifyrv      s+     F 	B776?r   c                     U =(       d    0 n U R                  SS5      nU R                  S/ 5      nU  Vs/ s H  o3S:w  d  M
  US:w  d  M  X0U   /PM     nnXU/$ s  snf )zBReturns an attribute list, constructed from the
dictionary attrs.
idr4   classes)get)attrsidentry   rG   keyvalss        r   
attributesr~      sf     KREIIdBEii	2&G&+NeY}19}8}eGNG$$ Os   	AA	
Ac                    ^ ^ U U4S jnU$ )Nc                     > [        U 5      nUT:w  a)  [        TS-   [        T5      -   S-   [        U5      -   5      eTS:X  a  / nO [        U 5      S:X  a  U S   nO[        U 5      nTUS.$ )Nz	 expects z arguments, but given r   rR   )r@   rA   )r[   
ValueErrorstrrC   )argslenargsxseltTypenumargss      r   fun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   PlainrR   Para	CodeBlock   RawBlock
BlockQuoteOrderedList
BulletListDefinitionListHeader   HorizontalRuleTable   DivNullr6   EmphStrong	StrikeoutSuperscript	Subscript	SmallCapsQuotedCiterl   rq   rn   rm   	RawInlineLinkImageNoterp   Spanr   )r4   )5__doc__rX   r   rS   rd   r   r   r   r   r   r(   r2   r8   r>   rE   rO   rN   r^   rv   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r   r   r   r   r   r   r   r   rl   rq   rn   rm   r   r   r   r   rp   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   