1<?php
2
3if(!defined('DOKU_INC')) die();
4
5require_once __DIR__.'/src/bootstrap.php';
6
7use DokuWiki\Plugin\Mdpage\Markdown;
8
9class syntax_plugin_mdpage extends DokuWiki_Syntax_Plugin {
10    protected $dokuwikiVersion = null;
11
12    private function getDokuWikiVersion() {
13        if ($this->dokuwikiVersion == null) {
14            $this->dokuwikiVersion = getVersionData()['date'];
15        }
16
17        return $this->dokuwikiVersion;
18    }
19
20    public function getType() {
21        return 'protected';
22    }
23
24    public function getPType() {
25        return 'block';
26    }
27
28    public function getSort() {
29        return 69;
30    }
31
32    public function getPluginName() {
33        return $this->getInfo()['base'];
34    }
35
36    public function getPluginMode() {
37        return 'plugin_' . $this->getPluginName();
38    }
39
40    public function connectTo($mode) {
41        if ($this->getConf('markdown_default')) {
42            $this->Lexer->addEntryPattern('\\A(?!.*</script>).', $mode, $this->getPluginMode());
43            $this->Lexer->addEntryPattern('<!DOCTYPE markdown>', $mode, $this->getPluginMode());
44            $this->Lexer->addEntryPattern('</script>', $mode, $this->getPluginMode());
45        } else {
46            $this->Lexer->addEntryPattern('<markdown>(?=.*</markdown>)', $mode, $this->getPluginMode());
47        }
48    }
49
50    public function postConnect() {
51        $pluginBaseMode = 'plugin_' . $this->getPluginName();
52
53        if ($this->getConf('markdown_default')) {
54            $this->Lexer->addExitPattern('\\z', $this->getPluginMode());
55            $this->Lexer->addExitPattern('<script type="text/x-dokuwiki">', $this->getPluginMode());
56        } else {
57            $this->Lexer->addExitPattern('</markdown>', $pluginBaseMode);
58        }
59    }
60
61    public function handle($match, $state, $pos, Doku_Handler $handler) {
62        switch ($state) {
63            case DOKU_LEXER_UNMATCHED:
64                $new_pos = $pos;
65                if ($this->getConf('markdown_default')) {
66                    if (preg_match('|^</script>|', $match)) {
67                        $new_pos = $new_pos - strlen('</script>');
68                    } else if (preg_match('|^<!DOCTYPE markdown>|', $match)) {
69                        $new_pos = $new_pos - strlen('<!DOCTYPE markdown>');
70                    }
71                } else {
72                    $new_pos = $new_pos - strlen('<markdown>');
73                }
74
75                return [
76                    'render' => true,
77                    'match' => $match,
78                    'pos' => $new_pos,
79                ];
80            default:
81                return [
82                    'render' => false,
83                ];
84        }
85    }
86
87    public function render($format, Doku_Renderer $renderer, $data) {
88        if (!$data['render']) {
89            return true;
90        }
91
92        $match = $data['match'];
93        return $this->renderWithRenderer($renderer, $match, $data);
94    }
95
96    protected function renderWithRenderer(Doku_Renderer $renderer, $match, $data) {
97        switch ($this->getConf('flavor')) {
98            case 'github-flavored':
99                $flavor = Markdown::GITHUB_FLAVORED;
100                break;
101            case 'markdown-extra':
102                $flavor = Markdown::MARKDOWN_EXTRA;
103                break;
104            case 'traditional':
105                $flavor = Markdown::TRADITIONAL;
106                break;
107            default:
108                $flavor = Markdown::GITHUB_FLAVORED;
109                break;
110        }
111
112        $context = [
113            'dokuwiki_version' => $this->dokuwikiVersion,
114            'flavor' => $flavor,
115        ];
116
117        $result = Markdown::parseWithRenderer($renderer, $match, $data, $context);
118        /*
119        echo '<pre>';
120        var_dump(htmlspecialchars($match));
121        var_dump(htmlspecialchars($result));
122        echo '</pre>';
123        */
124
125        return true;
126    }
127
128    protected function _debug($message, $err, $line, $file = __FILE__) {
129        if ($this->getConf('debug')) {
130            msg($message, $err, $line, $file);
131        }
132    }
133
134}
135