Martin Klöckner's Webpage


Testing code syntax highlight

21-Oct-2022 (last update 22-abr-2023)

This is a testing page for a script that I’m using to highlight code blocks within an html file.

The script is a heavily modified version of markdown-code-highlight-go that accepts an html file name as command line argument, and prints to stdout the html content but with all text inside code tags surrounded by css selectors.

For example, this is some C code:

#include <stdio.h>

int main(void) {
    char *hw = "Hello, world!\n";

    printf("%s\n", hw);
    return 0;
}

This is part of the shell script that I use to build the html blog pages from markdown files

sed -e "s/\$article-title\\$/$title/" -e "s/\$article-date\\$/$date/" \
    -e "s/\$pagetitle\\$/$pagetitle/" -e '/\$body\$/r./body.html' \
    -e "s/\$lang\\$/$lang/" -e "s/\$generator\\$/$generator/" \
    -e '/\$body\$/d' $template > "$dest_dir"/"$filename".html

This is an old JavaScript script that I was using to add the last modified date to a blog post:

// How do I format a date in javascript?
// stackoverflow.com/questions/3552461
function join(t, a, s) {
    function format(m) {
        let f = new Intl.DateTimeFormat('en', m);
        return f.format(t);
    }
    return a.map(format).join(s);
}

var dt = new Date(document.lastModified);

let format = [{day: 'numeric'}, {month: 'short'}, {year: 'numeric'}];
let dts = join(dt, format, '-');

document.querySelector('.article-date').innerHTML += " (last updated " + dts + ")";

I removed it from the webpage because it was not working properly, the document.lastModified was always returning the current date. In stead, I added a new part to the shell script that builds the pages, the new content parses the output of stat command and appends it to the article date.

This code is part of the script that I’m using for highlighting code blocks. It’s written in golang, a language that I didn’t knew until I needed to modify markdown-code-highlight-go to make it work on my use case.

rp := strings.NewReplacer("<code class=\"language-", "",
                           "\">", "", "</code>", "")

style := styles.Get("monokai")
if style == nil {
    style = styles.Fallback
}

formatter := formatters.Get("html")
if formatter == nil {
    formatter = formatters.Fallback
}

This part is using the chroma syntax highlighter, at the moment I couldn’t make it work, but I would like to, since it’s much richer in languages support and themes than the previous method.


Back to top