123 if ( $tree !==
false ) {
124 $store = json_decode( $tree );
125 if ( is_array( $store ) ) {
130 $forInclusion =
$flags & Parser::PTD_FOR_INCLUSION;
132 $xmlishElements = $this->parser->getStripList();
133 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
134 $enableOnlyinclude =
false;
135 if ( $forInclusion ) {
136 $ignoredTags = [
'includeonly',
'/includeonly' ];
137 $ignoredElements = [
'noinclude' ];
138 $xmlishElements[] =
'noinclude';
139 if ( strpos( $text,
'<onlyinclude>' ) !==
false
140 && strpos( $text,
'</onlyinclude>' ) !==
false
142 $enableOnlyinclude =
true;
145 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
146 $ignoredElements = [
'includeonly' ];
147 $xmlishElements[] =
'includeonly';
149 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
152 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
156 $searchBase =
"[{<\n";
163 $revText = strrev( $text );
164 $lengthText = strlen( $text );
169 $accum =& $stack->getAccum();
180 $noMoreClosingTag = [];
182 $findOnlyinclude = $enableOnlyinclude;
184 $fakeLineStart =
true;
189 if ( $findOnlyinclude ) {
191 $startPos = strpos( $text,
'<onlyinclude>', $i );
192 if ( $startPos ===
false ) {
194 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
197 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
198 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
200 $findOnlyinclude =
false;
203 if ( $fakeLineStart ) {
204 $found =
'line-start';
207 # Find next opening brace, closing brace or pipe
208 $search = $searchBase;
209 if ( $stack->top ===
false ) {
210 $currentClosing =
'';
212 $currentClosing = $stack->top->close;
213 $search .= $currentClosing;
223 # Output literal section, advance input counter
224 $literalLength = strcspn( $text, $search, $i );
225 if ( $literalLength > 0 ) {
227 $i += $literalLength;
229 if ( $i >= $lengthText ) {
230 if ( $currentClosing ==
"\n" ) {
239 $curChar = $curTwoChar = $text[$i];
240 if ( ( $i + 1 ) < $lengthText ) {
241 $curTwoChar .= $text[$i + 1];
243 if ( $curChar ==
'|' ) {
245 } elseif ( $curChar ==
'=' ) {
247 } elseif ( $curChar ==
'<' ) {
249 } elseif ( $curChar ==
"\n" ) {
253 $found =
'line-start';
255 } elseif ( $curTwoChar == $currentClosing ) {
257 $curChar = $curTwoChar;
258 } elseif ( $curChar == $currentClosing ) {
260 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
261 $curChar = $curTwoChar;
263 $rule = $this->rules[$curChar];
264 } elseif ( isset( $this->rules[$curChar] ) ) {
266 $rule = $this->rules[$curChar];
267 } elseif ( $curChar ==
'-' ) {
270 # Some versions of PHP have a strcspn which stops on null characters
271 # Ignore and continue
278 if ( $found ==
'angle' ) {
281 if ( $enableOnlyinclude
282 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
284 $findOnlyinclude =
true;
289 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
304 $endPos = strpos( $text,
'-->', $i + 4 );
305 if ( $endPos ===
false ) {
307 $inner = substr( $text, $i );
308 $accum[] = [
'comment', [ $inner ] ];
312 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
316 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
320 $comments = [ [ $wsStart, $wsEnd ] ];
321 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
322 $c = strpos( $text,
'-->', $wsEnd + 4 );
323 if ( $c ===
false ) {
326 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
327 $comments[] = [ $wsEnd + 1, $c ];
335 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
336 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
339 $wsLength = $i - $wsStart;
340 $endIndex = count( $accum ) - 1;
345 && is_string( $accum[$endIndex] )
346 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
348 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
352 foreach ( $comments
as $j => $com ) {
354 $endPos = $com[1] + 1;
355 if ( $j == ( count( $comments ) - 1 ) ) {
358 $inner = substr( $text, $startPos, $endPos - $startPos );
359 $accum[] = [
'comment', [ $inner ] ];
363 $fakeLineStart =
true;
371 $part = $stack->top->getCurrentPart();
372 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
373 $part->visualEnd = $wsStart;
376 $part->commentEnd = $endPos;
379 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
380 $accum[] = [
'comment', [ $inner ] ];
385 $lowerName = strtolower(
$name );
386 $attrStart = $i + strlen(
$name ) + 1;
389 $tagEndPos = $noMoreGT ?
false : strpos( $text,
'>', $attrStart );
390 if ( $tagEndPos ===
false ) {
400 if ( in_array( $lowerName, $ignoredTags ) ) {
401 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
407 if ( $text[$tagEndPos - 1] ==
'/' ) {
409 $attrEnd = $tagEndPos - 1;
414 $attrEnd = $tagEndPos;
417 !isset( $noMoreClosingTag[
$name] ) &&
418 preg_match(
"/<\/" . preg_quote(
$name,
'/' ) .
"\s*>/i",
419 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
421 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
426 if ( in_array(
$name, $xmlishAllowMissingEndTag ) ) {
428 $inner = substr( $text, $tagEndPos + 1 );
435 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
437 $noMoreClosingTag[
$name] =
true;
443 if ( in_array( $lowerName, $ignoredElements ) ) {
444 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
448 if ( $attrEnd <= $attrStart ) {
453 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
457 [
'name', [
$name ] ],
458 [
'attr', [ $attr ] ] ];
459 if ( $inner !==
null ) {
460 $children[] = [
'inner', [ $inner ] ];
462 if ( $close !==
null ) {
463 $children[] = [
'close', [ $close ] ];
465 $accum[] = [
'ext', $children ];
466 } elseif ( $found ==
'line-start' ) {
469 if ( $fakeLineStart ) {
470 $fakeLineStart =
false;
476 $count = strspn( $text,
'=', $i, 6 );
477 if ( $count == 1 && $findEquals ) {
482 } elseif ( $count > 0 ) {
486 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
489 $stack->push( $piece );
490 $accum =& $stack->getAccum();
491 extract( $stack->getFlags() );
494 } elseif ( $found ==
'line-end' ) {
495 $piece = $stack->top;
497 assert( $piece->open ===
"\n" );
498 $part = $piece->getCurrentPart();
502 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
503 $searchStart = $i - $wsLength;
504 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
507 $searchStart = $part->visualEnd;
508 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
510 $count = $piece->count;
511 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
512 if ( $equalsLength > 0 ) {
513 if ( $searchStart - $equalsLength == $piece->startPos ) {
517 $count = $equalsLength;
521 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
524 $count = min( $equalsLength, $count );
528 $element = [ [
'possible-h',
531 [
'@level', [ $count ] ],
532 [
'@i', [ $headingIndex++ ] ]
547 $accum =& $stack->getAccum();
548 extract( $stack->getFlags() );
551 array_splice( $accum, count( $accum ), 0, $element );
558 } elseif ( $found ==
'open' ) {
559 # count opening brace characters
560 $curLen = strlen( $curChar );
561 $count = ( $curLen > 1 ) ? 1 : strspn( $text, $curChar, $i );
563 # we need to add to stack only if opening brace count is enough for one of the rules
564 if ( $count >= $rule[
'min'] ) {
565 # Add it to the stack
568 'close' => $rule[
'end'],
570 'lineStart' => ( $i > 0 && $text[$i - 1] ==
"\n" ),
573 $stack->push( $piece );
574 $accum =& $stack->getAccum();
575 extract( $stack->getFlags() );
577 # Add literal brace(s)
580 $i += $curLen * $count;
581 } elseif ( $found ==
'close' ) {
582 $piece = $stack->top;
583 # lets check if there are enough characters for closing brace
584 $maxCount = $piece->count;
585 $curLen = strlen( $curChar );
586 $count = ( $curLen > 1 ) ? 1 : strspn( $text, $curChar, $i, $maxCount );
588 # check for maximum matching characters (if there are 5 closing
589 # characters, we will probably need only 3 - depending on the rules)
590 $rule = $this->rules[$piece->open];
591 if ( $count > $rule[
'max'] ) {
592 # The specified maximum exists in the callback array, unless the caller
594 $matchingCount = $rule[
'max'];
596 # Count is less than the maximum
597 # Skip any gaps in the callback array to find the true largest match
598 # Need to use array_key_exists not isset because the callback can be null
599 $matchingCount = $count;
600 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule[
'names'] ) ) {
605 if ( $matchingCount <= 0 ) {
606 # No matching element found in callback array
607 # Output a literal closing brace and continue
609 $i += $curLen * $count;
612 $name = $rule[
'names'][$matchingCount];
613 if (
$name ===
null ) {
615 $element = $piece->breakSyntax( $matchingCount );
619 $parts = $piece->parts;
620 $titleAccum = $parts[0]->out;
625 # The invocation is at the start of the line if lineStart is set in
626 # the stack, and all opening brackets are used up.
627 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
628 $children[] = [
'@lineStart', [ 1 ] ];
630 $titleNode = [
'title', $titleAccum ];
631 $children[] = $titleNode;
633 foreach ( $parts
as $part ) {
634 if ( isset( $part->eqpos ) ) {
635 $equalsNode = $part->out[$part->eqpos];
636 $nameNode = [
'name', array_slice( $part->out, 0, $part->eqpos ) ];
637 $valueNode = [
'value', array_slice( $part->out, $part->eqpos + 1 ) ];
638 $partNode = [
'part', [ $nameNode, $equalsNode, $valueNode ] ];
639 $children[] = $partNode;
641 $nameNode = [
'name', [ [
'@index', [ $argIndex++ ] ] ] ];
642 $valueNode = [
'value', $part->out ];
643 $partNode = [
'part', [ $nameNode, $valueNode ] ];
644 $children[] = $partNode;
647 $element = [ [
$name, $children ] ];
650 # Advance input pointer
651 $i += $curLen * $matchingCount;
655 $accum =& $stack->getAccum();
657 # Re-add the old stack element if it still has unmatched opening characters remaining
658 if ( $matchingCount < $piece->count ) {
660 $piece->count -= $matchingCount;
661 # do we still qualify for any callback with remaining count?
662 $min = $this->rules[$piece->open][
'min'];
663 if ( $piece->count >= $min ) {
664 $stack->push( $piece );
665 $accum =& $stack->getAccum();
671 extract( $stack->getFlags() );
673 # Add XML element to the enclosing accumulator
674 array_splice( $accum, count( $accum ), 0, $element );
675 } elseif ( $found ==
'pipe' ) {
678 $accum =& $stack->getAccum();
680 } elseif ( $found ==
'equals' ) {
682 $accum[] = [
'equals', [
'=' ] ];
683 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
685 } elseif ( $found ==
'dash' ) {
691 # Output any remaining unclosed brackets
692 foreach ( $stack->stack
as $piece ) {
693 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
696 # Enable top-level headings
697 foreach ( $stack->rootAccum
as &$node ) {
703 $rootStore = [ [
'root', $stack->rootAccum ] ];
707 $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
708 if ( $tree !==
false ) {