Code Highlighting in nanoc With rouge

By Jimmy Bonney | January 20, 2018

Code highlighting

Update March 18, 2018

Following this article, I did a performance review of the site, and changed the CSS framework from INK to mini.CSS. The CSS code presented in this page is therefore not fully up-to-date.

There hasn’t been much activities on the site for the past couple of years but I hope that 2018 will change that! It’s been a while that it was on the todo list so the first update of the year will bring better code highlighting with line numbering.

While I thought it would be a few minutes work, it turned out to be a little bit more complicated than that. nanoc offers syntax highlighting out of the box, and this is what I had originally in place on the site. However, despite trying a number of different options, I had issues activating the line numbering when using rouge. The solution was to add the relevant parameters to kramdown instead.

As a result, the Rules file now includes the following configuration (inspired by jekyll issue #4619):

1
2
filter :kramdown, syntax_highlighter: :rouge, syntax_highlighter_opts: {span: {line_numbers: false}, block: {line_numbers: true}}
filter :colorize_syntax, :default_colorizer => :rouge

And the associated stylesheet changes are as follow (inspired by Alex Peattie):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Style taken from https://alexpeattie.com/blog/better-syntax-highlighting-with-rouge
// with small changes for nanoc
pre {
  border: solid 1px #ddd;
  background: #fff;
  padding: 0;
  line-height: 23px;
  margin-bottom: 20px;
  white-space: pre;
  overflow-x: auto;
  word-break: inherit;
  word-wrap: inherit;

  // Added based on current nanoc implementation
  .lineno {
    border: none;
    background: transparent;
  }
}

// Added based on current nanoc implementation
.rouge-code pre{
  border: none;
  background: transparent;
}

.highlight td {
    padding: 8px 15px;
}

.gl {
  background: #fafafa;
  border-right: 1px solid #ddd;
  color: #999;

  /* Stop line numbers being visually selected */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

Let me know if you observe any inconsistencies following this.


For the time being, comments are managed by Disqus, a third-party library. I will eventually replace it with another solution, but the timeline is unclear. Considering the amount of data being loaded, if you would like to view comments or post a comment, click on the button below. For more information about why you see this button, take a look at the following article.