hugo/content/en/content-management/syntax-highlighting.md
Bjørn Erik Pedersen 98293eaa15 Squashed 'docs/' changes from 501c6e233..f59b3ab06
f59b3ab06 Fix typo in template lookup order
1e5536d6c Mutlilingual: Document "content directory per language" system (#509)
849a86048 Update index.md
0c24d229b Polish Hugo Next
a4c9b0ee2 Polish
bbec2c76e Some more in birthday post
fc9681e21 More on contributors
09fe3ea31 Some more on the birthday post
8da357240 Content and images for the 5th birthday blog post
fb45bb8dc Add draft for anniversary blog post
4666d0a18 Release 0.42.2
9b74d286a Merge branch 'temp422'
354e7b66b releaser: Add release notes to /docs for release of 0.42.2
57a617f34 releaser: Bump versions for release of 0.42.2
ccc3ac1b8 Update errorf.md
35706c21a Update errorf.md
1c0f35fd1 Update errorf.md
b6170774b Add syntax highlighting gallery links for Chroma
f91d9da47 Update usage.md
c9a8f0190 Improve theme components documentation
3c4e39ddd Release 0.42.1
b45eb453f Merge branch 'temp421'
c74682a10 releaser: Prepare repository for 0.43-DEV
321e07fa5 releaser: Add release notes to /docs for release of 0.42.1
7154271e0 releaser: Bump versions for release of 0.42.1
360d8244f Add link to Privacy Config
1f2454247 Fix typo
a8f5f994e Fix typo
d9f3f078c Update simple variants documentation (#500)
f5cfd44e0 Release 0.42
fe604b321 releaser: Prepare repository for 0.43-DEV
c3e5b3ca0 releaser: Add release notes to /docs for release of 0.42
3174d1b37 releaser: Bump versions for release of 0.42
48cc2d51f docs: Update theme documentation
1922fb1a6 docs: Remove some files now moved
d7e4c453a Merge commit 'b239595af5a9fc1fc9a1ccc666c3ab06ccc32f04'
c40964c1b tplimpl: Remove speakerdeck shortcode
081f8a0f9 tpl/strings: strings.RuneCount
828ea5f15 tpl: Add strings.Repeat
a6b9f654a Add a BlackFriday option for rel="noreferrer" on external links
edb786516 Add a BlackFriday option for rel="nofollow" on external links
e4374971f releaser: Prepare repository for 0.42-DEV

git-subtree-dir: docs
git-subtree-split: f59b3ab06f282c26bce07263c8be6672cf8f7969
2018-07-06 17:52:13 +02:00

7.2 KiB

title description date publishdate keywords categories menu weight sections_weight draft aliases toc
Syntax Highlighting Hugo comes with really fast syntax highlighting from Chroma. 2017-02-01 2017-02-01
highlighting
pygments
chroma
code blocks
syntax
content management
docs
parent weight
content-management 300
20 20 false
/extras/highlighting/
/extras/highlight/
/tools/syntax-highlighting/
true

From Hugo 0.28, the default syntax hightlighter in Hugo is Chroma; it is built in Go and is really, really fast -- and for the most important parts compatible with Pygments.

If you want to continue to use Pygments (see below), set pygmentsUseClassic=true in your site config.

The example below shows a simple code snippet from the Hugo source highlighted with the highlight shortcode. Note that the gohugo.io site is generated with pygmentsUseClasses=true (see Generate Syntax Highlighter CSS).

  • linenos=inline or linenos=table (table will give copy-and-paste friendly code blocks) turns on line numbers.
  • hl_lines lists a set of line numbers or line number ranges to be highlighted. Note that the hyphen range syntax is only supported for Chroma.
  • linenostart=199 starts the line number count from 199.

With that, this:

{{</* highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" */>}}
// ... code
{{</* / highlight */>}}

Gives this:

{{< highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" >}} // GetTitleFunc returns a func that can be used to transform a string to // title case. // // The supported styles are // // - "Go" (strings.Title) // - "AP" (see https://www.apstylebook.com/) // - "Chicago" (see http://www.chicagomanualofstyle.org/home.html) // // If an unknown or empty style is provided, AP style is what you get. func GetTitleFunc(style string) func(s string) string { switch strings.ToLower(style) { case "go": return strings.Title case "chicago": tc := transform.NewTitleConverter(transform.ChicagoStyle) return tc.Title default: tc := transform.NewTitleConverter(transform.APStyle) return tc.Title } } {{< / highlight >}}

Configure Syntax Highlighter

To make the transition from Pygments to Chroma seamless, they share a common set of configuration options:

pygmentsOptions
A comma separated list of options. See below for a full list.
pygmentsCodefences
Set to true to enable syntax highlighting in code fences with a language tag in markdown (see below for an example).
pygmentsStyle
The style of code highlighting. Note that this option is not relevant when pygmentsUseClasses is set.

Syntax highlighting galleries: Chroma (short snippets, long snippets), Pygments

pygmentsUseClasses
Set to true to use CSS classes to format your highlighted code. See Generate Syntax Highlighter CSS.
pygmentsCodefencesGuessSyntax
Set to true to try to do syntax highlighting on code fenced blocks in markdown without a language tag.
pygmentsUseClassic
Set to true to use Pygments instead of the much faster Chroma.

Options

pygmentsOptions can be set either in site config or overridden per code block in the Highlight shortcode or template func.

noclasses
Use inline style.
linenos
For Chroma, any value in this setting will print line numbers. Pygments has some more fine grained control.
linenostart
Start the line numbers from this value (default is 1).
hl_lines
Highlight a space separated list of line numbers. For Chroma, you can provide a list of ranges, i.e. "3-8 10-20".

The full set of supported options for Pygments is: encoding, outencoding, nowrap, full, title, style, noclasses, classprefix, cssclass, cssstyles, prestyles, linenos, hl_lines, linenostart, linenostep, linenospecial, nobackground, lineseparator, lineanchors, linespans, anchorlinenos, startinline. See the Pygments HTML Formatter Documentation for details.

Generate Syntax Highlighter CSS

If you run with pygmentsUseClasses=true in your site config, you need a style sheet.

You can generate one with Hugo:

hugo gen chromastyles --style=monokai > syntax.css

Run hugo gen chromastyles -h for more options. See https://help.farbox.com/pygments.html for a gallery of available styles.

Highlight Shortcode

Highlighting is carried out via the built-in shortcode highlight. highlight takes exactly one required parameter for the programming language to be highlighted and requires a closing shortcode. Note that highlight is not used for client-side javascript highlighting.

Example highlight Shortcode

{{< code file="example-highlight-shortcode-input.md" >}} {{</* highlight html */>}}

{{ .Title }}

{{ range .Data.Pages }} {{ .Render "summary"}} {{ end }}
{{}} {{< /code >}}

Highlight Template Func

See Highlight.

Highlight in Code Fences

It is also possible to add syntax highlighting with GitHub flavored code fences. To enable this, set the pygmentsCodeFences to true in Hugo's configuration file;

```go-html-template
<section id="main">
  <div>
    <h1 id="title">{{ .Title }}</h1>
    {{ range .Data.Pages }}
      {{ .Render "summary"}}
    {{ end }}
  </div>
</section>
```

List of Chroma Highlighting Languages

The full list of Chroma lexers and their aliases (which is the identifier used in the hightlight template func or when doing highlighting in code fences):

{{< chroma-lexers >}}

Highlight with Pygments Classic

If you for some reason don't want to use the built-in Chroma highlighter, you can set pygmentsUseClassic=true in your config and add Pygments to your path.

{{% note "Disclaimers on Pygments" %}}

  • Pygments is relatively slow and causes a performance hit when building your site, but Hugo has been designed to cache the results to disk.
  • The caching can be turned off by setting the --ignoreCache flag to true.
  • The languages available for highlighting depend on your Pygments installation. {{% /note %}}

If you have never worked with Pygments before, here is a brief primer:

  • Install Python from python.org. Version 2.7.x is already sufficient.
  • Run pip install Pygments in order to install Pygments. Once installed, Pygments gives you a command pygmentize. Make sure it sits in your PATH; otherwise, Hugo will not be able to find and use it.

On Debian and Ubuntu systems, you may also install Pygments by running sudo apt-get install python3-pygments.