4 * This is the part of the wikitext parser which handles automatic paragraphs
5 * and conversion of start-of-line prefixes to HTML lists.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
25 class BlockLevelPass
{
26 private $DTopen = false;
27 private $inPre = false;
28 private $lastParagraph = '';
32 # State constants for the definition list colon extraction
33 const COLON_STATE_TEXT
= 0;
34 const COLON_STATE_TAG
= 1;
35 const COLON_STATE_TAGSTART
= 2;
36 const COLON_STATE_CLOSETAG
= 3;
37 const COLON_STATE_TAGSLASH
= 4;
38 const COLON_STATE_COMMENT
= 5;
39 const COLON_STATE_COMMENTDASH
= 6;
40 const COLON_STATE_COMMENTDASHDASH
= 7;
41 const COLON_STATE_LC
= 8;
44 * Make lists from lines starting with ':', '*', '#', etc.
47 * @param bool $lineStart Whether or not this is at the start of a line.
48 * @return string The lists rendered as HTML
50 public static function doBlockLevels( $text, $lineStart ) {
51 $pass = new self( $text, $lineStart );
52 return $pass->execute();
57 * @param bool $lineStart
59 private function __construct( $text, $lineStart ) {
61 $this->lineStart
= $lineStart;
67 private function hasOpenParagraph() {
68 return $this->lastParagraph
!== '';
72 * If a pre or p is open, return the corresponding close tag and update
73 * the state. If no tag is open, return an empty string.
74 * @param bool $atTheEnd Omit trailing newline if we've reached the end.
77 private function closeParagraph( $atTheEnd = false ) {
79 if ( $this->hasOpenParagraph() ) {
80 $result = '</' . $this->lastParagraph
. '>';
86 $this->lastParagraph
= '';
91 * getCommon() returns the length of the longest common substring
92 * of both arguments, starting at the beginning of both.
99 private function getCommon( $st1, $st2 ) {
100 $shorter = min( strlen( $st1 ), strlen( $st2 ) );
102 for ( $i = 0; $i < $shorter; ++
$i ) {
103 if ( $st1[$i] !== $st2[$i] ) {
111 * Open the list item element identified by the prefix character.
113 * @param string $char
117 private function openList( $char ) {
118 $result = $this->closeParagraph();
120 if ( $char === '*' ) {
121 $result .= "<ul><li>";
122 } elseif ( $char === '#' ) {
123 $result .= "<ol><li>";
124 } elseif ( $char === ':' ) {
125 $result .= "<dl><dd>";
126 } elseif ( $char === ';' ) {
127 $result .= "<dl><dt>";
128 $this->DTopen
= true;
130 $result = '<!-- ERR 1 -->';
137 * Close the current list item and open the next one.
138 * @param string $char
142 private function nextItem( $char ) {
143 if ( $char === '*' ||
$char === '#' ) {
144 return "</li>\n<li>";
145 } elseif ( $char === ':' ||
$char === ';' ) {
147 if ( $this->DTopen
) {
150 if ( $char === ';' ) {
151 $this->DTopen
= true;
152 return $close . '<dt>';
154 $this->DTopen
= false;
155 return $close . '<dd>';
158 return '<!-- ERR 2 -->';
162 * Close the current list item identified by the prefix character.
163 * @param string $char
167 private function closeList( $char ) {
168 if ( $char === '*' ) {
169 $text = "</li></ul>";
170 } elseif ( $char === '#' ) {
171 $text = "</li></ol>";
172 } elseif ( $char === ':' ) {
173 if ( $this->DTopen
) {
174 $this->DTopen
= false;
175 $text = "</dt></dl>";
177 $text = "</dd></dl>";
180 return '<!-- ERR 3 -->';
189 private function execute() {
191 # Parsing through the text line by line. The main thing
192 # happening here is handling of block-level elements p, pre,
193 # and making lists from lines starting with * # : etc.
194 $textLines = StringUtils
::explode( "\n", $text );
196 $lastPrefix = $output = '';
197 $this->DTopen
= $inBlockElem = false;
199 $pendingPTag = false;
200 $inBlockquote = false;
202 $lineCount = count( $textLines );
203 foreach ( $textLines as $i => $inputLine ) {
205 if ( !$this->lineStart
) {
206 $output .= $inputLine;
207 $this->lineStart
= true;
215 $lastPrefixLength = strlen( $lastPrefix );
216 $preCloseMatch = preg_match( '/<\\/pre/i', $inputLine );
217 $preOpenMatch = preg_match( '/<pre/i', $inputLine );
218 # If not in a <pre> element, scan for and figure out what prefixes are there.
219 if ( !$this->inPre
) {
220 # Multiple prefixes may abut each other for nested lists.
221 $prefixLength = strspn( $inputLine, '*#:;' );
222 $prefix = substr( $inputLine, 0, $prefixLength );
225 # ; and : are both from definition-lists, so they're equivalent
226 # for the purposes of determining whether or not we need to open/close
228 $prefix2 = str_replace( ';', ':', $prefix );
229 $t = substr( $inputLine, $prefixLength );
230 $this->inPre
= (bool)$preOpenMatch;
232 # Don't interpret any other prefixes in preformatted text
234 $prefix = $prefix2 = '';
239 if ( $prefixLength && $lastPrefix === $prefix2 ) {
240 # Same as the last item, so no need to deal with nesting or opening stuff
241 $output .= $this->nextItem( substr( $prefix, -1 ) );
242 $pendingPTag = false;
244 if ( substr( $prefix, -1 ) === ';' ) {
245 # The one nasty exception: definition lists work like this:
246 # ; title : definition text
247 # So we check for : in the remainder text to split up the
248 # title and definition, without b0rking links.
250 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
252 // Trim whitespace in list items
253 $output .= trim( $term ) . $this->nextItem( ':' );
256 } elseif ( $prefixLength ||
$lastPrefixLength ) {
257 # We need to open or close prefixes, or both.
259 # Either open or close a level...
260 $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
261 $pendingPTag = false;
263 # Close all the prefixes which aren't shared.
264 while ( $commonPrefixLength < $lastPrefixLength ) {
265 $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
269 # Continue the current prefix if appropriate.
270 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
271 $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
274 # Close an open <dt> if we have a <dd> (":") starting on this line
275 if ( $this->DTopen
&& $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] === ':' ) {
276 $output .= $this->nextItem( ':' );
279 # Open prefixes where appropriate.
280 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
283 while ( $prefixLength > $commonPrefixLength ) {
284 $char = $prefix[$commonPrefixLength];
285 $output .= $this->openList( $char );
287 if ( $char === ';' ) {
288 # @todo FIXME: This is dupe of code above
289 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
291 // Trim whitespace in list items
292 $output .= trim( $term ) . $this->nextItem( ':' );
295 ++
$commonPrefixLength;
297 if ( !$prefixLength && $lastPrefix ) {
300 $lastPrefix = $prefix2;
303 # If we have no prefixes, go to paragraph mode.
304 if ( $prefixLength == 0 ) {
305 # No prefix (not in list)--go to paragraph mode
306 # @todo consider using a stack for nestable elements like span, table and div
308 // P-wrapping and indent-pre are suppressed inside, not outside
309 $blockElems = 'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
310 // P-wrapping and indent-pre are suppressed outside, not inside
311 $antiBlockElems = 'td|th';
313 $openMatch = preg_match(
315 . "({$blockElems})|\\/({$antiBlockElems})|"
317 . '\\/?(tr|dt|dd|li)'
321 $closeMatch = preg_match(
323 . "\\/({$blockElems})|({$antiBlockElems})|"
325 . '\\/?(center|blockquote|div|hr|mw:)'
330 // Any match closes the paragraph, but only when `!$closeMatch`
331 // do we enter block mode. The oddities with table rows and
332 // cells are to avoid paragraph wrapping in interstitial spaces
333 // leading to fostered content.
335 if ( $openMatch ||
$closeMatch ) {
336 $pendingPTag = false;
337 // Only close the paragraph if we're not inside a <pre> tag, or if
338 // that <pre> tag has just been opened
339 if ( !$this->inPre ||
$preOpenMatch ) {
340 // @todo T7718: paragraph closed
341 $output .= $this->closeParagraph();
343 if ( $preOpenMatch && !$preCloseMatch ) {
347 while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t,
348 $bqMatch, PREG_OFFSET_CAPTURE
, $bqOffset )
350 $inBlockquote = !$bqMatch[1][0]; // is this a close tag?
351 $bqOffset = $bqMatch[0][1] +
strlen( $bqMatch[0][0] );
353 $inBlockElem = !$closeMatch;
354 } elseif ( !$inBlockElem && !$this->inPre
) {
355 if ( substr( $t, 0, 1 ) == ' '
356 && ( $this->lastParagraph
=== 'pre' ||
trim( $t ) != '' )
360 if ( $this->lastParagraph
!== 'pre' ) {
361 $pendingPTag = false;
362 $output .= $this->closeParagraph() . '<pre>';
363 $this->lastParagraph
= 'pre';
365 $t = substr( $t, 1 );
366 } elseif ( preg_match( '/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
367 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
368 # But it should clear $pendingPTag.
369 if ( $pendingPTag ) {
370 $output .= $this->closeParagraph();
371 $pendingPTag = false;
375 if ( trim( $t ) === '' ) {
376 if ( $pendingPTag ) {
377 $output .= $pendingPTag . '<br />';
378 $pendingPTag = false;
379 $this->lastParagraph
= 'p';
381 if ( $this->lastParagraph
!== 'p' ) {
382 $output .= $this->closeParagraph();
383 $pendingPTag = '<p>';
385 $pendingPTag = '</p><p>';
389 if ( $pendingPTag ) {
390 $output .= $pendingPTag;
391 $pendingPTag = false;
392 $this->lastParagraph
= 'p';
393 } elseif ( $this->lastParagraph
!== 'p' ) {
394 $output .= $this->closeParagraph() . '<p>';
395 $this->lastParagraph
= 'p';
401 # somewhere above we forget to get out of pre block (T2785)
402 if ( $preCloseMatch && $this->inPre
) {
403 $this->inPre
= false;
405 if ( $pendingPTag === false ) {
406 if ( $prefixLength === 0 ) {
408 // Add a newline if there's an open paragraph
409 // or we've yet to reach the last line.
410 if ( $i < $lineCount - 1 ||
$this->hasOpenParagraph() ) {
414 // Trim whitespace in list items
415 $output .= trim( $t );
419 while ( $prefixLength ) {
420 $output .= $this->closeList( $prefix2[$prefixLength - 1] );
422 // Note that a paragraph is only ever opened when `prefixLength`
423 // is zero, but we'll choose to be overly cautious.
424 if ( !$prefixLength && $this->hasOpenParagraph() ) {
428 $output .= $this->closeParagraph( true );
433 * Split up a string on ':', ignoring any occurrences inside tags
434 * to prevent illegal overlapping.
436 * @param string $str The string to split
437 * @param string &$before Set to everything before the ':'
438 * @param string &$after Set to everything after the ':'
439 * @throws MWException
440 * @return string The position of the ':', or false if none found
442 private function findColonNoLinks( $str, &$before, &$after ) {
443 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE
) ) {
448 if ( $m[0][0] === ':' ) {
449 # Easy; no tag nesting to worry about
450 $colonPos = $m[0][1];
451 $before = substr( $str, 0, $colonPos );
452 $after = substr( $str, $colonPos +
1 );
456 # Ugly state machine to walk through avoiding tags.
457 $state = self
::COLON_STATE_TEXT
;
460 $len = strlen( $str );
461 for ( $i = $m[0][1]; $i < $len; $i++
) {
465 case self
::COLON_STATE_TEXT
:
468 # Could be either a <start> tag or an </end> tag
469 $state = self
::COLON_STATE_TAGSTART
;
472 if ( $ltLevel === 0 ) {
474 $before = substr( $str, 0, $i );
475 $after = substr( $str, $i +
1 );
478 # Embedded in a tag; don't break it.
481 # Skip ahead looking for something interesting
482 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE
, $i ) ) {
483 # Nothing else interesting
486 if ( $m[0][0] === '-{' ) {
487 $state = self
::COLON_STATE_LC
;
491 # Skip ahead to next interesting character.
497 case self
::COLON_STATE_LC
:
498 # In language converter markup -{ ... }-
499 if ( !preg_match( '/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE
, $i ) ) {
500 # Nothing else interesting to find; abort!
501 # We're nested in language converter markup, but there
502 # are no close tags left. Abort!
504 } elseif ( $m[0][0] === '-{' ) {
507 } elseif ( $m[0][0] === '}-' ) {
510 if ( $lcLevel === 0 ) {
511 $state = self
::COLON_STATE_TEXT
;
515 case self
::COLON_STATE_TAG
:
520 $state = self
::COLON_STATE_TEXT
;
523 # Slash may be followed by >?
524 $state = self
::COLON_STATE_TAGSLASH
;
530 case self
::COLON_STATE_TAGSTART
:
533 $state = self
::COLON_STATE_CLOSETAG
;
536 $state = self
::COLON_STATE_COMMENT
;
539 # Illegal early close? This shouldn't happen D:
540 $state = self
::COLON_STATE_TEXT
;
543 $state = self
::COLON_STATE_TAG
;
546 case self
::COLON_STATE_CLOSETAG
:
549 if ( $ltLevel > 0 ) {
552 # ignore the excess close tag, but keep looking for
553 # colons. (This matches Parsoid behavior.)
554 wfDebug( __METHOD__
. ": Invalid input; too many close tags\n" );
556 $state = self
::COLON_STATE_TEXT
;
559 case self
::COLON_STATE_TAGSLASH
:
561 # Yes, a self-closed tag <blah/>
562 $state = self
::COLON_STATE_TEXT
;
564 # Probably we're jumping the gun, and this is an attribute
565 $state = self
::COLON_STATE_TAG
;
568 case self
::COLON_STATE_COMMENT
:
570 $state = self
::COLON_STATE_COMMENTDASH
;
573 case self
::COLON_STATE_COMMENTDASH
:
575 $state = self
::COLON_STATE_COMMENTDASHDASH
;
577 $state = self
::COLON_STATE_COMMENT
;
580 case self
::COLON_STATE_COMMENTDASHDASH
:
582 $state = self
::COLON_STATE_TEXT
;
584 $state = self
::COLON_STATE_COMMENT
;
588 throw new MWException( "State machine error in " . __METHOD__
);
591 if ( $ltLevel > 0 ||
$lcLevel > 0 ) {
593 __METHOD__
. ": Invalid input; not enough close tags " .
594 "(level $ltLevel/$lcLevel, state $state)\n"