1<?php
2/**
3 * DokuWiki Plugin tablelayout (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14/** @noinspection AutoloadingIssuesInspection */
15class syntax_plugin_tablelayout extends DokuWiki_Syntax_Plugin
16{
17
18    const SYNTAX_PATTERN = '{{tablelayout\?[^\n]*?}}(?=\s*?\n[|^])';
19
20    /**
21     * @return string Syntax mode type
22     */
23    public function getType()
24    {
25        return 'container';
26    }
27
28    /**
29     * @return string Paragraph type
30     */
31    public function getPType()
32    {
33        return 'block';
34    }
35
36    /**
37     * @return int Sort order - Low numbers go before high numbers
38     */
39    public function getSort()
40    {
41        return 20;
42    }
43
44    /**
45     * Connect lookup pattern to lexer.
46     *
47     * @param string $mode Parser mode
48     */
49    public function connectTo($mode)
50    {
51        $this->Lexer->addSpecialPattern(self::SYNTAX_PATTERN, $mode, 'plugin_tablelayout');
52    }
53
54    /**
55     * Handle matches of the tablelayout syntax
56     *
57     * @param string $match The match of the syntax
58     * @param int $state The state of the handler
59     * @param int $pos The position in the document
60     * @param Doku_Handler $handler The handler
61     *
62     * @return array Data for the renderer
63     */
64    public function handle($match, $state, $pos, Doku_Handler $handler)
65    {
66
67        $prefixLength = strlen('{{tablelayout?');
68        $optionsString = substr($match, $prefixLength, -1 * strlen('}}'));
69        $options = explode('&', $optionsString);
70        $options = array_filter($options);
71        $data = array();
72        if (empty($options)) {
73            return $data;
74        }
75        foreach ($options as $option) {
76            list($key, $value) = explode('=', $option);
77            switch ($key) {
78                case 'rowsFixed': // for backwards compatibility
79                    $data['rowsHeaderSource'] = $value;
80                    break;
81                case 'float':
82                case 'rowsVisible':
83                case 'rowsHeaderSource':
84                    $data[$key] = $value;
85                    break;
86                case 'colwidth':
87                    $value = array_map('trim', explode(',', trim($value, '"\'')));
88                    $data[$key] = $value;
89                    break;
90                case 'tableSort':
91                case 'tableSearch':
92                case 'tablePrint':
93                    $data[$key] = !empty($value);
94                    break;
95                default:
96                    msg('Unknown option: ' . hsc($key), -1);
97            }
98        }
99        if (empty($data)) {
100            return $data;
101        }
102
103        return $data;
104    }
105
106    /**
107     * Render xhtml output or metadata
108     *
109     * @param string $mode Renderer mode (supported modes: xhtml)
110     * @param Doku_Renderer $renderer The renderer
111     * @param array $data The data from the handler() function
112     *
113     * @return bool If rendering was successful.
114     */
115    public function render($mode, Doku_Renderer $renderer, $data)
116    {
117        if (empty($data)) {
118            return false;
119        }
120        if ($mode !== 'xhtml') {
121            return false;
122        }
123
124        $json = hsc(json_encode($data));
125        $renderer->doc .= "<div class='plugin_tablelayout_placeholder' data-tablelayout=\"$json\"></div>";
126
127        return true;
128    }
129}
130
131// vim:ts=4:sw=4:et:
132