1<?php
2/**
3 * Changemarks Plugin: mark inserted 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_inserted extends DokuWiki_Syntax_Plugin {
20
21    var $ins = 'plugin_changemarks_inserted'; // instruction of this plugin
22    static protected $helper = NULL;
23
24    function getType() { return 'formatting'; }
25    function getSort() { return 121; }
26
27    function connectTo($mode) {
28        $this->Lexer->addEntryPattern('<ins[^\r\n]*?>(?=.*?</ins>)', $mode, $this->ins);
29        $this->Lexer->addEntryPattern('\+\+[^\r\n]*?>(?=.*?\+\+)', $mode, $this->ins);
30    }
31
32    function postConnect() {
33        $this->Lexer->addExitPattern('\+\+', $this->ins);
34        $this->Lexer->addExitPattern('</ins>', $this->ins);
35    }
36
37    /**
38     * Handle the match
39     */
40    function handle($match, $state, $pos, Doku_Handler $handler) {
41        switch ($state) {
42
43            // entry pattern with optional title
44            case DOKU_LEXER_ENTER:
45                // strip markup
46                if (substr($match, 0, 4) == '<ins') $match = substr($match, 5, -1);
47                else $match = substr($match, 2, -1);
48                return array($state, $match);
49
50                // inserted text
51            case DOKU_LEXER_UNMATCHED:
52                return array($state, $match);
53
54                // exit pattern
55            case DOKU_LEXER_EXIT:
56                return array($state);
57
58
59            default:
60                return false;
61        }
62    }
63
64    /**
65     * Create output
66     */
67    function render($mode, Doku_Renderer $renderer, $data) {
68        if (is_array($data)) {
69            if ($mode == 'xhtml') {
70                switch ($data[0]) {
71                    case DOKU_LEXER_ENTER:
72                        $title = ($data[1] ? ' title="'.hsc($data[1]).'"' : '');
73                        $renderer->doc .= '<ins'.$title.'>';
74                        return true;
75                    case DOKU_LEXER_UNMATCHED:
76                        $renderer->doc .= hsc($data[1]);
77                        return true;
78                    case DOKU_LEXER_EXIT:
79                        $renderer->doc .= '</ins>';
80                        return true;
81                    default:
82                        return false;
83                }
84            }
85            if ($mode == 'odt') {
86                if ($this->helper==NULL) {
87                    $this->helper = plugin_load('helper', 'changemarks');
88                }
89                switch ($data[0]) {
90                    case DOKU_LEXER_ENTER:
91                        $title = ($data[1] ? ' title="'.hsc($data[1]).'"' : '');
92                        if (class_exists('ODTDocument')) {
93                            $renderer->_odtSpanOpenUseCSS ('ins');
94                        }
95                        return true;
96                    case DOKU_LEXER_UNMATCHED:
97                        $renderer->cdata($data[1]);
98                        return true;
99                    case DOKU_LEXER_EXIT:
100                        if (class_exists('ODTDocument')) {
101                            $renderer->_odtSpanClose();
102                        }
103                        return true;
104                    default:
105                        return false;
106                }
107            }
108        }
109        return false;
110    }
111}
112// vim:ts=4:sw=4:et:enc=utf-8:
113