2014-02-18 18:35:45 -05:00
---
2014-08-25 14:02:39 -04:00
aliases:
- /extras/highlight/
2014-05-29 18:42:05 -04:00
date: 2013-07-01
2014-04-23 03:00:11 -04:00
menu:
main:
2014-05-29 18:42:05 -04:00
parent: extras
next: /extras/toc
prev: /extras/shortcodes
title: Syntax Highlighting
2014-11-25 03:07:18 -05:00
weight: 90
2014-02-18 18:35:45 -05:00
---
2014-04-28 15:47:44 -04:00
Hugo provides the ability for you to highlight source code in two different
2014-05-09 00:03:42 -04:00
ways — either pre-processed server side from your content, or to defer
the processing to the client side, using a JavaScript library. The advantage of
server side is that it doesn’ t depend on a JavaScript library and consequently
2014-09-03 00:12:26 -04:00
works very well when read from an RSS feed. The advantage of client side is that
2015-02-17 03:59:40 -05:00
it doesn’ t cost anything when building your site and some of the highlighting
2014-09-03 00:12:26 -04:00
scripts available cover more languages than Pygments does.
2014-02-18 18:35:45 -05:00
2015-02-16 13:50:53 -05:00
## Server-side
2014-04-28 15:47:44 -04:00
For the pre-processed approach, Highlighting is performed by an external
2015-01-27 21:17:09 -05:00
Python-based program called [Pygments ](http://pygments.org/ ) and is triggered
2015-02-16 13:50:53 -05:00
via an embedded shortcode (see example below). If Pygments is absent from the path, it will silently simply pass the content along unhighlighted.
2014-02-18 18:35:45 -05:00
2015-02-16 13:50:53 -05:00
### Pygments
2014-04-28 15:47:44 -04:00
2015-02-17 03:59:40 -05:00
If you have never worked with Pygments before, here is a brief primer.
2014-02-18 18:35:45 -05:00
2015-02-17 03:59:40 -05:00
+ Install Python from [python.org ](https://www.python.org/downloads/ ). 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 cannot find it.
2015-02-16 13:50:53 -05:00
2015-02-17 03:59:40 -05:00
On Debian and Ubuntu systems, you may also install Pygments by running `sudo apt-get install python3-pygments` .
2015-02-16 13:50:53 -05:00
2015-02-17 03:59:40 -05:00
Hugo gives you two options that you can set with the variable `pygmentsuseclasses` (default `false` ) in `config.toml` (or `config.yaml` ).
1. Color-codes for highlighting keywords are directly inserted if `pygmentsuseclasses = false` (default). See in the example below. The color-codes depend on your choice of the `pygmentsstyle` (default `"monokai"` ). You can explore the different color styles on [pygments.org ](http://pygments.org/ ) after inserting some example code.
2. If you choose `pygmentsuseclasses = true` , Hugo includes class names in your code instead of color-codes. For class-names to be meaningful, you need to include a `.css` -file in your website representing your color-scheme. You can either generate this `.css` -files according to this [description ](http://pygments.org/docs/cmdline/ ) or download the standard ones from the [GitHub pygments-css repository ](https://github.com/richleland/pygments-css ).
2014-02-18 18:35:45 -05:00
2014-05-09 00:03:42 -04:00
### Usage
2015-02-16 13:50:53 -05:00
Highlighting is carried out via the in-built shortcode `highlight` . `highlight` takes exactly one required parameter of language and requires a
2014-02-18 18:35:45 -05:00
closing shortcode.
2014-05-09 00:03:42 -04:00
### Example
2015-02-17 03:59:40 -05:00
If you want to highlight code, you need to either fence the code with ``` according to GitHub Flavored Markdown or preceed each line with 4 spaces to identify each line as a line of code.
Not doing either will result in the text being rendered as HTML. This will prevent Pygments highlighting from working.
2015-02-16 13:50:53 -05:00
```
{{< /* highlight html */>}}
2014-11-25 03:07:18 -05:00
< section id = "main" >
< div >
< h1 id = "title" > {{ .Title }}< / h1 >
{{ range .Data.Pages }}
{{ .Render "summary"}}
{{ end }}
< / div >
< / section >
2015-02-16 13:50:53 -05:00
{{< /* /highlight */>}}
2014-11-25 03:07:18 -05:00
```
2014-02-18 18:35:45 -05:00
2014-05-09 00:03:42 -04:00
### Example Output
2014-02-18 18:35:45 -05:00
2014-05-15 09:58:55 -04:00
< span style = "color: #f92672 " > < section</ span > < span style = "color: #a6e22e " > id=</ span >< span style = "color: #e6db74 " > " main" </ span >< span style = "color: #f92672 " > > </ span >
< span style = "color: #f92672 " > < div> </ span >
< span style = "color: #f92672 " > < h1</ span > < span style = "color: #a6e22e " > id=</ span >< span style = "color: #e6db74 " > " title" </ span >< span style = "color: #f92672 " > > </ span > {{ .Title }}< span style = "color: #f92672 " > < /h1> </ span >
{{ range .Data.Pages }}
{{ .Render " summary" }}
{{ end }}
< span style = "color: #f92672 " > < /div> </ span >
< span style = "color: #f92672 " > < /section> </ span >
2014-02-18 18:35:45 -05:00
2015-02-16 13:50:53 -05:00
### Disclaimers
* **Warning:** Pygments is relatively slow. Expect much longer build times when using server-side highlighting.
* Languages available depends on your Pygments installation.
* We have sought to have the simplest interface possible, which consequently
limits configuration. An ambitious user is encouraged to extend the current
functionality to offer more customization.
2015-02-17 03:59:40 -05:00
2015-02-16 13:50:53 -05:00
2014-04-28 15:47:44 -04:00
## Client-side
Alternatively, code highlighting can be done in client-side JavaScript.
Client-side syntax highlighting is very simple to add. You'll need to pick
a library and a corresponding theme. Some popular libraries are:
2014-05-15 09:58:55 -04:00
- [Highlight.js]
2014-04-28 15:47:44 -04:00
- [Rainbow]
- [Syntax Highlighter]
- [Google Prettify]
This example uses the popular [Highlight.js] library, hosted by [Yandex], a
popular Russian search engine.
2014-09-03 00:12:26 -04:00
In your `./layouts/partials/` (or `./layouts/chrome/` ) folder, depending on your specific theme, there
2014-04-28 15:47:44 -04:00
will be a snippet that will be included in every generated HTML page, such
as `header.html` or `header.includes.html` . Simply add:
2014-05-15 09:58:55 -04:00
< link rel = "stylesheet" href = "https://yandex.st/highlightjs/8.0/styles/default.min.css" >
< script src = "https://yandex.st/highlightjs/8.0/highlight.min.js" > < / script >
2014-09-23 10:09:40 -04:00
< script > hljs . initHighlightingOnLoad ( ) ; < / script >
2014-04-28 15:47:44 -04:00
You can of course use your own copy of these files, typically in `./static/` .
2014-05-15 09:58:55 -04:00
[Highlight.js]: http://highlightjs.org/
2014-04-28 15:47:44 -04:00
[Rainbow]: http://craig.is/making/rainbows
[Syntax Highlighter]: http://alexgorbatchev.com/SyntaxHighlighter/
[Google Prettify]: https://code.google.com/p/google-code-prettify/
[Yandex]: http://yandex.ru/
2014-05-09 00:03:42 -04:00
2014-09-03 00:12:26 -04:00
Please see individual libraries documentation for how to implement the JavaScript-based libraries.