# Embedding the DokuWiki parser into your website



## Tux (Jun 20, 2013)

I don't know where to stick this, but I just want it out before I code my stuff.

With everyone making replicas of household names like _SolusVM_ and _WHMCS_ I might as well share this.

Use cases might include:


In-context help
A lazy man's documentation package
Something to add gags onto like ads that will content you to the very topMySQL support
*Licensing note*: DokuWiki is licensed under GPLv2. I am also licensing the code I've included in the post as GPLv2.

Setup:


Get a copy of DokuWiki from http://www.dokuwiki.org and uncompress it.
Rename the folder to "doku".
If you want to edit files from the DokuWiki setup, get another copy and symlink your "working DokuWiki"'s folder to your "dumb DokuWiki"'s pages folder
Now, here's some code I hacked up. I did most of this while working on another project (which I stopped working on a while ago).

I've commented (most of) it so you can understand it.


```
<?php

// Include DokuWiki files
// We'll need these to parse the wikitext.

define("DOKU_INC", dirname(__FILE__) . "/doku/inc/");

require_once(DOKU_INC.'init.php');
require_once DOKU_INC . 'parser/parser.php';
 
// Create the parser
$Parser = & new Doku_Parser();
 
// Add the Handler
$Parser->Handler = & new Doku_Handler();
 
// Load all the modes
// You could strip some of these out ;)

// I would not advise removing:
// - notoc
// - listblock
// - preformatted
// - formats
// - code
// - unformatted

$Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
$Parser->addMode('preformatted',new Doku_Parser_Mode_Preformatted()); 
$Parser->addMode('notoc',new Doku_Parser_Mode_NoToc());
$Parser->addMode('header',new Doku_Parser_Mode_Header());
$Parser->addMode('table',new Doku_Parser_Mode_Table());
 

$formats = array (
    'strong', 'emphasis', 'underline', 'monospace',
    'subscript', 'superscript', 'deleted',
);
foreach ( $formats as $format ) {
    $Parser->addMode($format,new Doku_Parser_Mode_Formatting($format));
}
 
$Parser->addMode('linebreak',new Doku_Parser_Mode_Linebreak());
$Parser->addMode('footnote',new Doku_Parser_Mode_Footnote());
$Parser->addMode('hr',new Doku_Parser_Mode_HR());
 
$Parser->addMode('unformatted',new Doku_Parser_Mode_Unformatted());
$Parser->addMode('php',new Doku_Parser_Mode_PHP());
$Parser->addMode('html',new Doku_Parser_Mode_HTML());
$Parser->addMode('code',new Doku_Parser_Mode_Code());
$Parser->addMode('file',new Doku_Parser_Mode_File());
$Parser->addMode('quote',new Doku_Parser_Mode_Quote());
 
// These need data files. The get* functions are left to your imagination
//$Parser->addMode('acronym',new Doku_Parser_Mode_Acronym(array_keys(getAcronyms())));
//$Parser->addMode('wordblock',new Doku_Parser_Mode_Wordblock(array_keys(getBadWords())));
//$Parser->addMode('smiley',new Doku_Parser_Mode_Smiley(array_keys(getSmileys())));
//$Parser->addMode('entity',new Doku_Parser_Mode_Entity(array_keys(getEntities())));
 
$Parser->addMode('multiplyentity',new Doku_Parser_Mode_MultiplyEntity());
$Parser->addMode('quotes',new Doku_Parser_Mode_Quotes());
 
$Parser->addMode('camelcaselink',new Doku_Parser_Mode_CamelCaseLink());
$Parser->addMode('internallink',new Doku_Parser_Mode_InternalLink());
$Parser->addMode('media',new Doku_Parser_Mode_Media());
$Parser->addMode('externallink',new Doku_Parser_Mode_ExternalLink());
$Parser->addMode('emaillink',new Doku_Parser_Mode_EmailLink());
$Parser->addMode('windowssharelink',new Doku_Parser_Mode_WindowsShareLink());
$Parser->addMode('filelink',new Doku_Parser_Mode_FileLink());
$Parser->addMode('eol',new Doku_Parser_Mode_Eol());
 
// Load the raw wiki document

// $doc is the full wikimarkup to parse
// You can be pretty creative here.
// You can have a private DokuWiki install to make edits.
// You can get data from MySQL and not deal with MediaWiki.
// However, the code below sucks.
// It's an injection waiting to happen.

// -- START REPLACING THIS BLOCK OF CODE

if($_GET['id']=="") $_GET['id']="main";
$page="doku/pages/".str_replace("..","",$_GET['id']).".wiki";
echo "<h1>".$page."</h1>";
$doc = file_get_contents($page);

// -- STOP REPLACING CODE

// Get a list of instructions
$instructions = $Parser->parse($doc);
 
// Create a renderer
require_once DOKU_INC . 'parser/xhtml.php';
$Renderer = & new Doku_Renderer_XHTML();
 
# Load data like smileys into the Renderer here

// Loop through the instructions
foreach ( $instructions as $instruction ) {
 
    // Execute the callback against the Renderer
    call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
}
 
// Display the output
echo $Renderer->doc;
```


----------



## drmike (Jun 20, 2013)

Can we get this moved over to the Tutorials forum?


----------



## Tux (Jun 20, 2013)

buffalooed said:


> Can we get this moved over to the Tutorials forum?


This doesn't relate to VPSes as a whole.


----------

