updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / geshicodetag-mediawiki-ext / GeshiCodeTag.php
blob7bdc52b257ca7da8f4b275eaeafad7f94c6c2575
1 <?php
2 ########################
3 # GeshiCodeTag.php
4 # Licensed under GPLv3
5 # by Paul Nolasco
6 ########################
8 // change directory accordingly
9 require_once('geshi/geshi.php');
10 $languagesPath = "extensions/geshicodetag/geshi/geshi";
12 // 1 - ENABLED, 0 - DISABLED
13 $codeTag["simple"] = 1; // ex. <php> echo </php>
14 $codeTag["advanced"]["mode"] = 1; // ex. <code php n> echo </php>
16 // extra options
17 /*
18 strict mode - http://qbnz.com/highlighter/geshi-doc.html#using-strict-mode
19 ex. <img src="<?php echo rand(1, 100) ?>" />
21 $codeTag["advanced"]["strict"] = 0;
23 #############################################
25 $wgExtensionFunctions[] = "ExtensionCodeTag";
26 $wgExtensionCredits['parserhook'][] = array(
27 'name' => 'GeSHiCodeTag',
28 'author' => 'Paul Nolasco',
29 'version' => '1.65',
30 'description' => 'A tag to create a syntax-highlighted code using GeSHi',
31 'url' => 'http://www.mediawiki.org/wiki/Extension:GeSHiCodeTag'
33 $languages = array();
35 function ExtensionCodeTag()
37 global $wgParser, $codeTag, $languages, $languagesPath;
39 ReadLanguages();
41 if($codeTag["advanced"]["mode"])
42 $wgParser->setHook('code', 'AdvancedCodeTag');
44 if($codeTag["simple"])
45 foreach($languages as $lang)
47 $wgParser->setHook($lang,
48 create_function( '$source',
49 '$geshi = new GeSHi($source,\'' . $lang . '\', \'' . $languagesPath . '\');
50 return $geshi->parse_code();'
51 ));
55 function ReadLanguages()
57 global $languages, $languagesPath;
59 $dirHandle = opendir($languagesPath)
60 or die("ERROR: Invalid directory path - [$languagesPath], Modify the value of \$languagesPath'");
62 $pattern = "^(.*)\.php$";
64 while ($file = readdir($dirHandle))
66 if( eregi($pattern, $file) )
67 $languages[] = eregi_replace($pattern, "\\1", $file);
69 closedir($dirHandle);
72 function AdvancedCodeTag ($source, $settings){
74 global $languages, $languagesPath, $codeTag;
75 $language = array_shift($settings); // [arg1]
77 // [arg1]
78 if($language == '')
79 $language='text';
81 if($language == "list") // list all languages supported
82 return "<br>List of supported languages for <b>Geshi " . GESHI_VERSION . "</b>:<br>"
83 . implode("<br>", $languages);
85 if($language != "" && !in_array($language, $languages)) // list languages if invalid argument
86 return "<br>Invalid language argument, \"<b>" . $language . "</b>\", select one from the list:<br>"
87 . implode("<br>", $languages);
89 // set geshi
90 $geshi = new GeSHi(trim($source), $language, $languagesPath);
91 $geshi->enable_strict_mode($codeTag["advanced"]["strict"]);
94 // [arg2 or more]
95 if(in_array('n',$settings)) // display line numbers
96 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
99 Add more GeSHi features from [ http://qbnz.com/highlighter/geshi-doc.html ]
100 template:
101 if( in_array( '<PARAMETER NAME>', $settings ) )
103 $geshi-><GESHI FUNCTION CALL>
106 // removes newlines replaces with <br />
107 return str_replace("\n",'<br />', $geshi->parse_code());