Hugo Syntax Highlighting for Org

Code are stolen from ruippeixotog/quicksort

Inline

  1. =hl_lines=
  2. ~hl_lines~
  3. 1
    
    src_org{third}
    hugo exports this type of inline code block into actual code block :(

Src Block

Maybe hugo doesn't support hl_lines property to highlight certain lines in a src block in org.

However, you can use hugo shortcodes to achieve this.

Example

Traditionally adding property

 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
// #+begin_src rust :hl_lines 8
use std::io;
use std::io::BufRead;

fn quicksort(v: &mut [i32]) {
    if !v.is_empty() {
        let mut sep = 0;
        for i in 1..v.len() {
            if v[i] < v[0] {
                sep += 1;
                v.swap(sep, i);
            }
        }

        v.swap(0, sep);
        quicksort(&mut v[..sep]);
        quicksort(&mut v[(sep + 1)..]);
    }
}

fn main() {
    let stdin = io::stdin();
    let mut v: Vec<i32> = match stdin.lock().lines().nth(1) {
        Some(Ok(s)) => s.split(' ').flat_map(|s| s.parse()).collect(),
        _ => unreachable!()
    };

    quicksort(&mut v);

    let v_str: Vec<String> = v.iter().map(|x| x.to_string()).collect();
    println!("{}", v_str.join(" "));
}

Using shortcode

usage: (Note the /* and */ notations are to prevent itselves being interpreted by hugo.)

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

If you want to also highlight the src block in org mode, you can put #+BEGIN_SRC and #+END_SRC inside the shortcode. (However, the fences will be inside the code block) example: (Note two comma at the beginning of fences are to distinguish itself from the its parent src block.)

1
2
3
4
5
{{/* < highlight rust "linenos=table,hl_lines=8 15-17,linenostart=199" > */}}
#+begin_src rust
// code
#+end_src
{{/* < / highlight > */}}

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#+begin_src rust
use std::io;
use std::io::BufRead;

fn quicksort(v: &mut [i32]) {
    if !v.is_empty() {
        let mut sep = 0;
        for i in 1..v.len() {
            if v[i] < v[0] {
                sep += 1;
                v.swap(sep, i);
            }
        }

        v.swap(0, sep);
        quicksort(&mut v[..sep]);
        quicksort(&mut v[(sep + 1)..]);
    }
}

fn main() {
    let stdin = io::stdin();
    let mut v: Vec<i32> = match stdin.lock().lines().nth(1) {
        Some(Ok(s)) => s.split(' ').flat_map(|s| s.parse()).collect(),
        _ => unreachable!()
    };

    quicksort(&mut v);

    let v_str: Vec<String> = v.iter().map(|x| x.to_string()).collect();
    println!("{}", v_str.join(" "));
}
#+end_src