DATARAMA

Support syntax highlighting

January 29, 2016
Project: goon

In this patch, each code listing in tree entries and commit diffs is given a language-specific class — eg. Scheme programs are given a scheme class, Markdown docs a markdown class, and so on. This allows front-end syntax highlighters (such as highlight.js) to work with them without being forced to use clunky and unreliable autodetectors.

1 files changed, 22 insertions / 3 deletions

diff --git a/goon.scm b/goon.scm
index 7fd0a4e..3e27568 100755
--- a/goon.scm
+++ b/goon.scm
@@ -38,6 +38,25 @@
     (take lst size)
     lst))
 
+; == LANGUAGE CLASSES =========================================================
+(define language-map
+  `((".scm"  . "scheme")
+    (".pu"   . "markdown")
+    (".md"   . "markdown")
+    (".erl"  . "erlang")
+    (".hs"   . "haskell")
+    (".c"    . "c")
+    (".c++"  . "c++")
+    (".java" . "java")
+    (".css"  . "css")
+    (".html" . "html")
+    (".js"   . "javascript")
+    (".amap" . "scheme")))
+
+(define (file-language filename)
+  (let ((lang (assoc (filepath:take-extension filename) language-map)))
+    (if lang (cdr lang) "nohighlight")))
+
 ; == OUTPUT FILES =============================================================
 (define (output-frontpage page)
   (call-with-output-file (filepath:combine output-dir "index.html")
@@ -519,7 +538,7 @@
         (let ((diffs (diff repo 
                            (commit-tree (commit-parent comm)) 
                            (commit-tree comm))))
-          `(code (pre ,(diff->string diffs))))
+          `(pre (code (@ (class "diff")) ,(diff->string diffs))))
         #f)))
 
 (define (render-commitpage repo comm)
@@ -580,9 +599,9 @@
 
 (define (tree-entry-content repo entry)
   (let ((blob (tree-entry->object repo entry))
-        (filename (tree-entry-name entry)))
+        (lang (file-language (tree-entry-name entry))))
     `(div 
-       (code (pre 
+       (pre (code (@ (class ,lang))
                ,(if (blob-binary? blob)
                   "<binary file>"
                   (blob->string (blob-content blob))))))))
 
Powered by Plutonium