HighLight JS Saxon extension
This library is a Saxon XPath extension function that transform source code to highlighted HTML, as highlight.js does, but without any JavaScript embeded in HTML.
To use this library, declare it as dependency :
<dependency>
<groupId>top.marchand.xml.saxon.extension</groupId>
<artifactId>highlight</artifactId>
<version>1.00.01</version>
</dependency>
Then, declare this extension when creating Processor
:
Configuration configuration = Configuration.newConfiguration();
Processor proc = new Processor(configuration);
proc.registerExtensionFunction(
new top.marchand.xml.saxon.extension.highlight.HighlightExtension());
Function's namespace is top:marchand:xml:extfunctions
. You may use any prefix, but chm
is commonly adopted.
Then in XPath, function is available :
chm:highlight(language as xs:string, sourceCode as xs:string)
chm:highlight(
language as xs:string,
sourceCode as xs:string,
config as map(xs:string,xs:string)
)
First form, with two parameters, generates an item()*
sequence of span
elements and text()
, in http://www.w3.org/1999/xhtml
namespace. If you need a different namespace, use the second form with a map as third argument. The only supported map-entry is 'result-ns'
, and its value is the required target namespace.
chm:highlight('xml','<test>value</test>')
Produces
<span xmlns="http://www.w3.org/1999/xhtml" class="hljs-tag"><<span class="hljs-name">test</span>></span>value<span xmlns="http://www.w3.org/1999/xhtml" class="hljs-tag"></<span class="hljs-name">test</span>></span>
chm:highlight('xml','<test>value</test>',map{'result-ns':''})
Produces
<span class="hljs-tag"><<span class="hljs-name">test</span>></span>value<span class="hljs-tag"></<span class="hljs-name">test</span>></span>