chromalexers

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package chromalexers is github.com/alecthomas/chroma/v2/lexers at v2.27.0 with two deliberate changes: the lexer registry is built lazily on first use instead of at package init, and the 279 embedded XML lexer configs ship as one gzip-compressed tar archive decoded on first use. Upstream's init decodes the XML configs (~6ms, ~2.7MB of allocations) on every process start, including `orb --version`, and the raw XML adds ~1.9MB to the binary; here the decode cost moves to the first highlight and the configs ship compressed. Keep every other file byte-identical to upstream apart from the package clause, the init() funcs converted to registration hooks, and the XML-backed package vars converted to lazy constructors (Register vs. registerLazy below).

Index

Constants

This section is empty.

Variables

View Source
var Caddyfile = Register(MustNewLexer(
	&Config{
		Name:      "Caddyfile",
		Aliases:   []string{"caddyfile", "caddy"},
		Filenames: []string{"Caddyfile*"},
		MimeTypes: []string{},
	},
	caddyfileRules,
))

Caddyfile lexer.

View Source
var CaddyfileDirectives = Register(MustNewLexer(
	&Config{
		Name:      "Caddyfile Directives",
		Aliases:   []string{"caddyfile-directives", "caddyfile-d", "caddy-d"},
		Filenames: []string{},
		MimeTypes: []string{},
	},
	caddyfileDirectivesRules,
))

Caddyfile directive-only lexer.

View Source
var CommonLisp = registerLazy(func() Lexer {
	return TypeRemappingLexer(MustNewXMLLexer(
		embeddedLexers(),
		"embedded/common_lisp.xml",
	), TypeMapping{
		{NameVariable, NameFunction, clBuiltinFunctions},
		{NameVariable, Keyword, clSpecialForms},
		{NameVariable, NameBuiltin, clMacros},
		{NameVariable, Keyword, clLambdaListKeywords},
		{NameVariable, Keyword, clDeclarations},
		{NameVariable, KeywordType, clBuiltinTypes},
		{NameVariable, NameClass, clBuiltinClasses},
	})
})

Common Lisp lexer.

View Source
var ERB = registerLazy(func() Lexer {
	return DelegatingLexer(HTML(), MustNewXMLLexer(
		embeddedLexers(),
		"embedded/erb.xml",
	).SetConfig(
		&Config{
			Name:      "ERB",
			Aliases:   []string{"erb", "html+erb", "html+ruby", "rhtml"},
			Filenames: []string{"*.erb", "*.html.erb", "*.xml.erb", "*.rhtml"},
			MimeTypes: []string{"application/x-ruby-templating"},
			DotAll:    true,
		},
	).SetAnalyser(func(text string) float32 {
		if strings.Contains(text, "<%=") && strings.Contains(text, "%>") {
			return 0.4
		}
		if strings.Contains(text, "<%") {
			return 0.1
		}
		return 0.0
	}))
})

ERB lexer is Ruby embedded in HTML.

View Source
var EmacsLisp = registerLazy(func() Lexer {
	return TypeRemappingLexer(MustNewXMLLexer(
		embeddedLexers(),
		"embedded/emacslisp.xml",
	), TypeMapping{
		{NameVariable, NameFunction, emacsBuiltinFunction},
		{NameVariable, NameBuiltin, emacsSpecialForms},
		{NameVariable, NameException, emacsErrorKeywords},
		{NameVariable, NameBuiltin, append(emacsBuiltinFunctionHighlighted, emacsMacros...)},
		{NameVariable, KeywordPseudo, emacsLambdaListKeywords},
	})
})

EmacsLisp lexer.

View Source
var Fallback chroma.Lexer = chroma.MustNewLexer(&chroma.Config{
	Name:      "fallback",
	Filenames: []string{"*"},
	Priority:  -1,
}, PlaintextRules)

Fallback lexer if no other is found.

View Source
var Gemtext = Register(MustNewLexer(
	&Config{
		Name:      "Gemtext",
		Aliases:   []string{"gemtext", "gmi", "gmni", "gemini"},
		Filenames: []string{"*.gmi", "*.gmni", "*.gemini"},
		MimeTypes: []string{"text/gemini"},
	},
	gemtextRules,
))

Gemtext lexer.

View Source
var Genshi = Register(MustNewLexer(
	&Config{
		Name:         "Genshi",
		Aliases:      []string{"genshi", "kid", "xml+genshi", "xml+kid"},
		Filenames:    []string{"*.kid"},
		MimeTypes:    []string{"application/x-genshi", "application/x-kid"},
		NotMultiline: true,
		DotAll:       true,
	},
	genshiMarkupRules,
))

Genshi lexer.

View Source
var GenshiHTMLTemplate = Register(MustNewLexer(
	&Config{
		Name:         "Genshi HTML",
		Aliases:      []string{"html+genshi", "html+kid"},
		Filenames:    []string{},
		MimeTypes:    []string{"text/html+genshi"},
		NotMultiline: true,
		DotAll:       true,
	},
	genshiMarkupRules,
))

Html+Genshi lexer.

View Source
var GenshiText = Register(MustNewLexer(
	&Config{
		Name:      "Genshi Text",
		Aliases:   []string{"genshitext"},
		Filenames: []string{},
		MimeTypes: []string{"application/x-genshi-text", "text/x-genshi"},
	},
	genshiTextRules,
))

Genshi Text lexer.

View Source
var Go = Register(MustNewLexer(
	&Config{
		Name:      "Go",
		Aliases:   []string{"go", "golang"},
		Filenames: []string{"*.go"},
		MimeTypes: []string{"text/x-gosrc"},
	},
	goRules,
).SetAnalyser(func(text string) float32 {
	if strings.Contains(text, "fmt.") && strings.Contains(text, "package ") {
		return 0.5
	}
	if strings.Contains(text, "package ") {
		return 0.1
	}
	return 0.0
}))

Go lexer.

View Source
var GoHTMLTemplate = registerLazy(func() Lexer {
	return DelegatingLexer(HTML(), MustNewXMLLexer(
		embeddedLexers(),
		"embedded/go_template.xml",
	).SetConfig(
		&Config{
			Name:    "Go HTML Template",
			Aliases: []string{"go-html-template"},
		},
	))
})
View Source
var GoTextTemplate = registerLazy(func() Lexer {
	return MustNewXMLLexer(
		embeddedLexers(),
		"embedded/go_template.xml",
	).SetConfig(
		&Config{
			Name:    "Go Text Template",
			Aliases: []string{"go-text-template"},
		},
	)
})
View Source
var HTML = sync.OnceValue(func() chroma.Lexer {
	return chroma.MustNewXMLLexer(embeddedLexers(), "embedded/html.xml")
})

HTML lexer. Shared root for the delegating lexers below; memoized so its XML config decodes once, inside the lazy registry build rather than at package init.

View Source
var HTTP = Register(httpBodyContentTypeLexer(MustNewLexer(
	&Config{
		Name:         "HTTP",
		Aliases:      []string{"http"},
		Filenames:    []string{},
		MimeTypes:    []string{},
		NotMultiline: true,
		DotAll:       true,
	},
	httpRules,
)))

HTTP lexer.

View Source
var Haxe = Register(MustNewLexer(
	&Config{
		Name:      "Haxe",
		Aliases:   []string{"hx", "haxe", "hxsl"},
		Filenames: []string{"*.hx", "*.hxsl"},
		MimeTypes: []string{"text/haxe", "text/x-haxe", "text/x-hx"},
		DotAll:    true,
	},
	haxeRules,
))

Haxe lexer.

View Source
var Markdown = Register(&markdownLexer{Lexer: MustNewLexer(
	&Config{
		Name:      "markdown",
		Aliases:   []string{"md", "mkd"},
		Filenames: []string{"*.md", "*.mkd", "*.markdown"},
		MimeTypes: []string{"text/x-markdown"},
	},
	markdownRules,
)})

Markdown lexer with YAML frontmatter and HTML comment support.

View Source
var Markless = Register(MustNewLexer(
	&Config{
		Name:      "Markless",
		Aliases:   []string{"mess"},
		Filenames: []string{"*.mess", "*.markless"},
		MimeTypes: []string{"text/x-markless"},
	},
	marklessRules,
))

Markless lexer.

View Source
var Raku Lexer = Register(MustNewLexer(
	&Config{
		Name:    "Raku",
		Aliases: []string{"perl6", "pl6", "raku"},
		Filenames: []string{
			"*.pl", "*.pm", "*.nqp", "*.p6", "*.6pl", "*.p6l", "*.pl6", "*.6pm",
			"*.p6m", "*.pm6", "*.t", "*.raku", "*.rakumod", "*.rakutest", "*.rakudoc",
		},
		MimeTypes: []string{
			"text/x-perl6", "application/x-perl6",
			"text/x-raku", "application/x-raku",
		},
		DotAll: true,
	},
	rakuRules,
))

Raku lexer.

View Source
var Restructuredtext = Register(MustNewLexer(
	&Config{
		Name:      "reStructuredText",
		Aliases:   []string{"rst", "rest", "restructuredtext"},
		Filenames: []string{"*.rst", "*.rest"},
		MimeTypes: []string{"text/x-rst", "text/prs.fallenstein.rst"},
	},
	restructuredtextRules,
))

Restructuredtext lexer.

View Source
var Svelte = registerLazy(func() Lexer {
	return DelegatingLexer(HTML(), MustNewLexer(
		&Config{
			Name:      "Svelte",
			Aliases:   []string{"svelte"},
			Filenames: []string{"*.svelte"},
			MimeTypes: []string{"application/x-svelte"},
			DotAll:    true,
		},
		svelteRules,
	))
})

Svelte lexer.

View Source
var Typoscript = Register(MustNewLexer(
	&Config{
		Name:      "TypoScript",
		Aliases:   []string{"typoscript"},
		Filenames: []string{"*.ts"},
		MimeTypes: []string{"text/x-typoscript"},
		DotAll:    true,
		Priority:  0.1,
	},
	typoscriptRules,
))

Typoscript lexer.

View Source
var YAMLJinja = registerLazy(func() Lexer {
	return DelegatingLexer(
		MustNewXMLLexer(embeddedLexers(), "embedded/yaml.xml"),
		MustNewXMLLexer(embeddedLexers(), "embedded/django_jinja.xml").SetConfig(
			&Config{
				Name:      "YAML+Jinja",
				Aliases:   []string{"yaml+jinja", "salt", "sls", "ansible"},
				Filenames: []string{"*.sls"},
				MimeTypes: []string{"text/x-yaml+jinja", "text/x-sls"},
				DotAll:    true,
			},
		),
	)
})

YAML+Jinja is YAML with Jinja templating embedded. Used by Ansible playbooks and Salt SLS files.

Functions

func Aliases

func Aliases(skipWithoutAliases bool) []string

Aliases of all the lexers, and skip those lexers who do not have any aliases, or show their name instead

func Analyse

func Analyse(text string) chroma.Lexer

Analyse text content and return the "best" lexer..

func Get

func Get(name string) chroma.Lexer

Get a Lexer by name, alias or file extension.

Note that this if there isn't an exact match on name or alias, this will call Match(), so it is not efficient.

func Match

func Match(filename string) chroma.Lexer

Match returns the first lexer matching filename.

Note that this iterates over all file patterns in all lexers, so it's not particularly efficient.

func MatchMimeType

func MatchMimeType(mimeType string) chroma.Lexer

MatchMimeType attempts to find a lexer for the given MIME type.

func Names

func Names(withAliases bool) []string

Names of all lexers, optionally including aliases.

func PlaintextRules

func PlaintextRules() chroma.Rules

PlaintextRules is used for the fallback lexer as well as the explicit plaintext lexer.

func Register

func Register(lexer chroma.Lexer) chroma.Lexer

Register queues a Lexer for the global registry.

Types

This section is empty.

Directories

Path Synopsis
Command gen rebuilds embedded.tar.gz from the embedded/ XML lexer configs.
Command gen rebuilds embedded.tar.gz from the embedded/ XML lexer configs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL