Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandoc: where are CSS files for syntax highlighting code?

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?

like image 828
jds Avatar asked Jul 07 '20 11:07

jds


People also ask

How do you use extensions in Pandoc?

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.

Can Pandoc convert HTML to markdown?

Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.

What is Pandoc used for?

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.

What are the options for syntax highlighting in Pandoc?

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.

What is the default color for Highlighter in Pandoc?

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.

What does Pandoc look for in CSS files?

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.

What are the available options in Pandoc?

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.


2 Answers

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.

like image 118
tarleb Avatar answered Oct 19 '22 14:10

tarleb


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
like image 36
John MacFarlane Avatar answered Oct 19 '22 12:10

John MacFarlane