htdebeer / pandocomatic

Automate the use of pandoc

Home Page:https://heerdebeer.org/Software/markdown/pandocomatic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pandocomatic with codebraid?

ppenguin opened this issue · comments

I have been struggling to include codebraid in my pandocomatic workflow. It appears using codebraid as a preprocessor is not possible, since it "processes away" the (crucial) YAML headers in my markdown documents.

I tried to "fool" pandocomatic by aliasing pandoc to codebraid pandoc or make a wrapper script called pandoc, but it doesn't work.

Is there any way to combine the two?

TIA!

I am not familiar with "codebraid". Can you explain how you usually use "codebraid" with pandoc? What is the output of codebraid?

Do note that pandocomatic expects all preprocessors to read from STDIN and write to STDOUT and that the final output of all preprocessors is something pandoc can read and use.

I just successfully tested a workaround, which may for the time being be enough, in lieu of a "more elegant" solution:

Define a script which executes codebraid on (partial) md-files which adhere to a certain "input-md" naming convention (I use *.cbi.md). Make this script generate "output-md" files (also convention, e.g. *.cbo.md and include these using the pandoc-include filter in the main document.

To make the script execute as a preprocessor (the only reason for this is that the script is automatically called when executing pandocomatic and therefore does not need to be executed manually before executing pandocomatic), this script needs to pass through stdin to stdout. My script looks like this and appears to work in my workflow...

#! /bin/bash
# act as a passthrough filter and do some stuff on the side
# exec 2>/dev/null
rm *.cbo.md
for FI in *.cbi.md; do
    FO=$(basename ${FI} .cbi.md).cbo.md
    codebraid pandoc -t markdown -o "${FO}" "${FI}"
done
cat - 

So with this, one can define an input file (test1.cbi.md) like so:

'''{.python .cb.run show=code line_numbers=true}
var = 'Hello from Python!'
var += ' $2^8 = {}$'.format(2**8)
'''
'''{.python .cb.run show=code+stdout+stderr line_numbers=true}
print(var)
'''

(where the ' are actually bacticks, which I can't seem to escape correctly)
and expect an output (in test1.cbo.md) like:

'''{.python .numberLines startFrom="1"}
var = 'Hello from Python!'
var += ' $2^8 = {}$'.format(2**8)
'''

'''{.python .numberLines startFrom="3"}
print(var)
'''

'''{.stdout}
Hello from Python! $2^8 = 256$
'''

which is included in mydoc.md (using --filter=pandoc-include with:

!include test1.cbo.md

I am not familiar with "codebraid". Can you explain how you usually use "codebraid" with pandoc? What is the output of codebraid?

Do note that pandocomatic expects all preprocessors to read from STDIN and write to STDOUT and that the final output of all preprocessors is something pandoc can read and use.

Our answers crossed ;)
codebraid is (similar to pandocomatic in that it uses pandoc) a program that executes designated code in codeblocks of markdown-files, which can be used to dynamically generate documents containing code-examples and their outputs, or to generate content on-the-fly.
See https://github.com/gpoore/codebraid.

Sounds interesting! Thanks for posting the workaround. It might be useful for others as well..