【问题】
之前已经实现了,将Docbook中的代码的部分,实现了添加背景色:
现在想要继续对程序代码programlisting做代码高亮的处理,希望输出的程序代码,在HTML和PDF中都可以实现代码高亮。
目前已有的显示效果为:
docbook的xml源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | < para >就可以生成相应的字体的XML Metrics文件了,然后再把相应的如下的设置:</ para > < programlisting > <![CDATA[ <font metrics-url="E:\Dev_Root\docbook\dev\config\fop\fonts\simhei.xml" kerning="yes" embed-url="C:\Windows\Fonts\simhei.ttf"> <font-triplet name="SimHei" style="normal" weight="normal"/> <font-triplet name="SimHei" style="normal" weight="bold"/> <font-triplet name="SimHei" style="italic" weight="normal"/> <font-triplet name="SimHei" style="italic" weight="bold"/> </font> <font metrics-url="E:\Dev_Root\docbook\dev\config\fop\fonts\msyh.xml" kerning="yes" embed-url="C:\Windows\Fonts\msyh.ttf"> <font-triplet name="msyh" style="normal" weight="normal"/> <font-triplet name="msyh" style="normal" weight="bold"/> <font-triplet name="msyh" style="italic" weight="normal"/> <font-triplet name="msyh" style="italic" weight="bold"/> </font> <font metrics-url="E:\Dev_Root\docbook\dev\config\fop\fonts\msyhbd.xml" kerning="yes" embed-url="C:\Windows\Fonts\msyhbd.ttf"> <font-triplet name="msyhbd" style="normal" weight="normal"/> <font-triplet name="msyhbd" style="normal" weight="bold"/> <font-triplet name="msyhbd" style="italic" weight="normal"/> <font-triplet name="msyhbd" style="italic" weight="bold"/> </font> <directory recursive="true">file:///c:/windows/fonts/</directory> <auto-detect/> ]]> </ programlisting > < para >添加到< filename >fop.xconf</ filename >中去。</ para > |
HTML中显示效果:
PDF中显示效果:
【解决过程】
1.找到了官网关于这个参数的解释:
HTML中的highlight.source
FO中的:highlight.source
然后按照其解释,
先去下载了添加配置:
1 2 | < xsl:param name = "highlight.source" select = "1" ></ xsl:param > < xsl:param name = "highlight.default.language" ></ xsl:param > |
然后再去the XSLT syntax highlighting project下载了最新版本的xslthl-2.0.2.zip,然后把解压后所得的xslthl-2.0.2.jar拷贝到fop\lib下,这样其会自动搜索到,就不用手动添加对应jar的路径到CLASSPATH了。
然后再分别给HTML添加:
1 | < xsl:import href = "&xsl_ns_base_cygwin;/html/highlight.xsl" /> |
给FO添加:
1 | < xsl:import href = "&xsl_ns_base_cygwin;/fo/highlight.xsl" /> |
后来,去查看docbook-xsl-ns-1.76.1\html\highlight.xsl,看到其中有引用:
1 | < xsl:import href = "../highlighting/common.xsl" /> |
所以就去看看highlighting文件夹,结果就发现了原来,此文件夹中有一对的xml文件,就是对应的各个语言的高亮格式化的配置,而这些xml文件,下载的那个xslthl-2.0.2的highlighters下面,也有这些文件的。
然后再参考:highlight.xslthl.config,拷贝xslthl-config.xml另存为xslthl-config_crl.xml,再去添加对应配置:
1 | < xsl:param name = "highlight.xslthl.config" >file://&config_base_cygwin;/highlight/xslthl-config.xml</ xsl:param > |
到xsl配置文件中。
然后再给programlisting添加id,结果由于上述代码为xml,但是却发现此代码高亮部分不支持xml,只有个看似最接近的myxml,所以试了试:
1 | < programlisting language = "myxml" > |
但是结果生成的HTML和PDF都没有效果。
以为此代码高亮,只适用于pdf呢。
所以就单独去对pdf做测试。
然后又随便弄了份c代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < para >代码高亮测试:</ para > < programlisting language = "c" > <![CDATA[ /* * This should be approx 2 Bo*oMips to start (note initial shift), and will * still work even if initially too large, it will just take slightly longer */ unsigned long loops_per_jiffy = (1<<12); EXPORT_SYMBOL(loops_per_jiffy); static int __init debug_kernel(char *str) { console_loglevel = 10; return 0; } static int __init quiet_kernel(char *str) { console_loglevel = 4; return 0; } ]]> </ programlisting > |
然后生成的pdf和HTML中,也都还是没有实现代码高亮。
悲剧了,刚刚才看到上面那个官网的解释,原来此处的代码高亮的方法,只适用于Saxon 6.5.x和Xalan-J。
也就是说,此处我用xsltproc和fop,是没法用的。
目前还是懒得去折腾Saxon平台,所以,暂时还是放弃此代码高亮的功能了。