Fossil Forum

Syntax Highlighting
Login

Syntax Highlighting

Syntax Highlighting

(1.1) By llmII (lmartin92) on 2018-10-16 16:21:18 edited from 1.0 [link] [source]

This patch enables fossil to easily allow for syntax highlighting of code based on the file's extension as well as being compatible with line numbering. It does so in such a way that except for one new setting ('syntax-hl') fossil remains agnostic to syntax highlighting systems and allows for the option to forgo syntax highlighting for those wishing not to have said feature.

This is basically the same patch as before, albeit updated against (and tested) check-out bf8946a138564dbe949cac46e1b3331d66154cbf . 

In depth this is how the patch works:
It introduces a new way of rendering content, stripping '<blockquote>' and using '<pre><code>' blocks instead dependent upon if the new setting 'syntax-hl' is enabled. The code paths effected are 'src/info.c' and 'src/db.c'. In addition to the above, in the case of syntax highlighting enabled, to enable line numbering whilst syntax highlighting, a call to `void output_text_with_line_numbers(const char *, const char *)` is skipped and content is rendered as is without line numbering letting the javascript syntax highlighting library handle adding line numbers due to a detection of the url having certain parameters by th1 theme code. When 'syntax-hl' is off, this renders the line numbers the old way.

How to use:
One modifies the theme's TH1 code to test for if the "&ln=" parameter is available `[getParameter "ln"]` and if the setting 'syntax-hl' is on `[setting "syntax-hl"]`. This does require the user to serve up javascript files related to syntax highlighting from the same domain. The example will assume highlight.js useage and the utilization of my modified line numbering script which preserves all the capabilities of the current code's ability to link by line, highlight lines within a file, etc. The source for the modified line numbering script is available as well at a fossil repository, giving an example of highlighting, and highlighting with numbers, working. To get fossil to serve up the js and css files I use the following command (note that this could be done differently if fossil is reverse proxied by nginx): `/data/fossil server /data --port 9000 --scgi --repolist --files "*.js,*.css"`.

A full example of what to modify in the th1 code for a theme file is as follows:
added to header above `<div class="header">`:

<th1>
if {[setting "syntax-hl"]} {
  html "<link rel='stylesheet' href='https://code.amlegion.org/agate.min.css'>\n"
  html "<script src='https://code.amlegion.org/highlight.min.js'></script>\n"
  html "<script src='https://code.amlegion.org/hljsln.js'></script>\n"
  html "<script src='https://code.amlegion.org/hljsenable.js'></script>\n"
  if {[getParameter "ln"] ne ""} {
    html "<script src='https://code.amlegion.org/hljsenableln.js'></script>\n"
  }
}
</th1>

Notes:
This does not rely on highlight.js, it is just what the author found easiest to integrate and utilize. If others wish to use (for example) prism.js they need only modify their theme to detect the appropriate settings and load up javascript files and make javascript calls based upon the settings in fossil and the url parameters.

This has been tested and is working appropriately on my own repositories. The repo with the highlightjs line numbering customization script has links to show both syntax highlighting and the line numbering with selections, multiple selections, assorted selections, and first line scrolling.

Please let me know what might further need to be done to get this up to par to be included (or if its ultimately rejected). If testing this, let me know if there are any issues and I'll work through them as I can.

Sources:
fossil: https://fossil-scm.org
highlightjs: https://highlightjs.org/
hljs-line-numbers: https://code.amlegion.org/hljs_line_numbers

Patch: (generated using `fossil diff --exec-rel-paths`)
Index: src/db.c
==================================================================
--- src/db.c~0  2018-10-16 11:20:27.340374000 -0500
+++ src/db.c    2018-10-16 09:39:28.885064000 -0500
@@ -3358,0 +3359,5 @@
+/*
+** SETTING: syntax-hl        boolean default=off
+** This is a boolean set such that themes can use TH1 code
+** to detect if they should enable syntax highlighting.
+*/
Index: src/info.c
==================================================================
--- src/info.c~0        2018-10-16 11:20:27.378809000 -0500
+++ src/info.c  2018-10-16 09:39:28.930196000 -0500
@@ -670 +670 @@
-
+
@@ -2141 +2140,0 @@
-      @ <blockquote>
@@ -2143 +2142,2 @@
-        const char *z;
+        const char *z, *ext, *name;
+        char *tmp;
@@ -2145 +2145,6 @@
-        if( zLn ){
+        name = blob_str(&downloadName);
+        ext = (tmp = strrchr(name, '.')) == NULL ? "" : tmp+1;
+        @ <pre>
+        if( zLn && (db_get_boolean("syntax-hl", 0) != 0) ) {
+          @ <code class="%h(ext)">%h(z)</code>
+        }else if( zLn ){
@@ -2148,3 +2153 @@
-          @ <pre>
-          @ %h(z)
-          @ </pre>
+          @ <code class="%h(ext)">%h(z)</code>
@@ -2151,0 +2155 @@
+        @ </pre>
@@ -2152,0 +2157 @@
+        @ <blockquote>
@@ -2155,0 +2161 @@
+        @ </blockquote>
@@ -2156,0 +2163 @@
+        @ <blockquote>
@@ -2157,0 +2165 @@
+        @ </blockquote>
@@ -2159 +2166,0 @@
-      @ </blockquote>
@@ -2218 +2225 @@
-      ? db_text("(No title)",
+      ? db_text("(No title)",

(2.1) By llmII (lmartin92) on 2018-10-16 16:16:34 edited from 2.0 in reply to 1.0 [link] [source]

Deleted

(3.1) By llmII (lmartin92) on 2018-10-16 16:21:25 edited from 3.0 in reply to 2.0 [source]

Deleted