You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
5.6 KiB
163 lines
5.6 KiB
4 years ago
|
////
|
||
|
Included in:
|
||
|
|
||
|
- user-manual: Source Code Syntax Highlighting: Pygments installation
|
||
|
////
|
||
|
|
||
|
{pygments-org}[Pygments] is a popular syntax highlighter that supports a broad range of {pygments-lang}[programming, template and markup languages].
|
||
|
|
||
|
In order to use Pygments with Asciidoctor, you need https://www.python.org[Python 2] and the {pygments-gem}[pygments.rb gem].
|
||
|
|
||
|
TIP: You _do not_ need to install Pygments itself.
|
||
|
It comes bundled with the pygments.rb gem.
|
||
|
|
||
|
IMPORTANT: You must have Python 2 installed to use pygments.rb.
|
||
|
Python 3 is not compatible with the pygments.rb gem.
|
||
|
Check that you have a `python2` (Linux), `python` (macOS), or `py -2` (Windows) executable on your PATH.
|
||
|
(On macOS, verify that the `python` executable uses Python 2 by running `python -V`).
|
||
|
|
||
|
.Installing Python and the pygments.rb gem via the CLI (cross platform)
|
||
|
[source,console]
|
||
|
....
|
||
|
$ "`\which apt-get || \which dnf || \which yum || \which brew`" install python # <1>
|
||
|
$ gem install pygments.rb # <2>
|
||
|
....
|
||
|
<1> Install Python using your package manager
|
||
|
<2> Install the pygments.rb gem
|
||
|
|
||
|
Once you've installed these libraries, assign `pygments` to the `source-highlighter` attribute in your document's header.
|
||
|
|
||
|
[source]
|
||
|
----
|
||
|
:source-highlighter: pygments
|
||
|
----
|
||
|
|
||
|
You can further customize the source block output with additional Pygments attributes.
|
||
|
|
||
|
pygments-style::
|
||
|
Sets the name of the color theme Pygments uses.
|
||
|
To see the list of available style names, see <<listing-pygments-style-names>>.
|
||
|
Default: `pastie`.
|
||
|
|
||
|
pygments-css::
|
||
|
Controls what method is used for applying CSS to the tokens.
|
||
|
Can be `class` (CSS classes) or `style` (inline styles).
|
||
|
See <<user-manual#hl-css,Pygments stylesheet>> to learn more about how the value `class` is handled.
|
||
|
Default: `class`.
|
||
|
|
||
|
pygments-linenums-mode::
|
||
|
Controls how line numbers are arranged when line numbers are enabled on the source block.
|
||
|
Can be `table` or `inline`.
|
||
|
If line wrapping is enabled on preformatted blocks (i.e., `prewrap`), and you want to use line numbering on source blocks, you must set the value of this attribute to `inline` in order for the numbers to line up properly with their target lines.
|
||
|
Default: `table`.
|
||
|
|
||
|
.Customizing a source block with Pygments attributes
|
||
|
[source]
|
||
|
....
|
||
|
:source-highlighter: pygments
|
||
|
:pygments-style: manni
|
||
|
:pygments-linenums-mode: inline
|
||
|
|
||
|
[source,ruby,linenums]
|
||
|
----
|
||
|
ORDERED_LIST_KEYWORDS = {
|
||
|
'loweralpha' => 'a',
|
||
|
'lowerroman' => 'i',
|
||
|
'upperalpha' => 'A',
|
||
|
'upperroman' => 'I'
|
||
|
#'lowergreek' => 'a'
|
||
|
#'arabic' => '1'
|
||
|
#'decimal' => '1'
|
||
|
}
|
||
|
----
|
||
|
....
|
||
|
|
||
|
.Result: Source block using inline line numbers and the manni theme
|
||
|
====
|
||
|
image::custom-pygments.png[Line numbers and a custom Pygments theme for a source block.]
|
||
|
====
|
||
|
|
||
|
==== Highlight Select Lines
|
||
|
|
||
|
Not to be confused with syntax highlighting, you can configure Pygments to highlight (i.e., emphasize) select lines in order to call attention to them.
|
||
|
This feature is activated on a source block if the highlight attribute is defined.
|
||
|
|
||
|
The highlight attribute accepts a comma or semi-colon delimited list of line ranges.
|
||
|
A line range is represented by two numbers separated by a double period (e.g., `2..5`).
|
||
|
These numbers correspond to the line numbers of the source block, inclusive, where the first line is 1.
|
||
|
|
||
|
.Highlight select lines in a source block
|
||
|
[source]
|
||
|
....
|
||
|
:source-highlighter: pygments
|
||
|
|
||
|
[source,ruby,highlight=2..5]
|
||
|
----
|
||
|
ORDERED_LIST_KEYWORDS = {
|
||
|
'loweralpha' => 'a',
|
||
|
'lowerroman' => 'i',
|
||
|
'upperalpha' => 'A',
|
||
|
'upperroman' => 'I',
|
||
|
}
|
||
|
----
|
||
|
....
|
||
|
|
||
|
[#listing-pygments-style-names]
|
||
|
==== Available Pygments style names
|
||
|
|
||
|
To list the available Pygments styles, run the following command in a terminal:
|
||
|
|
||
|
$ $(dirname $(gem which pygments.rb))/../vendor/pygments-main/pygmentize -L styles
|
||
|
|
||
|
The pygments.rb gem uses a bundled version of Pygments (often ahead of the latest release).
|
||
|
This command ensures that you are invoking the `pygmentize` command from the Pygments used by that gem.
|
||
|
|
||
|
==== Using a custom Pygments installation
|
||
|
|
||
|
If you already have Pygments installed on your system, you want to use your own fork, or you want to customize how Pygments is configured, you can get Asciidoctor to use a custom version of Pygments instead of the one bundled with the pygments.rb gem.
|
||
|
|
||
|
First, install your own version of Pygments.
|
||
|
You can do this, for instance, by cloning the upstream Pygments repository:
|
||
|
|
||
|
$ hg clone https://bitbucket.org/birkenfeld/pygments-main pygments
|
||
|
|
||
|
Find the directory that contains the file [.path]_pygmentize_ or the [.path]_Makefile_.
|
||
|
That's your Pygments installation path.
|
||
|
Make note of it.
|
||
|
|
||
|
Next, create a script to run _before_ invoking Asciidoctor for the first time.
|
||
|
Let's call it [.path]_pygments_init.rb_.
|
||
|
Populate the script with the following content:
|
||
|
|
||
|
.pygments_init.rb
|
||
|
[source,ruby]
|
||
|
----
|
||
|
require 'pygments'
|
||
|
|
||
|
# use a custom Pygments installation (directory that contains pygmentize)
|
||
|
Pygments.start '/path/to/pygments'
|
||
|
|
||
|
# example of registering a missing or additional lexer
|
||
|
#Pygments::Lexer.create name: 'Turtle', aliases: ['turtle'],
|
||
|
# filenames: ['*.ttl'], mimetypes: ['text/turtle', 'application/x-turtle']
|
||
|
----
|
||
|
|
||
|
TIP: You could enhance this script to read the Pygments installation path from an environment variable (or configuration file).
|
||
|
|
||
|
Now just require this script before your invoke Asciidoctor the first time.
|
||
|
When using the `asciidoctor` command, pass the script using the `-r` flag:
|
||
|
|
||
|
$ asciidoctor -r ./pygments_init.rb document.adoc
|
||
|
|
||
|
When using the Asciidoctor API, require the script using `require` or `require_relative`:
|
||
|
|
||
|
[source,ruby]
|
||
|
----
|
||
|
require 'asciidoctor'
|
||
|
require_relative './pygments_init.rb'
|
||
|
|
||
|
Asciidoctor.convert_file 'document.adoc', safe: :safe
|
||
|
----
|
||
|
|
||
|
Now Asciidoctor is using your custom installation of Pygments instead of the one bundled with the pygments.rb gem.
|