Source Code Highlight

http://asciidoc.org/source-highlight-filter.html

Table of Contents

Pygments

The Pygments syntax highlighter can be used for xhtml11, html5 and bootstrap outputs (set the source-highlighter attribute to pygments).

Until now, AsciiDoc allow only to use Pygments with a default theme that look like to emacs theme. We have no way to customize this at document build time, unless editing contents of ./stylesheets/pygments.css file.

AsciiDoc-Bootstrap backend introduce in version 3.1.0 the new attribute pygments-style to change Pygments stylesheet at document build time.

Credit to AsciiDoctor GH-637 issue.

By default, AsciiDoc-Bootstrap used the AsciiDoc pygments theme. To change the style, set the pygments-style attribute and assign it the alternate style’s name.

To see what pygments styles you have available, open a terminal and type:

$ pygmentize -L styles

It will output the style names and brief descriptions.

Pygments 1.5 provides 19 themes, but only 3 pre-defined stylesheets are available in standard AsciiDoc-Bootstrap backend distribution (default, pastie, monokai).

Here are how to setup other themes. Open a terminal and type:

$ pygmentize -f html -S <theme> -a .highlight

where <theme> is one of those listed by Pygments (-L style). Copy output to the AsciiDoc-Bootstrap install dir stylesheets folder in a file named pygments.<theme>.css. (e.g. pygments.tango.css)

In the example document below, we used the monokai style as main theme, and pastie as local theme to highlight a block of CSS code.

Pygments token <span> tags will not use CSS classes, but inline styles. This is not recommended for larger pieces of code since it increases output size by quite a bit.
The source-highlighter args attribute allows the inclusion of arbitrary (highlighter dependent) command options.
== Alternate Pygments Theme
:source-highlighter: pygments
:pygments-style: monokai

[source,php,numbered]
.Using global theme [label label-default]#{pygments-style}#
----
<?php
use Symfony\Component\Finder\Finder;

$finder = new Finder();
$finder->files()->in(__DIR__);

foreach ($finder as $file) {
    // affiche le chemin absolu
    print $file->getRealpath()."\n";
    // affiche le chemin relatif d'un fichier, sans le nom du fichier
    print $file->getRelativePath()."\n";
    // affiche le chemin relatif du fichier
    print $file->getRelativePathname()."\n";
}
----

:local-css-style: pastie
[args="-O style={local-css-style},noclasses=True"]
[source,css]
.Using local theme [label label-default]#{local-css-style}#
----
/*
   Bootstrap backend responsive styles for Pygments Highlighter
*/

.listingblock  .content {
    margin-bottom: 1em;
    overflow: auto;
}
.highlighttable {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
}
.highlighttable  .linenos {
    padding: 0;
    width: 4%;
}
.highlighttable .code {
    overflow: auto;
    padding: 0 1px;
}
table  pre {
    margin: 0;
    white-space: pre;
}
.highlight {
    overflow: auto;
}
.highlight pre {
    background-color: transparent;
    margin: 0;
}
----

Renders

Alternate Pygments Theme

Using global theme monokai
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
use Symfony\Component\Finder\Finder;

$finder = new Finder();
$finder->files()->in(__DIR__);

foreach ($finder as $file) {
    // affiche le chemin absolu
    print $file->getRealpath()."\n";
    // affiche le chemin relatif d'un fichier, sans le nom du fichier
    print $file->getRelativePath()."\n";
    // affiche le chemin relatif du fichier
    print $file->getRelativePathname()."\n";
}
Using local theme pastie
/*
   Bootstrap backend responsive styles for Pygments Highlighter
*/

.listingblock  .content {
    margin-bottom: 1em;
    overflow: auto;
}
.highlighttable {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
}
.highlighttable  .linenos {
    padding: 0;
    width: 4%;
}
.highlighttable .code {
    overflow: auto;
    padding: 0 1px;
}
table  pre {
    margin: 0;
    white-space: pre;
}
.highlight {
    overflow: auto;
}
.highlight pre {
    background-color: transparent;
    margin: 0;
}