gpoore / codebraid

Live code in Pandoc Markdown
BSD 3-Clause "New" or "Revised" License
367 stars 13 forks source link

Feature request: override stdout code block language #61

Open W1M0R opened 1 year ago

W1M0R commented 1 year ago

I have a code block that looks like this:

{.bash .cb-run show=stdout:verbatim}
my-command-that-produces-json-stdout
EOF

The my-command-that-produces-json-stdout is a command that produces json stdout. This example uses json, but it applies to any other output format.

When the code block stdout is rendered in Pandoc, I want it to be rendered with json syntax highlighting, i.e. as a json code block.

Somehow I would need to inform the codebraid bash code block that a stdout JSON code block should be rendered.

Maybe the rich_format option can be used, e.g. rich_format:json or some other option can be introduced. Ideally, all of the languages that appear in pandoc --list-highlight-languages should be supported.

Another option could be to add a new keyword that is understood by codebraid, e.g. cb-bash-runner, that codebraid will prefer if it exists. If codebraid sees this annotation in the code block header, then it will prefer this as the code runner, and then regular pandoc syntax can be used. The following example indicates that the body of the code block should be rendered as json (something that pandoc does), and that codebraid should execute the body using bash, and then replace the body with the stdout:

{.json .cb-bash-runner .cb-run show=stdout:verbatim}
my-command-that-produces-json-stdout
EOF
gpoore commented 1 year ago

More customization for stdout formatting could be good. The current syntax could probably be extended to support something like show=stdout:verbatim:json or maybe even just show=stdout:json.

Do you anticipate any situations where a single code block will produce stdout in multiple formats? I've been working on new features that could make that possible.

I'm currently finishing the next version of Codebraid Preview, which will bring scroll sync for all Markdown variants, not just commonmark, commonmark_x, and gfm. Once that's done, I will be working on a new version of Codebraid and should be able to incorporate stdout format customization. In the meantime, you could probably try something like show=stdout:raw and then wrap your JSON output like this:

``````{.bash .cb-run show=stdout:raw}
printf "```json\n"
<my-command-that-produces-json-stdout>
printf "```\n"
W1M0R commented 1 year ago

Thanks for the trick! Using printf '```json\n' (single quotes) worked for me.

I don't anticipate a situation where a single block would produce multiple stdout formats.