Markdown plugin for dokuwiki

Quick test of the plugin-system in dokuwiki. Allows the user to embed markdown syntax in <markdown> tags. Also fixes bug #333.

Try it with the bookmarklet from The asciinator.

Note that this plugin is currently good only for formatting chunks of markdown text. Essential Dokuwiki features such as wiki links and the table of contents cannot yet be nested within the <markdown> tags.

Instructions

  1. Make the directory <dokuwiki-root>/lib/plugins/markdown
  2. Copy markdown.php from the php-markdown.zip to the directory you just made.
  3. Copy the plugin-source below, and paste it into <dokuwiki-root>/inc/plugins/markdown/syntax.php
  4. Enjoy your markdown’d code by placing it in <markdown> tags

Todo

  1. Put the plugin way higher in the plugin-sort-thingy and parse markdown to dokuwiki. That way, we’ll get the TOC and wiki links.

Source

This procedure and syntax.php source have been tried successfully with dokuwiki-2007-06-26. Please update this section as newer versions of Dokuwiki are released. For notes on use with older versions of Dokuwiki, see below

<?php
/**
 * Markdown-Plugin: Parses markdown-blocks
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Carl-Christian Salvesen <calle@ioslo.net>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'inc/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once(DOKU_PLUGIN.'markdown/markdown.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_markdown extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Carl-Christian Salvesen',
            'email'  => 'calle@ioslo.net',
            'date'   => '2006-05-24',
            'name'   => 'Markdown Plugin',
            'desc'   => 'Parses markdown-blocks',
            'url'    => 'http://wiki.ioslo.net/dokuwiki/markdown',
        );
    }
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'protected';
    }
 
    /**
     * Where to sort in?
     */
    function getSort(){
        return 205;
    }
 
    function getPType() {
        return 'block';
    }
 
    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addEntryPattern('<markdown>(?=.*</markdown>)',$mode,'plugin_markdown');
    }
 
    function postConnect() {
      $this->Lexer->addExitPattern('</markdown>','plugin_markdown');
    }
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        return array($match);
    }
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml' && $data[0] != "<markdown>" && $data[0] != "</markdown>") {
            $renderer->doc .= Markdown($data[0]);
            return true;
        }
        return false;
    }
 
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :
?>

Notes and Source for old versions of Dokuwiki

 
<?php
/**
 * Markdown-Plugin: Parses markdown-blocks
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Carl-Christian Salvesen <calle@ioslo.net>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'inc/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once(DOKU_PLUGIN.'markdown/markdown.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_markdown extends DokuWiki_Syntax_Plugin {
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'protected';
    }
 
    /**
     * Where to sort in?
     */
    function getSort(){
        return 205;
    }
 
 
    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addEntryPattern('<markdown>',$mode,'plugin_markdown');
    }
 
    function postConnect() {
      $this->Lexer->addExitPattern('</markdown>','plugin_markdown');
    }
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        $match = substr($match,10,-11); //strip <markdown> and </markdown>
        return array($match);
    }
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
            //handle various info stuff
            $renderer->doc .= Markdown($data[0]);
            return true;
        }
        return false;
    }
 
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :
?>
 
I could be wrong, but it looks like the substr() call to strip the <markdown> tags is no longer needed. (using version 2005-07-1) They were stripping some of my text, and I got the expected behavior when I just removed that line. — James Van Lommel 2005/07/03 23:15
The plugin needs to implement the getInfo() method or DokuWiki complains. — Alan Liu 2005/07/15 19:48
Using the 2005-09-22 version, I had to:
  1. put markdown.php into lib/plugins/markdown/markdown.php
  2. put syntax.php (copy/paste from above) into lib/plugins/markdown/syntax.php
  3. edit conf/local.php and add $conf[’pluginmanager’] = 1;

David Rasch 2006/01/23 14:22 EST

Using the 2006-03-09 version
  1. In conf/local.php I didn’t need to have $conf[’pluginmanager’] = 1;
  2. but I did need to disable html by commenting out $conf[’htmlok’] = 1; before it would work
  3. I also had to comment out the substr() line as detailed by James.

Andy Grimsdale 2006/04/06 23:50 GMT

 
dokuwiki/markdown.txt · Last modified: 2007/07/11 19:47 by ericnguyen
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki