I'm writing some documentation in markdown and trying to render it with pandoc. I'd like to be able to highlight an interactive shell session, by bolding commands typed by the user.
So, I hope that
```console
$ user text
program output
``` #
would be highlighted like
$ user text
program output
with user text in bold.
Is it possible to do this in pandoc?
I don't think there's a built-in syntax definition for bash console. But from http://pandoc.org/MANUAL.html#syntax-highlighting:
If you are not satisfied with the built-in highlighting, or you want highlight a language that isn't supported, you can use the
--syntax-definitionoption to load a KDE-style XML syntax definition file. Before writing your own, have a look at KDE's repository of syntax definitions.
It took me a little while to work out the syntax, but here's some simple XML to make lines starting with $ bold:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<language name="Shell" version="1" kateversion="2.4" section="Other" extensions="*.shell" mimetype="" author="Tim Hutt ([email protected])" license="MIT">
<highlighting>
<contexts>
<context attribute="Output" name="Output" lineEndContext="#stay" >
<DetectChar char="$" attribute="Command" context="Command" firstNonSpace="true"/>
</context>
<context attribute="Command" name="Command" lineEndContext="#pop" />
</contexts>
<itemDatas>
<itemData name="Output" defStyleNum="dsNormal"/>
<itemData name="Command" defStyleNum="dsOthers" bold="true"/>
</itemDatas>
</highlighting>
</language>
(Use ```shell instead of ```bash)
Unfortunately bold="true" does not work with Pandoc. I assume it just reads the resulting defStyleNum and feeds that into its highlighter, ignoring the other attributes.
We can abuse one of the other styles (I've used dsOthers here - pick one that isn't used elsewhere in your documents). Just use Pandoc's --css=foo.css option with some CSS like this:
Here's how it looks, with some Rust code above:

It's a small improvement. I wish there was a way to copy text with ANSI colour codes from the console and have Pandoc render that but I'll settle for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With