This patch changes the way `void artifact_page(void)` renders a files
content. It also adds a new setting in "src/db.c" relating to syntax
highlighting. As with the previous patch, it changes content rendering
to use `<pre><code>`. In addition, on the paths that need `<pre><code>`
it removes the `<blockquote>` from the section. The introduction of a
new setting 'syntax-hl' allows for the code to determine if syntax
highlighting is enabled, and if so, render things the new way. The new
way of rendering content to make it compatible with syntax highlighting,
whilst line numbering, is to skip a call to
`void output_text_with_line_numbers(const char *, const char *)` and
allow external javascript code to deal with line numbering. If syntax-hl
is "off" it will continue to render line numbers the old way, whilst
retaining the new rendering of content without line numbers.

This should allow for Fossil to remain agnostic to syntax highlighting
systems and allow for the option to forgo syntax highlighting for those
who would not wish to have it.

One utilizes it by modifying the theme with 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.
Currently this is tested with highlight.js and a modified line numbering
extension to it.

This code however *does not* rely on highlight.js and 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.

Provided at the end are first the patch, then an example as to how to
change the theme to support this, then the modified line numbering
extension to highlight.js (via link). All but the patch assume one is
utilizing version 9.12.0 of highlight.js, and will use the provided line
numbering extension. Do note that the line numbering extension does not
support the full extent of "&ln=" as of yet. Work will continue on that
but it does not hinder the ability to utilize this patch and for anyone
to properly implement "&ln=".

It is my opinion (and solely my opinion) that assuming I get the line
numbering script and "&ln=" up to par with the C source that we might
wish to make things even simpler for the user by including the gamut
of this stuff so it's as easy as checking a seperate setting
'easy-syntax-hl'. That of course depends on if its acceptable to either
include highlight.js and the line numbering extension I've modified,
or to link to them from within (meaning a relaxation of the content
security policy as noted in <head><meta>).

The patch:
Below is the output of `fossil diff` ran against checkout
'50daa540f8881cb022600fefcba3f20f44175e28' with my changes:

Index: src/db.c
--- /home/masky/misc/fossil/src/db.c~0 2018-07-01 18:15:59.427006000 -0500
+++ /home/masky/misc/fossil/src/db.c    2018-07-01 16:34:01.746686000 -0500
@@ -3263,0 +3264,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
--- /home/masky/misc/fossil/src/info.c~0 2018-07-01 18:15:59.457016000 -0500 +++ /home/masky/misc/fossil/src/info.c 2018-07-01 16:34:01.891965000 -0500
@@ -670 +670 @@
@@ -2155 +2154,0 @@
-      @ <blockquote>
@@ -2157 +2156,2 @@
-        const char *z;
+        const char *z, *ext, *name;
+        char *tmp;
@@ -2159 +2159,6 @@
-        if( zLn ){
+        name = blob_str(&downloadName);
+        ext = (tmp = strrchr(name, '.')) == NULL ? "" : tmp+1;
+        @ <pre><code class="%h(ext)">
+        if( zLn && (db_get_boolean("syntax-hl", 0) != 0) ) {
+          @ %h(z)
+        }else if( zLn ){
@@ -2162 +2166,0 @@
-          @ <pre>
@@ -2164 +2167,0 @@
-          @ </pre>
@@ -2165,0 +2169 @@
+        @ </code></pre>
@@ -2166,0 +2171 @@
+        @ <blockquote>
@@ -2169,0 +2175 @@
+        @ </blockquote>
@@ -2170,0 +2177 @@
+        @ <blockquote>
@@ -2171,0 +2179 @@
+        @ </blockquote>
@@ -2173 +2180,0 @@
-      @ </blockquote>
@@ -2232 +2239 @@
-      ? db_text("(No title)",
+      ? db_text("(No title)",


Below is the theme modification required (added to header above
`<div class="header">`):
if {[setting "syntax-hl"]} {
html "<link rel='stylesheet' href=''>\n" html "<script src=''></script>\n"
  html "<script src=''></script>\n"
  html "<script>hljs.configure({languages: []});</script>\n"
  html "<script>hljs.initHighlightingOnLoad();</script>\n"
  if {[getParameter "ln"] ne ""} {
    html "<script>hljs.initLineNumbersOnLoad();</script>"
    html "<script>hljs.initLineNumScroll();</script>"


The highlight.js specific customized line numbering extension destined
to see further work is available at:
fossil-users mailing list

Reply via email to