public function parse($text)
{
$this->texy->invokeHandlers('beforeBlockParse', [$this, &$text]);
// parser initialization
$this->text = $text;
$this->offset = 0;
// parse loop
$matches = [];
$priority = 0;
foreach ($this->patterns as $name => $pattern) {
$ms = Regexp::match($text, $pattern['pattern'], Regexp::OFFSET_CAPTURE | Regexp::ALL);
foreach ((array) $ms as $m) {
$offset = $m[0][1];
foreach ($m as $k => $v) {
$m[$k] = $v[0];
}
$matches[] = [$offset, $name, $m, $priority];
}
$priority++;
}
unset($name, $pattern, $ms, $m, $k, $v);
usort($matches, function ($a, $b) {
if ($a[0] === $b[0]) {
return $a[3] < $b[3] ? -1 : 1;
}
if ($a[0] < $b[0]) {
return -1;
}
return 1;
});
$matches[] = [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 !== '') {
$this->texy->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'], [$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 HtmlElement) {
$el->insert(NULL, $res);
} elseif (is_string($res)) {
$el->insert(NULL, $res);
}
} while (1);
}