1<?php
2
3
4namespace ComboStrap;
5
6
7class Mermaid
8{
9
10
11    const CANONICAL = "mermaid";
12    public const CLASS_NAME = "mermaid";
13
14    public static function addSnippet()
15    {
16        $snippetManager = PluginUtility::getSnippetManager();
17        $snippetId = \syntax_plugin_combo_mermaid::TAG;
18        $snippetManager->attachInternalJavascriptForSlot($snippetId);
19        $snippetManager->attachJavascriptLibraryForSlot(
20            $snippetId,
21            "https://cdn.jsdelivr.net/npm/mermaid@8.12.1/dist/mermaid.min.js",
22            "sha256-51Oz+q3qIYwzBL0k7JLyk158Ye4XqprPU0/9DUcZMQQ="
23        );
24    }
25
26    /**
27     * The enter tag
28     * @param $callStackAttributes
29     * @return string
30     */
31    public static function enter($callStackAttributes): string
32    {
33        /**
34         * This code is replaced at runtime by the diagram
35         */
36        $tagAttributes = TagAttributes::createFromCallStackArray($callStackAttributes);
37        $tagAttributes->addClassName(Mermaid::CLASS_NAME);
38        return $tagAttributes->toHtmlEnterTag("div");
39    }
40
41    /**
42     * The closing tag
43     * @return string
44     */
45    public static function close(): string
46    {
47        return "</div>";
48    }
49
50    /**
51     * The content cannot be HTML escaped
52     *
53     * because
54     * `->>` would become `→&gt;`
55     * or  <br/> would not work
56     *
57     * There is a parameter
58     * @param $content
59     * @return mixed
60     */
61    public static function sanitize($content)
62    {
63
64        return Sanitizer::sanitize($content, " in a mermaid language", self::CANONICAL);
65
66    }
67
68    public static function render($data, &$renderer)
69    {
70        $state = $data [PluginUtility::STATE];
71        switch ($state) {
72            case DOKU_LEXER_ENTER :
73                Mermaid::addSnippet();
74                $renderer->doc .= Mermaid::enter($data[PluginUtility::ATTRIBUTES]);
75                break;
76
77            case DOKU_LEXER_UNMATCHED :
78
79                $renderer->doc .= Mermaid::sanitize($data[PluginUtility::PAYLOAD]);
80                break;
81
82            case DOKU_LEXER_EXIT :
83                $renderer->doc .= Mermaid::close();
84                break;
85
86        }
87    }
88
89    public static function handle($state, $match, &$handler): array
90    {
91        switch ($state) {
92
93            case DOKU_LEXER_ENTER :
94                $tagAttributes = TagAttributes::createFromTagMatch($match);
95                return array(
96                    PluginUtility::STATE => $state,
97                    PluginUtility::ATTRIBUTES => $tagAttributes->toCallStackArray()
98                );
99
100            case DOKU_LEXER_UNMATCHED :
101
102                return PluginUtility::handleAndReturnUnmatchedData("", $match, $handler);
103
104
105            case DOKU_LEXER_EXIT :
106                return array(
107                    PluginUtility::STATE => $state
108                );
109
110
111        }
112        return array();
113    }
114
115
116}
117