1<?php
2/**
3 * DokuWiki Plugin Snippets Syntax Component
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Michael Klier <chi@chimeric.de>
6 */
7
8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'syntax.php');
11
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_snippets extends DokuWiki_Syntax_Plugin {
17
18    function getType(){ return 'protected';}
19    function getAllowedTypes() { return array('container','substition','protected','disabled','formatting','paragraphs'); }
20    function getPType(){ return 'block';}
21
22    function getSort(){ return 195; }
23
24    /**
25     * Connect pattern to lexer
26     */
27    function connectTo($mode) {
28      $this->Lexer->addEntryPattern('<snippet>(?=.*?</snippet>)',$mode,'plugin_snippets');
29      $this->Lexer->addSpecialPattern('~~SNIPPET_O\d*~~.*?~~',$mode,'plugin_snippets');
30      $this->Lexer->addSpecialPattern('~~SNIPPET_C~~.*?~~',$mode,'plugin_snippets');
31    }
32
33    function postConnect() {
34      $this->Lexer->addExitPattern('</snippet>', 'plugin_snippets');
35    }
36
37    /**
38     * Handle the match
39     */
40    function handle($match, $state, $pos, Doku_Handler $handler){
41
42        switch ($state) {
43            case DOKU_LEXER_ENTER:
44                return array('snippet_open',$data);
45                break;
46            case DOKU_LEXER_MATCHED:
47            case DOKU_LEXER_UNMATCHED:
48                return array('data', $match);
49                break;
50            case DOKU_LEXER_EXIT:
51                return array('snippet_close', $title);
52                break;
53            case  DOKU_LEXER_SPECIAL:
54               return array('update',$match);
55               break;
56        }
57        return false;
58    }
59
60    /**
61     * Create output
62     */
63    function render($mode, Doku_Renderer $renderer, $indata) {
64        list($instr, $data) = $indata;
65        if($mode == 'xhtml'){
66            switch ($instr) {
67                case 'snippet_open':
68                    $renderer->doc .= '<div class="plugin_snippets__info">' . DOKU_LF;
69                    break;
70                case 'snippet_close':
71                    $renderer->doc .= '</div>' . DOKU_LF;
72                    break;
73                case 'data' :
74                    $renderer->doc .= $renderer->_xmlEntities($data);
75                    break;
76                case 'update':
77                   $renderer->doc .="";
78                    break;
79            }
80            return true;
81        }
82        return false;
83    }
84
85}
86// vim:ts=4:sw=4:et:enc=utf-8:
87