Prism-JS Plug-in
This is a syntax highlighting DITA-OT Plug-in which integrates the flexible PrismJS highlighting library into the DITA Open Toolkit engine. This enables the generation of documents including code snippets which are automatically colorized according to language syntax. The plug-in extends both static HTML and PDF transtypes.
Background
Prism is a lightweight, extensible syntax highlighter, built with modern web standards in mind. It’s a spin-off project from Dabblet.
- Highlights embedded languages (e.g. CSS inside HTML, JavaScript inside HTML)
-
Highlights inline code (
<codeph>
) as well, not just code blocks (<codeblock>
) - Highlights nested languages (CSS in HTML, JavaScript in HTML)
- It doesn’t force you to use any Prism-specific markup
You can learn more on http://prismjs.com/.
Install
fox.jason.extend.css
to function correctly. It also needs Node.js running on user's machine to
avoid deprecation errors.
Run the plug-in installation commands:
dita install https://github.com/jason-fox/fox.jason.extend.css/archive/master.zip
dita install https://github.com/jason-fox/fox.jason.prismjs/archive/master.zip
The dita command line tool requires no additional configuration.
Installing Node.js
Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser
Due to the deprecation and removal of the Nashorn Engine in JDK11-14 JEP 335
any plug-in using JavaScript within <script>
or <scriptdef>
ANT tasks will start throwing warnings with Java 11 onwards and above. From
Java 15 onwards, these plugins will no longer work.
The DITA-OT Prism-JS syntax highlighter relies heavily the Prism-JS JavaScript library, and therefore has been updated to run using Node.js where present on a user’s machine. Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
To download a copy follow the instructions on the Install Page.
Usage
@outputclass
attribute to any <codeph>
or <codeblock>
elements in your *.dita
files. Alternatively add an @outputclass
attribute to the <body>
element, and all <codeph>
or <codeblock>
elements will inherit from it.
With the default Prism-JS library the following languages can be highlighted
-
@outputclass="language-markup"
- HTML, XML etc. -
@outputclass="language-css"
- Cascading Style Sheet highlighting -
@outputclass="language-clike"
- C-language family highlighting -
@outputclass="language-javascript"
- JavaScript highlighting …etc.
A test document including HTML, CSS and JavaScript code snippets can be found
within the plug-in at: PATH_TO_DITA_OT/plugins/fox.jason.prismjs/sample
.
Invocation from the command line
The Plug-in extends the existing PDF and HTML transforms.
- to create a PDF with highlighted code snippets run:
dita --format pdf \
--input document.ditamap \
--output out
- to create static HTML with highlighted code snippets run:
dita --format html5 \
--input document.ditamap \
--output out
Line numbering
To display line numbers in codeblocks in HTML and PDF, add the @ outputclass="show-line-numbers"
to the <codeblock>
function jsonFetch(json, filename) {
return new Promise(function (resolve, reject) {
if (json) {
return resolve(json);
}
fetch(JSON_PATH + filename)
.then((response) => {
if (!response.ok) {
return reject(new Error(`HTTP error, status = ${response.status}`));
}
return response.json();
})
.then((data) => {
return resolve(data);
});
});
}
Parameter Reference
- prism.default - Specifies the default Prism language
-
prism.use.theme - Specifies which of three included themes to use.
- default - default Prism.js theme
- solarized - theme based on solarized colors
- bootstrap - theme based on Bootstrap 5 colors
- prism.css.theme - Specifies the location of a custom color theme file relative to the output directory.
Customizing the output
Prism-JS is easily extended to other languages since it purely relies on regular expressions. Additional languages are loaded dynamically during processing. A large number of additional languages are supported - just look at the list on https://github.com/PrismJS/prism/tree/master/components.
Extend with an additional plug-in which overrides the default prismjs.css.file
property and amend a copy of the resource/style.css
file to
alter the look-and-feel of the rendered HTML.
A working example can be found in the Dark Theme CSS DITA-OT plug-in.
The cfg/fo/attrs/prismjs-attr.xsl
provides the colors for the
PDF output. The names of the attributes match the CSS file, copy and amend the
prismjs-attr.xsl file in your own plug-in.
Override the <xsl:template match="*[contains(@class,' topic/ph ') and
contains(@outputclass, 'token')]">
template as shown:
A working example can be found in the Dark Theme CSS DITA-OT plug-in.