xref: /plugin/struct/syntax/table.php (revision 16b7d914c0b69bf27d1ddd8a08e05beddbeedf02)
1<?php
2/**
3 * DokuWiki Plugin struct (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9
10use plugin\struct\meta\AggregationTable;
11use plugin\struct\meta\ConfigParser;
12use plugin\struct\meta\SearchConfig;
13use plugin\struct\meta\StructException;
14
15// must be run within Dokuwiki
16if (!defined('DOKU_INC')) die();
17
18class syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin {
19    /**
20     * @return string Syntax mode type
21     */
22    public function getType() {
23        return 'substition';
24    }
25    /**
26     * @return string Paragraph type
27     */
28    public function getPType() {
29        return 'block';
30    }
31    /**
32     * @return int Sort order - Low numbers go before high numbers
33     */
34    public function getSort() {
35        return 155;
36    }
37
38    /**
39     * Connect lookup pattern to lexer.
40     *
41     * @param string $mode Parser mode
42     */
43    public function connectTo($mode) {
44        $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table');
45    }
46
47
48    /**
49     * Handle matches of the struct syntax
50     *
51     * @param string $match The match of the syntax
52     * @param int    $state The state of the handler
53     * @param int    $pos The position in the document
54     * @param Doku_Handler    $handler The handler
55     * @return array Data for the renderer
56     */
57    public function handle($match, $state, $pos, Doku_Handler $handler){
58
59        $lines = explode("\n", $match);
60        array_shift($lines);
61        array_pop($lines);
62
63        try {
64            $parser = new ConfigParser($lines);
65            return  $parser->getConfig();
66        } catch (StructException $e) {
67            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
68            return null;
69        }
70    }
71
72    /**
73     * Render xhtml output or metadata
74     *
75     * @param string         $mode      Renderer mode (supported modes: xhtml)
76     * @param Doku_Renderer  $renderer  The renderer
77     * @param array          $data      The data from the handler() function
78     * @return bool If rendering was successful.
79     */
80    public function render($mode, Doku_Renderer $renderer, $data) {
81        if(!$data) return false;
82        global $ID;
83
84        try {
85            $search = new SearchConfig($data);
86            $table = new AggregationTable($ID, $mode, $renderer, $search);
87            $table->render();
88
89            if($mode == 'metadata') {
90                /** @var Doku_Renderer_metadata $renderer  */
91                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
92            }
93
94        } catch (StructException $e) {
95            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
96        }
97
98        return true;
99    }
100}
101
102// vim:ts=4:sw=4:et:
103