Public Interface

Public Interface

Highlights Module

Highlights.jl is a Julia package for source code highlighting. It provides a regular expression based mechanism for creating lexers in a similar way to Pygments.

The following names are exported from the root module, Highlights, and are available for external use. Note that unexported names are considered unstable and subject to change.

source

Highlight source code using a specific lexer, mimetype and theme.

highlight(io, mime, src, lexer)
highlight(io, mime, src, lexer, theme)

src is tokenised using the provided lexer, then colourised using theme, and finally output to io in the given format mime. theme defaults to Themes.DefaultTheme theme.

mime can be either MIME("text/html") or MIME("text/latex").

Examples

julia> using Highlights

julia> highlight(stdout, MIME("text/html"), "2x", Lexers.JuliaLexer)
<pre class='hljl'>
<span class='hljl-ni'>2</span><span class='hljl-n'>x</span>
</pre>

julia> highlight(stdout, MIME("text/latex"), "'x'", Lexers.JuliaLexer, Themes.VimTheme)
\begin{lstlisting}
(*@\HLJLsc{{\textquotesingle}x{\textquotesingle}}@*)
\end{lstlisting}
source
Highlights.lexerMethod.
lexer(name)

Return the AbstractLexer associated with the lexer named name. name must be a string. Internally this checks the :aliases field in each lexer definition to see whether it is a match.

Warning

This method is not type stable.

When no lexer matches the given name an ArgumentError is thrown.

source

Generate a "stylesheet" for the given theme.

stylesheet(io, mime)
stylesheet(io, mime, theme)

Prints out the style information needed to colourise source code in the given theme. Note that theme defaults to Themes.DefaultTheme. Output is printed to io in the format mime. mine can be one of

  • MIME("text/html")
  • MIME("text/css")
  • MIME("text/latex")

Examples

julia> using Highlights

julia> buf = IOBuffer();

julia> stylesheet(buf, MIME("text/css"), Themes.EmacsTheme)
source

Themes Submodule

Highlights.ThemesModule.

A submodule that provides a selection of themes that can be used to colourise source code.

source

The default colour scheme with colours based on the Julia logo.

source

A theme based on the Emacs colour scheme.

source

A GitHub inspired colour scheme.

source

A colour scheme similar to the Monokai theme that works on both light and dark backgrounds.

source

A colour scheme similar to the Monokai theme.

source

Based on the default colour scheme used by the Pygments highlighter.

source

A Tango-inspired colour scheme.

source

Based on the default trac highlighter.

source

A Vim 7.0 based colour scheme.

source

A theme based on the default Visual Studio colours.

source

A theme based on the default Xcode colour scheme.

source

Construct a Style object.

source

Declare the theme definition for theme T based on dict. dict must be a Dict and T must be a subtype of AbstractTheme.

Examples

julia> using Highlights.Themes

julia> abstract type CustomTheme <: AbstractTheme end

julia> @theme CustomTheme Dict(
           :name => "Custom",
           :tokens => Dict(
               # ...
           )
       );
source

Lexers Submodule

Highlights.LexersModule.

An exported submodule that provides a selection of lexer definitions for tokenising source code. The following names are exported from the module:

source

A FORTRAN 90 source code lexer.

source

A lexer for Julia REPL sessions.

source

A lexer for Julia source code.

source

A lexer for MATLAB source code.

source

A lexer for the R language.

source

TOML (Tom's Obvious, Minimal Language) lexer.

source

Declare the lexer definition for a custom lexer T using a Dict object dict. dict must either be a Dict literal or an expression that returns a Dict – such as a let block.

Examples

julia> using Highlights.Lexers

julia> abstract type CustomLexer <: AbstractLexer end

julia> @lexer CustomLexer Dict(
           :name => "Custom",
           :tokens => Dict(
               # ...
           )
       );
source

Tokens Submodule

Highlights.TokensModule.

This submodule provides a collection of token names for use in lexer and theme definitions. The table shown below lists all the exported tokens as well as the abbreviations used when printing stylesheets and highlighted source code.

TokenAbbreviation
TEXTt
WHITESPACEw
ESCAPEe
ERROReB
OTHERo
KEYWORDk
KEYWORD_CONSTANTkc
KEYWORD_DECLARATIONkd
KEYWORD_NAMESPACEkn
KEYWORD_PSEUDOkp
KEYWORD_RESERVEDkr
KEYWORD_TYPEkt
NAMEn
NAME_ATTRIBUTEna
NAME_BUILTINnb
NAME_BUILTIN_PSEUDOnbp
NAME_CLASSnc
NAME_CONSTANTncB
NAME_DECORATORnd
NAME_ENTITYne
NAME_EXCEPTIONneB
NAME_FUNCTIONnf
NAME_FUNCTION_MAGICnfm
NAME_PROPERTYnp
NAME_LABELnl
NAME_NAMESPACEnn
NAME_OTHERno
NAME_TAGnt
NAME_VARIABLEnv
NAME_VARIABLE_CLASSnvc
NAME_VARIABLE_GLOBALnvg
NAME_VARIABLE_INSTANCEnvi
NAME_VARIABLE_MAGICnvm
LITERALl
LITERAL_DATEld
STRINGs
STRING_AFFIXsa
STRING_BACKTICKsb
STRING_CHARsc
STRING_DELIMITERsd
STRING_DOCsdB
STRING_DOUBLEsdC
STRING_ESCAPEse
STRING_HEREDOCsh
STRING_INTERPOLsi
STRING_OTHERso
STRING_REGEXsr
STRING_SINGLEss
STRING_SYMBOLssB
NUMBERnB
NUMBER_BINnbB
NUMBER_FLOATnfB
NUMBER_HEXnh
NUMBER_INTEGERni
NUMBER_INTEGER_LONGnil
NUMBER_OCTnoB
OPERATORoB
OPERATOR_WORDow
PUNCTUATIONp
COMMENTc
COMMENT_HASHBANGch
COMMENT_MULTILINEcm
COMMENT_PREPROCcp
COMMENT_PREPROCFILEcpB
COMMENT_SINGLEcs
COMMENT_SPECIALcsB
GENERICg
GENERIC_DELETEDgd
GENERIC_EMPHge
GENERIC_ERRORgeB
GENERIC_HEADINGgh
GENERIC_INSERTEDgi
GENERIC_OUTPUTgo
GENERIC_PROMPTgp
GENERIC_STRONGgs
GENERIC_SUBHEADINGgsB
GENERIC_TRACEBACKgt
source

Format Submodule

Highlights.FormatModule.

The Format module provides a public interface that can be used to define custom formatters aside from the predefined HTML and LaTeX outputs supported by Highlights.

The Formatting section of the manual provides a example of how to go about extending Highlights to support additional output formats.

The following functions and types are exported from the module for public use:

source
struct TokenIterator

An iterator type used in user-defined render methods to provide custom output formats. This type supports the basic Julia iterator protocol: namely iterate which enables for-loop interation over tokenised text.

The iterator returns a 3-tuple of str, id, and style where

  • str is the SubString covered by the token;
  • id is the shorthand name of the token type as a Symbol;
  • style is the Style applied to the token.
source

The render function is used to define custom output formats for tokenised source code. Methods with signatures of the form

function Format.render(io::IO, mime::MIME, tokens::Format.TokenIterator)
    # ...
    for (str, id, style) in tokens
        # ...
    end
    # ...
end

can be defined for any mime type to allow the highlight function to support new output formats.

source