1<?php
2/**
3 * <TEXT> tag that embeds plain text with auto linebreaks in <pre>
4 */
5
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die();
8
9/**
10 * All DokuWiki plugins to extend the parser/rendering mechanism
11 * need to inherit from this class
12 */
13class syntax_plugin_plaintext_block extends DokuWiki_Syntax_Plugin {
14
15    function getType() { return 'protected'; }
16    function getPType() { return 'block'; }
17    function getAllowedTypes() { return array(); }
18    function getSort() { return 20; }
19
20    /**
21     * Connect pattern to lexer
22     */
23    function connectTo($mode) {
24        $this->Lexer->addEntryPattern('<TEXT>(?=.*?</TEXT>)',$mode,'plugin_plaintext_block');
25    }
26
27    function postConnect() {
28        $this->Lexer->addExitPattern('</TEXT>','plugin_plaintext_block');
29    }
30
31    /**
32     * Handle the match
33     */
34    function handle($match, $state, $pos, Doku_Handler $handler){
35        switch ($state) {
36            case DOKU_LEXER_ENTER:
37                return array($state);
38            case DOKU_LEXER_UNMATCHED :
39                $handler->_addCall('cdata', array($match), $pos);
40                return false;
41            case DOKU_LEXER_EXIT :
42                return array($state);
43        }
44        return false;
45    }
46
47    /**
48     * Create output
49     */
50    function render($format, Doku_Renderer $renderer, $data) {
51        if($format == 'xhtml'){
52            list($state) = $data;
53            switch ($state) {
54                case DOKU_LEXER_ENTER:
55                    $renderer->doc .= '<pre class="code plaintext">';
56                    break;
57                case DOKU_LEXER_EXIT:
58                    $renderer->doc .= "</pre>";
59                    break;
60            }
61            return true;
62        }else if($format == 'metadata'){
63            // do nothing since content is managed in "unmatched" state
64            return true;
65        }
66        return false;
67    }
68}
69