xref: /plugin/mizarverifiabledocs/syntax.php (revision 1197729184122a30475a0859b897e87e87d1d70d)
1<?php
2/**
3 * DokuWiki Plugin Mizar Verifiable Docs (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Yamada, M. <yamadam@mizar.work>
7 */
8class syntax_plugin_mizarverifiabledocs extends \dokuwiki\Extension\SyntaxPlugin {
9    /** @inheritDoc */
10    public function getType() {
11        return 'substition';
12    }
13
14    /** @inheritDoc */
15    public function getPType() {
16        return 'block';
17    }
18
19    /** @inheritDoc */
20    public function getSort() {
21        return 195;
22    }
23
24    /** @inheritDoc */
25    public function connectTo($mode) {
26        $this->Lexer->addSpecialPattern('<mizar\s+[^>]+>.*?</mizar>', $mode, 'plugin_mizarverifiabledocs');
27    }
28
29    public function handle($match, $state, $pos, Doku_Handler $handler) {
30        preg_match('/<mizar\s+([^>]+)>(.*?)<\/mizar>/s', $match, $matches);
31        $filename = htmlspecialchars(trim($matches[1]));
32        $content  = htmlspecialchars(trim($matches[2]));
33        return array($state, $filename, $content);
34    }
35
36    public function render($mode, Doku_Renderer $renderer, $data) {
37        // xhtml以外のモードはスキップ
38        if ($mode != 'xhtml') {
39            return false;
40        }
41        static $mizarCounter = 0; // 一意のカウンターを追加
42        list($state,$filename, $content) = $data;
43        $mizarId = 'mizarBlock' . $mizarCounter;
44        $blockNumber = $mizarCounter + 1;        // 表示用の番号 (1, 2, 3,...)
45        $mizarCounter++; // カウンターをインクリメント
46
47        if ($mode == 'xhtml') {
48            // ボタンやエディタのHTMLを生成
49            $renderer->doc .= '<div class="mizarWrapper" id="' . $mizarId . '">';
50            $renderer->doc .= '<dl class="file">';
51            $renderer->doc .= '<button class="copy-button" data-mizarid="' . $mizarId . '">Copy</button>';
52            $renderer->doc .= '<button id="resetButton' . $mizarId . '" class="reset-button">Reset</button>';
53            $renderer->doc .= '<button id="editButton' . $mizarId . '" class="edit-button">Edit</button>';
54            $renderer->doc .= '<button id="compileButton' . $mizarId . '" class="compile-button">Compile</button>';
55            $renderer->doc .= '<button id="hideButton' . $mizarId . '" class="hide-button">Hide</button>';
56            $renderer->doc .= '<button id="showButton' . $mizarId . '" class="show-button">Show</button>';
57
58            $renderer->doc .= '<dt>'
59                . '<a href="#" onclick="createMizarFile(\'' . $filename . '\'); return false;" '
60                . ' title="クリックしてコンテンツをダウンロード" class="file-download">'
61                . $filename. '(' . $blockNumber . ') '
62                . '</a>'
63                . '</dt>';
64
65            $renderer->doc .= '<dd><div class="editor-container" data-content="' . htmlspecialchars($content) . '"></div></dd>';
66            $renderer->doc .= '</dl>';
67            $renderer->doc .= '<div id="output' . $mizarId . '" class="output"></div>';
68            $renderer->doc .= '<script type="module" src="' . DOKU_BASE . 'lib/plugins/mizarverifiabledocs/dist/script.js"></script>';
69            $renderer->doc .= '</div>';
70        } else {
71            $renderer->doc .= "<mizar $filename>$content</mizar>";
72        }
73        return true;
74    }
75}