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
14    protected $codeBlocks = [];
15    protected $lastHeader = '';
16
17    /** @inheritDoc */
18    public function getFormat()
19    {
20        return 'dbquery';
21    }
22
23    /** @inheritDoc */
24    public function header($text, $level, $pos)
25    {
26        $this->lastHeader = $text;
27    }
28
29    /** @inheritDoc */
30    public function code($text, $lang = null, $file = null)
31    {
32        if (!isset($this->codeBlocks['_'])) {
33            // first code block is always the SQL query
34            $this->codeBlocks['_'] = trim($text);
35        } else {
36            // all other code blocks are treated as HTML named by their header
37            $this->codeBlocks[$this->lastHeader] = trim($text);
38        }
39    }
40
41    /** @inheritdoc */
42    public function document_start()
43    {
44        parent::document_start();
45        $this->info['dbquery']['transpose'] = false;
46    }
47
48    /** @inheritDoc */
49    public function document_end()
50    {
51        $this->doc = json_encode([
52            'codeblocks' => $this->codeBlocks,
53            'macros' => $this->info['dbquery'],
54        ]);
55    }
56
57}
58