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
|
<?php
namespace dokuwiki\Parsing\Handler;
class Lists extends AbstractRewriter
{
protected $listCalls = [];
protected $listStack = [];
protected $initialDepth = 0;
public const NODE = 1;
/** @inheritdoc */
public function finalise()
{
$last_call = end($this->calls);
$this->writeCall(['list_close', [], $last_call[2]]);
$this->process();
$this->callWriter->finalise();
unset($this->callWriter);
}
/** @inheritdoc */
public function process()
{
foreach ($this->calls as $call) {
switch ($call[0]) {
case 'list_item':
$this->listOpen($call);
break;
case 'list_open':
$this->listStart($call);
break;
case 'list_close':
$this->listEnd($call);
break;
default:
$this->listContent($call);
break;
}
}
$this->callWriter->writeCalls($this->listCalls);
return $this->callWriter;
}
protected function listStart($call)
{
$depth = $this->interpretSyntax($call[1][0], $listType);
$this->initialDepth = $depth;
// array(list type, current depth, index of current listitem_open)
$this->listStack[] = [$listType, $depth, 1];
$this->listCalls[] = ['list' . $listType . '_open', [], $call[2]];
$this->listCalls[] = ['listitem_open', [1], $call[2]];
$this->listCalls[] = ['listcontent_open', [], $call[2]];
}
protected function listEnd($call)
{
$closeContent = true;
while ($list = array_pop($this->listStack)) {
if ($closeContent) {
$this->listCalls[] = ['listcontent_close', [], $call[2]];
$closeContent = false;
}
$this->listCalls[] = ['listitem_close', [], $call[2]];
$this->listCalls[] = ['list' . $list[0] . '_close', [], $call[2]];
}
}
protected function listOpen($call)
{
$depth = $this->interpretSyntax($call[1][0], $listType);
$end = end($this->listStack);
$key = key($this->listStack);
// Not allowed to be shallower than initialDepth
if ($depth < $this->initialDepth) {
$depth = $this->initialDepth;
}
if ($depth == $end[1]) {
// Just another item in the list...
if ($listType == $end[0]) {
$this->listCalls[] = ['listcontent_close', [], $call[2]];
$this->listCalls[] = ['listitem_close', [], $call[2]];
$this->listCalls[] = ['listitem_open', [$depth - 1], $call[2]];
$this->listCalls[] = ['listcontent_open', [], $call[2]];
// new list item, update list stack's index into current listitem_open
$this->listStack[$key][2] = count($this->listCalls) - 2;
// Switched list type...
} else {
$this->listCalls[] = ['listcontent_close', [], $call[2]];
$this->listCalls[] = ['listitem_close', [], $call[2]];
$this->listCalls[] = ['list' . $end[0] . '_close', [], $call[2]];
$this->listCalls[] = ['list' . $listType . '_open', [], $call[2]];
$this->listCalls[] = ['listitem_open', [$depth - 1], $call[2]];
$this->listCalls[] = ['listcontent_open', [], $call[2]];
array_pop($this->listStack);
$this->listStack[] = [$listType, $depth, count($this->listCalls) - 2];
}
} elseif ($depth > $end[1]) { // Getting deeper...
$this->listCalls[] = ['listcontent_close', [], $call[2]];
$this->listCalls[] = ['list' . $listType . '_open', [], $call[2]];
$this->listCalls[] = ['listitem_open', [$depth - 1], $call[2]];
$this->listCalls[] = ['listcontent_open', [], $call[2]];
// set the node/leaf state of this item's parent listitem_open to NODE
$this->listCalls[$this->listStack[$key][2]][1][1] = self::NODE;
$this->listStack[] = [$listType, $depth, count($this->listCalls) - 2];
} else { // Getting shallower ( $depth < $end[1] )
$this->listCalls[] = ['listcontent_close', [], $call[2]];
$this->listCalls[] = ['listitem_close', [], $call[2]];
$this->listCalls[] = ['list' . $end[0] . '_close', [], $call[2]];
// Throw away the end - done
array_pop($this->listStack);
while (1) {
$end = end($this->listStack);
$key = key($this->listStack);
if ($end[1] <= $depth) {
// Normalize depths
$depth = $end[1];
$this->listCalls[] = ['listitem_close', [], $call[2]];
if ($end[0] == $listType) {
$this->listCalls[] = ['listitem_open', [$depth - 1], $call[2]];
$this->listCalls[] = ['listcontent_open', [], $call[2]];
// new list item, update list stack's index into current listitem_open
$this->listStack[$key][2] = count($this->listCalls) - 2;
} else {
// Switching list type...
$this->listCalls[] = ['list' . $end[0] . '_close', [], $call[2]];
$this->listCalls[] = ['list' . $listType . '_open', [], $call[2]];
$this->listCalls[] = ['listitem_open', [$depth - 1], $call[2]];
$this->listCalls[] = ['listcontent_open', [], $call[2]];
array_pop($this->listStack);
$this->listStack[] = [$listType, $depth, count($this->listCalls) - 2];
}
break;
// Haven't dropped down far enough yet.... ( $end[1] > $depth )
} else {
$this->listCalls[] = ['listitem_close', [], $call[2]];
$this->listCalls[] = ['list' . $end[0] . '_close', [], $call[2]];
array_pop($this->listStack);
}
}
}
}
protected function listContent($call)
{
$this->listCalls[] = $call;
}
protected function interpretSyntax($match, &$type)
{
if (str_ends_with($match, '*')) {
$type = 'u';
} else {
$type = 'o';
}
// Is the +1 needed? It used to be count(explode(...))
// but I don't think the number is seen outside this handler
return substr_count(str_replace("\t", ' ', $match), ' ') + 1;
}
}
|