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