/usr/share/php/texy/src/Texy/TexyParser.php is in php-texy 2.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
/**
* This file is part of the Texy! (http://texy.info)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
/**
* Texy parser base class.
*
* @author David Grudl
*/
class TexyParser extends TexyObject
{
/** @var Texy */
protected $texy;
/** @var TexyHtml */
protected $element;
/** @var array */
public $patterns;
/**
* @return Texy
*/
public function getTexy()
{
return $this->texy;
}
}
/**
* Parser for block structures.
*/
class TexyBlockParser extends TexyParser
{
/** @var string */
private $text;
/** @var int */
private $offset;
/** @var bool */
private $indented;
/**
* @param Texy
* @param TexyHtml
*/
public function __construct(Texy $texy, TexyHtml $element, $indented)
{
$this->texy = $texy;
$this->element = $element;
$this->indented = (bool) $indented;
$this->patterns = $texy->getBlockPatterns();
}
public function isIndented()
{
return $this->indented;
}
// match current line against RE.
// if succesfull, increments current position and returns TRUE
public function next($pattern, & $matches)
{
if ($this->offset > strlen($this->text)) {
return FALSE;
}
$matches = TexyRegexp::match(
$this->text,
$pattern . 'Am', // anchored & multiline
TexyRegexp::OFFSET_CAPTURE,
$this->offset
);
if ($matches) {
$this->offset += strlen($matches[0][0]) + 1; // 1 = "\n"
foreach ($matches as $key => $value) {
$matches[$key] = $value[0];
}
return TRUE;
}
}
public function moveBackward($linesCount = 1)
{
while (--$this->offset > 0) {
if ($this->text{ $this->offset-1 } === "\n") {
$linesCount--;
if ($linesCount < 1) {
break;
}
}
}
$this->offset = max($this->offset, 0);
}
public static function cmp($a, $b)
{
if ($a[0] === $b[0]) {
return $a[3] < $b[3] ? -1 : 1;
}
if ($a[0] < $b[0]) {
return -1;
}
return 1;
}
/**
* @param string
* @return void
*/
public function parse($text)
{
$tx = $this->texy;
$tx->invokeHandlers('beforeBlockParse', array($this, & $text));
// parser initialization
$this->text = $text;
$this->offset = 0;
// parse loop
$matches = array();
$priority = 0;
foreach ($this->patterns as $name => $pattern) {
$ms = TexyRegexp::match(
$text,
$pattern['pattern'],
TexyRegexp::OFFSET_CAPTURE | TexyRegexp::ALL
);
foreach ((array) $ms as $m) {
$offset = $m[0][1];
foreach ($m as $k => $v) {
$m[$k] = $v[0];
}
$matches[] = array($offset, $name, $m, $priority);
}
$priority++;
}
unset($name, $pattern, $ms, $m, $k, $v);
usort($matches, array(__CLASS__, 'cmp')); // generates strict error in PHP 5.1.2
$matches[] = array(strlen($text), NULL, NULL); // terminal cap
// process loop
$el = $this->element;
$cursor = 0;
do {
do {
list($mOffset, $mName, $mMatches) = $matches[$cursor];
$cursor++;
if ($mName === NULL || $mOffset >= $this->offset) {
break;
}
} while (1);
// between-matches content
if ($mOffset > $this->offset) {
$s = trim(substr($text, $this->offset, $mOffset - $this->offset));
if ($s !== '') {
$tx->paragraphModule->process($this, $s, $el);
}
}
if ($mName === NULL) {
break; // finito
}
$this->offset = $mOffset + strlen($mMatches[0]) + 1; // 1 = \n
$res = call_user_func_array(
$this->patterns[$mName]['handler'],
array($this, $mMatches, $mName)
);
if ($res === FALSE || $this->offset <= $mOffset) { // module rejects text
// asi by se nemelo stat, rozdeli generic block
$this->offset = $mOffset; // turn offset back
continue;
} elseif ($res instanceof TexyHtml) {
$el->insert(NULL, $res);
} elseif (is_string($res)) {
$el->insert(NULL, $res);
}
} while (1);
}
}
/**
* Parser for single line structures.
*/
class TexyLineParser extends TexyParser
{
/** @var bool */
public $again;
/**
* @param Texy
* @param TexyHtml
*/
public function __construct(Texy $texy, TexyHtml $element)
{
$this->texy = $texy;
$this->element = $element;
$this->patterns = $texy->getLinePatterns();
}
/**
* @param string
* @return void
*/
public function parse($text)
{
$tx = $this->texy;
// initialization
$pl = $this->patterns;
if (!$pl) {
// nothing to do
$this->element->insert(NULL, $text);
return;
}
$offset = 0;
$names = array_keys($pl);
$arrMatches = $arrOffset = array();
foreach ($names as $name) {
$arrOffset[$name] = -1;
}
// parse loop
do {
$min = NULL;
$minOffset = strlen($text);
foreach ($names as $index => $name) {
if ($arrOffset[$name] < $offset) {
$delta = 0;
if ($arrOffset[$name] === -2) {
do {
$delta++;
} while (isset($text[$offset + $delta]) && $text[$offset + $delta] >= "\x80" && $text[$offset + $delta] < "\xC0");
}
if ($offset + $delta > strlen($text)) {
unset($names[$index]);
continue;
} elseif ($arrMatches[$name] = TexyRegexp::match(
$text,
$pl[$name]['pattern'],
TexyRegexp::OFFSET_CAPTURE,
$offset + $delta)
) {
$m = & $arrMatches[$name];
if (!strlen($m[0][0])) {
continue;
}
$arrOffset[$name] = $m[0][1];
foreach ($m as $keyx => $value) {
$m[$keyx] = $value[0];
}
} else {
// try next time?
if (!$pl[$name]['again'] || !TexyRegexp::match($text, $pl[$name]['again'], NULL, $offset + $delta)) {
unset($names[$index]);
}
continue;
}
} // if
if ($arrOffset[$name] < $minOffset) {
$minOffset = $arrOffset[$name];
$min = $name;
}
} // foreach
if ($min === NULL) {
break;
}
$px = $pl[$min];
$offset = $start = $arrOffset[$min];
$this->again = FALSE;
$res = call_user_func_array(
$px['handler'],
array($this, $arrMatches[$min], $min)
);
if ($res instanceof TexyHtml) {
$res = $res->toString($tx);
} elseif ($res === FALSE) {
$arrOffset[$min] = -2;
continue;
}
$len = strlen($arrMatches[$min][0]);
$text = substr_replace(
$text,
(string) $res,
$start,
$len
);
$delta = strlen($res) - $len;
foreach ($names as $name) {
if ($arrOffset[$name] < $start + $len) {
$arrOffset[$name] = -1;
} else {
$arrOffset[$name] += $delta;
}
}
if ($this->again) {
$arrOffset[$min] = -2;
} else {
$arrOffset[$min] = -1;
$offset += strlen($res);
}
} while (1);
$this->element->insert(NULL, $text);
}
}
|