1<?php
2/**
3 * DataTables plugin: Add DataTables support to DokuWiki
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
7 * @copyright  (C) 2015-2016, Giuseppe Di Terlizzi
8 */
9class syntax_plugin_datatables extends DokuWiki_Syntax_Plugin
10{
11
12    /** @inheritdoc */
13    public function getType()
14    {
15        return 'container';
16    }
17
18    /** @inheritdoc */
19    public function getAllowedTypes()
20    {
21        return ['container', 'substition'];
22    }
23
24    /** @inheritdoc */
25    public function getPType()
26    {
27        return 'block';
28    }
29
30    /** @inheritdoc */
31    public function getSort()
32    {
33        return 195;
34    }
35
36    /** @inheritdoc */
37    public function connectTo($mode)
38    {
39        $this->Lexer->addEntryPattern(
40            '<(?:DATATABLES?|datatables?)\b.*?>(?=.*?</(?:DATATABLES?|datatables?)>)',
41            $mode,
42            'plugin_datatables'
43        );
44    }
45
46    /** @inheritdoc */
47    public function postConnect()
48    {
49        $this->Lexer->addExitPattern('</(?:DATATABLES?|datatables?)>', 'plugin_datatables');
50    }
51
52    /** @inheritdoc */
53    public function handle($match, $state, $pos, Doku_Handler $handler)
54    {
55
56        switch ($state) {
57            case DOKU_LEXER_UNMATCHED:
58            case DOKU_LEXER_EXIT:
59            case DOKU_LEXER_ENTER:
60                return [$state, $match];
61        }
62
63        return [];
64    }
65
66    /** @inheritdoc */
67    public function render($mode, Doku_Renderer $renderer, $data)
68    {
69
70        if (empty($data)) return false;
71        if ($mode !== 'xhtml') return false;
72
73        /** @var Doku_Renderer_xhtml $renderer */
74
75        list($state, $match) = $data;
76
77        switch ($state) {
78
79            case DOKU_LEXER_ENTER:
80
81                $html5_data = array();
82                $xml = @simplexml_load_string(str_replace('>', '/>', $match));
83
84                if (!is_object($xml)) {
85                    $xml = simplexml_load_string('<foo />');
86
87                    global $ACT;
88                    if ($ACT == 'preview') {
89                        msg(
90                            sprintf(
91                                '<strong>DataTable Plugin</strong> - Malformed tag (<code>%s</code>).' .
92                                ' Please check your code!',
93                                hsc($match)
94                            ),
95                            -1
96                        );
97                    }
98                }
99
100                foreach ($xml->attributes() as $key => $value) {
101                    $html5_data[] = sprintf("data-%s='%s'", $key, str_replace("'", "&apos;", (string)$value));
102                }
103
104                $renderer->doc .= sprintf('<div class="dt-wrapper" %s>', implode(' ', $html5_data));
105                return true;
106
107            case DOKU_LEXER_EXIT:
108                $renderer->doc .= '</div>';
109                return true;
110
111            case DOKU_LEXER_UNMATCHED:
112                $renderer->doc .= $match;
113                return true;
114        }
115
116        // should never be reached
117        return false;
118    }
119
120}
121