github.com/jbramsden/hugo@v0.47.1/docs/content/en/content-management/syntax-highlighting.md (about)

     1  ---
     2  title: Syntax Highlighting
     3  description: Hugo comes with really fast syntax highlighting from Chroma.
     4  date: 2017-02-01
     5  publishdate: 2017-02-01
     6  keywords: [highlighting,pygments,chroma,code blocks,syntax]
     7  categories: [content management]
     8  menu:
     9    docs:
    10      parent: "content-management"
    11      weight: 300
    12  weight: 20
    13  sections_weight: 20
    14  draft: false
    15  aliases: [/extras/highlighting/,/extras/highlight/,/tools/syntax-highlighting/]
    16  toc: true
    17  ---
    18  
    19  From Hugo 0.28, the default syntax hightlighter in Hugo is [Chroma](https://github.com/alecthomas/chroma); it is built in Go and is really, really fast -- and for the most important parts compatible with Pygments.
    20  
    21  If you want to continue to use Pygments (see below), set `pygmentsUseClassic=true` in your site config.
    22  
    23  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](#generate-syntax-highlighter-css)).
    24  
    25  * `linenos=inline` or `linenos=table` (`table` will give copy-and-paste friendly code blocks) turns on line numbers.
    26  * `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.
    27  * `linenostart=199` starts the line number count from 199.
    28  
    29  With that, this:
    30  
    31  ```
    32  {{</* highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" */>}}
    33  // ... code
    34  {{</* / highlight */>}}
    35  ```
    36  
    37  Gives this:
    38  
    39  {{< highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" >}}
    40  // GetTitleFunc returns a func that can be used to transform a string to
    41  // title case.
    42  //
    43  // The supported styles are
    44  //
    45  // - "Go" (strings.Title)
    46  // - "AP" (see https://www.apstylebook.com/)
    47  // - "Chicago" (see http://www.chicagomanualofstyle.org/home.html)
    48  //
    49  // If an unknown or empty style is provided, AP style is what you get.
    50  func GetTitleFunc(style string) func(s string) string {
    51    switch strings.ToLower(style) {
    52    case "go":
    53      return strings.Title
    54    case "chicago":
    55      tc := transform.NewTitleConverter(transform.ChicagoStyle)
    56      return tc.Title
    57    default:
    58      tc := transform.NewTitleConverter(transform.APStyle)
    59      return tc.Title
    60    }
    61  }
    62  {{< / highlight >}}
    63  
    64  
    65  ## Configure Syntax Highlighter
    66  To make the transition from Pygments to Chroma seamless, they share a common set of configuration options:
    67  
    68  pygmentsOptions
    69  :  A comma separated list of options. See below for a full list.
    70  
    71  pygmentsCodefences
    72  : Set to true to enable syntax highlighting in code fences with a language tag in markdown (see below for an example).
    73  
    74  pygmentsStyle
    75  : The style of code highlighting. Note that this option is not
    76    relevant when `pygmentsUseClasses` is set.
    77  
    78    Syntax highlighting galleries:
    79    **Chroma** ([short snippets](https://xyproto.github.io/splash/docs/all.html),
    80    [long snippets](https://xyproto.github.io/splash/docs/longer/all.html)),
    81    [Pygments](https://help.farbox.com/pygments.html)
    82  
    83  pygmentsUseClasses
    84  : Set to `true` to use CSS classes to format your highlighted code. See [Generate Syntax Highlighter CSS](#generate-syntax-highlighter-css).
    85  
    86  pygmentsCodefencesGuessSyntax
    87  : Set to `true` to try to do syntax highlighting on code fenced blocks in markdown without a language tag.
    88  
    89  pygmentsUseClassic
    90  : Set to true to use Pygments instead of the much faster Chroma.
    91  
    92  ### Options
    93  
    94  `pygmentsOptions` can be set either in site config or overridden per code block in the Highlight shortcode or template func.
    95  
    96  noclasses
    97  : Use inline style.
    98  
    99  linenos
   100  : For Chroma, any value in this setting will print line numbers. Pygments has some more fine grained control.
   101  
   102  linenostart
   103  : Start the line numbers from this value (default is 1).
   104  
   105  
   106  hl_lines
   107  : Highlight a space separated list of line numbers. For Chroma, you can provide a list of ranges, i.e. "3-8 10-20".
   108  
   109  
   110  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](http://pygments.org/docs/formatters/#HtmlFormatter) for details.
   111  
   112  
   113  ## Generate Syntax Highlighter CSS
   114  
   115  If you run with `pygmentsUseClasses=true` in your site config, you need a style sheet.
   116  
   117  You can generate one with Hugo:
   118  
   119  ```bash
   120  hugo gen chromastyles --style=monokai > syntax.css
   121  ```
   122  
   123  Run `hugo gen chromastyles -h` for more options. See https://help.farbox.com/pygments.html for a gallery of available styles.
   124  
   125  
   126  ## Highlight Shortcode
   127  
   128  Highlighting is carried out via the [built-in shortcode](/content-management/shortcodes/) `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.
   129  
   130  ### Example `highlight` Shortcode
   131  
   132  {{< code file="example-highlight-shortcode-input.md" >}}
   133  {{</* highlight html */>}}
   134  <section id="main">
   135    <div>
   136      <h1 id="title">{{ .Title }}</h1>
   137      {{ range .Pages }}
   138        {{ .Render "summary"}}
   139      {{ end }}
   140    </div>
   141  </section>
   142  {{</* /highlight */>}}
   143  {{< /code >}}
   144  
   145  
   146  
   147  ## Highlight Template Func
   148  
   149  See [Highlight](/functions/highlight/).
   150  
   151  ## Highlight in Code Fences
   152  
   153  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](/getting-started/configuration/);
   154  
   155  ````
   156  ```go-html-template
   157  <section id="main">
   158    <div>
   159      <h1 id="title">{{ .Title }}</h1>
   160      {{ range .Pages }}
   161        {{ .Render "summary"}}
   162      {{ end }}
   163    </div>
   164  </section>
   165  ```
   166  ````
   167  
   168  ## List of Chroma Highlighting Languages
   169  
   170  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):
   171  
   172  {{< chroma-lexers >}}
   173  
   174  ## Highlight with Pygments Classic
   175  
   176  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.
   177  
   178  {{% note "Disclaimers on Pygments" %}}
   179  * Pygments is relatively slow and _causes a performance hit when building your site_, but Hugo has been designed to cache the results to disk.
   180  * The caching can be turned off by setting the `--ignoreCache` flag to `true`.
   181  * The languages available for highlighting depend on your Pygments installation.
   182  {{% /note %}}
   183  
   184  If you have never worked with Pygments before, here is a brief primer:
   185  
   186  + Install Python from [python.org](https://www.python.org/downloads/). Version 2.7.x is already sufficient.
   187  + 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.
   188  
   189  On Debian and Ubuntu systems, you may also install Pygments by running `sudo apt-get install python3-pygments`.
   190  
   191  
   192  
   193  [Prism]: http://prismjs.com
   194  [prismdownload]: http://prismjs.com/download.html
   195  [Highlight.js]: http://highlightjs.org/
   196  [Rainbow]: http://craig.is/making/rainbows
   197  [Syntax Highlighter]: http://alexgorbatchev.com/SyntaxHighlighter/
   198  [Google Prettify]: https://github.com/google/code-prettify
   199  [Yandex]: http://yandex.ru/