Discussion:
Patch: Syntax Highlighting Integration (line numbers working)
(too old to reply)
Lester L. Martin II
2018-07-01 23:23:48 UTC
Permalink
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)",

!!END!!

Below is the theme modification required (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>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>"
}
}
</th1>

!!END!!

The highlight.js specific customized line numbering extension destined
to see further work is available at:
https://code.amlegion.org/hljsln.v1.js
Lester L. Martin II
2018-07-02 00:52:33 UTC
Permalink
Note:

Have noticed that the functions, utilized without highlighting,
actually generate 2 new lines, one at the beginning, and one at
the end. This breaks line numbering (file grew longer in lines).
Believe this has existed *before* I worked on my syntax
highlighting and line numbering patch. If anyone can confirm
this is the case in Fossil 2.6 (have to inspect the html
markup), please let me know. This behaviour isn't exhibited
in the default builtin line numbering code, just the
non-line-numbering code.

Will follow up with a patch to fix that as well likely.

--
Lester L. Martin II
Lester L. Martin II
2018-07-02 01:03:23 UTC
Permalink
Post by Lester L. Martin II
Have noticed that the functions, utilized without highlighting,
actually generate 2 new lines, one at the beginning, and one at
the end. This breaks line numbering (file grew longer in lines).
Believe this has existed *before* I worked on my syntax
highlighting and line numbering patch. If anyone can confirm
this is the case in Fossil 2.6 (have to inspect the html
markup), please let me know. This behaviour isn't exhibited
in the default builtin line numbering code, just the
non-line-numbering code.
Will follow up with a patch to fix that as well likely.
The fix (patch is redone) is as follows:
Index: src/db.c
==================================================================
--- /home/masky/misc/fossil/src/db.c~0 2018-07-01 20:02:18.687830000
-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 20:02:18.714191000
-0500
+++ /home/masky/misc/fossil/src/info.c 2018-07-01 19:57:46.872570000
-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>
+ if( zLn && (db_get_boolean("syntax-hl", 0) != 0) ) {
+ @ <code class="%h(ext)">%h(z)</code>
+ }else if( zLn ){
@@ -2162,3 +2167 @@
- @ <pre>
- @ %h(z)
- @ </pre>
+ @ <code class="%h(ext)">%h(z)</code>
@@ -2165,0 +2169 @@
+ @ </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)",


--
Lester L. Martin II
Lester L. Martin II
2018-07-02 02:31:46 UTC
Permalink
Post by Lester L. Martin II
Have noticed that the functions, utilized without highlighting,
actually generate 2 new lines, one at the beginning, and one at
the end. This breaks line numbering (file grew longer in lines).
Believe this has existed *before* I worked on my syntax
highlighting and line numbering patch. If anyone can confirm
this is the case in Fossil 2.6 (have to inspect the html
markup), please let me know. This behaviour isn't exhibited
in the default builtin line numbering code, just the
non-line-numbering code.
Will follow up with a patch to fix that as well likely.
Apparently the previous patch (generated by fossil diff) wouldn't
work properly. This patch is generated by fossil diff -i... and
has worked at least locally. Thanks to an IRC user for letting
me know the patch was failing to apply properly. Please let me
know if this won't apply.

Index: src/db.c
==================================================================
--- src/db.c
+++ src/db.c
@@ -3259,10 +3259,15 @@
** A shell command used to launch your preferred
** web browser when given a URL as an argument.
** Defaults to "start" on windows, "open" on Mac,
** and "firefox" on Unix.
*/
+/*
+** 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.
+*/

/*
** Look up a control setting by its name. Return a pointer to the
Setting
** object, or NULL if there is no such setting.
**

Index: src/info.c
==================================================================
--- src/info.c
+++ src/info.c
@@ -665,11 +665,11 @@
" FROM blob, event"
" WHERE blob.rid=%d"
" AND event.objid=%d",
rid, rid
);
-
+
cookie_link_parameter("diff","diff","2");
diffType = atoi(PD("diff","2"));
if( db_step(&q1)==SQLITE_ROW ){
const char *zUuid = db_column_text(&q1, 0);
int nUuid = db_column_bytes(&q1, 0);
@@ -2150,29 +2150,36 @@
@ </iframe>
}else{
style_submenu_element("Hex", "%s/hexdump?name=%s", g.zTop,
zUuid);
blob_to_utf8_no_bom(&content, 0);
zMime = mimetype_from_content(&content);
- @ <blockquote>
if( zMime==0 ){
- const char *z;
+ const char *z, *ext, *name;
+ char *tmp;
z = blob_str(&content);
- 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 ){
output_text_with_line_numbers(z, zLn);
}else{
- @ <pre>
- @ %h(z)
- @ </pre>
+ @ <code class="%h(ext)">%h(z)</code>
}
+ @ </pre>
}else if( strncmp(zMime, "image/", 6)==0 ){
+ @ <blockquote>
@ <i>(file is %d(blob_size(&content)) bytes of image
data)</i><br />
@ <img src="%R/raw/%s(zUuid)?m=%s(zMime)" />
style_submenu_element("Image", "%R/raw/%s?m=%s", zUuid, zMime);
+ @ </blockquote>
}else{
+ @ <blockquote>
@ <i>(file is %d(blob_size(&content)) bytes of binary data)</i>
+ @ </blockquote>
}
- @ </blockquote>
}
}
style_footer();
}

@@ -2227,11 +2234,11 @@
if( strcmp(zModAction,"approve")==0 ){
moderation_approve(rid);
}
}
zTktTitle = db_table_has_column("repository", "ticket", "title" )
- ? db_text("(No title)",
+ ? db_text("(No title)",
"SELECT title FROM ticket WHERE tkt_uuid=%Q", zTktName)
: 0;
style_header("Ticket Change Details");
style_submenu_element("Raw", "%R/artifact/%s", zUuid);
style_submenu_element("History", "%R/tkthistory/%s", zTktName);
Lester L. Martin II
2018-07-02 02:48:16 UTC
Permalink
Format flowed was breaking the patch, it's now attached,
sorry for the trouble.

--
Lester L. Martin II

Loading...