Extension talk:Syntax Highlighting

Instructions need to explain LOCAL_SERVER_ROOT
In the case of IIS, what does LOCAL_SERVER_ROOT point to? Nothing obvious appears to work - have tried a lot of variations on "c:/inetpub/wwwroot"...

When I started playing with this extension I was rather confused.

First off, there is nothing in the extension syntax to specify the language the content is in - and beautifier supports a lot of different languages. I though perhaps the beautifier detected the format (a very clever trick if you can do it), but although it does include some magic for detecting formats this seems to only be directed at files and isn't actually called by beautifier (Just there for convenience?). Looking closer at the extension I realised it is hardcoded to load the php3 highlighter - it explicitly uses the file HFile/HFile_php3.php and the class therein.

Not being a huge php guy I went off and, through some trial and error, poking around in other extensions and the code for TWiki Beautifier Plugin, I managed to come up with the following code to add a language parameter to the  element.

/**        * Require the main file for the beautifier. */       require_once ($BEAUT_PATH."Beautifier/Core.php"); /**        * Set up the highlight object based on whether * you want the output as CSS freindly or straight HTML. */       /**         * If a specific language is requested load the relevant file and create the highlighter */       $myhfile = null; if($argv["language"]){ require_once ($BEAUT_PATH."HFile/HFile_".$argv["language"].".php"); if (FALSE === eval("\$myhfile = new HFile_".$argv["language"].";") ) { $output .= "Error:1 Could not instantiate HFile_".$argv["language"]." "; }       }        /**         * Otherwise use php as a default */       else {               require_once ($BEAUT_PATH."HFile/HFile_php3.php"); $myhfile = new HFile_php3; }       if($OUTPUT_HIGHLIGHT_AS=="CSS"){ require_once ($BEAUT_PATH."Output/Output_CSS.php"); $outputter = new Output_css($myhfile); }       else { require_once ($BEAUT_PATH."Output/Output_HTML.php"); $outputter = new Output_Html($myhfile); }       $highlighter = new Core($myhfile, $outputter);

This allows you to specify the HFile for beautifier to use. NOTE: This code sample includes some other edits based on changes to beautifier itself below. It will not work with the standard beautifier code currently available from | the beautifer site

With this change I could see the different highlighter files being accessed, but alot of the highlighting didn't seem to work - At the time I was working on some SqlServer SQL and I wanted colours similar to the MS SqlManager. That's when I realised there appears to be a major flaw in the beautifier itself. The highlighter files specify alot of information about what colours to dispaly, but the outputter actually hardcodes the output completely ignoring what is in then highlighter. So I rewrote the HTML highlighter to output based on Highlighter. To do this I've passed the highlighter into the outputter. The html outputter I am using is as follows:

class Output_HTML {       	function Output_HTML($file=undef) {       		if (!isset($file)) $file = new HFile; $this->code		= '_WORD_'; $this->linecomment 	= 'linecommentcolour .'" style="font-style=italic;">_WORD_ '; $this->blockcomment 	= 'blockcommentcolour .'" style="font-style=italic;">_WORD_ '; $this->prepro 		= ' _WORD_ '; $this->select 		= ' _WORD_ '; $this->quote 		= 'quotecolour .'">_WORD_ '; for($i=0; $icolours); $i++) {       		   $varname = "category_".($i+1); $this->{$varname} = 'colours[$i] .'">_WORD_ '; }       	}        }

Using this I can now highlight code snippets and wotnot in any language that the beautifier has a highlighter defined for.

There are a few bits I need to tidy up on this, but once I've done that if there are no objections I will update the main body of the extension to support the language parameter. I'm also going to contact the guy behind beautifier to see if I'm missing the point or my changes to beautifier are sensible.

JonBoy 16:28, 16 June 2006 (UTC)

Looks like there is something seriously broken in this wiki. At least my geckos-based browser shows completely different (and completely broken) code on this discussion page. only by editing and copying the (wiki-) source from the textarea did I manage to get the code as was intended. Sp 21:01, 26 June 2006 (UTC)

There is a tiny bug when trying to install the basic extension from the article page and setting output format to CSS you first get an error that Output_CSS.php cannot be found even if you set the path right. Then if you look closer you see that its because the file from the package is actually called Output_css.php and mv Output_css.php Output_CSS.php fixes it. Maybe rename the packaged file or change the include line. Mutante 22:03, 22 June 2006 (UTC)

Sp, this may have been my fault for not putting pre tags around the second code block which contained html markup - I've added them in now.

Mutante, I've integrated my code changes that add the language attribute and made the case change to the call for Output_css. I think it's right that the extension is changed as it is consuming the files provided by an external package. I hadn't noticed the problem since I'm running on Windows/IIS so casing issues don't arise.

JonBoy

I'm using this extension and is great. But... I'm having troubles using many code blocks in the same article. Server hangs up and connection is refused. Also, sometimes when i save a page (and it includes several syntaxis) the wiki tells me that some other user is trying to save it too (an edit conflict). But I am the only one connected to it. Any ideas? Help, please :( - Alejandro 15:33, 02 August 2006 (UTC)

I'm having some problems getting this extension to work. I've installed the extension in extensions/beatufier/, made sure all the permsissions look okay. I added the syntaxHighlightExtension.php file and set the variables at the top of the file to point to the correct locations. And nothing happens. At all. My highlightSytnax tags aren't getting parsed as tags at all, they just show up as plain text in the wiki document. I've played around with the path vars trying with/without trailing slashes, with/without /Beatufier/ to no avail. Any suggestions? I did a copy/paste of the SQL hightlighting example, and it doesn't do anything at all. -Matt

/etc/passwd
This code are not dangerous?




 * I don't see anything that would prevent something like that from working but it would need more like "/../../../ect/passwd". The langage loading part also seems dangerous
 * 70.20.157.163 12:24, 10 January 2007 (UTC)

Bugs
This could break the code, including the page, a check is required to see if the file exists first. 

fix: replace if($argv["language"]) require_once ($BEAUT_PATH."HFile/HFile_".$argv["language"].".php");

with if($argv["language"] && file_exists($BEAUT_PATH."HFile/HFile_".$argv["language"].".php") ){ require_once ($BEAUT_PATH."HFile/HFile_".$argv["language"].".php");

Suggestions

 * Generate

to highlight PHP code - what extension is it? It's not by default in MediaWiki.