Public Interface
Highlights Module
Highlights.Highlights — ModuleHighlights.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.
Highlights.highlight — MethodHighlight 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}
Highlights.lexer — Methodlexer(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.
When no lexer matches the given name an ArgumentError is thrown.
Highlights.stylesheet — MethodGenerate 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)
Themes Submodule
Highlights.Themes — ModuleA submodule that provides a selection of themes that can be used to colourise source code.
Highlights.Themes.DefaultTheme — TypeThe default colour scheme with colours based on the Julia logo.
Highlights.Themes.EmacsTheme — TypeA theme based on the Emacs colour scheme.
Highlights.Themes.GitHubTheme — TypeA GitHub inspired colour scheme.
Highlights.Themes.MonokaiMiniTheme — TypeA colour scheme similar to the Monokai theme that works on both light and dark backgrounds.
Highlights.Themes.MonokaiTheme — TypeA colour scheme similar to the Monokai theme.
Highlights.Themes.PygmentsTheme — TypeBased on the default colour scheme used by the Pygments highlighter.
Highlights.Themes.TangoTheme — TypeA Tango-inspired colour scheme.
Highlights.Themes.TracTheme — TypeBased on the default trac highlighter.
Highlights.Themes.VimTheme — TypeA Vim 7.0 based colour scheme.
Highlights.Themes.VisualStudioTheme — TypeA theme based on the default Visual Studio colours.
Highlights.Themes.XcodeTheme — TypeA theme based on the default Xcode colour scheme.
Highlights.Themes.@S_str — MacroConstruct a Style object.
Highlights.Themes.@theme — MacroDeclare 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(
               # ...
           )
       );Lexers Submodule
Highlights.Lexers — ModuleAn exported submodule that provides a selection of lexer definitions for tokenising source code. The following names are exported from the module:
Highlights.Lexers.FortranLexer — TypeA FORTRAN 90 source code lexer.
Highlights.Lexers.JuliaConsoleLexer — TypeA lexer for Julia REPL sessions.
Highlights.Lexers.JuliaLexer — TypeA lexer for Julia source code.
Highlights.Lexers.MatlabLexer — TypeA lexer for MATLAB source code.
Highlights.Lexers.RLexer — TypeA lexer for the R language.
Highlights.Lexers.TOMLLexer — TypeTOML (Tom's Obvious, Minimal Language) lexer.
Highlights.Lexers.@lexer — MacroDeclare 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(
               # ...
           )
       );
Tokens Submodule
Highlights.Tokens — ModuleThis 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.
| Token | Abbreviation | 
|---|---|
TEXT | t | 
WHITESPACE | w | 
ESCAPE | e | 
ERROR | eB | 
OTHER | o | 
KEYWORD | k | 
KEYWORD_CONSTANT | kc | 
KEYWORD_DECLARATION | kd | 
KEYWORD_NAMESPACE | kn | 
KEYWORD_PSEUDO | kp | 
KEYWORD_RESERVED | kr | 
KEYWORD_TYPE | kt | 
NAME | n | 
NAME_ATTRIBUTE | na | 
NAME_BUILTIN | nb | 
NAME_BUILTIN_PSEUDO | nbp | 
NAME_CLASS | nc | 
NAME_CONSTANT | ncB | 
NAME_DECORATOR | nd | 
NAME_ENTITY | ne | 
NAME_EXCEPTION | neB | 
NAME_FUNCTION | nf | 
NAME_FUNCTION_MAGIC | nfm | 
NAME_PROPERTY | np | 
NAME_LABEL | nl | 
NAME_NAMESPACE | nn | 
NAME_OTHER | no | 
NAME_TAG | nt | 
NAME_VARIABLE | nv | 
NAME_VARIABLE_CLASS | nvc | 
NAME_VARIABLE_GLOBAL | nvg | 
NAME_VARIABLE_INSTANCE | nvi | 
NAME_VARIABLE_MAGIC | nvm | 
LITERAL | l | 
LITERAL_DATE | ld | 
STRING | s | 
STRING_AFFIX | sa | 
STRING_BACKTICK | sb | 
STRING_CHAR | sc | 
STRING_DELIMITER | sd | 
STRING_DOC | sdB | 
STRING_DOUBLE | sdC | 
STRING_ESCAPE | se | 
STRING_HEREDOC | sh | 
STRING_INTERPOL | si | 
STRING_OTHER | so | 
STRING_REGEX | sr | 
STRING_SINGLE | ss | 
STRING_SYMBOL | ssB | 
NUMBER | nB | 
NUMBER_BIN | nbB | 
NUMBER_FLOAT | nfB | 
NUMBER_HEX | nh | 
NUMBER_INTEGER | ni | 
NUMBER_INTEGER_LONG | nil | 
NUMBER_OCT | noB | 
OPERATOR | oB | 
OPERATOR_WORD | ow | 
PUNCTUATION | p | 
COMMENT | c | 
COMMENT_HASHBANG | ch | 
COMMENT_MULTILINE | cm | 
COMMENT_PREPROC | cp | 
COMMENT_PREPROCFILE | cpB | 
COMMENT_SINGLE | cs | 
COMMENT_SPECIAL | csB | 
GENERIC | g | 
GENERIC_DELETED | gd | 
GENERIC_EMPH | ge | 
GENERIC_ERROR | geB | 
GENERIC_HEADING | gh | 
GENERIC_INSERTED | gi | 
GENERIC_OUTPUT | go | 
GENERIC_PROMPT | gp | 
GENERIC_STRONG | gs | 
GENERIC_SUBHEADING | gsB | 
GENERIC_TRACEBACK | gt | 
Format Submodule
Highlights.Format — ModuleThe 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:
Highlights.Format.TokenIterator — Typestruct TokenIteratorAn 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
stris theSubStringcovered by the token;idis the shorthand name of the token type as aSymbol;styleis theStyleapplied to the token.
Highlights.Format.render — FunctionThe 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
    # ...
endcan be defined for any mime type to allow the highlight function to support new output formats.