summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/refnotes/syntax/notes.php
blob: 32263ac070a2d68dc61a352721824d7bee998c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php

/**
 * Plugin RefNotes: Note renderer
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
 */

require_once(DOKU_PLUGIN . 'refnotes/core.php');

class syntax_plugin_refnotes_notes extends DokuWiki_Syntax_Plugin {

    private $mode;

    /**
     * Constructor
     */
    public function __construct() {
        $this->mode = substr(get_class($this), 7);
    }

    /**
     * What kind of syntax are we?
     */
    public function getType() {
        return 'substition';
    }

    public function getPType() {
        return 'block';
    }

    /**
     * Where to sort in?
     */
    public function getSort() {
        return 150;
    }

    public function connectTo($mode) {
        $this->Lexer->addSpecialPattern('~~REFNOTES.*?~~', $mode, $this->mode);
        $this->Lexer->addSpecialPattern('<refnotes[^>]*?\/>', $mode, $this->mode);
        $this->Lexer->addSpecialPattern('<refnotes(?:[^>]*?[^/>])?>.*?<\/refnotes>', $mode, $this->mode);
    }

    /**
     * Handle the match
     */
    public function handle($match, $state, $pos, Doku_Handler $handler) {
        switch ($match[0]) {
            case '~':
                return $this->handleBasic($match);

            case '<':
                return $this->handleExtended($match);
        }

        return false;
    }

    /**
     * Create output
     */
    public function render($mode, Doku_Renderer $renderer, $data) {
        try {
            if($mode == 'xhtml') {
                switch ($data[0]) {
                    case 'style':
                        refnotes_renderer_core::getInstance()->styleNamespace($data[1]['ns'], $data[2]);
                        break;

                    case 'map':
                        refnotes_renderer_core::getInstance()->setNamespaceMapping($data[1]['ns'], $data[2]);
                        break;

                    case 'render':
                        $this->renderNotes($mode, $renderer, $data[1]);
                        break;
                }

                return true;
            }
            elseif ($mode == 'odt') {
                switch ($data[0]) {
                    case 'render':
                        $this->renderNotes($mode, $renderer, $data[1]);
                        break;
                }

                return true;
            }
        }
        catch (Exception $error) {
            msg($error->getMessage(), -1);
        }

        return false;
    }

    /**
     *
     */
    private function handleBasic($syntax) {
        preg_match('/~~REFNOTES(.*?)~~/', $syntax, $match);

        return array('render', $this->parseAttributes($match[1]));
    }

    /**
     *
     */
    private function handleExtended($syntax) {
        preg_match('/<refnotes(.*?)(?:\/>|>(.*?)<\/refnotes>)/s', $syntax, $match);
        $attribute = $this->parseAttributes($match[1]);
        $style = array();

        if ($match[2] != '') {
            $style = $this->parseStyles($match[2]);
        }

        if (count($style) > 0) {
            return array('split', $attribute, $style);
        }
        else {
            return array('render', $attribute);
        }
    }

    /**
     *
     */
    private function parseAttributes($syntax) {
        $propertyMatch = array(
            'ns' => '/^' . refnotes_namespace::getNamePattern('required') . '$/',
            'limit' => '/^\/?\d+$/'
        );

        $attribute = array();
        $token = preg_split('/\s+/', $syntax, -1, PREG_SPLIT_NO_EMPTY);
        foreach ($token as $t) {
            foreach ($propertyMatch as $name => $pattern) {
                if (preg_match($pattern, $t) == 1) {
                    $attribute[$name][] = $t;
                    break;
                }
            }
        }

        if (array_key_exists('ns', $attribute)) {
            /* Ensure that namespaces are in canonic form */
            $attribute['ns'] = array_map('refnotes_namespace::canonizeName', $attribute['ns']);

            if (count($attribute['ns']) > 1) {
                $attribute['map'] = array_slice($attribute['ns'], 1);
            }

            $attribute['ns'] = $attribute['ns'][0];
        }
        else {
            $attribute['ns'] = ':';
        }

        if (array_key_exists('limit', $attribute)) {
            $attribute['limit'] = end($attribute['limit']);
        }

        return $attribute;
    }

    /**
     *
     */
    private function parseStyles($syntax) {
        $style = array();
        preg_match_all('/([-\w]+)\s*:[ \t]*([^\s\n;].*?)[ \t]*?(?:[\n;]|$)/', $syntax, $match, PREG_SET_ORDER);
        foreach ($match as $m) {
            $style[$m[1]] = $m[2];
        }

        /* Validate direct-to-html styles */
        if (array_key_exists('notes-separator', $style)) {
            if (preg_match('/(?:\d+\.?|\d*\.\d+)(?:%|em|px)|none/', $style['notes-separator'], $match) == 1) {
                $style['notes-separator'] = $match[0];
            }
            else {
                $style['notes-separator'] = '';
            }
        }

        /* Ensure that namespaces are in canonic form */
        if (array_key_exists('inherit', $style)) {
            $style['inherit'] = refnotes_namespace::canonizeName($style['inherit']);
        }

        return $style;
    }

    /**
     *
     */
    private function renderNotes($mode, $renderer, $attribute) {
        $limit = array_key_exists('limit', $attribute) ? $attribute['limit'] : '';
        $doc = refnotes_renderer_core::getInstance()->renderNotes($mode, $attribute['ns'], $limit);

        if ($doc != '') {
            if ($mode == 'xhtml') {
                $open = '<div class="refnotes">' . DOKU_LF;
                $close = '</div>' . DOKU_LF;
            }
            else {
                $open = '';
                $close = '';
            }

            $renderer->doc .= $open;
            $renderer->doc .= $doc;
            $renderer->doc .= $close;
        }
    }
}