-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaction.php
More file actions
374 lines (317 loc) · 11.3 KB
/
action.php
File metadata and controls
374 lines (317 loc) · 11.3 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Plugin QnA: Layout parser
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Mykola Ostrovskyy <dwpforge@gmail.com>
*/
class action_plugin_qna extends DokuWiki_Action_Plugin {
const STATE_CLOSED = 0;
const STATE_QUESTION = 1;
const STATE_ANSWER = 2;
private $rewriter;
private $blockState;
private $headerIndex;
private $headerTitle;
private $headerLevel;
private $headerId;
private $headerCheck;
/**
* Register callbacks
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'beforeDokuwikiStarted');
$controller->register_hook('PARSER_HANDLER_DONE', 'AFTER', $this, 'afterParserHandlerDone');
$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'beforeParserCacheUse');
}
/**
* Prepare plugin stylesheet file
*/
public function beforeDokuwikiStarted($event) {
$fromConf = dirname(__FILE__) . '/style/' . $this->getConf('style') . '.less';
$inUse = dirname(__FILE__) . '/all.less';
if (!@file_exists($inUse) || @filesize($inUse) != @filesize($fromConf)) {
@copy($fromConf, $inUse);
}
}
/**
*
*/
public function afterParserHandlerDone($event, $param) {
$this->reset();
$this->fixLayout($event);
}
/**
* Reset internal state
*/
private function reset() {
$this->rewriter = new qna_instruction_rewriter();
$this->blockState = self::STATE_CLOSED;
$this->headerIndex = -1;
$this->headerTitle = '';
$this->headerLevel = 0;
$this->headerId = '';
$this->headerCheck = array();
}
/**
* Insert implicit instructions
*/
private function fixLayout($event) {
$instructions = count($event->data->calls);
for ($i = 0; $i < $instructions; $i++) {
$instruction = $event->data->calls[$i];
switch ($instruction[0]) {
case 'header':
$this->headerIndex = $i;
$this->headerTitle = $instruction[1][0];
$this->headerLevel = $instruction[1][1];
$this->headerId = sectionID($instruction[1][0], $this->headerCheck);
/* Fall through */
case 'section_close':
case 'section_edit':
case 'section_open':
if ($this->blockState != self::STATE_CLOSED) {
$this->rewriter->insertBlockCall($i, 'close_block', 2);
$this->blockState = self::STATE_CLOSED;
}
break;
case 'plugin':
switch ($instruction[1][0]) {
case 'qna_block':
$this->handlePluginQnaBlock($i, $instruction[1][1]);
break;
case 'qna_header':
$this->handlePluginQnaHeader($i);
break;
}
break;
}
}
if ($this->blockState != self::STATE_CLOSED) {
$this->rewriter->appendBlockCall('close_block', 2);
}
$this->rewriter->apply($event->data->calls);
}
/**
* Insert implicit instructions
*/
private function handlePluginQnaBlock($index, $data) {
switch ($data[0]) {
case 'open_question':
if ($this->blockState != self::STATE_CLOSED) {
$this->rewriter->insertBlockCall($index, 'close_block', 2);
}
$this->rewriter->insertBlockCall($index, 'open_block');
$this->rewriter->setQuestionLevel($index, $this->headerLevel + 1);
$this->blockState = self::STATE_QUESTION;
break;
case 'open_answer':
switch ($this->blockState) {
case self::STATE_CLOSED:
$this->rewriter->delete($index);
break;
case self::STATE_QUESTION:
case self::STATE_ANSWER:
$this->rewriter->insertBlockCall($index, 'close_block');
$this->blockState = self::STATE_ANSWER;
break;
}
break;
case 'close_block':
switch ($this->blockState) {
case self::STATE_CLOSED:
$this->rewriter->delete($index);
break;
case self::STATE_QUESTION:
case self::STATE_ANSWER:
$this->rewriter->insertBlockCall($index, 'close_block');
$this->blockState = self::STATE_CLOSED;
break;
}
break;
}
}
/**
* Wrap the last header
*/
private function handlePluginQnaHeader($index) {
/* On a clean install the distance between the header instruction and qna_header dummy
sould be 2 (one section_open in between). Allowing distance to be in the range from
1 to 3 gives some flexibility for better compatibility with other plugins that might
rearrange instructions around the header. */
if (($index - $this->headerIndex) < 4) {
$data[0] ='open';
$data[1] = $this->headerTitle;
$data[2] = $this->headerId;
$data[3] = $this->headerLevel;
$this->rewriter->insertHeaderCall($this->headerIndex, $data);
$this->rewriter->insertHeaderCall($this->headerIndex + 1, 'close');
}
$this->rewriter->delete($index);
}
/**
*
*/
public function beforeParserCacheUse($event, $param) {
global $ID;
$cache = $event->data;
if (isset($cache->page) && ($cache->page == $ID)) {
if (isset($cache->mode) && ($cache->mode == 'xhtml')) {
$depends = p_get_metadata($ID, 'relation depends');
if (!empty($depends) && isset($depends['rendering'])) {
$this->addDependencies($cache, array_keys($depends['rendering']));
}
}
}
}
/**
* Add extra dependencies to the cache
*/
private function addDependencies($cache, $depends) {
foreach ($depends as $file) {
if (!in_array($file, $cache->depends['files']) && file_exists($file)) {
$cache->depends['files'][] = $file;
}
}
}
}
class qna_instruction_rewriter {
const DELETE = 1;
const INSERT = 2;
const SET_LEVEL = 3;
private $correction;
/**
* Constructor
*/
public function __construct() {
$this->correction = array();
}
/**
* Remove instruction at $index
*/
public function delete($index) {
$this->correction[$index][] = array(self::DELETE);
}
/**
* Insert a plugin call in front of instruction at $index
*/
public function insertPluginCall($index, $name, $data, $state, $text = '') {
$this->correction[$index][] = array(self::INSERT, array('plugin', array($name, $data, $state, $text)));
}
/**
* Insert qna_block plugin call in front of instruction at $index
*/
public function insertBlockCall($index, $data, $repeat = 1) {
for ($i = 0; $i < $repeat; $i++) {
$this->insertPluginCall($index, 'qna_block', array($data), DOKU_LEXER_SPECIAL);
}
}
/**
* Insert qna_header plugin call in front of instruction at $index
*/
public function insertHeaderCall($index, $data) {
if (!is_array($data)) {
$data = array($data);
}
$this->insertPluginCall($index, 'qna_header', $data, DOKU_LEXER_SPECIAL);
}
/**
* Append a plugin call at the end of the instruction list
*/
public function appendPluginCall($name, $data, $state, $text = '') {
$this->correction[-1][] = array(self::INSERT, array('plugin', array($name, $data, $state, $text)));
}
/**
* Append qna_block plugin call at the end of the instruction list
*/
public function appendBlockCall($data, $repeat = 1) {
for ($i = 0; $i < $repeat; $i++) {
$this->appendPluginCall('qna_block', array($data), DOKU_LEXER_SPECIAL);
}
}
/**
* Set open_question list level for TOC
*/
public function setQuestionLevel($index, $level) {
$this->correction[$index][] = array(self::SET_LEVEL, $level);
}
/**
* Apply the corrections
*/
public function apply(&$instruction) {
if (count($this->correction) > 0) {
$index = $this->getCorrectionIndex();
$corrections = count($index);
$instructions = count($instruction);
$output = array();
for ($c = 0, $i = 0; $c < $corrections; $c++, $i++) {
/* Copy all instructions, which are ahead of the next correction */
for ( ; $i < $index[$c]; $i++) {
$output[] = $instruction[$i];
}
$this->applyCorrections($i, $instruction, $output);
}
/* Copy the rest of instructions after the last correction */
for ( ; $i < $instructions; $i++) {
$output[] = $instruction[$i];
}
/* Handle appends */
if (array_key_exists(-1, $this->correction)) {
$this->applyAppend($output);
}
$instruction = $output;
}
}
/**
* Sort corrections on instruction index, remove appends
*/
private function getCorrectionIndex() {
$result = array_keys($this->correction);
asort($result);
$result = array_values($result);
/* Remove appends */
if ($result[0] == -1) {
array_shift($result);
}
return $result;
}
/**
* Apply corrections at $index
*/
private function applyCorrections($index, $input, &$output) {
$delete = false;
$position = $input[$index][2];
foreach ($this->correction[$index] as $correction) {
switch ($correction[0]) {
case self::DELETE:
$delete = true;
break;
case self::INSERT:
$output[] = array($correction[1][0], $correction[1][1], $position);
break;
case self::SET_LEVEL:
if (($input[$index][0] == 'plugin') && ($input[$index][1][0] == 'qna_block') && ($input[$index][1][1][0] == 'open_question')) {
$input[$index][1][1][3] = $correction[1];
}
break;
}
}
if (!$delete) {
$output[] = $input[$index];
}
}
/**
*
*/
private function applyAppend(&$output) {
$lastCall = end($output);
$position = $lastCall[2];
foreach ($this->correction[-1] as $correction) {
switch ($correction[0]) {
case self::INSERT:
$output[] = array($correction[1][0], $correction[1][1], $position);
break;
}
}
}
}