Question

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>
Was it helpful?

Solution 2

Since September 11, 2012 Ditac now support syntax colouring out of the box so to speak!

The language is specified using the outputclass attribute:

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

This has indeed been implemented by XML Mind using XSLT Syntax Highlighting as mentioned in the original question.

The following values are supported for outputclass:

  • language-c
  • language-cpp
  • language-csharp
  • language-delphi
  • language-ini
  • language-java
  • language-javascript
  • language-m2
  • language-perl
  • language-php
  • language-python
  • language-ruby
  • language-tcl
  • language-xml

OTHER TIPS

It looks as if this stylesheet uses the old "reflexive" extension mechanism, which is not supported in Saxon-HE. Possible solutions are:

(a) use the old open-source Saxon-B product: this mechanism was supported in releases up to 9.1

(b) use Saxon-PE (cost £50): this mechanism is still supported in current releases of Saxon-PE and Saxon-EE.

(c) write some Java wrapper code to implement the extension as an "integrated extension function" which is supported in Saxon-HE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top