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 $lastSection = '';
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();
58 private function __construct( $text, $lineStart ) {
60 $this->lineStart
= $lineStart;
64 * If a pre or p is open, return the corresponding close tag and update
65 * the state. If no tag is open, return an empty string.
68 private function closeParagraph() {
70 if ( $this->lastSection
!== '' ) {
71 $result = '</' . $this->lastSection
. ">\n";
74 $this->lastSection
= '';
79 * getCommon() returns the length of the longest common substring
80 * of both arguments, starting at the beginning of both.
87 private function getCommon( $st1, $st2 ) {
88 $shorter = min( strlen( $st1 ), strlen( $st2 ) );
90 for ( $i = 0; $i < $shorter; ++
$i ) {
91 if ( $st1[$i] !== $st2[$i] ) {
99 * Open the list item element identified by the prefix character.
101 * @param string $char
105 private function openList( $char ) {
106 $result = $this->closeParagraph();
108 if ( '*' === $char ) {
109 $result .= "<ul><li>";
110 } elseif ( '#' === $char ) {
111 $result .= "<ol><li>";
112 } elseif ( ':' === $char ) {
113 $result .= "<dl><dd>";
114 } elseif ( ';' === $char ) {
115 $result .= "<dl><dt>";
116 $this->DTopen
= true;
118 $result = '<!-- ERR 1 -->';
125 * Close the current list item and open the next one.
126 * @param string $char
130 private function nextItem( $char ) {
131 if ( '*' === $char ||
'#' === $char ) {
132 return "</li>\n<li>";
133 } elseif ( ':' === $char ||
';' === $char ) {
135 if ( $this->DTopen
) {
138 if ( ';' === $char ) {
139 $this->DTopen
= true;
140 return $close . '<dt>';
142 $this->DTopen
= false;
143 return $close . '<dd>';
146 return '<!-- ERR 2 -->';
150 * Close the current list item identified by the prefix character.
151 * @param string $char
155 private function closeList( $char ) {
156 if ( '*' === $char ) {
157 $text = "</li></ul>";
158 } elseif ( '#' === $char ) {
159 $text = "</li></ol>";
160 } elseif ( ':' === $char ) {
161 if ( $this->DTopen
) {
162 $this->DTopen
= false;
163 $text = "</dt></dl>";
165 $text = "</dd></dl>";
168 return '<!-- ERR 3 -->';
177 private function execute() {
179 # Parsing through the text line by line. The main thing
180 # happening here is handling of block-level elements p, pre,
181 # and making lists from lines starting with * # : etc.
182 $textLines = StringUtils
::explode( "\n", $text );
184 $lastPrefix = $output = '';
185 $this->DTopen
= $inBlockElem = false;
187 $pendingPTag = false;
188 $inBlockquote = false;
190 foreach ( $textLines as $inputLine ) {
192 if ( !$this->lineStart
) {
193 $output .= $inputLine;
194 $this->lineStart
= true;
202 $lastPrefixLength = strlen( $lastPrefix );
203 $preCloseMatch = preg_match( '/<\\/pre/i', $inputLine );
204 $preOpenMatch = preg_match( '/<pre/i', $inputLine );
205 # If not in a <pre> element, scan for and figure out what prefixes are there.
206 if ( !$this->inPre
) {
207 # Multiple prefixes may abut each other for nested lists.
208 $prefixLength = strspn( $inputLine, '*#:;' );
209 $prefix = substr( $inputLine, 0, $prefixLength );
212 # ; and : are both from definition-lists, so they're equivalent
213 # for the purposes of determining whether or not we need to open/close
215 $prefix2 = str_replace( ';', ':', $prefix );
216 $t = substr( $inputLine, $prefixLength );
217 $this->inPre
= (bool)$preOpenMatch;
219 # Don't interpret any other prefixes in preformatted text
221 $prefix = $prefix2 = '';
226 if ( $prefixLength && $lastPrefix === $prefix2 ) {
227 # Same as the last item, so no need to deal with nesting or opening stuff
228 $output .= $this->nextItem( substr( $prefix, -1 ) );
229 $pendingPTag = false;
231 if ( substr( $prefix, -1 ) === ';' ) {
232 # The one nasty exception: definition lists work like this:
233 # ; title : definition text
234 # So we check for : in the remainder text to split up the
235 # title and definition, without b0rking links.
237 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
239 // Trim whitespace in list items
240 $output .= trim( $term ) . $this->nextItem( ':' );
243 } elseif ( $prefixLength ||
$lastPrefixLength ) {
244 # We need to open or close prefixes, or both.
246 # Either open or close a level...
247 $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
248 $pendingPTag = false;
250 # Close all the prefixes which aren't shared.
251 while ( $commonPrefixLength < $lastPrefixLength ) {
252 $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
256 # Continue the current prefix if appropriate.
257 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
258 $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
261 # Close an open <dt> if we have a <dd> (":") starting on this line
262 if ( $this->DTopen
&& $commonPrefixLength > 0 && $prefix[$commonPrefixLength - 1] === ':' ) {
263 $output .= $this->nextItem( ':' );
266 # Open prefixes where appropriate.
267 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
270 while ( $prefixLength > $commonPrefixLength ) {
271 $char = $prefix[$commonPrefixLength];
272 $output .= $this->openList( $char );
274 if ( ';' === $char ) {
275 # @todo FIXME: This is dupe of code above
276 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
278 // Trim whitespace in list items
279 $output .= trim( $term ) . $this->nextItem( ':' );
282 ++
$commonPrefixLength;
284 if ( !$prefixLength && $lastPrefix ) {
287 $lastPrefix = $prefix2;
290 # If we have no prefixes, go to paragraph mode.
291 if ( 0 == $prefixLength ) {
292 # No prefix (not in list)--go to paragraph mode
293 # @todo consider using a stack for nestable elements like span, table and div
295 // P-wrapping and indent-pre are suppressed inside, not outside
296 $blockElems = 'table|h1|h2|h3|h4|h5|h6|pre|p|ul|ol|dl';
297 // P-wrapping and indent-pre are suppressed outside, not inside
298 $antiBlockElems = 'td|th';
300 $openMatch = preg_match(
302 . "({$blockElems})|\\/({$antiBlockElems})|"
304 . '\\/?(tr|dt|dd|li)'
308 $closeMatch = preg_match(
310 . "\\/({$blockElems})|({$antiBlockElems})|"
312 . '\\/?(center|blockquote|div|hr|mw:)'
317 // Any match closes the paragraph, but only when `!$closeMatch`
318 // do we enter block mode. The oddities with table rows and
319 // cells are to avoid paragraph wrapping in interstitial spaces
320 // leading to fostered content.
322 if ( $openMatch ||
$closeMatch ) {
323 $pendingPTag = false;
324 // Only close the paragraph if we're not inside a <pre> tag, or if
325 // that <pre> tag has just been opened
326 if ( !$this->inPre ||
$preOpenMatch ) {
327 // @todo T7718: paragraph closed
328 $output .= $this->closeParagraph();
330 if ( $preOpenMatch && !$preCloseMatch ) {
334 while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t,
335 $bqMatch, PREG_OFFSET_CAPTURE
, $bqOffset )
337 $inBlockquote = !$bqMatch[1][0]; // is this a close tag?
338 $bqOffset = $bqMatch[0][1] +
strlen( $bqMatch[0][0] );
340 $inBlockElem = !$closeMatch;
341 } elseif ( !$inBlockElem && !$this->inPre
) {
342 if ( ' ' == substr( $t, 0, 1 )
343 && ( $this->lastSection
=== 'pre' ||
trim( $t ) != '' )
347 if ( $this->lastSection
!== 'pre' ) {
348 $pendingPTag = false;
349 $output .= $this->closeParagraph() . '<pre>';
350 $this->lastSection
= 'pre';
352 $t = substr( $t, 1 );
353 } elseif ( preg_match( '/^(?:<style\\b[^>]*>.*?<\\/style>\s*|<link\\b[^>]*>\s*)+$/iS', $t ) ) {
354 # T186965: <style> or <link> by itself on a line shouldn't open or close paragraphs.
355 # But it should clear $pendingPTag.
356 if ( $pendingPTag ) {
357 $output .= $this->closeParagraph();
358 $pendingPTag = false;
359 $this->lastSection
= '';
363 if ( trim( $t ) === '' ) {
364 if ( $pendingPTag ) {
365 $output .= $pendingPTag . '<br />';
366 $pendingPTag = false;
367 $this->lastSection
= 'p';
369 if ( $this->lastSection
!== 'p' ) {
370 $output .= $this->closeParagraph();
371 $this->lastSection
= '';
372 $pendingPTag = '<p>';
374 $pendingPTag = '</p><p>';
378 if ( $pendingPTag ) {
379 $output .= $pendingPTag;
380 $pendingPTag = false;
381 $this->lastSection
= 'p';
382 } elseif ( $this->lastSection
!== 'p' ) {
383 $output .= $this->closeParagraph() . '<p>';
384 $this->lastSection
= 'p';
390 # somewhere above we forget to get out of pre block (T2785)
391 if ( $preCloseMatch && $this->inPre
) {
392 $this->inPre
= false;
394 if ( $pendingPTag === false ) {
395 if ( $prefixLength === 0 ) {
399 // Trim whitespace in list items
400 $output .= trim( $t );
404 while ( $prefixLength ) {
405 $output .= $this->closeList( $prefix2[$prefixLength - 1] );
407 if ( !$prefixLength ) {
411 if ( $this->lastSection
!== '' ) {
412 $output .= '</' . $this->lastSection
. '>';
413 $this->lastSection
= '';
420 * Split up a string on ':', ignoring any occurrences inside tags
421 * to prevent illegal overlapping.
423 * @param string $str The string to split
424 * @param string &$before Set to everything before the ':'
425 * @param string &$after Set to everything after the ':'
426 * @throws MWException
427 * @return string The position of the ':', or false if none found
429 private function findColonNoLinks( $str, &$before, &$after ) {
430 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE
) ) {
435 if ( $m[0][0] === ':' ) {
436 # Easy; no tag nesting to worry about
437 $colonPos = $m[0][1];
438 $before = substr( $str, 0, $colonPos );
439 $after = substr( $str, $colonPos +
1 );
443 # Ugly state machine to walk through avoiding tags.
444 $state = self
::COLON_STATE_TEXT
;
447 $len = strlen( $str );
448 for ( $i = $m[0][1]; $i < $len; $i++
) {
452 case self
::COLON_STATE_TEXT
:
455 # Could be either a <start> tag or an </end> tag
456 $state = self
::COLON_STATE_TAGSTART
;
459 if ( $ltLevel === 0 ) {
461 $before = substr( $str, 0, $i );
462 $after = substr( $str, $i +
1 );
465 # Embedded in a tag; don't break it.
468 # Skip ahead looking for something interesting
469 if ( !preg_match( '/:|<|-\{/', $str, $m, PREG_OFFSET_CAPTURE
, $i ) ) {
470 # Nothing else interesting
473 if ( $m[0][0] === '-{' ) {
474 $state = self
::COLON_STATE_LC
;
478 # Skip ahead to next interesting character.
484 case self
::COLON_STATE_LC
:
485 # In language converter markup -{ ... }-
486 if ( !preg_match( '/-\{|\}-/', $str, $m, PREG_OFFSET_CAPTURE
, $i ) ) {
487 # Nothing else interesting to find; abort!
488 # We're nested in language converter markup, but there
489 # are no close tags left. Abort!
491 } elseif ( $m[0][0] === '-{' ) {
494 } elseif ( $m[0][0] === '}-' ) {
497 if ( $lcLevel === 0 ) {
498 $state = self
::COLON_STATE_TEXT
;
502 case self
::COLON_STATE_TAG
:
507 $state = self
::COLON_STATE_TEXT
;
510 # Slash may be followed by >?
511 $state = self
::COLON_STATE_TAGSLASH
;
517 case self
::COLON_STATE_TAGSTART
:
520 $state = self
::COLON_STATE_CLOSETAG
;
523 $state = self
::COLON_STATE_COMMENT
;
526 # Illegal early close? This shouldn't happen D:
527 $state = self
::COLON_STATE_TEXT
;
530 $state = self
::COLON_STATE_TAG
;
533 case self
::COLON_STATE_CLOSETAG
:
536 if ( $ltLevel > 0 ) {
539 # ignore the excess close tag, but keep looking for
540 # colons. (This matches Parsoid behavior.)
541 wfDebug( __METHOD__
. ": Invalid input; too many close tags\n" );
543 $state = self
::COLON_STATE_TEXT
;
546 case self
::COLON_STATE_TAGSLASH
:
548 # Yes, a self-closed tag <blah/>
549 $state = self
::COLON_STATE_TEXT
;
551 # Probably we're jumping the gun, and this is an attribute
552 $state = self
::COLON_STATE_TAG
;
555 case self
::COLON_STATE_COMMENT
:
557 $state = self
::COLON_STATE_COMMENTDASH
;
560 case self
::COLON_STATE_COMMENTDASH
:
562 $state = self
::COLON_STATE_COMMENTDASHDASH
;
564 $state = self
::COLON_STATE_COMMENT
;
567 case self
::COLON_STATE_COMMENTDASHDASH
:
569 $state = self
::COLON_STATE_TEXT
;
571 $state = self
::COLON_STATE_COMMENT
;
575 throw new MWException( "State machine error in " . __METHOD__
);
578 if ( $ltLevel > 0 ||
$lcLevel > 0 ) {
580 __METHOD__
. ": Invalid input; not enough close tags " .
581 "(level $ltLevel/$lcLevel, state $state)\n"