kloeckner.com.ar

a backup of my entire webpage
Log Files Refs README LICENSE

testing-syntax-highlight.md (2696B) - raw

   1 %%
   2 title: "Testing code syntax highlight"
   3 date: "21-Oct-2022"
   4 %%
   5 
   6 # Testing code syntax highlight
   7 
   8 This is a testing page for a script that I'm using to highlight code blocks
   9 within an html file.
  10 
  11 The script is a heavily modified version of
  12 [markdown-code-highlight-go](https://github.com/zupzup/markdown-code-highlight-go)
  13 that accepts an html file name as command line argument, and prints to stdout
  14 the html content but with all text inside code tags surrounded by css selectors.
  15 
  16 For example, this is some C code:
  17 
  18 ```c
  19 #include <stdio.h>
  20 
  21 int main(void) {
  22     char *hw = "Hello, world!\n";
  23 
  24     printf("%s\n", hw);
  25     return 0;
  26 }
  27 ```
  28 
  29 This is part of the shell script that I use to build the html blog pages
  30 from markdown files
  31 
  32 ```console
  33 sed -e "s/\$article-title\\$/$title/" -e "s/\$article-date\\$/$date/" \
  34     -e "s/\$pagetitle\\$/$pagetitle/" -e '/\$body\$/r./body.html' \
  35     -e "s/\$lang\\$/$lang/" -e "s/\$generator\\$/$generator/" \
  36     -e '/\$body\$/d' $template > "$dest_dir"/"$filename".html
  37 ```
  38 
  39 This is an old JavaScript script that I was using to add the last modified date
  40 to a blog post:
  41 
  42 ```js
  43 // How do I format a date in javascript?
  44 // stackoverflow.com/questions/3552461
  45 function join(t, a, s) {
  46     function format(m) {
  47         let f = new Intl.DateTimeFormat('en', m);
  48         return f.format(t);
  49     }
  50     return a.map(format).join(s);
  51 }
  52 
  53 var dt = new Date(document.lastModified);
  54 
  55 let format = [{day: 'numeric'}, {month: 'short'}, {year: 'numeric'}];
  56 let dts = join(dt, format, '-');
  57 
  58 document.querySelector('.article-date').innerHTML += " (last updated " + dts + ")";
  59 ```
  60 I removed it from the webpage because it was not working properly, the
  61 `document.lastModified` was always returning the current date. In stead, I
  62 added a new part to the shell script that builds the pages, the new content
  63 parses the output of `stat` command and appends it to the article date.
  64 
  65 This code is part of the script that I'm using for highlighting code blocks.
  66 It's written in golang, a language that I didn't knew until I needed to modify
  67 [markdown-code-highlight-go](https://github.com/zupzup/markdown-code-highlight-go)
  68 to make it work on my use case.
  69 
  70 ```go
  71 rp := strings.NewReplacer("<code class=\"language-", "",
  72                            "\">", "", "</code>", "")
  73 
  74 style := styles.Get("monokai")
  75 if style == nil {
  76     style = styles.Fallback
  77 }
  78 
  79 formatter := formatters.Get("html")
  80 if formatter == nil {
  81     formatter = formatters.Fallback
  82 }
  83 ```
  84 
  85 This part is using the [chroma](https://github.com/alecthomas/chroma) syntax
  86 highlighter, at the moment I couldn't make it work, but I would like to, since
  87 it's much richer in languages support and themes than the previous method.