1. # Chroma — A general purpose syntax highlighter in pure Go
    
  2. 
    
  3. [![Golang Documentation](https://godoc.org/github.com/alecthomas/chroma?status.svg)](https://godoc.org/github.com/alecthomas/chroma) [![CI](https://github.com/alecthomas/chroma/actions/workflows/ci.yml/badge.svg)](https://github.com/alecthomas/chroma/actions/workflows/ci.yml) [![Slack chat](https://img.shields.io/static/v1?logo=slack&style=flat&label=slack&color=green&message=gophers)](https://invite.slack.golangbridge.org/)
    
  4. 
    
  5. Chroma takes source code and other structured text and converts it into syntax
    
  6. highlighted HTML, ANSI-coloured text, etc.
    
  7. 
    
  8. Chroma is based heavily on [Pygments](http://pygments.org/), and includes
    
  9. translators for Pygments lexers and styles.
    
  10. 
    
  11. <a id="markdown-table-of-contents" name="table-of-contents"></a>
    
  12. 
    
  13. ## Table of Contents
    
  14. 
    
  15. <!-- TOC -->
    
  16. 
    
  17. 1. [Table of Contents](#table-of-contents)
    
  18. 2. [Supported languages](#supported-languages)
    
  19. 3. [Try it](#try-it)
    
  20. 4. [Using the library](#using-the-library)
    
  21.    1. [Quick start](#quick-start)
    
  22.    2. [Identifying the language](#identifying-the-language)
    
  23.    3. [Formatting the output](#formatting-the-output)
    
  24.    4. [The HTML formatter](#the-html-formatter)
    
  25. 5. [More detail](#more-detail)
    
  26.    1. [Lexers](#lexers)
    
  27.    2. [Formatters](#formatters)
    
  28.    3. [Styles](#styles)
    
  29. 6. [Command-line interface](#command-line-interface)
    
  30. 7. [Testing lexers](#testing-lexers)
    
  31. 8. [What's missing compared to Pygments?](#whats-missing-compared-to-pygments)
    
  32. 
    
  33. <!-- /TOC -->
    
  34. 
    
  35. <a id="markdown-supported-languages" name="supported-languages"></a>
    
  36. 
    
  37. ## Supported languages
    
  38. 
    
  39. | Prefix | Language                                                                                                                                                                                                                             |
    
  40. | :----: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
    
  41. |   A    | ABAP, ABNF, ActionScript, ActionScript 3, Ada, Agda, AL, Alloy, Angular2, ANTLR, ApacheConf, APL, AppleScript, ArangoDB AQL, Arduino, ArmAsm, AutoHotkey, AutoIt, Awk                                                                             |
    
  42. |   B    | Ballerina, Bash, Bash Session, Batchfile, BibTeX, Bicep, BlitzBasic, BNF, BQN, Brainfuck                                                                                                                                             |
    
  43. |   C    | C, C#, C++, Caddyfile, Caddyfile Directives, Cap'n Proto, Cassandra CQL, Ceylon, CFEngine3, cfstatement, ChaiScript, Chapel, Cheetah, Clojure, CMake, COBOL, CoffeeScript, Common Lisp, Coq, Crystal, CSS, Cython                    |
    
  44. |   D    | D, Dart, Dax, Desktop Entry, Diff, Django/Jinja, dns, Docker, DTD, Dylan                                                                                                                                                                                 |
    
  45. |   E    | EBNF, Elixir, Elm, EmacsLisp, Erlang                                                                                                                                                                                                 |
    
  46. |   F    | Factor, Fennel, Fish, Forth, Fortran, FortranFixed, FSharp                                                                                                                                                                           |
    
  47. |   G    | GAS, GDScript, Genshi, Genshi HTML, Genshi Text, Gherkin, GLSL, Gnuplot, Go, Go HTML Template, Go Text Template, GraphQL, Groff, Groovy                                                                                              |
    
  48. |   H    | Handlebars, Hare, Haskell, Haxe, HCL, Hexdump, HLB, HLSL, HolyC, HTML, HTTP, Hy                                                                                                                                                      |
    
  49. |   I    | Idris, Igor, INI, Io, ISCdhcpd                                                                                                                                                                                                       |
    
  50. |   J    | J, Java, JavaScript, JSON, Julia, Jungle                                                                                                                                                                                             |
    
  51. |   K    | Kotlin                                                                                                                                                                                                                               |
    
  52. |   L    | Lighttpd configuration file, LLVM, Lua                                                                                                                                                                                               |
    
  53. |   M    | Makefile, Mako, markdown, Mason, Materialize SQL dialect, Mathematica, Matlab, mcfunction, Meson, Metal, MiniZinc, MLIR, Modula-2, MonkeyC, MorrowindScript, Myghty, MySQL                                                                                    |
    
  54. |   N    | NASM, Natural, Newspeak, Nginx configuration file, Nim, Nix                                                                                                                                                                          |
    
  55. |   O    | Objective-C, OCaml, Octave, Odin, OnesEnterprise, OpenEdge ABL, OpenSCAD, Org Mode                                                                                                                                                   |
    
  56. |   P    | PacmanConf, Perl, PHP, PHTML, Pig, PkgConfig, PL/pgSQL, plaintext, Plutus Core, Pony, PostgreSQL SQL dialect, PostScript, POVRay, PowerQuery, PowerShell, Prolog, PromQL, properties, Protocol Buffer, PRQL, PSL, Puppet, Python, Python 2 |
    
  57. |   Q    | QBasic, QML                                                                                                                                                                                                                          |
    
  58. |   R    | R, Racket, Ragel, Raku, react, ReasonML, reg, reStructuredText, Rexx, Ruby, Rust                                                                                                                                                     |
    
  59. |   S    | SAS, Sass, Scala, Scheme, Scilab, SCSS, Sed, Sieve, Smali, Smalltalk, Smarty, Snobol, Solidity, SourcePawn, SPARQL, SQL, SquidConf, Standard ML, stas, Stylus, Svelte, Swift, SYSTEMD, systemverilog                                 |
    
  60. |   T    | TableGen, Tal, TASM, Tcl, Tcsh, Termcap, Terminfo, Terraform, TeX, Thrift, TOML, TradingView, Transact-SQL, Turing, Turtle, Twig, TypeScript, TypoScript, TypoScriptCssData, TypoScriptHtmlData                                      |
    
  61. |   V    | V, V shell, Vala, VB.net, verilog, VHDL, VHS, VimL, vue                                                                                                                                                                              |
    
  62. |   W    | WDTE, WebGPU Shading Language, Whiley                                                                                                                                                                                                |
    
  63. |   X    | XML, Xorg                                                                                                                                                                                                                            |
    
  64. |   Y    | YAML, YANG                                                                                                                                                                                                                           |
    
  65. |   Z    | Z80 Assembly, Zed, Zig                                                                                                                                                                                                               |
    
  66. 
    
  67. _I will attempt to keep this section up to date, but an authoritative list can be
    
  68. displayed with `chroma --list`._
    
  69. 
    
  70. <a id="markdown-try-it" name="try-it"></a>
    
  71. 
    
  72. ## Try it
    
  73. 
    
  74. Try out various languages and styles on the [Chroma Playground](https://swapoff.org/chroma/playground/).
    
  75. 
    
  76. <a id="markdown-using-the-library" name="using-the-library"></a>
    
  77. 
    
  78. ## Using the library
    
  79. 
    
  80. Chroma, like Pygments, has the concepts of
    
  81. [lexers](https://github.com/alecthomas/chroma/tree/master/lexers),
    
  82. [formatters](https://github.com/alecthomas/chroma/tree/master/formatters) and
    
  83. [styles](https://github.com/alecthomas/chroma/tree/master/styles).
    
  84. 
    
  85. Lexers convert source text into a stream of tokens, styles specify how token
    
  86. types are mapped to colours, and formatters convert tokens and styles into
    
  87. formatted output.
    
  88. 
    
  89. A package exists for each of these, containing a global `Registry` variable
    
  90. with all of the registered implementations. There are also helper functions
    
  91. for using the registry in each package, such as looking up lexers by name or
    
  92. matching filenames, etc.
    
  93. 
    
  94. In all cases, if a lexer, formatter or style can not be determined, `nil` will
    
  95. be returned. In this situation you may want to default to the `Fallback`
    
  96. value in each respective package, which provides sane defaults.
    
  97. 
    
  98. <a id="markdown-quick-start" name="quick-start"></a>
    
  99. 
    
  100. ### Quick start
    
  101. 
    
  102. A convenience function exists that can be used to simply format some source
    
  103. text, without any effort:
    
  104. 
    
  105. ```go
    
  106. err := quick.Highlight(os.Stdout, someSourceCode, "go", "html", "monokai")
    
  107. ```
    
  108. 
    
  109. <a id="markdown-identifying-the-language" name="identifying-the-language"></a>
    
  110. 
    
  111. ### Identifying the language
    
  112. 
    
  113. To highlight code, you'll first have to identify what language the code is
    
  114. written in. There are three primary ways to do that:
    
  115. 
    
  116. 1. Detect the language from its filename.
    
  117. 
    
  118.    ```go
    
  119.    lexer := lexers.Match("foo.go")
    
  120.    ```
    
  121. 
    
  122. 2. Explicitly specify the language by its Chroma syntax ID (a full list is available from `lexers.Names()`).
    
  123. 
    
  124.    ```go
    
  125.    lexer := lexers.Get("go")
    
  126.    ```
    
  127. 
    
  128. 3. Detect the language from its content.
    
  129. 
    
  130.    ```go
    
  131.    lexer := lexers.Analyse("package main\n\nfunc main()\n{\n}\n")
    
  132.    ```
    
  133. 
    
  134. In all cases, `nil` will be returned if the language can not be identified.
    
  135. 
    
  136. ```go
    
  137. if lexer == nil {
    
  138.   lexer = lexers.Fallback
    
  139. }
    
  140. ```
    
  141. 
    
  142. At this point, it should be noted that some lexers can be extremely chatty. To
    
  143. mitigate this, you can use the coalescing lexer to coalesce runs of identical
    
  144. token types into a single token:
    
  145. 
    
  146. ```go
    
  147. lexer = chroma.Coalesce(lexer)
    
  148. ```
    
  149. 
    
  150. <a id="markdown-formatting-the-output" name="formatting-the-output"></a>
    
  151. 
    
  152. ### Formatting the output
    
  153. 
    
  154. Once a language is identified you will need to pick a formatter and a style (theme).
    
  155. 
    
  156. ```go
    
  157. style := styles.Get("swapoff")
    
  158. if style == nil {
    
  159.   style = styles.Fallback
    
  160. }
    
  161. formatter := formatters.Get("html")
    
  162. if formatter == nil {
    
  163.   formatter = formatters.Fallback
    
  164. }
    
  165. ```
    
  166. 
    
  167. Then obtain an iterator over the tokens:
    
  168. 
    
  169. ```go
    
  170. contents, err := ioutil.ReadAll(r)
    
  171. iterator, err := lexer.Tokenise(nil, string(contents))
    
  172. ```
    
  173. 
    
  174. And finally, format the tokens from the iterator:
    
  175. 
    
  176. ```go
    
  177. err := formatter.Format(w, style, iterator)
    
  178. ```
    
  179. 
    
  180. <a id="markdown-the-html-formatter" name="the-html-formatter"></a>
    
  181. 
    
  182. ### The HTML formatter
    
  183. 
    
  184. By default the `html` registered formatter generates standalone HTML with
    
  185. embedded CSS. More flexibility is available through the `formatters/html` package.
    
  186. 
    
  187. Firstly, the output generated by the formatter can be customised with the
    
  188. following constructor options:
    
  189. 
    
  190. - `Standalone()` - generate standalone HTML with embedded CSS.
    
  191. - `WithClasses()` - use classes rather than inlined style attributes.
    
  192. - `ClassPrefix(prefix)` - prefix each generated CSS class.
    
  193. - `TabWidth(width)` - Set the rendered tab width, in characters.
    
  194. - `WithLineNumbers()` - Render line numbers (style with `LineNumbers`).
    
  195. - `WithLinkableLineNumbers()` - Make the line numbers linkable and be a link to themselves.
    
  196. - `HighlightLines(ranges)` - Highlight lines in these ranges (style with `LineHighlight`).
    
  197. - `LineNumbersInTable()` - Use a table for formatting line numbers and code, rather than spans.
    
  198. 
    
  199. If `WithClasses()` is used, the corresponding CSS can be obtained from the formatter with:
    
  200. 
    
  201. ```go
    
  202. formatter := html.New(html.WithClasses(true))
    
  203. err := formatter.WriteCSS(w, style)
    
  204. ```
    
  205. 
    
  206. <a id="markdown-more-detail" name="more-detail"></a>
    
  207. 
    
  208. ## More detail
    
  209. 
    
  210. <a id="markdown-lexers" name="lexers"></a>
    
  211. 
    
  212. ### Lexers
    
  213. 
    
  214. See the [Pygments documentation](http://pygments.org/docs/lexerdevelopment/)
    
  215. for details on implementing lexers. Most concepts apply directly to Chroma,
    
  216. but see existing lexer implementations for real examples.
    
  217. 
    
  218. In many cases lexers can be automatically converted directly from Pygments by
    
  219. using the included Python 3 script `pygments2chroma_xml.py`. I use something like
    
  220. the following:
    
  221. 
    
  222. ```sh
    
  223. python3 _tools/pygments2chroma_xml.py \
    
  224.   pygments.lexers.jvm.KotlinLexer \
    
  225.   > lexers/embedded/kotlin.xml
    
  226. ```
    
  227. 
    
  228. See notes in [pygments-lexers.txt](https://github.com/alecthomas/chroma/blob/master/pygments-lexers.txt)
    
  229. for a list of lexers, and notes on some of the issues importing them.
    
  230. 
    
  231. <a id="markdown-formatters" name="formatters"></a>
    
  232. 
    
  233. ### Formatters
    
  234. 
    
  235. Chroma supports HTML output, as well as terminal output in 8 colour, 256 colour, and true-colour.
    
  236. 
    
  237. A `noop` formatter is included that outputs the token text only, and a `tokens`
    
  238. formatter outputs raw tokens. The latter is useful for debugging lexers.
    
  239. 
    
  240. <a id="markdown-styles" name="styles"></a>
    
  241. 
    
  242. ### Styles
    
  243. 
    
  244. Chroma styles are defined in XML. The style entries use the
    
  245. [same syntax](http://pygments.org/docs/styles/) as Pygments.
    
  246. 
    
  247. All Pygments styles have been converted to Chroma using the `_tools/style.py`
    
  248. script.
    
  249. 
    
  250. When you work with one of [Chroma's styles](https://github.com/alecthomas/chroma/tree/master/styles),
    
  251. know that the `Background` token type provides the default style for tokens. It does so
    
  252. by defining a foreground color and background color.
    
  253. 
    
  254. For example, this gives each token name not defined in the style a default color
    
  255. of `#f8f8f8` and uses `#000000` for the highlighted code block's background:
    
  256. 
    
  257. ```xml
    
  258. <entry type="Background" style="#f8f8f2 bg:#000000"/>
    
  259. ```
    
  260. 
    
  261. Also, token types in a style file are hierarchical. For instance, when `CommentSpecial` is not defined, Chroma uses the token style from `Comment`. So when several comment tokens use the same color, you'll only need to define `Comment` and override the one that has a different color.
    
  262. 
    
  263. For a quick overview of the available styles and how they look, check out the [Chroma Style Gallery](https://xyproto.github.io/splash/docs/).
    
  264. 
    
  265. <a id="markdown-command-line-interface" name="command-line-interface"></a>
    
  266. 
    
  267. ## Command-line interface
    
  268. 
    
  269. A command-line interface to Chroma is included.
    
  270. 
    
  271. Binaries are available to install from [the releases page](https://github.com/alecthomas/chroma/releases).
    
  272. 
    
  273. The CLI can be used as a preprocessor to colorise output of `less(1)`,
    
  274. see documentation for the `LESSOPEN` environment variable.
    
  275. 
    
  276. The `--fail` flag can be used to suppress output and return with exit status
    
  277. 1 to facilitate falling back to some other preprocessor in case chroma
    
  278. does not resolve a specific lexer to use for the given file. For example:
    
  279. 
    
  280. ```shell
    
  281. export LESSOPEN='| p() { chroma --fail "$1" || cat "$1"; }; p "%s"'
    
  282. ```
    
  283. 
    
  284. Replace `cat` with your favourite fallback preprocessor.
    
  285. 
    
  286. When invoked as `.lessfilter`, the `--fail` flag is automatically turned
    
  287. on under the hood for easy integration with [lesspipe shipping with
    
  288. Debian and derivatives](https://manpages.debian.org/lesspipe#USER_DEFINED_FILTERS);
    
  289. for that setup the `chroma` executable can be just symlinked to `~/.lessfilter`.
    
  290. 
    
  291. <a id="markdown-whats-missing-compared-to-pygments" name="whats-missing-compared-to-pygments"></a>
    
  292. 
    
  293. <a id="markdown-testing-lexers" name="testing-lexers"></a>
    
  294. 
    
  295. ## Testing lexers
    
  296. 
    
  297. If you edit some lexers and want to try it, open a shell in `cmd/chromad` and run:
    
  298. 
    
  299. ```shell
    
  300. go run .
    
  301. ```
    
  302. 
    
  303. A Link will be printed. Open it in your Browser. Now you can test on the Playground with your local changes.
    
  304. 
    
  305. If you want to run the tests and the lexers, open a shell in the root directory and run:
    
  306. 
    
  307. ```shell
    
  308. go test ./lexers
    
  309. ```
    
  310. 
    
  311. When updating or adding a lexer, please add tests. See [lexers/README.md](lexers/README.md) for more.
    
  312. 
    
  313. ## What's missing compared to Pygments?
    
  314. 
    
  315. - Quite a few lexers, for various reasons (pull-requests welcome):
    
  316.   - Pygments lexers for complex languages often include custom code to
    
  317.     handle certain aspects, such as Raku's ability to nest code inside
    
  318.     regular expressions. These require time and effort to convert.
    
  319.   - I mostly only converted languages I had heard of, to reduce the porting cost.
    
  320. - Some more esoteric features of Pygments are omitted for simplicity.
    
  321. - Though the Chroma API supports content detection, very few languages support them.
    
  322.   I have plans to implement a statistical analyser at some point, but not enough time.