-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
156 lines (146 loc) · 6.04 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Plugin MicroAlg: Embed MicroAlg interactive snippets.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Prof Gragnic <profgra.org@gmail.com>
*/
// must be run within DokuWiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
define(MALG_TAG, "MicroAlg");
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_microalg extends DokuWiki_Syntax_Plugin {
function getType(){
return 'protected';
}
function getPType(){
return 'block';
}
function getSort(){
return 157;
}
function connectTo($mode) {
$this->Lexer->addEntryPattern('\(' . MALG_TAG . '.*?\)$(?=.*?\(/' . MALG_TAG . '\))',$mode,'plugin_microalg');
}
function postConnect() {
$this->Lexer->addExitPattern('\(/' . MALG_TAG . '\)','plugin_microalg');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler){
switch ($state) {
case DOKU_LEXER_ENTER :
return array($state, $match);
case DOKU_LEXER_UNMATCHED :
return array($state, $match);
case DOKU_LEXER_EXIT :
return array($state, '');
}
return array();
}
/**
* Create output
*/
function render($mode, &$renderer, $data) {
if($mode == 'xhtml'){
list($state, $match) = $data;
switch ($state) {
case DOKU_LEXER_ENTER :
list($div_id, $conf) = $this->_malg_parse_tag($match);
$error_msg = "";
// Check the div_id :
if ($div_id == "")
$error_msg = "Il manque l’identifiant du programme.";
if (!preg_match('/^[_a-zA-Z]+[_a-zA-Z0-9-]*$/', $div_id))
$error_msg = "L’identifiant ne doit contenir que des lettres, des chiffres et des tirets";
if (preg_match('/\s/', $div_id))
$error_msg = "L’identifiant ne doit pas contenir d’espace.";
// Check the conf :
$parsed_conf = json_decode($conf);
if ($conf != "" && $parsed_conf == NULL)
$error_msg = "La configuration est mal formée. Elle doit être au format JSON.";
// Display error messages and raise the error flag for UNMATCHED and EXIT :
if ($error_msg != "") {
$renderer->error = true;
$renderer->doc .= "<div class=\"error\">";
$renderer->doc .= "Vous voulez insérer du code MicroAlg ?<br>\n";
$renderer->doc .= $error_msg . "<br>\n";
$renderer->doc .= "Merci de taper:\n";
$renderer->doc .= "<pre>(MicroAlg \"identifiant_du_prg\" {configuration facultative au format JSON})\n";
$renderer->doc .= "(... ici votre programme ...)\n";
$renderer->doc .= "(/MicroAlg)</pre>\n";
$renderer->doc .= "</div>\n";
} else {
$renderer->doc .= '<div id="' . $div_id . '"></div>' . "\n";
$renderer->doc .= '<script>inject_microalg_editor_in("' . $div_id . '", {' . "\n";
// 2 and -2 to remove "{ and }"
$encoded = json_encode($conf);
$patterns = array('\\u0394');
$substs = array('Δ');
$semi_encoded = str_replace($patterns, $substs, $encoded);
$json_injection = stripslashes(substr($semi_encoded, 2, -2));
if ($json_injection) {
$renderer->doc .= $json_injection . ",\n";
}
$renderer->doc .= "src:\n";
}
break;
case DOKU_LEXER_UNMATCHED :
if (! $renderer->error)
$renderer->doc .= $this->_malg_escape($match);
break;
case DOKU_LEXER_EXIT :
if (! $renderer->error)
$renderer->doc .= "''});</script>" . "\n";
break;
}
return true;
}
if($mode == 'microalg') {
list($state, $match) = $data;
switch ($state) {
case DOKU_LEXER_ENTER :
// Entry tag is like (MALG_TAG "div_id")
list($div_id, $conf_not_used) = $this->_malg_parse_tag($match);
$renderer->doc .= "\n# Programme " . $div_id;
break;
case DOKU_LEXER_UNMATCHED :
$renderer->doc .= $match;
break;
case DOKU_LEXER_EXIT :
// nothing
break;
}
return true;
}
return false;
}
function _malg_parse_tag($match) {
// Entry tag is like (MALG_TAG "div_id" {…optional conf as JSON…})
$content = substr($match, strlen('(' . MALG_TAG . ' '), -(strlen(')')));
// Grab the div_id :
$first_quote = strpos($content, '"');
$second_quote = strpos($content, '"', $first_quote + 1);
$div_id = substr($content, $first_quote + 1, $second_quote - $first_quote - 1);
// Grab the optional JSON :
$conf = trim(substr($content, $second_quote + 1));
return array($div_id, $conf);
}
function _malg_escape($src) {
$result = '';
foreach (explode("\n", trim($src, "\n")) as $line) {
$line = str_replace('"', '\"', $line);
$line = str_replace('//', '', $line);
$line = str_replace('/*', '', $line);
$result .= ('"' . $line . '\n" +' . "\n");
}
return $result;
}
}
?>