Extension talk:Syntax Highlighting

From mediawiki.org

Instructions need to explain LOCAL_SERVER_ROOT[edit]

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"...


The following discussion has been transferred from Meta-Wiki.
Any user names refer to users of that site, who are not necessarily users of MediaWiki.org (even if they share the same username).

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 <highlightSyntax> 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"]."()<br />";
                }
        }
        
        /**
         * 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 	= '<font color="'. $file->linecommentcolour .'" style="font-style=italic;">_WORD_</font>';
        		$this->blockcomment 	= '<font color="'. $file->blockcommentcolour .'" style="font-style=italic;">_WORD_</font>';
        		$this->prepro 		= '<font color="purple">_WORD_</font>';
        		$this->select 		= '<font color="red"><b>_WORD_</b></font>';
        		$this->quote 		= '<font color="'. $file->quotecolour .'">_WORD_</font>';
        
        		for($i=0; $i<sizeof($file->colours); $i++)
        		{
        		   $varname = "category_".($i+1);
        		   $this->{$varname} = '<font color="'. $file->colours[$i] .'">_WORD_</font>';
        		}
        	}
        }

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)Reply


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)Reply


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)Reply


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[edit]

This code are not dangerous?

<highlightSyntax serverFile="/etc/passwd"></highlightSyntax>
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)Reply

Bugs[edit]

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

<highlightSyntax language="invalid_language"></highlightSyntax>

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[edit]

  • Generate <code> instead of <pre>
  • Use syntax like <code language="php"> instead of <syntaxHighlight language="php">

Logixoul 22:25, 13 September 2006 (UTC)Reply

Line Numbers would be great, especially if you can start at a specific number, for display code snippets. --62.242.235.2 10:48, 17 October 2006 (UTC)Reply

http://www.beautifier.org defunct[edit]

The site seems to have turned into an adfarm, and http://www.beautifier.org/php/beautifier-php-full-current.tar.gz is no longer available. Does anyone have an alternative source?

81.133.76.232 14:16, 21 September 2007 (UTC)Reply

I found the file on Sourceforge but although it seems to be the referred file, I don't get the extension to run using MediaWiki 1.9.2. --141.41.137.2 16:21, 22 October 2007 (UTC)Reply

What extension does this page use to highlight code?[edit]

What extension does this page use to highlight code?

I can see it uses:

<syntaxhighlight lang="php">
  some
   PHP
    code
 </syntaxhighlight>

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

see Extension:SyntaxHighlight_GeSHi --Erikvw 12:20, 22 November 2008 (UTC)Reply

PHP Notice: Undefined property: Context::$inselection in[edit]

I added this line to the Beautifier/Context.php

$this->inselection	= array();

to get rid of this error in the apache log. Perhaps not the correct place to add it but the error is gone. --Erikvw 08:30, 3 July 2008 (UTC)Reply