xref: /plugin/dbquery/renderer.php (revision 9730265cac55f032a41e08c80cc41610b870373b)
1<?php
2
3/**
4 * DokuWiki Plugin dbquery (Renderer Component)
5 *
6 * Extracts code blocks from pages and returns them as JSON
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
10 */
11class renderer_plugin_dbquery extends \Doku_Renderer
12{
13    protected $codeBlocks = [];
14    protected $lastHeader = '';
15
16    /** @inheritDoc */
17    public function getFormat()
18    {
19        return 'dbquery';
20    }
21
22    /** @inheritDoc */
23    public function header($text, $level, $pos)
24    {
25        $this->lastHeader = $text;
26    }
27
28    /** @inheritDoc */
29    public function code($text, $lang = null, $file = null)
30    {
31        if (!isset($this->codeBlocks['_'])) {
32            // first code block is always the SQL query
33            $this->codeBlocks['_'] = trim($text);
34        } else {
35            // all other code blocks are treated as HTML named by their header
36            $this->codeBlocks[$this->lastHeader] = trim($text);
37        }
38    }
39
40    /** @inheritdoc */
41    public function document_start()
42    {
43        parent::document_start();
44        $this->info['dbquery']['transpose'] = false;
45    }
46
47    /** @inheritDoc */
48    public function document_end()
49    {
50        $this->doc = json_encode([
51            'codeblocks' => $this->codeBlocks,
52            'macros' => $this->info['dbquery'],
53        ]);
54    }
55}
56