Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Automatic coloring of comments in the code


  • Please log in to reply
59 replies to this topic
PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
[quote name="Chris"]The help file currently uses
 sections for all code examples. Assuming 
 doesn't allow any kind of coloring or CSS inside it[/quote]No, you can put spans and such in pre sections. But we don't need no pre section as long as the styles use a fixed width font.
Example of HTML export in SciTE:
[color=green]        a := F(5, 6) ; Comment
[/color]
Full syntax highlighting can be very verbose... Thus can generate big pages.

[quote name="Chris"]I believe phpBB already has a syntax highlighting mod/framework to which the syntax rules for a language can be applied.[/quote]Yes, the mod I wrote about in the message I gave in reference above is at [url=http://www.phpbb.com/phpBB/viewtopic.php?t=119512]http://www.phpbb.com/phpBB/viewtopic.php?t=119512[/url]
I looked at it because I don't know how phpBB MODs work. Lot of stuff around the real code of the MOD, which itself rely entirely on PHP's highlight_string() function... (only works for PHP code...).
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks both of you for the info.

I'll take a look at the following to see if it can be improved to recognize the string:
b := "Foo ; This becomes a bit tricky"

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
If you set the parser to convert comments before strings and regexp to check for such conversions you can prevent a css override.

Syntax-highlighting manually or with PHP will increase code size so I would use Javascript, e.g.
function syntax() {
	var container = "pre";
	var class = "code";
	
	var style = new Array(
"" // direc
, "font-weight: bold; color: #003399;" // command
, "" // vars
, "" // keys
, "" // res
, "font-weight: bold; color: #FF0000;" // label
, "color: #FF0000;" // string
, "font-style: italic; color: #008000;" // comm
);
	
	var command = new Array(
"AutoTrim"
, "BlockInput"
, "Break"
, "Click"
, "ClipWait"
, "Continue"
, "Control"
, "ControlClick"
, "ControlFocus"
, "ControlGet"
, "ControlGetFocus"
, "ControlGetPos"
, "ControlGetText"
, "ControlMove"
, "ControlSend"
, "ControlSendRaw"
, "ControlSetText"
, "CoordMode"
, "Critical"
, "DetectHiddenText"
, "DetectHiddenWindows"
, "Drive"
, "DriveGet"
);
	
	command = command.sort(bylen);
	var e = document.getElementsByTagName("body")[0].getElementsByTagName(container);
	for (var i = 0; i < e.length; i++)
		if(e[i].className == class) {
			var html = e[i].innerHTML;

			for (var ix = 0; ix < command.length; ix++) {
				var regex = new RegExp("\n(" + command[ix] + ")", "gi");
				html = html.replace(regex, "<span style=\"" + style[1] + "\">\n" + command[ix]  + "</span>");
			}

			[color=blue]// other replacements[/color]

			e[i].innerHTML = html;
		}
}

function bylen(a, b) {
	return b.length - a.length;
}

window.onload = syntax;
With the list of keywords, vars and etc the js may come upto 25kb in size but since it's all text you'll be easily able to gzip it down to about 5kb.

The javascript above is just an example that only works in Firefox and automatically changes case. I can continue to work on it if you would like me to?

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Yes I would like you to.

Really
Posted Image

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
If the JavaScript in question must be generated by the server -- and it doesn't work on MSIE -- I think I'd rather not have it. You might say, "you can put it in a separate .js file for better caching and reduced bandwidth." If so, I'm concerned that such a technique is not compatible with older browsers (not to mention the incompatibility with MSIE). As far as I know, phpBB is meticulously compatible with older browsers -- and if true, I'd like to avoid anything that reduces compatibility.

In addition, I'd greatly prefer a server-generated solution to a JavaScript one, though I realize this may mean you would not wish to work on it. If so, perhaps your JavaScript can be configured locally in a user's own Firefox rather than making any changes to phpBB?

Thanks.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

In addition, I'd greatly prefer a server-generated solution to a JavaScript one, though I realize this may mean you would not wish to work on it.

I find PHP easier than javascript so I wouldn't mind doing either. The reason I would prefer to use javascript in this case is because:[*:1pu1l0wy]Only needs to be added to your template
[*:1pu1l0wy]You can apply this to the docs by using the above technique
[*:1pu1l0wy]Using PHP to parse each page on the fly can affect server performance
[*:1pu1l0wy]The page size won't increase
[*:1pu1l0wy]Javascript can be deferred to prevent page render latency
[*:1pu1l0wy]Users who don't like colouring can simply turn off javascript in their browserHowever there are a couple of downsides:[*:1pu1l0wy]I'm not sure how good pcre is supported in different browsers, I heard Opera has some serious flaws
[*:1pu1l0wy]If the page is very large the browser may become unresponsive
The code I gave above was just an example. Adapting it to MSIE's DOM (for IE 3+) should only be a case of switching innerHTML to outerHTML among a few other minor things.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Because of the points you made, I'm much more receptive to the JavaScript approach now.

Only needs to be added to your template

That's a good point. Also, if a browser happens not to want to load the .js file for any reason, the text simply won't be colorized.

Adapting it to MSIE's DOM (for IE 3+) should only be a case of switching innerHTML to outerHTML among a few other minor things.
You can apply this to the docs by using the above technique

That's good to hear (I'd thought you meant it'd only ever work in Firefox). It'd also be great if the same method can be applied to the docs and yet still work in the .CHM help file.

The page size won't increase

That's good, and hopefully most browsers are smart enough to cache the .js file (as long as Apache yields a last-modified date for it).

Javascript can be deferred to prevent page render latency

Great feature.

I'm not sure how good pcre is supported in different browsers, I heard Opera has some serious flaws

That would be a drawback; but if it goes beserk, you can instead have the script detect Opera and abort.

If the page is very large the browser may become unresponsive

The danger is reduced because because phpBB limits how much text you can have in a post.

Finally, I think it's important to note that primary focus here is to draw the comments in a different color than the code itself. Colorizing the commands doesn't seem as important to me (indeed, it might be annoying to some people in spite of the fact that they can turn of JavaScript in their browsers). So it might be better if you focus on comments first, namely:
1) Lines that begin with ;
2) Lines that contain a ; with at least one space or tab to its left.
3) /* ... */

Feel free to do the commands too, though if it's too controversial we should have a poll to decide on it.

Thanks.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

Users who don't like colouring can simply turn off javascript in their browser

Not very serviceable, and it would turn off BBCode buttons and such.
Another objection is that there are people that turn of JS for security reasons, but it is not a big problem, they just don't have the convenience.

Note that for Firefox, you can rewrite your code to do a GreaseMonkey script, so no need to way for Chris to implement it...

Note also that I prefer the approach suggested in my (confusing) HTML snippet: define some CSS classes, with short names, and use them in spans, instead of defining a full style on each keyword. Of course, it is just a trivial point of detail.

A point in favor of JS is that you can add a combobox to choose the preferred color scheme. But this suppose there is some cookie to memorize the choice. And you can do that in PHP too.

I didn't knew the defer attribute, it isn't much used (at least in page sources I saw), but it is useful.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Cmon... you are arguing about things that can be solved in a minute:

Profile Options:
color comments (on/off)
comment color : hex

BTW: defer is really great.. i didn't know about it either.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Well, I don't know phpBB, so I don't know if it is so easy to add Profile options. If it is, great.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Finally, I think it's important to note that primary focus here is to draw the comments in a different color than the code itself. Colorizing the commands doesn't seem as important to me (indeed, it might be annoying to some people in spite of the fact that they can turn of JavaScript in their browsers). So it might be better if you focus on comments first, namely:
1) Lines that begin with ;
2) Lines that contain a ; with at least one space or tab to its left.
3) /* ... */

Here is a test version that includes an example page: ahk_syntax.zip
This supports #CommentFlag too.

Due to my regex the only problem is that multiline comments that are over 2 lines long aren't recognized. JSLover/PhiLho could you take a look into it please :)

I don't think a 2kb javascript will do much harm but if you want I could gzip it inside a PHP wrapper with special headers to suggest a permanent cache?

Not very serviceable, and it would turn off BBCode buttons and such.

If PHP is used instead it will be difficult to apply for the docs. The nice thing about javascript is that it can be used anywhere without the need for specific configuration/requirements.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Started to take a look.
First remark: drop the #CommentFlag... Good idea, but I doubt anybody would use it, so it will save some CPU cycles... Otherwise, improve the search: /^\s*#CommentFlag/i

Chris, I played with this directive. Perhaps you should add to the help that it must be used only once, and before any comment...

More analysis later.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

First remark: drop the #CommentFlag... Good idea, but I doubt anybody would use it, so it will save some CPU cycles... Otherwise, improve the search: /^\s*#CommentFlag/i

One extra regex doesn't make a difference. There may be other directives/code above #CommentFlag so \n should be used instead of ^.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Yes, I come back to my computer thinking about that... I still think line per line... And forgot the mandatory space: /\n\s*#CommentFlag\s/i
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

/\n\s*#CommentFlag\s/i

#CommentFlag //
MsgBox, a ; b // c
MsgBox, 1 ; 2 // 3
Hmm...