Page 1 of 2

Better Code Highlighting

Posted: Tue Aug 13, 2013 12:58 am
by phillid
Currently, I've noticed that the only highlighting of code on the forum is that it's put in green text. Could we use some means of syntax-highlighting code inside [code] tags? I understand that it might take a bit of work thanks to different languages - and especially assembly with all its syntax-variants and processor types...

Just a thought.
Cheers

Re: Better Code Highlighting

Posted: Tue Aug 13, 2013 1:21 am
by Combuster
You'll be getting quite a source of nuisance if you are ever going to try to implement this properly:
Forum Rules wrote:6: Please try to write legibly
(...) Use tags when posting code. Use indentation and source comments. Use a spell checker. Don't use colours - not everybody uses the same theme and colouring your text might render it invisible. (...)
(Also, green text? Where? :wink:)

Re: Better Code Highlighting

Posted: Tue Aug 13, 2013 1:32 am
by phillid
Okay, I see a point there, but that aside, surely highlighted code would be better? Couldn't we put a neutral colour choice like white or black for code block backgrounds and do the highlighting from there?

Re: Better Code Highlighting

Posted: Tue Aug 13, 2013 1:43 am
by Kevin
In theory the highlighting colours could be specific to each theme. Not sure what extensions exist and how hard it would be to set up, though.

Re: Better Code Highlighting

Posted: Tue Aug 13, 2013 1:48 am
by phillid
I'm not aware of any PHPBB-specific plugins, but a quick Google Search would deal with my ignorance. I've used one or two non-PHPBB-plugins before, and I hear Prettify is supposed to be quite good.

Re: Better Code Highlighting

Posted: Tue Aug 13, 2013 3:26 am
by bluemoon
You should narrow down the scope and avoid posting long code in the first place.

Code: Select all

If (SLOC() < 10) {
  YouDontNeedColor = true;
} else {
  TL();DR();
}

Re: Better Code Highlighting

Posted: Tue Aug 13, 2013 11:53 am
by DavidCooper
Combuster wrote:(Also, green text? Where? :wink:)

Code: Select all

If (SLOC() < 10) {
  YouDontNeedColor = true;
} else {
  TL();DR();
}
I hadn't noticed this before. The above code doesn't look green, until you scroll it, and then it turns green while it's moving.

Re: Better Code Highlighting

Posted: Tue Aug 13, 2013 11:25 pm
by phillid
bluemoon wrote:You should narrow down the scope and avoid posting long code in the first place.
Correct, but sometimes long-ish code may need to be posted. Plus highlighting would make languages like assembly easier to read for example.

Re: Better Code Highlighting

Posted: Wed Aug 14, 2013 8:47 pm
by pcmattman
phillid wrote:Currently, I've noticed that the only highlighting of code on the forum is that it's put in green text.
Alas, code in my code tags is white. That is to say, any syntax highlighting solution must integrate nicely with the multiple themes this board supports. But Combuster basically said that already.

I also use the theme I do specifically because it's dark, it would be awfully disconcerting to have every code block suddenly became bright white in background (assuming a 'global' scheme was decided upon, irrespective of theme).

Re: Better Code Highlighting

Posted: Thu Aug 15, 2013 9:27 am
by iansjack
I'm not convinced that syntax highlighting would be useful. It serves a purpose in a code editor, helping to make errors stand out, but that shouldn't be necessary here, assuming the OP has already checked for syntax errors.

Most books don't employ syntax highlighting, and I don't miss it.

Re: Better Code Highlighting

Posted: Fri Aug 16, 2013 4:56 am
by phillid
pcmattman wrote:it would be awfully disconcerting to have every code block suddenly became bright white in background
Fair enough. To cater for those who use dark themes, the background colour of a code segment could be dark for a dark theme and light for a light theme. Admittedly, that would cause problems with, say, black parts of code on that dark background of the code segment, but it'd be easy for such text's colour to be changed depending on the theme.
iansjack wrote:It serves a purpose in a code editor, helping to make errors stand out...
Correct, but how does it make errors more apparent? By making it more readable by, for example, making (valid) types of variables stand out better, mathematical operations a different colour from values around the operators, and so on. I believe that it would make the 'consumption' of code much more efficient.

Re: Better Code Highlighting

Posted: Fri Aug 16, 2013 5:38 am
by xenos
I also think that highlighting improves the readability of code, both in an editor and here in the forum or the wiki (where we already have some nice highlighting, and if it works there, why not in the forum as well?). When I read some code in the wiki or elsewhere on the web I'm usually happy when I see immediately what's going on, and the structure becomes clear. Highlighting helps in this scenario.

I'm not 100% familiar with how PHPBB styles work, or how those syntax highlighting plugins work. But if one had a plugin that simply puts some <span class="keyword">...</span> around keywords (and similar for variables, functions, types, asm stuff, strings etc.), and if each style simply comes with a CSS file with all the color definitions, it shouldn't be too hard to come up with, say, one highlighting color scheme for dark styles and one for bright styles, that one could copy into the CSS style definitions.

Re: Better Code Highlighting

Posted: Fri Aug 16, 2013 8:40 am
by HugeCode
Well, it's true that users use different styles, but I think that another special bbcode for code part (as a keyword alone in text or some constant) would be good. It wouldn't be full code highlighting, but it would serve for explaining code. Currently, when somebody adds code to his post and he has some mistake in name of variable or keyword, other refer it using italic text or ``, ' '. On CodeProject, there's special var button for it. I think in administrator control panel of phpBB there are settings for BBcodes.

Re: Better Code Highlighting

Posted: Sat Aug 17, 2013 5:45 pm
by phillid
iansjack wrote:Most books don't employ syntax highlighting
That'd be due to the fact that a lot of them are printed primarily in black and white, I guess. Just because they don't use it doesn't mean that it's the best option, especially on a different medium. On the web, it's easier and not at all more expensive (Unless you're going to start talking about a few extra KB of internet usage :roll: ) to publish in colour, so why not do it?

Oh well, you're entitled to your opinion :D

Re: Better Code Highlighting

Posted: Sun Aug 18, 2013 12:08 am
by iansjack
phillid wrote:Oh well, you're entitled to your opinion :D
That's very generous of you.

If such a facility were ever to be implemented then I would hope it would be a user-configurable option. I would find it distracting and I can see it causing all sorts of problem with some browser/display setups. I think here of those nasty versions of xterm that do colour coding and can end up with near-invisible text.