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.
To highlight code, you'll first have to identify what language the code is written in. There are three primary ways to do that:
-
Detect the language from its filename.
lexer := lexers.Match("foo.go")
-
Explicitly specify the language by its Chroma syntax ID (a full list is available from
lexers.Names()
).lexer := lexers.Get("go")
-
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)