2017年10月3日星期二

在 Docbook 中给源代码加语法高亮


想用 Docbook 写一篇编程方面的教程, 并加上 syntax highlighting 方便代码阅读,没想到这个任务并不容易。

GNU/Linux
========
Fedora 24, dnf install saxon 安装的是 saxon-9.4.0.9-2.fc24.noarch
而 saxon 9 只支持 XLST 2 而不是以前的 version 1, 然后我试图安装  docbook xslt20-stylesheets 也不成功。


回到 Windows
=========
按照 [1] 中的方法,在 Windows 7 中实验成功。


回到 GNU/Linux
==========
1. 下载 docbook-xsl-1.79.1
https://sourceforge.net/projects/docbook/files/docbook-xsl/

2. 下载 XSLT 1.0 proccessor
http://saxon.sourceforge.net/#F6.5.5

3. 下载 XSLT syntax highlighting (Java based XSLT Processor extension for syntax highlighting)
https://sourceforge.net/projects/xslthl/

4. 把它们解压放在某个目录里,比如我放到 ~/docbook 目录里。
  • ~/docbook/docbook-xsl-1.79.1
  • ~/docbook/saxon6-5-5
  • ~/xslthl-2.1.3
5. 然后就可以用 Gary 的文件来做测试了。

我把测试的文件放在了 github 上,见 [4].

本文基本是按照链接 [1] 中 Gary 的方法操作,只是他在 Windows 中,我是在 GNU/Linux 中。

另外,saxon 在我电脑上生成 html 的速度很慢,我估计是 java 的原因。要知道 xsltproc 的速度是很快的!

在 my_article.xsl 文件里我加了一句
<xsl:param name="use.extensions" select="1"></xsl:param>
这样在命令行就不需要再加 use.extensions=1 了。

参考
[1] DocBook 的 Line Numbering 和 Code Highlighting
http://garyliutw.blogspot.jp/2013/07/docbook-line-numbering-code-highlighting.html
很有用的文章,我是看了这篇文章才成功了。

[2] https://github.com/docbook/xslt20-stylesheets

[3] Installing an XSLT processor
http://www.sagehill.net/docbookxsl/InstallingAProcessor.html
docbook 的 xml 文件可以用各种处理器来处理,上面的文档讲了安装和使用方法。

[4]  https://github.com/atommann/syntax-highlighting-line-no-test-docbook

[5] Show me your highlighted code
http://xmlguru.cz/2006/07/docbook-syntax-highlighting
最后讲了如何定制。

[6] 10.1. Syntax Highlighting
http://www.vogella.com/tutorials/DocBook/article.html
讲如何定制自己的高亮颜色。