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;
43 * Make lists from lines starting with ':', '*', '#', etc.
46 * @param bool $lineStart Whether or not this is at the start of a line.
47 * @return string The lists rendered as HTML
49 public static function doBlockLevels( $text, $lineStart ) {
50 $pass = new self( $text, $lineStart );
51 return $pass->execute();
57 private function __construct( $text, $lineStart ) {
59 $this->lineStart
= $lineStart;
63 * If a pre or p is open, return the corresponding close tag and update
64 * the state. If no tag is open, return an empty string.
67 private function closeParagraph() {
69 if ( $this->lastSection
!== '' ) {
70 $result = '</' . $this->lastSection
. ">\n";
73 $this->lastSection
= '';
78 * getCommon() returns the length of the longest common substring
79 * of both arguments, starting at the beginning of both.
86 private function getCommon( $st1, $st2 ) {
87 $shorter = min( strlen( $st1 ), strlen( $st2 ) );
89 for ( $i = 0; $i < $shorter; ++
$i ) {
90 if ( $st1[$i] !== $st2[$i] ) {
98 * Open the list item element identified by the prefix character.
100 * @param string $char
104 private function openList( $char ) {
105 $result = $this->closeParagraph();
107 if ( '*' === $char ) {
108 $result .= "<ul><li>";
109 } elseif ( '#' === $char ) {
110 $result .= "<ol><li>";
111 } elseif ( ':' === $char ) {
112 $result .= "<dl><dd>";
113 } elseif ( ';' === $char ) {
114 $result .= "<dl><dt>";
115 $this->DTopen
= true;
117 $result = '<!-- ERR 1 -->';
124 * Close the current list item and open the next one.
125 * @param string $char
129 private function nextItem( $char ) {
130 if ( '*' === $char ||
'#' === $char ) {
131 return "</li>\n<li>";
132 } elseif ( ':' === $char ||
';' === $char ) {
134 if ( $this->DTopen
) {
137 if ( ';' === $char ) {
138 $this->DTopen
= true;
139 return $close . '<dt>';
141 $this->DTopen
= false;
142 return $close . '<dd>';
145 return '<!-- ERR 2 -->';
149 * Close the current list item identified by the prefix character.
150 * @param string $char
154 private function closeList( $char ) {
155 if ( '*' === $char ) {
156 $text = "</li></ul>";
157 } elseif ( '#' === $char ) {
158 $text = "</li></ol>";
159 } elseif ( ':' === $char ) {
160 if ( $this->DTopen
) {
161 $this->DTopen
= false;
162 $text = "</dt></dl>";
164 $text = "</dd></dl>";
167 return '<!-- ERR 3 -->';
176 private function execute() {
178 # Parsing through the text line by line. The main thing
179 # happening here is handling of block-level elements p, pre,
180 # and making lists from lines starting with * # : etc.
181 $textLines = StringUtils
::explode( "\n", $text );
183 $lastPrefix = $output = '';
184 $this->DTopen
= $inBlockElem = false;
186 $pendingPTag = false;
187 $inBlockquote = false;
189 foreach ( $textLines as $inputLine ) {
191 if ( !$this->lineStart
) {
192 $output .= $inputLine;
193 $this->lineStart
= true;
201 $lastPrefixLength = strlen( $lastPrefix );
202 $preCloseMatch = preg_match( '/<\\/pre/i', $inputLine );
203 $preOpenMatch = preg_match( '/<pre/i', $inputLine );
204 # If not in a <pre> element, scan for and figure out what prefixes are there.
205 if ( !$this->inPre
) {
206 # Multiple prefixes may abut each other for nested lists.
207 $prefixLength = strspn( $inputLine, '*#:;' );
208 $prefix = substr( $inputLine, 0, $prefixLength );
211 # ; and : are both from definition-lists, so they're equivalent
212 # for the purposes of determining whether or not we need to open/close
214 $prefix2 = str_replace( ';', ':', $prefix );
215 $t = substr( $inputLine, $prefixLength );
216 $this->inPre
= (bool)$preOpenMatch;
218 # Don't interpret any other prefixes in preformatted text
220 $prefix = $prefix2 = '';
225 if ( $prefixLength && $lastPrefix === $prefix2 ) {
226 # Same as the last item, so no need to deal with nesting or opening stuff
227 $output .= $this->nextItem( substr( $prefix, -1 ) );
228 $pendingPTag = false;
230 if ( substr( $prefix, -1 ) === ';' ) {
231 # The one nasty exception: definition lists work like this:
232 # ; title : definition text
233 # So we check for : in the remainder text to split up the
234 # title and definition, without b0rking links.
236 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
238 $output .= $term . $this->nextItem( ':' );
241 } elseif ( $prefixLength ||
$lastPrefixLength ) {
242 # We need to open or close prefixes, or both.
244 # Either open or close a level...
245 $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
246 $pendingPTag = false;
248 # Close all the prefixes which aren't shared.
249 while ( $commonPrefixLength < $lastPrefixLength ) {
250 $output .= $this->closeList( $lastPrefix[$lastPrefixLength - 1] );
254 # Continue the current prefix if appropriate.
255 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
256 $output .= $this->nextItem( $prefix[$commonPrefixLength - 1] );
259 # Open prefixes where appropriate.
260 if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
263 while ( $prefixLength > $commonPrefixLength ) {
264 $char = substr( $prefix, $commonPrefixLength, 1 );
265 $output .= $this->openList( $char );
267 if ( ';' === $char ) {
268 # @todo FIXME: This is dupe of code above
269 if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
271 $output .= $term . $this->nextItem( ':' );
274 ++
$commonPrefixLength;
276 if ( !$prefixLength && $lastPrefix ) {
279 $lastPrefix = $prefix2;
282 # If we have no prefixes, go to paragraph mode.
283 if ( 0 == $prefixLength ) {
284 # No prefix (not in list)--go to paragraph mode
285 # @todo consider using a stack for nestable elements like span, table and div
286 $openMatch = preg_match(
287 '/(?:<table|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|'
288 . '<p|<ul|<ol|<dl|<li|<\\/tr|<\\/td|<\\/th)/iS',
291 $closeMatch = preg_match(
292 '/(?:<\\/table|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'
293 . '<td|<th|<\\/?blockquote|<\\/?div|<hr|<\\/pre|<\\/p|<\\/mw:|'
294 . Parser
::MARKER_PREFIX
295 . '-pre|<\\/li|<\\/ul|<\\/ol|<\\/dl|<\\/?center)/iS',
299 if ( $openMatch ||
$closeMatch ) {
300 $pendingPTag = false;
301 # @todo bug 5718: paragraph closed
302 $output .= $this->closeParagraph();
303 if ( $preOpenMatch && !$preCloseMatch ) {
307 while ( preg_match( '/<(\\/?)blockquote[\s>]/i', $t,
308 $bqMatch, PREG_OFFSET_CAPTURE
, $bqOffset )
310 $inBlockquote = !$bqMatch[1][0]; // is this a close tag?
311 $bqOffset = $bqMatch[0][1] +
strlen( $bqMatch[0][0] );
313 $inBlockElem = !$closeMatch;
314 } elseif ( !$inBlockElem && !$this->inPre
) {
315 if ( ' ' == substr( $t, 0, 1 )
316 && ( $this->lastSection
=== 'pre' ||
trim( $t ) != '' )
320 if ( $this->lastSection
!== 'pre' ) {
321 $pendingPTag = false;
322 $output .= $this->closeParagraph() . '<pre>';
323 $this->lastSection
= 'pre';
325 $t = substr( $t, 1 );
328 if ( trim( $t ) === '' ) {
329 if ( $pendingPTag ) {
330 $output .= $pendingPTag . '<br />';
331 $pendingPTag = false;
332 $this->lastSection
= 'p';
334 if ( $this->lastSection
!== 'p' ) {
335 $output .= $this->closeParagraph();
336 $this->lastSection
= '';
337 $pendingPTag = '<p>';
339 $pendingPTag = '</p><p>';
343 if ( $pendingPTag ) {
344 $output .= $pendingPTag;
345 $pendingPTag = false;
346 $this->lastSection
= 'p';
347 } elseif ( $this->lastSection
!== 'p' ) {
348 $output .= $this->closeParagraph() . '<p>';
349 $this->lastSection
= 'p';
355 # somewhere above we forget to get out of pre block (bug 785)
356 if ( $preCloseMatch && $this->inPre
) {
357 $this->inPre
= false;
359 if ( $pendingPTag === false ) {
361 if ( $prefixLength === 0 ) {
366 while ( $prefixLength ) {
367 $output .= $this->closeList( $prefix2[$prefixLength - 1] );
369 if ( !$prefixLength ) {
373 if ( $this->lastSection
!== '' ) {
374 $output .= '</' . $this->lastSection
. '>';
375 $this->lastSection
= '';
382 * Split up a string on ':', ignoring any occurrences inside tags
383 * to prevent illegal overlapping.
385 * @param string $str The string to split
386 * @param string &$before Set to everything before the ':'
387 * @param string &$after Set to everything after the ':'
388 * @throws MWException
389 * @return string The position of the ':', or false if none found
391 private function findColonNoLinks( $str, &$before, &$after ) {
392 $colonPos = strpos( $str, ':' );
393 if ( $colonPos === false ) {
398 $ltPos = strpos( $str, '<' );
399 if ( $ltPos === false ||
$ltPos > $colonPos ) {
400 # Easy; no tag nesting to worry about
401 $before = substr( $str, 0, $colonPos );
402 $after = substr( $str, $colonPos +
1 );
406 # Ugly state machine to walk through avoiding tags.
407 $state = self
::COLON_STATE_TEXT
;
409 $len = strlen( $str );
410 for ( $i = 0; $i < $len; $i++
) {
414 case self
::COLON_STATE_TEXT
:
417 # Could be either a <start> tag or an </end> tag
418 $state = self
::COLON_STATE_TAGSTART
;
421 if ( $level === 0 ) {
423 $before = substr( $str, 0, $i );
424 $after = substr( $str, $i +
1 );
427 # Embedded in a tag; don't break it.
430 # Skip ahead looking for something interesting
431 $colonPos = strpos( $str, ':', $i );
432 if ( $colonPos === false ) {
433 # Nothing else interesting
436 $ltPos = strpos( $str, '<', $i );
437 if ( $level === 0 ) {
438 if ( $ltPos === false ||
$colonPos < $ltPos ) {
440 $before = substr( $str, 0, $colonPos );
441 $after = substr( $str, $colonPos +
1 );
445 if ( $ltPos === false ) {
446 # Nothing else interesting to find; abort!
447 # We're nested, but there's no close tags left. Abort!
450 # Skip ahead to next tag start
452 $state = self
::COLON_STATE_TAGSTART
;
455 case self
::COLON_STATE_TAG
:
460 $state = self
::COLON_STATE_TEXT
;
463 # Slash may be followed by >?
464 $state = self
::COLON_STATE_TAGSLASH
;
470 case self
::COLON_STATE_TAGSTART
:
473 $state = self
::COLON_STATE_CLOSETAG
;
476 $state = self
::COLON_STATE_COMMENT
;
479 # Illegal early close? This shouldn't happen D:
480 $state = self
::COLON_STATE_TEXT
;
483 $state = self
::COLON_STATE_TAG
;
486 case self
::COLON_STATE_CLOSETAG
:
491 wfDebug( __METHOD__
. ": Invalid input; too many close tags\n" );
494 $state = self
::COLON_STATE_TEXT
;
497 case self
::COLON_STATE_TAGSLASH
:
499 # Yes, a self-closed tag <blah/>
500 $state = self
::COLON_STATE_TEXT
;
502 # Probably we're jumping the gun, and this is an attribute
503 $state = self
::COLON_STATE_TAG
;
506 case self
::COLON_STATE_COMMENT
:
508 $state = self
::COLON_STATE_COMMENTDASH
;
511 case self
::COLON_STATE_COMMENTDASH
:
513 $state = self
::COLON_STATE_COMMENTDASHDASH
;
515 $state = self
::COLON_STATE_COMMENT
;
518 case self
::COLON_STATE_COMMENTDASHDASH
:
520 $state = self
::COLON_STATE_TEXT
;
522 $state = self
::COLON_STATE_COMMENT
;
526 throw new MWException( "State machine error in " . __METHOD__
);
530 wfDebug( __METHOD__
. ": Invalid input; not enough close tags (level $level, state $state)\n" );