Source Highlighting
Source highlighting is applied to text that’s assigned the source
block style and a language.
You can define the source language directly on the block or globally using the source-language
document attribute.
source-highlighter attribute
Source highlighting isn’t enabled by default.
To enable source highlighting, you must set the source-highlighter
attribute in the document header using an attribute entry.
= Document Title
:source-highlighter: <value>
For example, here’s how to enable syntax highlighting using Rouge:
= Document Title
:source-highlighter: rouge
You can also declare this attribute using the CLI or API.
Available source highlighters
Table 1 lists the recognized values for the source-highlighter
attribute and the toolchains that support the usage of the syntax highlighting libraries.
Library | Value | Toolchain |
---|---|---|
CodeRay |
|
Asciidoctor, AsciidoctorJ, Asciidoctor PDF |
highlight.js |
|
Asciidoctor, AsciidoctorJ, Asciidoctor.js |
Pygments |
|
Asciidoctor, Asciidoctor PDF |
Rouge |
|
Asciidoctor, AsciidoctorJ, Asciidoctor PDF |
To use Rouge, CodeRay, or Pygments, you must have the appropriate library installed on your system. See Rouge, CodeRay, or Pygments for installation instructions.
If you’re using the client-side library Highlight.js, there’s no need to install additional libraries. The generated HTML will load the required source files from a CDN, custom URL, or file path.
Apply source highlighting
To apply highlighting to source code, you must add the source
style to a listing block, literal block, or paragraph and specify a source language.
[source#hello,ruby] (1) (2) (3) --(4) require 'sinatra' get '/hi' do "Hello World!" end ----
1 | Assign the block style source to the first position in the attribute list. |
2 | An optional ID can be added to the block by appending it to style using the shorthand syntax (# ) for id . |
3 | Assign a source language to the second position. |
4 | The source style is typically assigned to delimited listing blocks. |
The result of Example 1 is displayed below.
require 'sinatra'
get '/hi' do
"Hello World!"
end
[source,xml] (1)
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
(2)
This is normal content.
1 | Place the attribute list directly on the paragraph. |
2 | Once an empty line is encountered the syntax highlighting is unset. |
The result of Example 2 is displayed below.
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
This is normal content.
Disable source highlighting
To disable source highlighting for a given source block, specify the language as text
or remove the source
style.
source-language attribute
If the majority of your source blocks use the same source language, you can set the source-language
attribute in the document header and assign a language to it.
= Document Title :source-highlighter: pygments :source-language: java [source] ---- public void setAttributes(Attributes attributes) { this.options.put(ATTRIBUTES, attributes.map()); } ----
You can override the global source language on an individual block by specifying a source language after source
.
= Document Title :source-highlighter: pygments :source-language: java [source,ruby] require 'sinatra'