Automatic account creation is now enabled. Captcha authentication required for account creation and editing unless you are trusted here.

Code Extension

From WiX Wiki at MindCapers

Jump to: navigation, search

Contents

Description

The Code extension Paul Grinberg to beautify the wiki appearance. By taking advantage of GeSHi, the General Syntax Hilighting software, this extension can make any code listing look pretty. Also, since GeSHi supports syntax highlighting for DOS and Bash, even CLI documentation is greatly improved.

Hide Instructions

Installation

Since GeSHi is used as the syntax highlighting engine, it must be installed first. Download the latest version from SourceForge and uncompress/unpackage it straight into the wiki/extensions directory. The extracted files will all be placed inside a folder called geshi.

Next, install Code.php
  1. <?php
  2.  
  3. $wgExtensionFunctions[] = "wfCodeExtension";
  4. $wgExtensionCredits['parserhook'][] = array(
  5. 'version' => '0.5',
  6. 'name' => 'Code',
  7. 'author' => 'Paul Grinberg',
  8. 'description' => 'adds <Code> tag',
  9. 'url' => 'LicSvr Extensions'
  10. );
  11.  
  12. function wfCodeExtension()
  13. {
  14. global $wgParser;
  15. $wgParser->setHook( "Code", "renderCode" );
  16. }
  17.  
  18. function renderCode($input, $argv, &$parser)
  19. {
  20. global $wgShowHideDiv;
  21. //$parser->disableCache();
  22. //wfDebug( __METHOD__.": '$input'\n" );
  23.  
  24. // default values
  25. $language = 'text';
  26. $showLineNumbers = false;
  27. $source = $input;
  28. $tabwidth = 4;
  29. $convertTabs = true;
  30.  
  31. foreach ($argv as $key => $value) {
  32. switch ($key) {
  33. case 'lang':
  34. $language = $value;
  35. break;
  36. case 'linenumbers':
  37. $showLineNumbers = true;
  38. break;
  39. case 'tabwidth':
  40. $tabwidth = $value;
  41. break;
  42. case 'tabs':
  43. $convertTabs = false;
  44. break;
  45. case 'fileurl':
  46. $html = $parser->unstrip($parser->recursiveTagParse($value),$parser->mStripState);
  47. $i = preg_match('/<a.*?>(.*?)<\/a>/', $html, $matches);
  48. $url = $matches[1];
  49. //print("URL is '$url'");
  50. $source = file_get_contents($url);
  51. break;
  52. default :
  53. wfDebug( __METHOD__.": Requested '$key ==> $value'\n" );
  54. break;
  55. }
  56. }
  57. if (!defined('GESHI_VERSION')) {
  58. include('geshi/geshi.php'); // include only once or else wiki dies
  59. }
  60.  
  61. // Convert Tabs to Spaces if desired
  62. $geshi_code = $source;
  63. if ($convertTabs) {
  64. $tabSpaces = '';
  65. for ($i=0; $i<$tabwidth; $i++) {
  66. $tabSpaces .= ' ';
  67. }
  68. $geshi_code = str_replace("\t", $tabSpaces, $source);
  69. }
  70. $geshi = new GeSHi($geshi_code, $language);
  71. $error = $geshi->error(); // die gracefully if errors found
  72. if ($error) {
  73. return "Code Extension Error: $error";
  74. }
  75. if ($showLineNumbers) { // enable line numbers if requested
  76. $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
  77. }
  78. return $geshi->parse_code();
  79. }
  80.  
  81. ?>
  82.  
into the wiki/extensions directory (make sure the saved filename is Code.php). Lastly, enable the extension in LocalSettings.php by adding
require_once 'extensions/Code.php';

Usage

There are two typical usages of this extension. The first is to perform Syntax Highlighting on a code snippet that is typed directly into the wiki page

<code lang='perl' linenumbers>
use strict;
use warnings;

print "Hello World\n";
</code>
which produces
  1.  
  2. use strict;
  3. use warnings;
  4.  
  5. print "Hello World\n";
  6.  

The second typical usage is to pass the URL of a file containing some code directly to the extension, like so

<code fileurl='http://nostarch.com/extras/hownotc/hello4.c' lang='c'/>
which produces
#include <stdio.h>
int main()
{
    printf("Hello World/n");
    return (0);
}
 
 

It is also sometimes nice to hide the code from the wiki page by taking advantage of the ShowHide Extension. For example,

<ShowHideDiv default='hide' showmsg='(Click to show code)' hidemsg='(Click to hide code)'><code fileurl='http://nostarch.com/extras/hownotc/hello4.c' lang='c'/></ShowHideDiv>
would produce (Click to hide code)
#include <stdio.h>
int main()
{
    printf("Hello World/n");
    return (0);
}
 
 

The extension has the following options

Option Default Value Possible Values
lang Any of the supported 80+ syntaxes supported by GeSHi
linenumbers false false, true
fileurl Any URL accessible by the wiki
tabwidth 4 Number of spaces used to replace a tab character
tabs false Leave the tabs as they were

Supported Languages

Over 80, including ...

  • c
  • cpp
  • csharp
  • lua
  • php
  • sql
  • text
  • vb
  • vbnet
  • wix
  • xml

Bug Reports

Revision History

  • v0.5 - 4/25/2007 - Installed
Personal tools