Difference between revisions of "GeSHi"

From Organic Design wiki
m
(better way)
Line 1: Line 1:
 
[[Category:MediaWiki]]
 
[[Category:MediaWiki]]
The standard GeSHi MediaWiki extensions don't quite suit our purposes because our articles containing code are usually directly executed which means that we can't include GeSHi code tags. To get around this I've added a few extra lines after the GeSHi include to allow the tags to be added before parsing based on the article content or title as follows:
+
The standard GeSHi MediaWiki extensions don't quite suit our purposes because our articles containing code are usually directly executed which means that we can't include GeSHi code tags.
<php5>
+
 
 +
Another problem is that we'd often like the ability to use MediaWiki templates even if the content is code, for example when defining ''LocalSettings'' articles by transcluding the [[Template:Wiki|wiki template]] and extension templates. So I've also added an extra line of code into the GeSHi extension before it parses the content to achieve this:
 +
 
 +
To get around these issues, I've added a few extra lines after the GeSHi include to allow the tags to be added before parsing if a language template is transcluded. Here's the updated include line:
 +
<php>
 
include('extensions/geshi/GeSHiCodeTag.php');
 
include('extensions/geshi/GeSHiCodeTag.php');
 
$wgHooks['ParserBeforeStrip'][] = 'GeSHi';
 
$wgHooks['ParserBeforeStrip'][] = 'GeSHi';
 
function GeSHi(&$parser, &$text, &$strip_state) {
 
function GeSHi(&$parser, &$text, &$strip_state) {
         if (preg_match('/^<\\?/',$text)) $text = "<php>\n$text\n</php>";
+
         if (preg_match('/\\{\\{#(php|perl|c|actionscript|javascript|css)\\}\\}/i',$text,$m)) $text = "<$m[1]>\n$text\n</$m[1]>";
        if (preg_match('/^#!\\/usr\\/bin\\/perl/',$text)) $text = "<perl>\n$text\n</perl>";
 
 
         return true;
 
         return true;
        }
+
</php>
</php5>
+
Templates or variables must be added which expand to nothing so they don't render as a red link. Also an extra line must be added to the GeSHiCodeTag.php file just before the geshi object is instantiated in the hook function to force the templates to expand.
 
 
Another problem is that we'd often like the ability to use MediaWiki templates even if the content is code, for example when defining ''LocalSettings'' articles by transcluding the [[Template:Wiki|wiki template]] and extension templates. So I've also added an extra line of code into the GeSHi extension before it parses the content to achieve this:
 
 
<php>
 
<php>
 
$source = $GLOBALS["wgParser"]->replaceVariables($source);
 
$source = $GLOBALS["wgParser"]->replaceVariables($source);
 
</php>
 
</php>

Revision as of 02:55, 18 April 2007

The standard GeSHi MediaWiki extensions don't quite suit our purposes because our articles containing code are usually directly executed which means that we can't include GeSHi code tags.

Another problem is that we'd often like the ability to use MediaWiki templates even if the content is code, for example when defining LocalSettings articles by transcluding the wiki template and extension templates. So I've also added an extra line of code into the GeSHi extension before it parses the content to achieve this:

To get around these issues, I've added a few extra lines after the GeSHi include to allow the tags to be added before parsing if a language template is transcluded. Here's the updated include line: <php> include('extensions/geshi/GeSHiCodeTag.php'); $wgHooks['ParserBeforeStrip'][] = 'GeSHi'; function GeSHi(&$parser, &$text, &$strip_state) {

       if (preg_match('/\\{\\{#(php|perl|c|actionscript|javascript|css)\\}\\}/i',$text,$m)) $text = "<$m[1]>\n$text\n</$m[1]>";
       return true;

</php> Templates or variables must be added which expand to nothing so they don't render as a red link. Also an extra line must be added to the GeSHiCodeTag.php file just before the geshi object is instantiated in the hook function to force the templates to expand. <php> $source = $GLOBALS["wgParser"]->replaceVariables($source); </php>