PHPBB3-13919 - Syntax highlighting in [code]

Discuss requests for comments/changes posted in the Issue Tracker for the development of phpBB. Upcoming releases are 3.2/Rhea and 3.3.
User avatar
JoshyPHP
Registered User
Posts: 350
Joined: Fri Jul 08, 2011 9:43 pm

PHPBB3-13919 - Syntax highlighting in [code]

Post by JoshyPHP » Fri Jun 05, 2015 2:20 am

3.1 provides syntax highlighting for PHP (and only PHP) via highlight_string(), using the BBCode [code=php].

3.2 has no syntax highlighting in code blocks for any language. My recommendation is to use a JavaScript highlighter such as highlight.js or SyntaxHighlighter. My preference goes to highlight.js because it automatically detects the language of the source and it also requires one script tag to be installed. Both are available on various CDNs.

I don't know whether 3.2 should provide syntax highlighting at all. As far as I'm concerned, I wrote an extension for it to see how hard it would be to implement. All it takes is a script block in overall_footer to dynamically load highligher.js whererever needed. It costs 490 bytes per page. Unminified code here.
Last edited by JoshyPHP on Sat Jun 06, 2015 3:06 am, edited 2 times in total.

User avatar
VSE
Extension Customisations
Extension Customisations
Posts: 670
Joined: Mon Mar 08, 2010 9:18 am

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by VSE » Fri Jun 05, 2015 4:40 am

No more php highlighting :(

Code: Select all

if (!$auth->acl_get('a_'))
{
	trigger_error('NO_ADMIN');
}
Also tabs look too long IMO (should be 4 chars, looks like 8)
Has an irascible disposition.

User avatar
JoshyPHP
Registered User
Posts: 350
Joined: Fri Jul 08, 2011 9:43 pm

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by JoshyPHP » Fri Jun 05, 2015 5:06 am

The default CSS needs some touchups. On non-IE you can change the size of tabs to 4.


aleha
Registered User
Posts: 143
Joined: Tue Mar 26, 2013 2:19 am

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by aleha » Sun Jun 07, 2015 3:06 am

I have struggled a bit myself with code highlight. Since 3.0.12, I have written an (ugly) custom solution that uses the idea here to call pygments and produce html output for code bbcode, using language detection.
This was very easy to incorporate also in 3.1.x, because the only thing you do is change a line in includes/bbcode.php to redirect the control to an external php script.

Unfortunately, GeSHi is the only php syntax highlight library that I know of and looks sort of abandoned or stalled.

Back in the time of 3.0.12 highlightjs didn't support as much languages as now and language detection didn't work that well compared to pygments. Plus, one can cache the pygments html output.
I am not sure if and how caching be done with js. Maybe by calling NodeJS locally on server? While this may work in the same manner as executing a python script or command, updating JS files within an extension is a few clicks away instead of asking the admin to upgrade your pygments installation.

Since autodetecting a language doesn't always play well, ideally in phpBB a system like github's could be used.
That is, a filtered dropdown with supported languages for optional code highlight. Probably an extension is more fitting for this.

User avatar
JoshyPHP
Registered User
Posts: 350
Joined: Fri Jul 08, 2011 9:43 pm

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by JoshyPHP » Sun Jun 07, 2015 4:32 am

aleha wrote:I am not sure if and how caching be done with js.
That wouldn't be necessary. A big code block only takes something like 150 ms to be highlighted, and most of that is spent initializing the library.
aleha wrote:Since autodetecting a language doesn't always play well, ideally in phpBB a system like github's could be used.
Do you mean replacing the Code button with a dropdown?

aleha
Registered User
Posts: 143
Joined: Tue Mar 26, 2013 2:19 am

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by aleha » Sun Jun 07, 2015 5:33 am

Yes, something like that. Maybe replace it with a custom split dropdown that has a filter.

User avatar
VSE
Extension Customisations
Extension Customisations
Posts: 670
Joined: Mon Mar 08, 2010 9:18 am

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by VSE » Mon Jun 08, 2015 2:31 pm

Code: Select all

[code=php]
[code=html]
[code=js]
Normal bbcode usage such as those are sufficient.
Has an irascible disposition.

User avatar
Oyabun1
Former Team Member
Posts: 20
Joined: Thu Mar 31, 2011 9:48 am

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by Oyabun1 » Sun Nov 01, 2015 10:10 am

Syntax highlighting is an often requested feature.

Maybe highlight.js could be set up to use automatic highlighting unless a class is specified in the BBCode, such as, [code=html]. That would allow correcting it if the wrong class was automatically selected.

User avatar
CarpCharacin
Registered User
Posts: 55
Joined: Fri Mar 18, 2016 4:49 am

Re: PHPBB3-13919 - Syntax highlighting in [code]

Post by CarpCharacin » Wed Sep 14, 2016 5:52 pm

Why not use GeSHi? http://qbnz.com/highlighter/

Post Reply