1<?php
2/**
3 * DokuWiki Plugin infobox (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 */
7if (!defined('DOKU_INC')) die();
8
9class syntax_plugin_infobox extends DokuWiki_Syntax_Plugin {
10    public function getType() {
11        return 'substition';
12    }
13
14    public function getPType() {
15        return 'block';
16    }
17
18    public function getSort() {
19        return 199;
20    }
21
22    public function connectTo($mode) {
23        $this->Lexer->addSpecialPattern('\{\{infobox>.*?\}\}', $mode, 'plugin_infobox');
24    }
25
26    public function handle($match, $state, $pos, Doku_Handler $handler) {
27        $data = substr($match, 10, -2);
28        $lines = explode("\n", $data);
29        $params = [];
30        foreach ($lines as $line) {
31            $line = trim($line);
32            if (strpos($line, '=') !== false) {
33                list($key, $value) = explode('=', $line, 2);
34                $params[trim($key)] = trim($value);
35            }
36        }
37        return $params;
38    }
39
40    public function render($mode, Doku_Renderer $renderer, $data) {
41        if ($mode != 'xhtml') return false;
42
43        $renderer->doc .= '<div class="infobox" style="border: 1px solid #aaa; background-color: #333; color: #eee; width: 300px; float: right; margin: 0 0 1em 1em; padding: 0.5em; font-size: 95%; line-height: 1.5em;">';
44
45        if (isset($data['name'])) {
46            $renderer->doc .= '<div class="infobox-title" style="font-size: 120%; font-weight: bold; margin-bottom: 0.5em; color: #fff;">' . $this->_parseWikiText($data['name']) . '</div>';
47        }
48
49        if (isset($data['image'])) {
50            $renderer->doc .= '<div class="infobox-image" style="text-align: center; margin-bottom: 0.5em;"><img src="' . ml($data['image']) . '" alt="' . hsc($data['name']) . '" style="max-width: 100%; height: auto;"></div>';
51        }
52
53        $renderer->doc .= '<table class="infobox-table" style="width: 100%; border-spacing: 0;">';
54        foreach ($data as $key => $value) {
55            if ($key != 'name' && $key != 'image') {
56                $renderer->doc .= '<tr><th style="text-align: left; vertical-align: top; padding-right: 0.5em; font-weight: bold; color: #fff;">' . hsc($key) . '</th><td style="vertical-align: top; color: #ccc;">' . $this->_parseWikiText($value) . '</td></tr>';
57            }
58        }
59        $renderer->doc .= '</table>';
60
61        $renderer->doc .= '</div>';
62        return true;
63    }
64
65    private function _parseWikiText($text) {
66        $info = array();
67        $xhtml = p_render('xhtml', p_get_instructions($text), $info);
68        return $xhtml;
69    }
70}