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
|
<?php
namespace dokuwiki\Parsing\Handler;
/**
* Handler for paragraphs
*
* @author Harry Fuecks <hfuecks@gmail.com>
*/
class Block
{
protected $calls = [];
protected $skipEol = false;
protected $inParagraph = false;
// Blocks these should not be inside paragraphs
protected $blockOpen = [
'header', 'listu_open', 'listo_open', 'listitem_open', 'listcontent_open', 'table_open', 'tablerow_open',
'tablecell_open', 'tableheader_open', 'tablethead_open', 'quote_open', 'code', 'file', 'hr', 'preformatted',
'rss', 'footnote_open'
];
protected $blockClose = [
'header', 'listu_close', 'listo_close', 'listitem_close', 'listcontent_close', 'table_close',
'tablerow_close', 'tablecell_close', 'tableheader_close', 'tablethead_close', 'quote_close', 'code', 'file',
'hr', 'preformatted', 'rss', 'footnote_close'
];
// Stacks can contain paragraphs
protected $stackOpen = ['section_open'];
protected $stackClose = ['section_close'];
/**
* Constructor. Adds loaded syntax plugins to the block and stack
* arrays
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
public function __construct()
{
global $DOKU_PLUGINS;
//check if syntax plugins were loaded
if (empty($DOKU_PLUGINS['syntax'])) return;
foreach ($DOKU_PLUGINS['syntax'] as $n => $p) {
$ptype = $p->getPType();
if ($ptype == 'block') {
$this->blockOpen[] = 'plugin_' . $n;
$this->blockClose[] = 'plugin_' . $n;
} elseif ($ptype == 'stack') {
$this->stackOpen[] = 'plugin_' . $n;
$this->stackClose[] = 'plugin_' . $n;
}
}
}
protected function openParagraph($pos)
{
if ($this->inParagraph) return;
$this->calls[] = ['p_open', [], $pos];
$this->inParagraph = true;
$this->skipEol = true;
}
/**
* Close a paragraph if needed
*
* This function makes sure there are no empty paragraphs on the stack
*
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string|integer $pos
*/
protected function closeParagraph($pos)
{
if (!$this->inParagraph) return;
// look back if there was any content - we don't want empty paragraphs
$content = '';
$ccount = count($this->calls);
for ($i = $ccount - 1; $i >= 0; $i--) {
if ($this->calls[$i][0] == 'p_open') {
break;
} elseif ($this->calls[$i][0] == 'cdata') {
$content .= $this->calls[$i][1][0];
} else {
$content = 'found markup';
break;
}
}
if (trim($content) == '') {
//remove the whole paragraph
//array_splice($this->calls,$i); // <- this is much slower than the loop below
for (
$x = $ccount; $x > $i;
$x--
) array_pop($this->calls);
} else {
// remove ending linebreaks in the paragraph
$i = count($this->calls) - 1;
if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0], "\n");
$this->calls[] = ['p_close', [], $pos];
}
$this->inParagraph = false;
$this->skipEol = true;
}
protected function addCall($call)
{
$key = count($this->calls);
if ($key && $call[0] == 'cdata' && $this->calls[$key - 1][0] == 'cdata') {
$this->calls[$key - 1][1][0] .= $call[1][0];
} else {
$this->calls[] = $call;
}
}
// simple version of addCall, without checking cdata
protected function storeCall($call)
{
$this->calls[] = $call;
}
/**
* Processes the whole instruction stack to open and close paragraphs
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param array $calls
*
* @return array
*/
public function process($calls)
{
// open first paragraph
$this->openParagraph(0);
foreach ($calls as $key => $call) {
$cname = $call[0];
if ($cname == 'plugin') {
$cname = 'plugin_' . $call[1][0];
$plugin = true;
$plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
$plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
} else {
$plugin = false;
}
/* stack */
if (in_array($cname, $this->stackClose) && (!$plugin || $plugin_close)) {
$this->closeParagraph($call[2]);
$this->storeCall($call);
$this->openParagraph($call[2]);
continue;
}
if (in_array($cname, $this->stackOpen) && (!$plugin || $plugin_open)) {
$this->closeParagraph($call[2]);
$this->storeCall($call);
$this->openParagraph($call[2]);
continue;
}
/* block */
// If it's a substition it opens and closes at the same call.
// To make sure next paragraph is correctly started, let close go first.
if (in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
$this->closeParagraph($call[2]);
$this->storeCall($call);
$this->openParagraph($call[2]);
continue;
}
if (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
$this->closeParagraph($call[2]);
$this->storeCall($call);
continue;
}
/* eol */
if ($cname == 'eol') {
// Check this isn't an eol instruction to skip...
if (!$this->skipEol) {
// Next is EOL => double eol => mark as paragraph
if (isset($calls[$key + 1]) && $calls[$key + 1][0] == 'eol') {
$this->closeParagraph($call[2]);
$this->openParagraph($call[2]);
} else {
//if this is just a single eol make a space from it
$this->addCall(['cdata', ["\n"], $call[2]]);
}
}
continue;
}
/* normal */
$this->addCall($call);
$this->skipEol = false;
}
// close last paragraph
$call = end($this->calls);
$this->closeParagraph($call[2]);
return $this->calls;
}
}
|