1<?php
2
3if (!defined('DOKU_INC')) die();
4
5include_once 'PlantUmlDiagram.php';
6
7class syntax_plugin_plantumlparser_injector extends DokuWiki_Syntax_Plugin {
8    private $TAG = 'uml';
9
10    /**
11     * @return string Syntax mode type
12     */
13    public function getType() {
14        return 'substition';
15    }
16    /**
17     * @return int Sort order - Low numbers go before high numbers
18     */
19    public function getSort() {
20        return 199; // In case we are operating in a Dokuwiki that has the other PlantUML plugin we want to beat it.
21    }
22
23    /**
24     * Connect lookup pattern to lexer.
25     *
26     * @param string $mode Parser mode
27     */
28    public function connectTo($mode) {
29        $this->Lexer->addSpecialPattern('<'.$this->TAG.'>\n*.*?\n*</'.$this->TAG.'>',$mode,'plugin_plantumlparser_injector');
30    }
31
32    /**
33     * Handle matches of the plantumlparser syntax
34     *
35     * @param string          $match   The match of the syntax
36     * @param int             $state   The state of the handler
37     * @param int             $pos     The position in the document
38     * @param Doku_Handler    $handler The handler
39     * @return array Data for the renderer
40     */
41    public function handle($match, $state, $pos, Doku_Handler $handler)
42    {
43        $markup        = str_replace('</' . $this->TAG . '>', '', str_replace('<' . $this->TAG . '>', '', $match));
44        $plantUmlUrl   = trim($this->getConf('PlantUMLURL'));
45		if(!$plantUmlUrl)
46		{
47			$plantUmlUrl = "https://www.plantuml.com/plantuml/";
48		}
49		else
50		{
51			$plantUmlUrl = trim($plantUmlUrl, '/') . '/';
52		}
53        $diagramObject = new PlantUmlDiagram($markup,$plantUmlUrl);
54
55        return [
56            'svg' => strstr($diagramObject->getSVG(), "<svg"),
57            'markup' => $diagramObject->getMarkup(),
58            'id' => sha1($diagramObject->getSVGDiagramUrl()),
59            'include_links' => $this->getConf('DefaultShowLinks'),
60            'url' => [
61                'svg' => $diagramObject->getSVGDiagramUrl(),
62                'png' => $diagramObject->getPNGDiagramUrl(),
63                'txt' => $diagramObject->getTXTDiagramUrl(),
64            ],
65        ];
66    }
67
68    /**
69     * Render xhtml output or metadata
70     *
71     * @param string         $mode      Renderer mode (supported modes: xhtml)
72     * @param Doku_Renderer  $renderer  The renderer
73     * @param array          $data      The data from the handler() function
74     * @return bool If rendering was successful.
75     */
76    public function render($mode, Doku_Renderer $renderer, $data) {
77        if($mode != 'xhtml') return false;
78
79        $renderer->doc .= "<div id='plant-uml-diagram-".$data['id']."'>";
80        if(strlen($data['svg']) > 0) {
81			if(is_a($renderer,'renderer_plugin_dw2pdf') && (preg_match("/(@startlatex|@startmath|<math|<latex)/", $data['markup']))){
82				$renderer->doc .= "<img src='".$data['url']['png']."'>";
83			}
84			else {
85				$renderer->doc .= $data['svg'];
86			}
87        } else {
88            $renderer->doc .= "<object data='".$data['url']['svg']."' type='image/svg+xml'>";
89            $renderer->doc .= "<span>".$data['markup']."</span>";
90            $renderer->doc .= "</object>";
91        }
92        if($data['include_links'] == "1") {
93            $renderer->doc .= "<div id=\"plantumlparse_link_section\">";
94            $renderer->doc .= "<a target='_blank' href='".$data['url']['svg']."'>SVG</a> | ";
95            $renderer->doc .= "<a target='_blank' href='".$data['url']['png']."'>PNG</a> | ";
96            $renderer->doc .= "<a target='_blank' href='".$data['url']['txt']."'>TXT</a>";
97            $renderer->doc .= "</div>";
98        }
99
100        $renderer->doc .= "</div>";
101
102        return true;
103    }
104}
105