Public Interface
Highlights
Module
Highlights.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.
Highlights.highlight
— Method.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}
Highlights.lexer
— Method.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.
This method is not type stable.
When no lexer matches the given name
an ArgumentError
is thrown.
Highlights.stylesheet
— Method.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)
Themes
Submodule
Highlights.Themes
— Module.A submodule that provides a selection of themes that can be used to colourise source code.
Highlights.Themes.DefaultTheme
— Type.The default colour scheme with colours based on the Julia logo.
Highlights.Themes.EmacsTheme
— Type.A theme based on the Emacs colour scheme.
Highlights.Themes.GitHubTheme
— Type.A GitHub inspired colour scheme.
A colour scheme similar to the Monokai theme that works on both light and dark backgrounds.
Highlights.Themes.MonokaiTheme
— Type.A colour scheme similar to the Monokai theme.
Highlights.Themes.PygmentsTheme
— Type.Based on the default colour scheme used by the Pygments highlighter.
Highlights.Themes.TangoTheme
— Type.A Tango-inspired colour scheme.
Highlights.Themes.TracTheme
— Type.Based on the default trac highlighter.
Highlights.Themes.VimTheme
— Type.A Vim 7.0 based colour scheme.
A theme based on the default Visual Studio colours.
Highlights.Themes.XcodeTheme
— Type.A theme based on the default Xcode colour scheme.
Highlights.Themes.@S_str
— Macro.Construct a Style
object.
Highlights.Themes.@theme
— Macro.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(
# ...
)
);
Lexers
Submodule
Highlights.Lexers
— Module.An exported submodule that provides a selection of lexer definitions for tokenising source code. The following names are exported from the module:
Highlights.Lexers.FortranLexer
— Type.A FORTRAN 90 source code lexer.
A lexer for Julia REPL sessions.
Highlights.Lexers.JuliaLexer
— Type.A lexer for Julia source code.
Highlights.Lexers.MatlabLexer
— Type.A lexer for MATLAB source code.
Highlights.Lexers.RLexer
— Type.A lexer for the R language.
Highlights.Lexers.TOMLLexer
— Type.TOML (Tom's Obvious, Minimal Language) lexer.
Highlights.Lexers.@lexer
— Macro.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(
# ...
)
);
Tokens
Submodule
Highlights.Tokens
— Module.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.
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
— Module.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:
Highlights.Format.TokenIterator
— Type.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 theSubString
covered by the token;id
is the shorthand name of the token type as aSymbol
;style
is theStyle
applied to the token.
Highlights.Format.render
— Function.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.