1<?php
2/**
3 * DokuWiki Plugin yuml (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Pavel Laupe Dvořák <pavel@laupe.me>
7 */
8
9if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
11
12class syntax_plugin_yuml_usecase extends DokuWiki_Syntax_Plugin
13{
14    protected $special_pattern = '<usecase.*?>.*?</usecase>';
15    protected $handle_pattern = '/<usecase(.*?)>(.*)<\/usecase>/is';
16    protected $yuml_type = 'usecase';
17
18    /**
19     * Get the type of syntax this plugin defines.
20     *
21     * @param none
22     * @return string Syntax mode type
23     */
24    public function getType()
25    {
26        return 'substition';
27    }
28
29    /**
30     * @return string Paragraph type
31     */
32    public function getPType()
33    {
34        return 'normal';
35    }
36
37    /**
38     * Where to sort in?
39     *
40     * @param none
41     * @return int Sort order - Low numbers go before high numbers
42     */
43    public function getSort()
44    {
45        return 999;
46    }
47
48    /**
49     * Connect lookup pattern to lexer.
50     *
51     * @param string $mode Parser mode
52     * @return void
53     */
54    public function connectTo($mode)
55    {
56        $this->Lexer->addSpecialPattern($this->special_pattern, $mode, 'plugin_yuml_' . $this->getPluginComponent());
57    }
58
59    /**
60     * Handle matches of the yuml syntax
61     *
62     * @param string $match The match of the syntax
63     * @param int $state The state of the handler
64     * @param int $pos The position in the document
65     * @param Doku_Handler $handler The handler
66     * @return array Data for the renderer
67     */
68    public function handle($match, $state, $pos, Doku_Handler $handler)
69    {
70        if ($state != DOKU_LEXER_SPECIAL) return array();
71
72        // Look for style
73        $result = array();
74        preg_match($this->handle_pattern, $match, $result);
75
76        $style = $result[1];
77        $match = $result[2];
78
79        return array($state, $match, $style);
80    }
81
82    /**
83     * Render xhtml output or metadata
84     *
85     * @param string $mode Renderer mode (supported modes: xhtml)
86     * @param Doku_Renderer $renderer The renderer
87     * @param array $data The data from the handler() function
88     * @return bool If rendering was successful.
89     */
90    public function render($mode, Doku_Renderer $renderer, $data)
91    {
92        if ($mode != 'xhtml') return false;
93
94        list($state, $match, $style) = $data;
95
96        if ($state == DOKU_LEXER_SPECIAL) {
97            $renderer->doc .= $this->getYumlIMG($this->yuml_type, $match, $style);
98        }
99
100        return true;
101    }
102
103    public function getYumlIMG($type, $uml_code, $style = null)
104    {
105        if ($style == null) {
106            $style = "plain";
107        }
108        $uml_code = preg_replace(array("/\n/", "/,,/"), array(", ", ","), trim($uml_code));
109        $output = '<img src="https://yuml.me/diagram/' . trim($style) . '/' . $type . '/';
110        return $output . htmlspecialchars($uml_code) . '"/>';
111    }
112}