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
I just reworked slightly my script to transform it to a GreaseMonkey one, so it works in Firefox in the current forum! Yeah! What a proof of concept.
// ==UserScript==
//
// @name          AutoHotkey Scripts Comment Coloring
// @author        Philippe Lhoste aka. PhiLho, based on Titan's code
// @namespace     http://Phi.Lho.free.fr/
// @description   Colorize inside PRE (CODE class) the comments of AHK scripts.
// @version       1.00.000
// @include       http://www.autohotkey.com/forum/viewtopic.php?*
//
// ==/UserScript==
/* Copyright notice: For details, see the following file:
http://Phi.Lho.free.fr/softwares/PhiLhoSoft/PhiLhoSoftLicence.txt
This program is distributed under the zlib/libpng license.
Copyright (c) 2006 Philippe Lhoste / PhiLhoSoft
*/

var container = "td", containerClass = "code"; // container and class name

var b = document.getElementsByTagName("head");
b[0].innerHTML +=
'\n<style type="text/css">\n.MLC { background-color: #EEE; color: #888; }\n.SLC { color: #AAA; }\n</style>\n';

var e = document.getElementsByTagName(container);
for (var i = 0; i < e.length; i++)	// Loop on all PRE blocks
	if (e[i].className == containerClass)  // That's the right class (we never know...)
	{
		var html = e[i].innerHTML;
		var prevLen = html.len;
		var commentDelimiter = ";";
		var newCommentDelimiter = /^(?:\s| )*#CommentFlag(?:\s| )+(\S+)\s/mi.exec(html);
		if (newCommentDelimiter != null)
			commentDelimiter = newCommentDelimiter[1];

		var regex = new RegExp("(\\s| |^)(" + commentDelimiter + ".*)$", "mg");
		html = html.replace(regex, '$1<span class="SLC">$2</span>');
		html = html.replace(
				/^((?:\s| )*\/\*(.|\r|\n)*?^(?:\s| )*\*\/)/mg,
				'<span class="MLC">$1</span>');
		e[i].innerHTML = html;
	}
I have this code in AutoHotkey_scripts_comment_coloring.user.js
To use it, just drag'n'drop this file in Mozilla (with GM installed, of course). GM identify it as script for it, and will ask if you want to install it. Accept, of course.

[EDIT] Better handling of  
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

Otherwise, I find more elegant to get the comment delimiter by capture rather than by complex string manipulations.

Bad coding styles do exist :?

Thank you. :cry:

I was referring to code that others might post on the forum. I decided that parsing with /(\n\s*#CommentFlag)/i.exec(html)[0] would be foolproof enough.

I haven't seen yet anything on the cost of exec usage. Can you point me on some reference on the topic?

exec like StringSplit creates an array of all the matches which consumes more memory than needed and slows down the script.

In my code, I have only one capture and no g, so it has only one match. No problem here.

I've used this kind of exec for #CommentFlag if that's what you're talking about.

I stick to my idea of spans with classes...

A whole new CSS stylesheet doesn't seem worth it for just two styles.

Why whole new? Just add them to the existing stylesheet. Of course, that will be Chris' final choice, when/if he adopts your solution.

Validators or old browsers could drop those styles for being redundant since they're only referenced dynamically. Chris asked for a cross-browser compatible solution so I went for the explicit style option.

And I reworked the code to use my coding conventions, so I find my way there.

I've tried to keep my coding style standard so others can understand it.

Standard? There is no standard, just preferences and good taste.
Nothing wrong with your code style! I wasn't criticizing it.

Never said you were, just though I'd mention it ;)
By standard I mean K&R OTB (with a few exceptions).

(not pcre, that's a library probably not used in JS implementations...)

Doesn't javascript use pcre for regex like PHP?

For Mozilla, I don't know, but JS doesn't use (full) PCRE syntax anyway, so I guess it has an engine of its own. And for MS, I am almost sure it doesn't. It even differs on $ handling in multiline REs: in Mozilla, which probably follows ECMAScript rules, it "matches immediately before a line break character." For IE, it matches after...

I know MS' JScript doesn't use pcre but I thought other browsers would do. It doesn't matter anyway because I avoided advanced quantifiers to reduce the chance of conflicts with different engines.

I just reworked slightly my script to transform it to a GreaseMonkey one, so it works in Firefox in the current forum! Yeah! What a proof of concept.

What about the CHM docs and other browsers? :p
That was the reason why I created the javascript.

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

I just reworked slightly my script to transform it to a GreaseMonkey one, so it works in Firefox in the current forum! Yeah! What a proof of concept.

What about the CHM docs and other browsers? :p
That was the reason why I created the javascript.

Yes. My GM script doesn't intend to make the JS obsolete. It rather proves its utility and faisability.
I made the GM script:
1) Because it was fun and educational (it is only the second GM script I write, the first one skips the phpBB page stating I will be redirected to my post after submitting it);
2) Because I use Firefox ;-);
3) To test the script on real pages;
4) Etc.
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

3) To test the script on real pages;

You can test the javascript on real pages using the Firefox Web Developer extension.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Ah? I have this extension, but I can't find how to run arbitrary JS on a page. How do you do it?

Beside, I have automatically colored comments now. :-P
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

Ah? I have this extension, but I can't find how to run arbitrary JS on a page. How do you do it?

Misc. > Edit HTML

Chris, if you decide to use the javascript I wrote you can compress it with this wrapper:
<?php

$file = fopen("syn_comments.js", "r");
$data = fread($file, filesize($file));
fclose($file);
ob_start("ob_gzhandler");
echo $data;

?>
If you'd like to enable HTTP compression for a whole directory or the entire site add this to the htaccess or change the respective values in php.ini:
<ifModule mod_[color=blue]php4[/color].c>
 php_value zlib.output_compression 1
 php_value zlib.output_compression_level [color=red]9[/color]
</ifModule>


majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006

In the new ahk_syntax.zip 1.0b:

It works well in the Opera now, everything looks great, no problems.


Opera users can set it up as easy as this:

RMB->Edit site properties->Scripting->Use user script files
U must also add .user.js instead .js (it didn't work without this)
Posted Image

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

It works well in the Opera now, everything looks great, no problems.

Out of curiosity, can you test my own script? Thank you.
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

It works well in the Opera now, everything looks great, no problems.

Out of curiosity, can you test my own script? Thank you.

Can you use GM in Opera?

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Not the GM script, the hopefully portable variation of your own script:
http://www.autohotke... ... 8309#78309
If Opera's JS chokes on "advanced" REs, it might fail.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
2 Phil, 2 Titan
I never used GM but from the site I can conclude that it is something Opera have integrated at least 3 years. I understood it is ability to run user scripts on any page.

With newever versions of Opera it is even simpler

Philho your version of the script works fine too, on Titans sample.
I just had to add function wrapper to test it.

For some reason, Opera doesn't execute this user script for me.... (I use 9beta2). I will down the latest final build and try again. Thats why I didn't test it on AHK forum, but I guess it works OK, since test html page worked fine.
Posted Image

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

For some reason, Opera doesn't execute this user script for me.... (I use 9beta2)

Who's script is that? My javascript should fully on Opera 8+ on all pages.

Edit: nvm, I get it.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006

See also the Greasemonkey Script Repository, since Opera is capable of running many of those too.

http://www.howtocrea... ... cript.html


To enable user JavaScript, use Tools > Preferences > Advanced > Content > JavaScript options, and select the directory where you will put your User JavaScript files. Opera will load all files in the specified directory whose names end with .js and will use them as User JavaScript files. If a file name ends with .user.js it will be assumed to use Greasemonkey notation.

http://www.opera.com...torials/userjs/

This means that Opera CAN run GM scripts just with .user.js added.
For non GM scripts .user MUST NOT be added. That was my mistake before....


Interestingly enough PhiHos notation does work in Opera and your doesn't. Both script do work when executed ordinary. I guess it is some GS incompatibility....

ANd yeah.. Philho.. what ugly color is that :)))
Please put something normal there :)
It is almost folded with white background. :)
Posted Image

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

Interestingly enough PhiHos notation does work in Opera and your doesn't. Both script do work when executed ordinary.

Are you talking about his GM script because my javascript is just standard and cross-browser compatable (as Chris requested). The script I posted is generic for the purpose of example, i.e. you have to change the cont and cls variables to suit this forum, td and code respectively I believe.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Thank you for testing.
The grey I used is a bit clear, perhaps. Well, changing the colors is trivial.
And Titan is right, of course, the regular .js is made for pre sections, while the GM one, which have to deal with current forum choice, is made for td sections.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")