1<?php
2/**
3 * UnformattedCode Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Anika Henke <anika@selfthinker.org>
7 */
8
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14class syntax_plugin_unformattedcode extends DokuWiki_Syntax_Plugin {
15
16    function getType() {
17        return 'protected';
18    }
19    function getAllowedTypes() {
20        return array('formatting', 'disabled');
21    }
22    function getPType() {
23        return 'normal';
24    }
25    function getSort() {
26        return 99; // one less than core 'monospace', so it overwrites it
27    }
28
29    /**
30     * Connect pattern to lexer
31     */
32    function connectTo($mode) {
33        $this->Lexer->addEntryPattern('\x22\x22(?=.*\x22\x22)',$mode,'plugin_unformattedcode'); // ""code""
34        if ($this->getConf('overwrite')) {
35            $this->Lexer->addEntryPattern('\x27\x27(?=.*\x27\x27)',$mode,'plugin_unformattedcode'); // ''code''
36        }
37    }
38    function postConnect() {
39        $this->Lexer->addExitPattern('\x22\x22', 'plugin_unformattedcode');
40        if ($this->getConf('overwrite')) {
41            $this->Lexer->addExitPattern('\x27\x27', 'plugin_unformattedcode');
42        }
43    }
44
45    /**
46     * Handle the match
47     */
48    function handle($match, $state, $pos, Doku_Handler $handler){
49        switch ($state) {
50            case DOKU_LEXER_ENTER:
51                return array($state);
52
53            case DOKU_LEXER_UNMATCHED :
54                $handler->_addCall('cdata', array($match), $pos);
55                return false;
56
57            case DOKU_LEXER_EXIT :
58                return array($state);
59        }
60        return false;
61    }
62
63    /**
64     * Create output
65     */
66    function render($mode, Doku_Renderer $renderer, $indata) {
67
68        if (empty($indata)) return false;
69        list($state, $data) = $indata;
70
71        if($mode == 'xhtml'){
72            switch ($state) {
73                case DOKU_LEXER_ENTER:
74                    $renderer->doc .= '<code>';
75                    break;
76
77                case DOKU_LEXER_EXIT:
78                    $renderer->doc .= "</code>";
79                    break;
80            }
81            return true;
82        }
83        return false;
84    }
85
86}
87