Collapse AllExpand All

Chapter 7. Syntax highlighting Previous topic Parent topic Child topic Next topic

This chapter explains how you can automatically colorize the source code contained in <pre>, <codeblock> or any other element specializing <pre>.

You can automatically colorize the source code contained in <pre>, <codeblock> or any other element specializing <pre>. This feature, commonly called syntax highlighting, has been implemented using an open source software component called "XSLT syntax highlighting Opens in new window".
If you want to turn on syntax highlighting in a DITA document, suffice to add attribute @outputclass to a <pre>, <codeblock> or any other element specializing <pre>. The value of attribute @outputclass must be any of: language-c, language-cpp, language-csharp, language-delphi, language-ini, language-java, language-javascript, language-m2 (Modula 2), language-perl, language-php, language-python, language-ruby, language-tcl, language-xml.
If you want to customize syntax highlighting for an HTML-based output format (XHTML, EPUB, etc), then redefine any of the following CSS styles:
  • .hl-keyword (keywords of a programming language),
  • .hl-string (string literal),
  • .hl-number (number literal),
  • .hl-comment (any type of comment),
  • .hl-doccomment (comments used as documentation, i.e. javadoc, or xmldoc),
  • .hl-directive (preprocessor directive or in XML, a processing-instruction),
  • .hl-annotation (annotations or "attributes" as they are called in .NET),
  • .hl-tag (XML tag, i.e. element name),
  • .hl-attribute (XML attribute name),
  • .hl-value (XML attribute value),
  • .hl-doctype (<!DOCTYPE>and all its content).

Customization of the syntax highlighting of a keyword for HTML-based output formats

.hl-keyword {
    font-weight: bold;
    color: #602060;
}
How to use a custom CSS stylesheet is explained in Part II, Chapter 9, Section 1.
If you want to customize syntax highlighting for an XSL-FO-based output format (PDF, RTF, etc), then redefine any of the following <attribute-set>s: hl-keyword, hl-string, hl-number, hl-comment, hl-doccomment, hl-directive, hl-annotation, hl-tag, hl-attribute, hl-value, hl-doctype.

Customization of the syntax highlighting of a keyword for XSL-FO-based output formats

<xsl:attribute-set name="hl-keyword" use-attribute-sets="hl-style">
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="color">#602060</xsl:attribute>
</xsl:attribute-set>
How to use a custom XSLT stylesheet generating XSL-FO is explained in Part II, Chapter 9, Section 2.