我有一个 Markdown 文件,例如
---
title: Question
date: 2020-07-07
---
This is some code:
```python
def add(a, b):
return a+b
```
Run Code Online (Sandbox Code Playgroud)
我想利用 Pandoc 的语法突出显示。这工作正常:
---
title: Question
date: 2020-07-07
---
This is some code:
```python
def add(a, b):
return a+b
```
Run Code Online (Sandbox Code Playgroud)
因为它包含必要的 CSS,例如:
pandoc -s --to=html5 2020-07-07-question.md
Run Code Online (Sandbox Code Playgroud)
但是,我实际上是使用 Pypandoc 将 Markdown 编译为 HTML,然后我将 HTML 包含到网页中。因此,我希望 CSS 是独立的,我可以在文件中引用的东西,例如
<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;}
...
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
可以通过运行来检查用于 HTML 生成的默认模板
pandoc --print-default-template=html5
Run Code Online (Sandbox Code Playgroud)
结果将取决于您的版本,但应包含所有有趣的内容。例如,对于 pandoc 2.10,这将包括代码
pandoc --print-default-template=html5
Run Code Online (Sandbox Code Playgroud)
这导致 pandoc 包含一个文件styles.html,其内容可通过
pandoc --print-default-data-file=templates/styles.html
Run Code Online (Sandbox Code Playgroud)
原则上,这就是您要寻找的。现在,您会注意到有很多模板命令,并且似乎没有包含突出显示 CSS 的语法。这是因为 pandoc 会动态生成 CSS:样式的存储方式也使其易于与其他输出一起使用。结帐--list-highlight-styles和--print-highlight-style。
这对您来说意味着您可以只生成 HTML 输出并从那里复制粘贴代码。或者您可以创建一个仅包含
<style>
$styles.html()$
</style>
Run Code Online (Sandbox Code Playgroud)
然后使用该模板创建您的highlighting.css:
pandoc --template=highlighting-css.template sample.md -o highlighting.css
Run Code Online (Sandbox Code Playgroud)
请注意,sample.md必须包含可突出显示的代码块,例如
~~~html
<p>placeholder</p>
~~~
Run Code Online (Sandbox Code Playgroud)
这是必要的,因为 pandoc 仅在有要突出显示的内容时才会生成突出显示的 CSS。
这是一个小 shell 脚本,它执行 @tarleb 在他的答案中描述的内容并在自行清理后进行清理:
#!/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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1161 次 |
| 最近记录: |