1<?php
2/*
3 * Github style codeblocks, starting and ending with three backticks, optionally
4 * providing a language to be used for syntax highlighting.
5 *
6 * ```php
7 * ...
8 * ```
9 */
10
11if(!defined('DOKU_INC')) die();
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_markdowku_githubcodeblocks extends DokuWiki_Syntax_Plugin {
16
17    function getType()  { return 'protected'; }
18    function getPType() { return 'block'; }
19    function getSort()  { return 91; }
20
21    function connectTo($mode) {
22        $this->Lexer->addSpecialPattern(
23            '\n```[a-z0-9_]*\n.+?\n```(?=\n)',
24            $mode,
25            'plugin_markdowku_githubcodeblocks');
26    }
27
28    function handle($match, $state, $pos, Doku_Handler $handler) {
29		if (preg_match('/^\n```([a-z0-9_]+)\n/', $match, $matches) > 0) {
30			$lang = $matches[1];
31		} else {
32			$lang = NULL;
33		}
34
35		$text = preg_replace('/^```[a-z0-9_]+\n/m', '', $match);
36		$text = preg_replace('/^```$/m', '', $text);
37		if ($lang)
38			$handler->_addCall('file', array($text, $lang, 'snippet.'.$lang), $pos);
39		else
40			$handler->_addCall('code', array($text, $lang), $pos);
41        return true;
42    }
43
44    function render($mode, Doku_Renderer $renderer, $data) {
45        return true;
46    }
47}
48//Setup VIM: ex: et ts=4 enc=utf-8 :
49