Theme Guide

Theme Guide

This page outlines how to go about adding new theme definitions to Highlights.

Required Imports

To get started adding a new theme definition you will need to import the following two submodules of Highlights.

using Highlights.Tokens, Highlights.Themes

Tokens provides a collection of tokens that are used to label different parts of the tokenised source code – such as TEXT or STRING. Themes provides the AbstractTheme type and @theme macro used to define new themes.

The AbstractTheme Type

AbstractTheme is the super type of all theme definitions in Highlights. A theme is just an abstract type that is a subtype of AbstractTheme. For this example we will define a new theme called, very imaginatively, CustomTheme:

abstract type CustomTheme <: AbstractTheme end

That's all there is to the type itself. Next we'll define what colours should be used for each token [1] when we highlight source code using our new theme.

The @theme Macro

Now we'll use the @theme macro to tell Highlights what colours we want different parts of our source code to be highlighted in. We do this by calling the @theme macro to define a set of rules that must be applied to each token.

@theme CustomTheme Dict(
    :style => S"bg: f7f3ee; fg: 605b53",
    :tokens => Dict(
        TEXT    => S"",
        KEYWORD => S"fg: 614c60; bold",
        STRING  => S"fg: a1789f",
        COMMENT => S"fg: ad9c84; italic",
    ),
)

There's a couple of things going on up there, so let's split it into sections:

Using the theme

Now that we've written a new theme we might as well try it out. We'll use the new theme to highlight itself:

open("custom-theme.html", "w") do stream
    stylesheet(stream, MIME("text/html"), CustomTheme)
    highlight(stream, MIME("text/html"), source, Lexers.JuliaLexer, CustomTheme)
end

The highlighted code can be found here.

[1]

"Tokens" refer to the substrings of the input string with an identified "meaning". For example :string, :number, :text, or :keyword.