I have a Markdown file, e.g.
---
title: Question
date: 2020-07-07
---
This is some code:
```python
def add(a, b):
return a+b
```
and I'd like to leverage the syntax highlighting of Pandoc. This works fine:
pandoc -s --to=html5 2020-07-07-question.md
Because it includes the necessary CSS, e.g.:
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
...
However, I'm actually using Pypandoc to compile the Markdown into HTML, and then I'm including the HTML into a web page. Therefore, I'd like the CSS to be standalone, something I can reference in a file, e.g.
<link rel='stylesheet' href='/path/to/some/highlighting.css'/>
How can I do this?
An extensions can be enabled by adding +EXTENSION to the format name and disabled by adding -EXTENSION . For example, markdown_strict+footnotes is strict Markdown with footnotes enabled, while markdown-footnotes-pipe_tables is pandoc's Markdown without footnotes or pipe tables.
Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.
Pandoc is a command-line tool for converting files from one markup language to another. Markup languages use tags to annotate sections of a document. Commonly used markup languages include Markdown, ReStructuredText, HTML, LaTex, ePub, and Microsoft Word DOCX.
Specifies the coloring style to be used in highlighted source code. Options are pygments (the default), kate, monochrome , breezeDark, espresso, zenburn, haddock, and tango . For more information on syntax highlighting in pandoc, see Syntax highlighting, below.
The Pandoc README says: Specifies the coloring style to be used in highlighted source code. Options are pygments (the default), kate, monochrome, breezeDark, espresso, zenburn, haddock, and tango. For more information on syntax highlighting in pandoc, see Syntax highlighting, below.
If none is provided using this option (or the css or stylesheet metadata fields), pandoc will look for a file epub.css in the user data directory (see --data-dir ). If it is not found there, sensible defaults will be used.
Options are pygments (the default), kate, monochrome , breezeDark, espresso, zenburn, haddock, and tango . For more information on syntax highlighting in pandoc, see Syntax highlighting, below.
One can inspect the default template used for HTML generation by running
pandoc --print-default-template=html5
The result will depend on your version, but should contain everything interesting. E.g., for pandoc 2.10, this will include the code
<style>
$styles.html()$
</style>
which causes pandoc to include a file styles.html
, the content of which is retrievable via
pandoc --print-default-data-file=templates/styles.html
In principle, this is what you are looking for. Now, you'll notice that there are a lot of templating commands, and the syntax highlighting CSS seems not to be included. This is because pandoc generates the CSS on the fly: the styles are stored in a way which makes it easy to use them with other outputs as well. Checkout --list-highlight-styles
and --print-highlight-style
.
What this means for you is that you can either just generate HTML output and copy-paste the code from there. Or you can create a helper template which just contains
$-- this is file highlighting-css.template
$highlighting-css$
Then use that template to create your highlighting.css
:
pandoc --template=highlighting-css.template sample.md -o highlighting.css
Note that sample.md
must contain a highlightable code block such as
~~~html
<p>placeholder</p>
~~~
This is necessary, as pandoc generates highlighting CSS only if there is something to highlight.
Here's a small shell script that does what @tarleb describes in his answer and cleans up after itself:
#!/bin/sh
style=${1:-pygments}
tmp=
trap 'rm -f "$tmp"' EXIT
tmp=$(mktemp)
echo '$highlighting-css$' > "$tmp"
echo '`test`{.c}' | pandoc --highlight-style=$style --template=$tmp
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