Re:Cog: Use Python in your source files to generate pieces o...
by lvncelot on Thursday December 14, 2023 @04:17AM
(#38639414)
Oh wow that looks really neat. I'm using Pandoc a lot anyways, so this might fit nicely into my workload. Thanks for sharing!
Re:Cog: Use Python in your source files to generate pieces o...
by shoeffner on Thursday December 14, 2023 @09:52AM
(#38641971)
Pandoc even poses codeblock execution as an exercise in their custom-filter documentation: https://pandoc.org/filters.html
The python package panflute is also really nice if you don't want to play around with haskell or the AST JSON directly.
I wrote a script to do this called panpipe: https://github.com/warbo/panpipe
It uses Pandoc, so it supports many formats (I've personally used it with Markdown code fences and with HTML <code> elements). The idea is pretty simple and powerful: if a code block has an attribute/annotation like `pipe="foo"` then that value is executed as a shell command, and the contents of the code block gets piped through it and replaced by the stdout.
This supports running basically all programming languages, by giving their interpreter as the value of `pipe`. I use this for my web site ( chriswarbo.net ) to execute code in PHP, Haskell, Racket, Bash, Coq, etc. It can also run things indirectly, e.g. if you want to compile and run C you could do something like this:
A more scalable alternative, for documents which will run many such snippets, is to write a helper script to use as our command, e.g. I've also written a companion script called panhandle: https://github.com/warbo/panhandlePanhandle allows the contents of a code block (annotated with class `unwrap`) to be spliced into its containing document. This is useless on its own (since we could just write that part of the document directly, rather than putting it inside a code block); but it allows us to generate markup using panpipe, and insert it into the document. For example, the following is a HTML page containing a <p> element and a <code> element; panpipe can execute the contents of the <code> element, which generates HTML for an <img> element, but that HTML is stuck as the text of a <code> element. Panhandle lets us "unwrap" it, to become a real <img> element in the document:
Note that panhandle needs to parse the content of a code block in order to unwrap it; to keep things simple and unambiguous it only supports Pandoc JSON format (a serialisation of Pandoc's internal AST). Since the above shell code generates HTML, we pipe it through `pandoc -f html -t json` to get the required JSON format.This whole approach is described at http://www.chriswarbo.net/projects/activecode