0

Is there a way to perform syntax colouring using Ditac (the DITA converter by XML Mind)? Any XSLT1 or XSLT2 based solution would work provided that it supports both XHTML and XSL:FO output.

XSLTHL (http://sourceforge.net/projects/xslthl/) has been suggested, though Saxon-HE (used by Ditac) doesn't seem to detect the extension JAR file.

I modified the provided XSL files so that they should work with DITA; but the default behaviour of just copying the non-coloured source listing occurs because the extension function is not being detected:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        xmlns:ditac="http://www.xmlmind.com/ditac/schema/ditac"
        xmlns:u="http://www.xmlmind.com/namespace/ditac"
        version="2.0">

    <xsl:import href="ditac-xsl:fo/fo.xsl"/>
    <xsl:import href="highlighting/fo.xsl"/>

    <!-- Syntax highlighting -->
    <xsl:template match="*[contains(@class,' topic/pre ')]">
        <fo:block xsl:use-attribute-sets="pre">
            <xsl:call-template name="commonAttributes"/>
            <xsl:call-template name="displayAttributes"/>
            <xsl:call-template name="apply-highlighting"/>
        </fo:block>
    </xsl:template>

    ...

"highlighting/fo.xsl" includes another file called "highlighting/common.xsl" (I took and modified these files from Docbook). Here is the part that should link with Saxon:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

        xmlns:sbhl="http://net.sf.xslthl/ConnectorSaxonB"
        xmlns:saxonb="http://saxon.sf.net/" 

        xmlns:exsl="http://exslt.org/common"
        xmlns:xslthl="http://xslthl.sf.net"
        exclude-result-prefixes="exsl xslthl sbhl"
        version='1.0'>

<!-- ********************************************************************
     $Id: common.xsl,v 1.11 2009/06/02 11:41:23 sorin Exp $
     ********************************************************************

     This file is part of the XSL DocBook Stylesheet distribution.
     See ../README or http://docbook.sf.net/release/xsl/current/ for
     and other information.

     ******************************************************************** -->

<!-- for saxon 8.5 and later -->
<saxonb:script implements-prefix="sbhl" language="java" src="java:net.sf.xslthl.ConnectorSaxonB" />


<xsl:variable name="highlight.default.language" select="csharp"/>


<!-- You can override this template to do more complex mapping of
     language attribute to highlighter language ID (see xslthl-config.xml) -->
<xsl:template name="language.to.xslthl">
  <xsl:param name="context"/>

  <xsl:choose>
    <xsl:when test="$context/@outputclass != ''">
      <xsl:value-of select="$context/@outputclass"/>
    </xsl:when>
    <xsl:when test="$highlight.default.language != ''">
      <xsl:value-of select="$highlight.default.language"/>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<xsl:template name="apply-highlighting">
    <xsl:variable name="language">
        <xsl:call-template name="language.to.xslthl">
            <xsl:with-param name="context" select="."/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$language != ''">
            <xsl:variable name="content">
                <xsl:apply-templates/>
            </xsl:variable>
            <xsl:choose>
                <xsl:when test="function-available('sbhl:highlight')">
                    <xsl:apply-templates select="sbhl:highlight($language, exsl:node-set($content), 'xslthl-config.xml')" mode="xslthl"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$content"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<!-- A fallback when the specific style isn't recognized -->
<xsl:template match="xslthl:*" mode="xslthl">
  <xsl:message>
    <xsl:text>unprocessed xslthl style: </xsl:text>
    <xsl:value-of select="local-name(.)" />
  </xsl:message>
  <xsl:apply-templates mode="xslthl"/>
</xsl:template>

<!-- Copy over already produced markup (FO/HTML) -->
<xsl:template match="node()" mode="xslthl" priority="-1">
  <xsl:copy>
    <xsl:apply-templates select="node()" mode="xslthl"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="xslthl">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="node()" mode="xslthl"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
4

2 回答 2

2

看起来这个样式表使用了旧的“自反”扩展机制,而 Saxon-HE 不支持这种机制。可能的解决方案是:

(a) 使用旧的开源 Saxon-B 产品:此机制在 9.1 之前的版本中得到支持

(b) 使用 Saxon-PE(成本 50 英镑):当前版本的 Saxon-PE 和 Saxon-EE 仍支持此机制。

(c) 编写一些 Java 包装器代码以将扩展实现为 Saxon-HE 支持的“集成扩展功能”。

于 2012-08-01T08:59:46.440 回答
0

自 2012 年 9 月 11 日起,Ditac 现在支持开箱即用的语法着色!

使用outputclass属性指定语言:

<codeblock outputclass="language-csharp"><![CDATA[
    public class SomethingNeat {
        public string someString = "Hello World!";
    }
]]></codeblock>

这确实由 XML Mind 使用XSLT Syntax Highlighting实现,如原始问题中所述。

支持以下值outputclass

  • 语言-c
  • 语言-cpp
  • 语言-csharp
  • 语言-delphi
  • 语言-ini
  • 语言-java
  • 语言-javascript
  • 语言-m2
  • 语言-perl
  • 语言-php
  • 语言-python
  • 语言-红宝石
  • 语言-tcl
  • 语言-xml
于 2013-01-29T02:28:46.253 回答