Skip to content

Commit

Permalink
document how to define and apply a custom Rouge theme
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Nov 23, 2016
1 parent fdb2624 commit d8ad7cd
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/theming-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3174,6 +3174,71 @@ As a workaround, Asciidoctor PDF inserts a blank page, if necessary, to ensure t

You can check on the status of this defect by following https://github.com/asciidoctor/asciidoctor-pdf/issues/95[issue #95].

== Source Highlighting Theme

You can define and apply your own source highlighting theme to source blocks when using Rouge as the source highlighter.
This section explains how.

A custom Rouge theme is defined using a Ruby source file.
Start by creating a Ruby file to define your theme.
Name the file according to the name of your theme and put the file in a folder of your choice (e.g., [.path]_rouge_themes/custom.rb_).
The name of the Ruby class doesn't matter, though it's customary to name it according to the name of the theme as well.

.rouge_themes/custom.rb
[source,ruby]
----
module Rouge; module Themes
class Custom < CSSTheme
name 'custom'
style Comment, fg: '#008800', italic: true
style Error, fg: '#a61717', bg: '#e3d2d2'
style Str, fg: '#0000ff'
style Str::Char, fg: '#800080'
style Num, fg: '#0000ff'
style Keyword, fg: '#000080', bold: true
style Operator::Word, bold: true
style Name::Tag, fg: '#000080', bold: true
style Name::Attribute, fg: '#ff0000'
style Generic::Deleted, fg: '#000000', bg: '#ffdddd', inline_block: true, extend: true
style Generic::Inserted, fg: '#000000', bg: '#ddffdd', inline_block: true, extend: true
style Text, {}
end
end; end
----

Each style declaration accepts the following properties:

* `fg` - sets the foreground (text) color
* `bg` - sets the background color
* `bold` - change the font weight to bold
* `italic` - change the font style to italic
* `underline` - add an underline to the text
* `inline_block` - fill the background color to the height of the line (Asciidoctor PDF only)
* `extend` - extend the background color to the end of the line for a line-oriented match (Asciidoctor PDF only)

Colors are defined using hexidecimal format (e.g., #ff0000 for red).

Use the `Text` token to set the background color of the source block and the default text color.

The complete list of tokens can be found in the https://github.com/jneen/rouge/blob/master/lib/rouge/token.rb[token.rb] file from Rouge.
Refer to the https://github.com/jneen/rouge/tree/master/lib/rouge/themes[bundled themes] to find more examples.

Once you've defined your theme, you need to enable it use it using the `rouge-style` document attribute, which you specify in the document header or via the Asciidoctor CLI or API.

[source,asciidoc]
----
:source-highlighter: rouge
:rouge-style: custom
----

Finally, you need to active your theme by requiring the theme file when you invoke Asciidoctor.

$ asciidoctor -r ./rouge_themes/custom.rb sample.adoc

You should now see that source code is highlighted to your liking.
For more information about source highlighting with Rouge, refer to the http://rouge.jneen.net/[Rouge project page].

////
== Resources for Extending Asciidoctor PDF
Expand Down

0 comments on commit d8ad7cd

Please sign in to comment.