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>
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
  • It isn't clear from your question what the output format should be. In case this is HTML, you may be interested to have a look at the source code of the XPath Visualizer, which pretty-prints XML in a way, similar to that of IE. http://www.huttar.net/dimitre/XPV/TopXML-XPV.html – Dimitre Novatchev Aug 01 '12 at 03:27
  • I think part of the problem is that XSLTHL is a [kind of extension](http://www.saxonica.com/documentation/extensibility/functions.xml) that is not supported by Saxon-HE. – mzjn Aug 01 '12 at 08:53

2 Answers2

2

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I feared that was the case. Out of interest, has Saxon-PE been reduced in price? I seem to remember it being more costly. I actually did try substituting the "saxon9.jar" file for Saxon-B but got a number of missing class errors from Ditac. Thanks for your response! – Lea Hayes Aug 01 '12 at 12:06
  • With option (c) how does the extension JAR file get loaded? Can the extension be loaded via the XSL stylesheet? or by being placed into a special directory? – Lea Hayes Aug 01 '12 at 12:38
  • Saxon-PE was always £50, perhaps you were thinking of Saxon-EE. With option (c) there is no reliance on dynamic loading, which is one of the benefits. You register the extension with the Saxon Configuration, either programmatically (Configuration.registerExtensionFunction) or by means of a configuration file. From the command line it can be done with the help of the -init option. – Michael Kay Aug 01 '12 at 13:52
  • I don't think Ditac allows Saxon configuration to be overridden (though I may be mistaken). I was hoping to contribute this change into the XSLTHL project for those who use Ditac. But it sounds like I would need to make changes to Ditac to get this working. – Lea Hayes Aug 01 '12 at 14:25
  • The most that seems possible by configuration is to change the class path: `cp="$libDir/ditac.jar:$libDir/whcmin.jar:$libDir/resolver.jar:$libDir/saxon9.jar"` not sure if that helps matters though – Lea Hayes Aug 01 '12 at 14:30
0

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
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111