repos/html-syntax-highlight

highlights code within an html file
Commits Files Refs README LICENSE
README.md (46 lines)

Highlight code on a static html file

This program parses an html file and adds chroma classes to code within pre and code tags. Then you can add chroma themes with css.

Running

Make sure you have golang installed (if not, follow this instructions) then run:

$ go run main.go <test_file.html>

with <test_file.html> being an html file containing some code to highlight.

Input file format

This is an example of a code snippet contained within an html file that this program adds chroma css classes

(...)
<pre><code class="language-c">
#include <stdio.h>

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

    printf("%s\n", hw);
    return 0;
}
</code></pre>
(...)

To capture the code snippet it uses regular expression so it should be easier to adapt to other formats.

Example

The main purpose of this program was to highlight code snippets on my blog. Checkout this post that I made to test it.

License

MIT

   1 # Highlight code on a static html file
   2 
   3 This program parses an html file and adds
   4 [chroma](https://github.com/alecthomas/chroma) classes to code within pre and
   5 code tags. Then you can add chroma themes with css.
   6 
   7 ## Running
   8 
   9 Make sure you have [golang](https://go.dev/) installed (if not, follow [this
  10 instructions](https://go.dev/doc/install)) then run:
  11 
  12 ```shell
  13 $ go run main.go <test_file.html>
  14 ```
  15 
  16 with `<test_file.html>` being an html file containing some code to highlight.
  17 
  18 ## Input file format
  19 This is an example of a code snippet contained within an html file that this
  20 program adds chroma css classes
  21 
  22 ```html
  23 (...)
  24 <pre><code class="language-c">
  25 #include <stdio.h>
  26 
  27 int main(void) {
  28     char *hw = "Hello, world!\n";
  29 
  30     printf("%s\n", hw);
  31     return 0;
  32 }
  33 </code></pre>
  34 (...)
  35 ```
  36 
  37 To capture the code snippet it uses regular expression so it should be easier to
  38 adapt to other formats.
  39 
  40 ## Example
  41 The main purpose of this program was to highlight code snippets on my blog.
  42 Checkout [this post](https://kloeckner.com.ar/blog/testing-syntax-highlight/testing-syntax-highlight)
  43 that I made to test it.
  44 
  45 ## License
  46 [MIT](https://opensource.org/licenses/MIT)