1<?php
2/**
3 * Changemarks Plugin: highlight text with !!>text!!
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <wikidesign@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_changemarks_highlighted extends DokuWiki_Syntax_Plugin {
20
21    var $ins = 'plugin_changemarks_highlighted'; // instruction of this plugin
22    static protected $helper = NULL;
23
24    function getType() { return 'formatting'; }
25    function getSort() { return 123; }
26
27    function connectTo($mode) {
28        $this->Lexer->addEntryPattern('\!\![^\r\n]*?>(?=.*?\!\!)', $mode, $this->ins);
29    }
30
31    function postConnect() {
32        $this->Lexer->addExitPattern('\!\!', $this->ins);
33    }
34
35    /**
36     * Handle the match
37     */
38    function handle($match, $state, $pos, Doku_Handler $handler) {
39        switch ($state) {
40
41            // entry pattern with optional title
42            case DOKU_LEXER_ENTER:
43                // strip markup
44                $match = substr($match, 2, -1);
45                return array($state, $match);
46
47                // inserted text
48            case DOKU_LEXER_UNMATCHED:
49                return array($state, $match);
50
51                // exit pattern
52            case DOKU_LEXER_EXIT:
53                return array($state);
54
55
56            default:
57                return false;
58        }
59    }
60
61    /**
62     * Create output
63     */
64    function render($mode, Doku_Renderer $renderer, $data) {
65        if (is_array($data)) {
66            if (($mode == 'xhtml') && (is_array($data))) {
67                switch ($data[0]) {
68                    case DOKU_LEXER_ENTER:
69                        $title = ($data[1] ? ' title="'.hsc($data[1]).'"' : '');
70                        $renderer->doc .= '<span class="highlighted"'.$title.'>';
71                        return true;
72                    case DOKU_LEXER_UNMATCHED:
73                        $renderer->doc .= hsc($data[1]);
74                        return true;
75                    case DOKU_LEXER_EXIT:
76                        $renderer->doc .= '</span>';
77                        return true;
78                    default:
79                        return false;
80                }
81            }
82            if ($mode == 'odt') {
83                if ($this->helper==NULL) {
84                    $this->helper = plugin_load('helper', 'changemarks');
85                }
86                switch ($data[0]) {
87                    case DOKU_LEXER_ENTER:
88                        $title = ($data[1] ? ' title="'.hsc($data[1]).'"' : '');
89                        if (class_exists('ODTDocument')) {
90                            $renderer->_odtSpanOpenUseCSS (NULL, 'class="highlighted"');
91                        }
92                        return true;
93                    case DOKU_LEXER_UNMATCHED:
94                        $renderer->cdata($data[1]);
95                        return true;
96                    case DOKU_LEXER_EXIT:
97                        if (class_exists('ODTDocument')) {
98                            $renderer->_odtSpanClose();
99                        }
100                        return true;
101                    default:
102                        return false;
103                }
104            }
105        }
106        return false;
107    }
108}
109// vim:ts=4:sw=4:et:enc=utf-8:
110