1<?php
2/**
3 * DokuWiki Plugin wavedrom (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Raymond Wu <wusixin@1gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13/**
14* if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
15* if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16* require_once(DOKU_PLUGIN.'syntax.php');
17*/
18
19class syntax_plugin_wavedrom extends DokuWiki_Syntax_Plugin
20{
21    function getType(){ return 'protected'; }
22
23    // must return a number lower than returned by native 'code' mode (200)
24    function getSort(){ return 158; }
25
26
27
28
29    /**
30     * Connect lookup pattern to lexer.
31     *
32     * @param string $mode Parser mode
33     */
34     function connectTo($mode) {
35        $this->Lexer->addEntryPattern('<wavedrom>(?=.*?</wavedrom>)',$mode,'plugin_wavedrom');
36    }
37    function postConnect() {
38        $this->Lexer->addExitPattern('</wavedrom>','plugin_wavedrom');
39    }
40
41
42    /**
43     * Handle matches of the wavedrom syntax
44     */
45    function handle($match, $state, $pos, Doku_Handler $handler){
46        switch ($state) {
47            case DOKU_LEXER_ENTER:
48                return array($state, '');
49
50            case DOKU_LEXER_UNMATCHED :
51                return array($state, $match);
52
53            case DOKU_LEXER_EXIT :
54                return array($state, '');
55
56        }
57        return false;
58    }
59
60
61    /**
62     * Render xhtml output or metadata
63     */
64    function render($mode, Doku_Renderer $renderer, $indata) {
65        if($mode == 'xhtml'){
66            list($state, $match) = $indata;
67            switch ($state) {
68
69            case DOKU_LEXER_ENTER :
70                $renderer->doc .= '<script type="WaveDrom">';
71                break;
72
73              case DOKU_LEXER_UNMATCHED :
74                $renderer->doc .= $match;
75                break;
76
77              case DOKU_LEXER_EXIT :
78                $renderer->doc .= "</script>";
79                break;
80            }
81            return true;
82        }
83        return false;
84    }
85
86}
87
88