Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
/ chroma-v1 Public archive
forked from alecthomas/chroma

A general purpose syntax highlighter in pure Go

License

Notifications You must be signed in to change notification settings

wakatime/chroma-v1

 
 

Repository files navigation

Chroma — A general purpose syntax highlighter in pure Go

Golang Documentation CI Slack chat

This is a fork for detecting languages in wakatime-cli using Chroma Lexers.

Chroma is based heavily on Pygments, and includes translators for Pygments lexers and styles.

Identifying the language

To highlight code, you'll first have to identify what language the code is written in. There are three primary ways to do that:

  1. Detect the language from its filename.

    lexer := lexers.Match("foo.go")
  2. Explicitly specify the language by its Chroma syntax ID (a full list is available from lexers.Names()).

    lexer := lexers.Get("go")
  3. Detect the language from its content.

    lexer := lexers.Analyse("package main\n\nfunc main()\n{\n}\n")

In all cases, nil will be returned if the language can not be identified.

if lexer == nil {
  lexer = lexers.Fallback
}

At this point, it should be noted that some lexers can be extremely chatty. To mitigate this, you can use the coalescing lexer to coalesce runs of identical token types into a single token:

lexer = chroma.Coalesce(lexer)

About

A general purpose syntax highlighter in pure Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.2%
  • Other 0.8%