Documentation
¶
Overview ¶
Package rouge is a pure-Go (CGO=0), MRI-faithful port of the Ruby `rouge` syntax-highlighting gem (https://github.com/rouge-ruby/rouge). It tokenizes source code with state-machine regex lexers and formats the token stream as HTML, emitting exactly the CSS short-codes (k, s, c, nf, ...) the gem emits.
The token hierarchy, the regex-lexer engine, and the HTML formatters mirror the gem's data structures so the rendered HTML matches byte-for-byte on the supported lexers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Text is the root token; it has the empty short-code. Text = tok(nil, "Text", "") TextWhitespace = tok(Text, "Whitespace", "w") Escape = tok(nil, "Escape", "esc") Error = tok(nil, "Error", "err") Other = tok(nil, "Other", "x") Keyword = tok(nil, "Keyword", "k") KeywordConstant = tok(Keyword, "Constant", "kc") KeywordDeclaration = tok(Keyword, "Declaration", "kd") KeywordNamespace = tok(Keyword, "Namespace", "kn") KeywordPseudo = tok(Keyword, "Pseudo", "kp") KeywordReserved = tok(Keyword, "Reserved", "kr") KeywordType = tok(Keyword, "Type", "kt") KeywordVariable = tok(Keyword, "Variable", "kv") Name = tok(nil, "Name", "n") NameAttribute = tok(Name, "Attribute", "na") NameBuiltin = tok(Name, "Builtin", "nb") NameBuiltinPseudo = tok(NameBuiltin, "Pseudo", "bp") NameClass = tok(Name, "Class", "nc") NameConstant = tok(Name, "Constant", "no") NameDecorator = tok(Name, "Decorator", "nd") NameEntity = tok(Name, "Entity", "ni") NameException = tok(Name, "Exception", "ne") NameFunction = tok(Name, "Function", "nf") NameFunctionMagic = tok(NameFunction, "Magic", "fm") NameProperty = tok(Name, "Property", "py") NameLabel = tok(Name, "Label", "nl") NameNamespace = tok(Name, "Namespace", "nn") NameOther = tok(Name, "Other", "nx") NameTag = tok(Name, "Tag", "nt") NameVariable = tok(Name, "Variable", "nv") NameVariableClass = tok(NameVariable, "Class", "vc") NameVariableGlobal = tok(NameVariable, "Global", "vg") NameVariableInstance = tok(NameVariable, "Instance", "vi") NameVariableMagic = tok(NameVariable, "Magic", "vm") Literal = tok(nil, "Literal", "l") LiteralDate = tok(Literal, "Date", "ld") LiteralString = tok(Literal, "String", "s") LiteralStringAffix = tok(LiteralString, "Affix", "sa") LiteralStringBacktick = tok(LiteralString, "Backtick", "sb") LiteralStringChar = tok(LiteralString, "Char", "sc") LiteralStringDelimiter = tok(LiteralString, "Delimiter", "dl") LiteralStringDoc = tok(LiteralString, "Doc", "sd") LiteralStringDouble = tok(LiteralString, "Double", "s2") LiteralStringEscape = tok(LiteralString, "Escape", "se") LiteralStringHeredoc = tok(LiteralString, "Heredoc", "sh") LiteralStringInterpol = tok(LiteralString, "Interpol", "si") LiteralStringOther = tok(LiteralString, "Other", "sx") LiteralStringRegex = tok(LiteralString, "Regex", "sr") LiteralStringSingle = tok(LiteralString, "Single", "s1") LiteralStringSymbol = tok(LiteralString, "Symbol", "ss") LiteralNumber = tok(Literal, "Number", "m") LiteralNumberBin = tok(LiteralNumber, "Bin", "mb") LiteralNumberFloat = tok(LiteralNumber, "Float", "mf") LiteralNumberHex = tok(LiteralNumber, "Hex", "mh") LiteralNumberInteger = tok(LiteralNumber, "Integer", "mi") LiteralNumberIntegerLong = tok(LiteralNumberInteger, "Long", "il") LiteralNumberOct = tok(LiteralNumber, "Oct", "mo") LiteralNumberOther = tok(LiteralNumber, "Other", "mx") Operator = tok(nil, "Operator", "o") OperatorWord = tok(Operator, "Word", "ow") Punctuation = tok(nil, "Punctuation", "p") PunctuationIndicator = tok(Punctuation, "Indicator", "pi") Comment = tok(nil, "Comment", "c") CommentHashbang = tok(Comment, "Hashbang", "ch") CommentDoc = tok(Comment, "Doc", "cd") CommentMultiline = tok(Comment, "Multiline", "cm") CommentPreproc = tok(Comment, "Preproc", "cp") CommentPreprocFile = tok(Comment, "PreprocFile", "cpf") CommentSingle = tok(Comment, "Single", "c1") CommentSpecial = tok(Comment, "Special", "cs") Generic = tok(nil, "Generic", "g") GenericDeleted = tok(Generic, "Deleted", "gd") GenericEmph = tok(Generic, "Emph", "ge") GenericEmphStrong = tok(Generic, "EmphStrong", "ges") GenericError = tok(Generic, "Error", "gr") GenericHeading = tok(Generic, "Heading", "gh") GenericInserted = tok(Generic, "Inserted", "gi") GenericLineno = tok(Generic, "Lineno", "gl") GenericOutput = tok(Generic, "Output", "go") GenericPrompt = tok(Generic, "Prompt", "gp") GenericStrong = tok(Generic, "Strong", "gs") GenericSubheading = tok(Generic, "Subheading", "gu") GenericTraceback = tok(Generic, "Traceback", "gt") )
The token hierarchy. These mirror Rouge::Token::Tokens one-for-one, including the CSS short-codes, which are kept in sync with pygments STANDARD_TYPES.
var Base16 = &Theme{name: "base16", rules: map[string]string{
"Text": "color: #383838",
"Error": "color: #181818;background-color: #ab4642",
"Comment": "color: #585858",
"Comment.Preproc": "color: #f7ca88",
"Name.Tag": "color: #f7ca88",
"Operator": "color: #d8d8d8",
"Punctuation": "color: #d8d8d8",
"Generic.Inserted": "color: #a1b56c",
"Generic.Deleted": "color: #ab4642",
"Generic.Heading": "color: #7cafc2;background-color: #181818;font-weight: bold",
"Generic.Emph": "font-style: italic",
"Generic.EmphStrong": "font-weight: bold;font-style: italic",
"Generic.Strong": "font-weight: bold",
"Keyword": "color: #ba8baf",
"Keyword.Constant": "color: #dc9656",
"Keyword.Type": "color: #dc9656",
"Keyword.Declaration": "color: #dc9656",
"Literal.String": "color: #a1b56c",
"Literal.String.Affix": "color: #ba8baf",
"Literal.String.Regex": "color: #86c1b9",
"Literal.String.Interpol": "color: #a16946",
"Literal.String.Escape": "color: #a16946",
"Name.Namespace": "color: #f7ca88",
"Name.Class": "color: #f7ca88",
"Name.Constant": "color: #f7ca88",
"Name.Attribute": "color: #7cafc2",
"Literal.Number": "color: #a1b56c",
"Literal.String.Symbol": "color: #a1b56c",
}}
Base16 is the rouge "base16" theme.
var Github = &Theme{name: "github", rules: map[string]string{
"Text": "color: #24292f;background-color: #f6f8fa",
"Keyword": "color: #cf222e",
"Generic.Error": "color: #f6f8fa",
"Generic.Deleted": "color: #82071e;background-color: #ffebe9",
"Name.Builtin": "color: #953800",
"Name.Class": "color: #953800",
"Name.Constant": "color: #953800",
"Name.Namespace": "color: #953800",
"Literal.String.Regex": "color: #116329",
"Name.Attribute": "color: #116329",
"Name.Tag": "color: #116329",
"Generic.Inserted": "color: #116329;background-color: #dafbe1",
"Generic.EmphStrong": "font-weight: bold;font-style: italic",
"Keyword.Constant": "color: #0550ae",
"Literal": "color: #0550ae",
"Literal.String.Backtick": "color: #0550ae",
"Name.Builtin.Pseudo": "color: #0550ae",
"Name.Exception": "color: #0550ae",
"Name.Label": "color: #0550ae",
"Name.Property": "color: #0550ae",
"Name.Variable": "color: #0550ae",
"Operator": "color: #0550ae",
"Generic.Heading": "color: #0550ae;font-weight: bold",
"Generic.Subheading": "color: #0550ae;font-weight: bold",
"Literal.String": "color: #0a3069",
"Name.Decorator": "color: #8250df",
"Name.Function": "color: #8250df",
"Error": "color: #f6f8fa;background-color: #82071e",
"Comment": "color: #6e7781",
"Generic.Lineno": "color: #6e7781",
"Generic.Traceback": "color: #6e7781",
"Name.Entity": "color: #24292f",
"Literal.String.Interpol": "color: #24292f",
"Generic.Emph": "color: #24292f;font-style: italic",
"Generic.Strong": "color: #24292f;font-weight: bold",
}}
Github is the rouge "github" theme.
var ThankfulEyes = &Theme{name: "thankful_eyes", rules: map[string]string{
"Text": "color: #faf6e4;background-color: #122b3b",
"Generic.Lineno": "color: #dee5e7;background-color: #4e5d62",
"Generic.Prompt": "color: #a8e1fe;font-weight: bold",
"Comment": "color: #6c8b9f;font-style: italic",
"Comment.Preproc": "color: #b2fd6d;font-weight: bold",
"Error": "color: #fefeec;background-color: #cc0000",
"Generic.Error": "color: #cc0000;font-weight: bold;font-style: italic",
"Keyword": "color: #f6dd62;font-weight: bold",
"Operator": "color: #4df4ff;font-weight: bold",
"Punctuation": "color: #4df4ff",
"Generic.Deleted": "color: #cc0000",
"Generic.Inserted": "color: #b2fd6d",
"Generic.Emph": "font-style: italic",
"Generic.EmphStrong": "font-weight: bold;font-style: italic",
"Generic.Strong": "font-weight: bold",
"Generic.Traceback": "color: #dee5e7;background-color: #4e5d62",
"Keyword.Constant": "color: #f696db;font-weight: bold",
"Keyword.Namespace": "color: #ffb000;font-weight: bold",
"Keyword.Pseudo": "color: #ffb000;font-weight: bold",
"Keyword.Reserved": "color: #ffb000;font-weight: bold",
"Generic.Heading": "color: #ffb000;font-weight: bold",
"Generic.Subheading": "color: #ffb000;font-weight: bold",
"Keyword.Type": "color: #b2fd6d;font-weight: bold",
"Name.Constant": "color: #b2fd6d;font-weight: bold",
"Name.Class": "color: #b2fd6d;font-weight: bold",
"Name.Decorator": "color: #b2fd6d;font-weight: bold",
"Name.Namespace": "color: #b2fd6d;font-weight: bold",
"Name.Builtin.Pseudo": "color: #b2fd6d;font-weight: bold",
"Name.Exception": "color: #b2fd6d;font-weight: bold",
"Name.Label": "color: #ffb000;font-weight: bold",
"Name.Tag": "color: #ffb000;font-weight: bold",
"Literal.Number": "color: #f696db;font-weight: bold",
"Literal.Date": "color: #f696db;font-weight: bold",
"Literal.String.Symbol": "color: #f696db;font-weight: bold",
"Literal.String": "color: #fff0a6;font-weight: bold",
"Literal.String.Affix": "color: #f6dd62;font-weight: bold",
"Literal.String.Escape": "color: #4df4ff;font-weight: bold",
"Literal.String.Char": "color: #4df4ff;font-weight: bold",
"Literal.String.Interpol": "color: #4df4ff;font-weight: bold",
"Name.Builtin": "font-weight: bold",
"Name.Entity": "color: #999999;font-weight: bold",
"Text.Whitespace": "color: #BBBBBB",
"Generic.Output": "color: #BBBBBB",
"Name.Function": "color: #a8e1fe",
"Name.Property": "color: #a8e1fe",
"Name.Attribute": "color: #a8e1fe",
"Name.Variable": "color: #a8e1fe;font-weight: bold",
}}
ThankfulEyes is the rouge "thankful_eyes" theme.
Functions ¶
func Highlight ¶
Highlight tokenizes text with the named lexer and renders it with the named formatter, returning the formatted string. It mirrors Rouge.highlight. An unknown lexer or formatter name returns an error.
Types ¶
type Formatter ¶
type Formatter interface {
// Format renders the whole token stream to a string.
Format(tokens []TokenValue) string
// Tag returns the formatter's registry tag (e.g. "html").
Tag() string
}
Formatter renders a token stream into a string (HTML, inline-styled HTML, ...), mirroring Rouge::Formatter.
func FindFormatter ¶
FindFormatter returns the formatter registered under tag, or nil. Mirrors Rouge::Formatter.find.
type HTMLFormatter ¶
type HTMLFormatter struct{}
HTMLFormatter renders the token stream as <span class="shortcode"> elements, mirroring Rouge::Formatters::HTML. The bare Text token and the Escape token pass their value through without a wrapping span. Highlight does not add a <pre>/<code> wrapper; use WrapHTML for the gem's themed-page wrapper.
func (HTMLFormatter) Format ¶
func (HTMLFormatter) Format(tokens []TokenValue) string
Format renders the stream. Each token becomes its escaped value wrapped in a span carrying the token's CSS short-code, except Text (passed through) and Escape (passed through unescaped, as in Rouge's escape?-aware path).
type HTMLInlineFormatter ¶
type HTMLInlineFormatter struct {
// Theme supplies per-token CSS declarations.
Theme *Theme
}
HTMLInlineFormatter renders each token with an inline style attribute computed from a Theme, mirroring Rouge::Formatters::HTMLInline. Text passes through without a span.
func (HTMLInlineFormatter) Format ¶
func (f HTMLInlineFormatter) Format(tokens []TokenValue) string
Format renders the stream with inline styles from the formatter's Theme.
type Lexer ¶
type Lexer interface {
// Lex returns the token stream for text. The stream never contains empty
// values.
Lex(text string) []TokenValue
// Tag returns the lexer's primary tag.
Tag() string
// Title returns the human-readable title.
Title() string
// Aliases returns alternate names.
Aliases() []string
}
Lexer tokenizes source text into a stream of (token, value) pairs. Every lexer in this package is a *RegexLexer, but the interface lets callers and formatters work against the abstraction, mirroring Rouge::Lexer.
func FindFancy ¶
FindFancy resolves a "fancy" lexer spec of the form "tag" or "tag?option=...", mirroring Rouge::Lexer.find_fancy. Options are accepted and ignored (this port has no per-lex options that change tokenization). An empty or "guess" name returns nil so callers can fall back to Guess. An unknown tag returns nil.
func FindLexer ¶
FindLexer returns the lexer registered under the given tag or alias, or nil if none matches. The lookup is case-insensitive on the tag, mirroring Rouge::Lexer.find.
func Guess ¶
Guess picks a lexer for text using each lexer's content sniffer (Rouge's self.detect?). The first lexer (in registration order) that claims the text wins; if none does, PlainText is returned, so Guess never returns nil. This is a deliberately small subset of Rouge's multi-signal guesser: it uses only the content detectors, which is enough for the formats whose detectors are unambiguous (e.g. diff). See README "Simplified" notes.
type RegexLexer ¶
type RegexLexer struct {
// contains filtered or unexported fields
}
RegexLexer is a stateful, regex-rule lexer. A concrete lexer registers its states with newRegexLexer / state-builder helpers and exposes itself through the Lexer interface. It is immutable after construction and safe for concurrent use; per-lex mutable state lives in lexState.
func (*RegexLexer) Aliases ¶
func (rl *RegexLexer) Aliases() []string
Aliases returns the lexer's alternate names (e.g. "rb" for Ruby).
func (*RegexLexer) Lex ¶
func (rl *RegexLexer) Lex(text string) []TokenValue
Lex tokenizes text and returns the token stream, mirroring Rouge::Lexer#lex for a RegexLexer. The stream never contains empty values.
func (*RegexLexer) Tag ¶
func (rl *RegexLexer) Tag() string
Tag returns the lexer's primary tag (e.g. "ruby").
func (*RegexLexer) Title ¶
func (rl *RegexLexer) Title() string
Title returns the lexer's human-readable title.
type Theme ¶
type Theme struct {
// contains filtered or unexported fields
}
Theme maps tokens to inline CSS declarations for the HTMLInline formatter, mirroring the resolved output of Rouge::Theme#style_for(tok).rendered_rules. Lookup walks the token's ancestor chain and returns the rules of the most specific ancestor that has a style, falling back to the base (Text) style, exactly like Rouge::Theme.get_style.
func FindTheme ¶
FindTheme returns the theme registered under name, or nil. Mirrors Rouge::Theme.find.
func (*Theme) StyleFor ¶
StyleFor returns the ";"-joined inline CSS declarations for tok, resolving up the ancestor chain the way Rouge::Theme.get_style does: the closest ancestor (including tok itself) with a defined style wins; if none is defined the base Text style is used; if even Text is undefined the result is empty.
type Token ¶
type Token struct {
// Name is the simple token name, e.g. "Function".
Name string
// Shortname is the CSS class the HTML formatter emits, e.g. "nf". The root
// Text token has the empty short-code.
Shortname string
// Parent is the enclosing token, nil for the root Text token.
Parent *Token
// contains filtered or unexported fields
}
Token is a syntactic token type, e.g. Keyword or Literal.String. Tokens form a tree mirroring Rouge::Token::Tokens: every token has a Name, a one- or two-letter CSS Shortname the HTML formatter emits, and a Parent. Tokens are compared by identity (pointer), so the package-level token variables below are the canonical instances.
func TokenByName ¶
TokenByName returns the canonical token for a dotted qualified name (e.g. "Keyword.Constant"), or nil if no such token exists. It mirrors Rouge::Token::Tokens[qualname].
func (*Token) Matches ¶
Matches reports whether t is other or a descendant of other, mirroring Rouge::Token.matches? (other appears in t's ancestor chain).
type TokenValue ¶
TokenValue is a (token, value) pair in the lexed stream.
type UnknownError ¶
type UnknownError struct {
// Kind is "lexer" or "formatter".
Kind string
// Name is the unrecognized name.
Name string
}
UnknownError reports an unknown lexer or formatter name from Highlight.
func (*UnknownError) Error ¶
func (e *UnknownError) Error() string
