Syntax highlighting v2

Discuss issues and requests related with the forum software
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Syntax highlighting v2

Post by lexikos » 25 Nov 2022, 23:23

Code blocks need to support highlighting v2 syntax.

As a minimum improvement, the default code block could highlight single quoted strings, despite them being unsupported in v1. I think failure to highlight single quoted strings is the one thing that impacts readability most.

Ideally, v2 code blocks would highlight v2 keywords and link to v2 documentation.

Ideally it would be possible to specify the version for a code block, and the version would default to v2 when posted in a v2 subforum (whether this is done when the editor buttons are clicked or when the page is rendered).

Also, the current "Select code" dropdown (which generates tags which allow for a filename to be specified) is a pain to use. Surely we can put AutoHotkey at the top of the language list.

User avatar
Ragnar
Posts: 764
Joined: 30 Sep 2013, 15:25

Re: Syntax highlighting v2

Post by Ragnar » 26 Nov 2022, 10:33

It is also possible to use the syntax highlighter from the docs, which is far more accurate and pretty than the default highlighter used in these forums.

Currently I use the following userscript:

Code: Select all

// ==UserScript==
// @name         AutoHotkey Forum Codebox Highlighting
// @namespace    https://*.autohotkey.com/board*
// @version      0.1
// @description  Use custom syntax highlighter
// @author       Ragnar-F
// @match        https://*.autohotkey.com/board*
// ==/UserScript==

h2_a = document.querySelector('h2 a');
isV2 = (location.href.match(/f=(82|83|37|92)/) || (h2_a && h2_a.href.match(/f=(82|83|37|92)/))) || false; // forum id is sometimes not always present in the url
unsafeWindow.forceNoScript = true;

var css = '.codebox{border-color:transparent;}*{scrollbar-base-color: #252525;scrollbar-face-color: #4b4b4b;scrollbar-3dlight-color: #252525;scrollbar-highlight-color: #252525;scrollbar-track-color: #252525;scrollbar-arrow-color: #4b4b4b;scrollbar-shadow-color: #252525;}::-webkit-scrollbar{background-color: #252525;}::-webkit-scrollbar-track{background-color: #252525;}::-webkit-scrollbar-thumb{background-color: #4b4b4b;border: 2px solid #252525;}::-webkit-scrollbar-thumb:hover{background-color: #5f5f5f;}::-webkit-scrollbar-corner{background-color: inherit;}::-ms-expand{background-color: #1e1e1e;color: #4b4b4b;border-color: #252525;}code,pre{font-family:Consolas!important;font-size:14px!important;tab-size:4;color:#d5d5d5!important;background-color:#2e2e2e!important;}code span>a,code span>a:hover,code span>a:link,pre span>a,pre span>a:hover,pre span>a:link{color:inherit!important}code>.bif,code>.cmd,pre>.bif,pre>.cmd{color:#569cd6}code>.met,pre>.met{color:#60c5dc}code>.cfs,code>.dec,pre>.cfs,pre>.dec{color:#c586c0}code>.str,pre>.str{color:#ce9178}code .str>.esc,pre .str>.esc{color:#ff6868}pre>.biv,pre>.cls,code>.biv,code>.cls{color:#4ec9b0}code>.dir,pre>.dir{color:#dcdcaa}code>.fun,code>.lab,pre>.fun,pre>.lab{font-weight:normal;color:#dcdcaa}code>.num,pre>.num{color:#b5cea8}code em,code .cmt,pre.origin em,pre .cmt{color:#6a9955!important}';
//var css = 'code,pre {  font-family: Consolas !important;  font-size: 14px !important;  tab-size: 4;  color: #000 !important;  background-color: #eff0f1 !important;}code span>a,code span>a:hover,code span>a:link,pre span>a,pre span>a:hover,pre span>a:link {  color: inherit !important}code>.bif,code>.cmd,pre>.bif,pre>.cmd {  color: #0148c2}code>.met,pre>.met {  color: #097f9a}code>.cfs,code>.dec,pre>.cfs,pre>.dec {  color: #6F008A}code>.str,pre>.str {  color: #A31515}code .str>.esc,pre .str>.esc {  color: #FF0000}pre>.biv,pre>.cls,code>.biv,code>.cls {  color: #006400}code>.dir,pre>.dir {  color: green}code>.fun,code>.lab,pre>.fun,pre>.lab {  font-weight: bold;  color: #290e90}code>.num,pre>.num {  color: #1a6c4e}code em,code .cmt,pre.origin em,pre .cmt {  color: #708090 !important}';

addGlobalStyle(css);
if (isV2)
{
    scriptDir = 'https://www.autohotkey.com/docs/v2/static';
    loadScript(scriptDir + '/content.js', function() {
        var pres = document.querySelectorAll("code.lang-autohotkey, pre.prettyprint");
        // unsafeWindow.features.addCodeBoxButtons(pres);
        unsafeWindow.features.addSyntaxColors(pres);
    });
}
else
{
    scriptDir = 'https://www.autohotkey.com/docs/v1/static';
    loadScript(scriptDir + '/content.js', function() {
        var pres = document.querySelectorAll("code.lang-autohotkey, pre.prettyprint");
        // unsafeWindow.features.addCodeBoxButtons(pres);
        unsafeWindow.features.addSyntaxColors(pres);
    });
}

function loadScript(url, callback) {
    var script = document.createElement("script")
    script.type = "text/javascript";

    if (script.readyState){  // IE
        script.onreadystatechange = function() {
            if (script.readyState == "loaded" ||
                    script.readyState == "complete") {
                script.onreadystatechange = null;
                callback();
            }
        };
    } else { // Others
        script.onload = function() {
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}
and blocked the default highlighter script with an adblocker (ublock) using the filter:

Code: Select all

||www.autohotkey.com/boards/assets/prism/prism.js$script,domain=www.autohotkey.com
Theoretically, this could also be natively integrated into the forum.

Example: new vs. old
image.png
image.png (5.11 KiB) Viewed 8432 times
image.png
image.png (7.19 KiB) Viewed 8432 times
Last edited by Ragnar on 02 May 2023, 16:33, edited 2 times in total.

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 26 Nov 2022, 10:51

Thanks, I'll have a look to bring these suggestions in.
@Ragnar how is the light or dark theme set? Is there a class name? Haven't fully checked the code, just if you off hand.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
Ragnar
Posts: 764
Joined: 30 Sep 2013, 15:25

Re: Syntax highlighting v2

Post by Ragnar » 26 Nov 2022, 13:50

In the userscript this is "hardcoded". See the var css = line. A mixture of CSS from the content.css and dark.css files (specifically <pre>, <code> and scrollbar).

The docs default to light theme CSS defined in content.css, which is overridden with dark.css when set to dark theme.

Formatted, the CSS line would look like the following, which is currently for dark theme:

Code: Select all

.codebox {
  border-color: transparent;
}

* {
  scrollbar-base-color: #252525;
  scrollbar-face-color: #4b4b4b;
  scrollbar-3dlight-color: #252525;
  scrollbar-highlight-color: #252525;
  scrollbar-track-color: #252525;
  scrollbar-arrow-color: #4b4b4b;
  scrollbar-shadow-color: #252525;
}

::-webkit-scrollbar {
  background-color: #252525;
}

::-webkit-scrollbar-track {
  background-color: #252525;
}

::-webkit-scrollbar-thumb {
  background-color: #4b4b4b;
  border: 2px solid #252525;
}

::-webkit-scrollbar-thumb:hover {
  background-color: #5f5f5f;
}

::-webkit-scrollbar-corner {
  background-color: inherit;
}

::-ms-expand {
  background-color: #1e1e1e;
  color: #4b4b4b;
  border-color: #252525;
}

code,
pre {
  font-family: Consolas !important;
  font-size: 14px !important;
  tab-size: 4;
  color: #d5d5d5 !important;
  background-color: #2e2e2e !important;
}

code span>a,
code span>a:hover,
code span>a:link,
pre span>a,
pre span>a:hover,
pre span>a:link {
  color: inherit !important
}

code>.bif,
code>.cmd,
pre>.bif,
pre>.cmd {
  color: #569cd6
}

code>.met,
pre>.met {
  color: #60c5dc
}

code>.cfs,
code>.dec,
pre>.cfs,
pre>.dec {
  color: #c586c0
}

code>.str,
pre>.str {
  color: #ce9178
}

code .str>.esc,
pre .str>.esc {
  color: #ff6868
}

pre>.biv,
pre>.cls,
code>.biv,
code>.cls {
  color: #4ec9b0
}

code>.dir,
pre>.dir {
  color: #dcdcaa
}

code>.fun,
code>.lab,
pre>.fun,
pre>.lab {
  font-weight: normal;
  color: #dcdcaa
}

code>.num,
pre>.num {
  color: #b5cea8
}

code em,
code .cmt,
pre.origin em,
pre .cmt {
  color: #6a9955 !important
}
Light theme would be:

Code: Select all

code,
pre {
  font-family: Consolas !important;
  font-size: 14px !important;
  tab-size: 4;
  color: #000 !important;
  background-color: #eff0f1 !important;
}

code span>a,
code span>a:hover,
code span>a:link,
pre span>a,
pre span>a:hover,
pre span>a:link {
  color: inherit !important
}

code>.bif,
code>.cmd,
pre>.bif,
pre>.cmd {
  color: #0148c2
}

code>.met,
pre>.met {
  color: #097f9a
}

code>.cfs,
code>.dec,
pre>.cfs,
pre>.dec {
  color: #6F008A
}

code>.str,
pre>.str {
  color: #A31515
}

code .str>.esc,
pre .str>.esc {
  color: #FF0000
}

pre>.biv,
pre>.cls,
code>.biv,
code>.cls {
  color: #006400
}

code>.dir,
pre>.dir {
  color: green
}

code>.fun,
code>.lab,
pre>.fun,
pre>.lab {
  font-weight: bold;
  color: #290e90
}

code>.num,
pre>.num {
  color: #1a6c4e
}

code em,
code .cmt,
pre.origin em,
pre .cmt {
  color: #708090 !important
}

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 28 Nov 2022, 10:26

Do you think it's possible to port parts of it into prism.js?
https://github.com/PrismJS/prism/blob/master/components/prism-autohotkey.js
I've taken a quick peek at the code (for ref. https://github.com/Lexikos/AutoHotkey_L-Docs/blob/d06a1c4a20/docs/static/content.js#L1767)
It looks like just regex won't suffice...

Okay, when I'm free I'll try to implement something so that we can reference it directly like you've done so above. I'll likely add a v2 option or something like that for the bbcode.

For now, I've managed to place AutoHotkey first in the drop down. the simple [\code] tag defaults to ahk as well.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Syntax highlighting v2

Post by boiler » 31 Jan 2023, 05:37

@joedf - If it would not be possible for the version to be automatically selected based on the sub-forum being posted in as lexikos suggested (which would be best if possible), is the plan to have separate AHK code boxes available for each version in the dropdown list? The simple code tag would be specific to v2, but the list could be used by those wanting to select v1 specifically (with v2 also available there for completeness).

This would allow new posts to use either style, with the handy button applying to v2. We would just have to live with existing posts that use the simple code tags to be rendered with the v2 rules unless there was a way to do a bulk edit of posts by sub-forum to replace “code” tags with “codebox=autohotkeyv1” tags, as I suggested here.

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 31 Jan 2023, 11:23

I haven't gotten around to it, but the gist of it is to default to v2 for new posts, have some sort of auto-detect if v1 or v2 based on the forum sections, and finally have both v1 and v2 selectable from the dropdown.
I am not sure we have a bulk edit option here... Running SQL script could work, but I don't trust myself doing that and is perhaps more challenging or at least more time consuming. :think:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Syntax highlighting v2

Post by boiler » 31 Jan 2023, 12:19

OK. Thanks.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Syntax highlighting v2

Post by swagfag » 23 Feb 2023, 14:02

is this implemented yet(doesnt appear so)

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 23 Feb 2023, 15:26

@swagfag Nope, not yet! Still on my to-do list. Thanks for raising the concern though! :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 02 May 2023, 13:15

@Ragnar I was just testing your userscript now, but it did not fully work. I got this error in the web console:

Code: Select all

Uncaught TypeError: document.getElementById(...) is null
    openSite https://www.autohotkey.com/docs/static/content.js:1421
    <anonymous> https://www.autohotkey.com/docs/static/content.js:275
    i https://www.autohotkey.com/docs/static/content.js:2338
    fireWith https://www.autohotkey.com/docs/static/content.js:2338
    ready https://www.autohotkey.com/docs/static/content.js:2338
Is it possible to port it to prism.js ? It would be much easier for me, since we have the line numbers among other features integrated with it.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
Ragnar
Posts: 764
Joined: 30 Sep 2013, 15:25

Re: Syntax highlighting v2

Post by Ragnar » 02 May 2023, 14:11

I have updated the userscript above as some links have changed in the meantime. Maybe it works now. This error message doesn't help much. To try reproducing this, I'd still need to know what browser, adblocker and userscript addon you're using.
Is it possible to port it to prism.js ? It would be much easier for me, since we have the line numbers among other features integrated with it.
I have no knowledge about prism.js, so I don't know. My guess is that it probably won't work because the syntax highlighter is handmade.

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 02 May 2023, 15:25

Prism.js essentially does it in regex patterns.. but I am guessing it might not be sufficient or so straight-forward... :think:
https://github.com/PrismJS/prism/blob/master/components/prism-autohotkey.js

Funny enough, they are also transitioning to a V2 ... :mrgreen:

As for my browser: firefox v112.02, uBlock Origin, and greasemonkey.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
Ragnar
Posts: 764
Joined: 30 Sep 2013, 15:25

Re: Syntax highlighting v2

Post by Ragnar » 02 May 2023, 16:47

GreaseMonkey was the problem. It seems to be more restrictive than TamperMonkey in terms of security. I fixed the userscript above by adding unsafeWindow and removing the line "// @grant none". It should now work with both addons.

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 02 May 2023, 17:47

ahh okay thanks! I'll give it a try later :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
kczx3
Posts: 1677
Joined: 06 Oct 2015, 21:39

Re: Syntax highlighting v2

Post by kczx3 » 20 Sep 2023, 19:06

Curious if there’s been any progress on this front

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 23 Sep 2023, 21:58

None, to be honest. :P
The easiest would be to have the prism.js regex / parse code updated.
Or I just hunker down on this in the morning. right now, I am looking into why emails are not working for anything other than gmail... :think:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
Ragnar
Posts: 764
Joined: 30 Sep 2013, 15:25

Re: Syntax highlighting v2

Post by Ragnar » 26 Jul 2024, 05:34

I'll try to make this more implementable for the forum.

But one thing has to be left out (for now): Line numbering. This would require significant changes to the docs' highlighter, but I think it's bearable.

@joedf, are any forum addons used for the codeboxes? Is prism.js injected by an addon or is it "standalone"?

User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Syntax highlighting v2

Post by joedf » 26 Jul 2024, 10:57

Thanks for any effort you put into this! :D Things are a bit stressful and busy for me at the moment and I've left this drift into the ether... :oops:

Wherever I made a modification, I commented "joedf". So you can search for that for any relevant code. I tried to document the following as much as I can for now, but please dont hesitate to bug me for more info / details.
A lot of these were "emergency" patches when the codeboxes were completely broken making parts of the forum nearly unusable. Please excuse me for any bad practices I used here, but feel to change and scrutinize as use please. :mrgreen:

This contains code by tmplinshi and I to handle some features with the codeboxes, mainly: Download, Expand/Collapse, and Show/Hide line numbers
assets/javascript/core.js

This has a modification to load in the prism css and js files (each theme has this but I doubt any change is needed here):
styles/digi/template/forum_fn.js

This prism.js that essentially creates the codeboxes, but they do have some modifications to load in prism.urls.js and make the line-number show/hide-able:
assets/prism/prism.js

as commented in the prism.js file, this is the version / config used of prism:
PrismJS 1.20.0
https://prismjs.com/download.html#theme ... ne-numbers

The css styles for light/dark theme adjustments
assets/prism/light/prism.css
assets/prism/dark/prism.css

Not used anymore?
assets/prism/prism.css

This is code I wrote to add docs linking:
assets/prism/prism.urls.js

As for the forum plugin / extension, this is used but doesn't work correctly. All I did, as far as I can remember, was to just to get it to parse the bbcode into <pre> elements and add the language as a attibute/property.
Basically, all we use it for now is for the <pre> elements and the code language dropdown in the reply editor...

Code: Select all

Display Name:    Codebox Plus - modified joedf
Clean Name:    o0johntam0o/codeboxplus
Description:    Syntax highlighting 200+ programming languages (using GeSHi); Expand/collapse code block; Downloading code contents.
Version:    3.0.0
Homepage:    https://github.com/o0johntam0o/phpBB-Extension-Codebox-Plus
Licence:    GPL-2.0 
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
tank
Posts: 3164
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Syntax highlighting v2

Post by tank » 29 Jul 2024, 21:38

Yikes somehow I never saw this thread. I have plans next 2 weekends but I will dip my toes in on this as well
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter

Post Reply

Return to “Forum Issues”