197 $forInclusion =
$flags & Parser::PTD_FOR_INCLUSION;
199 $xmlishElements = $this->parser->getStripList();
200 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
201 $enableOnlyinclude =
false;
202 if ( $forInclusion ) {
203 $ignoredTags = [
'includeonly',
'/includeonly' ];
204 $ignoredElements = [
'noinclude' ];
205 $xmlishElements[] =
'noinclude';
206 if ( strpos( $text,
'<onlyinclude>' ) !==
false
207 && strpos( $text,
'</onlyinclude>' ) !==
false
209 $enableOnlyinclude =
true;
212 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
213 $ignoredElements = [
'includeonly' ];
214 $xmlishElements[] =
'includeonly';
216 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
219 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
223 $searchBase =
"[{<\n"; # }
229 $revText = strrev( $text );
230 $lengthText = strlen( $text );
235 $accum =& $stack->getAccum();
247 $noMoreClosingTag = [];
249 $findOnlyinclude = $enableOnlyinclude;
251 $fakeLineStart =
true;
256 if ( $findOnlyinclude ) {
258 $startPos = strpos( $text,
'<onlyinclude>', $i );
259 if ( $startPos ===
false ) {
261 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i ) ) .
'</ignore>';
264 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
265 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) .
'</ignore>';
267 $findOnlyinclude =
false;
270 if ( $fakeLineStart ) {
271 $found =
'line-start';
274 # Find next opening brace, closing brace or pipe
275 $search = $searchBase;
276 if ( $stack->top ===
false ) {
277 $currentClosing =
'';
279 $stack->top->close ===
'}-' &&
280 $stack->top->count > 2
282 # adjust closing for -{{{...{{
283 $currentClosing =
'}';
284 $search .= $currentClosing;
286 $currentClosing = $stack->top->close;
287 $search .= $currentClosing;
297 # Output literal section, advance input counter
298 $literalLength = strcspn( $text, $search, $i );
299 if ( $literalLength > 0 ) {
300 $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) );
301 $i += $literalLength;
303 if ( $i >= $lengthText ) {
304 if ( $currentClosing ==
"\n" ) {
313 $curChar = $curTwoChar = $text[$i];
314 if ( ( $i + 1 ) < $lengthText ) {
315 $curTwoChar .= $text[$i + 1];
317 if ( $curChar ==
'|' ) {
319 } elseif ( $curChar ==
'=' ) {
321 } elseif ( $curChar ==
'<' ) {
323 } elseif ( $curChar ==
"\n" ) {
327 $found =
'line-start';
329 } elseif ( $curTwoChar == $currentClosing ) {
331 $curChar = $curTwoChar;
332 } elseif ( $curChar == $currentClosing ) {
334 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
335 $curChar = $curTwoChar;
337 $rule = $this->rules[$curChar];
338 } elseif ( isset( $this->rules[$curChar] ) ) {
340 $rule = $this->rules[$curChar];
342 # Some versions of PHP have a strcspn which stops on
343 # null characters; ignore these and continue.
344 # We also may get '-' and '}' characters here which
345 # don't match -{ or $currentClosing. Add these to
346 # output and continue.
347 if ( $curChar ==
'-' || $curChar ==
'}' ) {
356 if ( $found ==
'angle' ) {
359 if ( $enableOnlyinclude
360 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
362 $findOnlyinclude =
true;
367 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
381 $endPos = strpos( $text,
'-->', $i + 4 );
382 if ( $endPos ===
false ) {
384 $inner = substr( $text, $i );
385 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
389 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
393 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
397 $comments = [ [ $wsStart, $wsEnd ] ];
398 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
399 $c = strpos( $text,
'-->', $wsEnd + 4 );
400 if ( $c ===
false ) {
403 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
404 $comments[] = [ $wsEnd + 1, $c ];
412 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
413 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
417 $wsLength = $i - $wsStart;
419 && strspn( $accum,
" \t", -$wsLength ) === $wsLength
421 $accum = substr( $accum, 0, -$wsLength );
425 foreach ( $comments as $j => $com ) {
427 $endPos = $com[1] + 1;
428 if ( $j == ( count( $comments ) - 1 ) ) {
431 $inner = substr( $text, $startPos, $endPos - $startPos );
432 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
436 $fakeLineStart =
true;
444 $part = $stack->top->getCurrentPart();
445 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
446 $part->visualEnd = $wsStart;
449 $part->commentEnd = $endPos;
452 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
453 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
458 $lowerName = strtolower( $name );
459 $attrStart = $i + strlen( $name ) + 1;
462 $tagEndPos = $noMoreGT ?
false : strpos( $text,
'>', $attrStart );
463 if ( $tagEndPos ===
false ) {
473 if ( in_array( $lowerName, $ignoredTags ) ) {
475 . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) )
482 if ( $text[$tagEndPos - 1] ==
'/' ) {
483 $attrEnd = $tagEndPos - 1;
488 $attrEnd = $tagEndPos;
491 !isset( $noMoreClosingTag[$name] ) &&
492 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
493 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
495 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
497 $close =
'<close>' . htmlspecialchars(
$matches[0][0] ) .
'</close>';
500 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
502 $inner = substr( $text, $tagEndPos + 1 );
508 $accum .= htmlspecialchars( substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
510 $noMoreClosingTag[
$name] =
true;
516 if ( in_array( $lowerName, $ignoredElements ) ) {
517 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) )
523 if ( $attrEnd <= $attrStart ) {
526 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
528 $accum .=
'<name>' . htmlspecialchars( $name ) .
'</name>' .
531 '<attr>' . htmlspecialchars( $attr ) .
'</attr>';
532 if ( $inner !==
null ) {
533 $accum .=
'<inner>' . htmlspecialchars( $inner ) .
'</inner>';
535 $accum .= $close .
'</ext>';
536 } elseif ( $found ==
'line-start' ) {
539 if ( $fakeLineStart ) {
540 $fakeLineStart =
false;
546 $count = strspn( $text,
'=', $i, 6 );
547 if ( $count == 1 && $findEquals ) {
553 } elseif ( $count > 0 ) {
557 'parts' => [
new PPDPart( str_repeat(
'=', $count ) ) ],
560 $stack->push( $piece );
561 $accum =& $stack->getAccum();
562 $flags = $stack->getFlags();
566 } elseif ( $found ==
'line-end' ) {
567 $piece = $stack->top;
569 assert( $piece->open ===
"\n" );
570 $part = $piece->getCurrentPart();
574 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
575 $searchStart = $i - $wsLength;
576 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
579 $searchStart = $part->visualEnd;
580 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
582 $count = $piece->count;
583 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
584 if ( $equalsLength > 0 ) {
585 if ( $searchStart - $equalsLength == $piece->startPos ) {
589 $count = $equalsLength;
593 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
596 $count = min( $equalsLength, $count );
600 $element =
"<h level=\"$count\" i=\"$headingIndex\">$accum</h>";
612 $accum =& $stack->getAccum();
613 $flags = $stack->getFlags();
623 } elseif ( $found ==
'open' ) {
624 # count opening brace characters
625 $curLen = strlen( $curChar );
626 $count = ( $curLen > 1 ) ?
627 # allow the
final character to repeat
628 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
629 strspn( $text, $curChar, $i );
631 # we need to add to stack only if opening brace count is enough for one of the rules
632 if ( $count >= $rule[
'min'] ) {
633 # Add it to the stack
636 'close' => $rule[
'end'],
638 'lineStart' => ( $i > 0 && $text[$i - 1] ==
"\n" ),
641 $stack->push( $piece );
642 $accum =& $stack->getAccum();
643 $flags = $stack->getFlags();
646 # Add literal brace(s)
647 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) );
650 } elseif ( $found ==
'close' ) {
651 $piece = $stack->top;
652 # lets check if there are enough characters for closing brace
653 $maxCount = $piece->count;
654 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
655 $maxCount--; # don
't try to match closing '-
' as a '}
'
657 $curLen = strlen( $curChar );
658 $count = ( $curLen > 1 ) ? $curLen :
659 strspn( $text, $curChar, $i, $maxCount );
661 # check for maximum matching characters (if there are 5 closing
662 # characters, we will probably need only 3 - depending on the rules)
663 $rule = $this->rules[$piece->open];
664 if ( $piece->close === '}-
' && $piece->count > 2 ) {
665 # tweak for -{..{{ }}..}-
666 $rule = $this->rules['{
'];
668 if ( $count > $rule['max
'] ) {
669 # The specified maximum exists in the callback array, unless the caller
671 $matchingCount = $rule['max
'];
673 # Count is less than the maximum
674 # Skip any gaps in the callback array to find the true largest match
675 # Need to use array_key_exists not isset because the callback can be null
676 $matchingCount = $count;
677 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
682 if ( $matchingCount <= 0 ) {
683 # No matching element found in callback array
684 # Output a literal closing brace and continue
685 $endText = substr( $text, $i, $count );
686 $accum .= htmlspecialchars( $endText );
690 $name = $rule['names
'][$matchingCount];
691 if ( $name === null ) {
692 // No element, just literal text
693 $endText = substr( $text, $i, $matchingCount );
694 $element = $piece->breakSyntax( $matchingCount ) . $endText;
697 # Note: $parts is already XML, does not need to be encoded further
698 $parts = $piece->parts;
699 $title = $parts[0]->out;
702 # The invocation is at the start of the line if lineStart is set in
703 # the stack, and all opening brackets are used up.
704 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
705 $attr = ' lineStart=
"1"';
710 $element = "<$name$attr>";
711 $element .= "<title>$title</title>";
713 foreach ( $parts as $part ) {
714 if ( isset( $part->eqpos ) ) {
715 $argName = substr( $part->out, 0, $part->eqpos );
716 $argValue = substr( $part->out, $part->eqpos + 1 );
717 $element .= "<part><name>$argName</name>=<value>$argValue</value></part>";
719 $element .= "<part><name index=\"$argIndex\" /><value>{$part->out}</value></part>";
723 $element .= "</$name>";
726 # Advance input pointer
727 $i += $matchingCount;
731 $accum =& $stack->getAccum();
733 # Re-add the old stack element if it still has unmatched opening characters remaining
734 if ( $matchingCount < $piece->count ) {
735 $piece->parts = [ new PPDPart ];
736 $piece->count -= $matchingCount;
737 # do we still qualify for any callback with remaining count?
738 $min = $this->rules[$piece->open]['min
'];
739 if ( $piece->count >= $min ) {
740 $stack->push( $piece );
741 $accum =& $stack->getAccum();
743 $s = substr( $piece->open, 0, -1 );
745 substr( $piece->open, -1 ),
746 $piece->count - strlen( $s )
751 $flags = $stack->getFlags();
754 # Add XML element to the enclosing accumulator
756 } elseif ( $found == 'pipe
' ) {
757 $findEquals = true; // shortcut for getFlags()
759 $accum =& $stack->getAccum();
761 } elseif ( $found == 'equals
' ) {
762 $findEquals = false; // shortcut for getFlags()
763 $stack->getCurrentPart()->eqpos = strlen( $accum );
766 } elseif ( $found == 'dash
' ) {
772 # Output any remaining unclosed brackets
773 foreach ( $stack->stack as $piece ) {
774 $stack->rootAccum .= $piece->breakSyntax();
776 $stack->rootAccum .= '</
root>
';
777 $xml = $stack->rootAccum;