You are here

function Markdown_Parser::doLists in Markdown 6

Same name and namespace in other branches
  1. 5 markdown.php \Markdown_Parser::doLists()
1 call to Markdown_Parser::doLists()
Markdown_Parser::_processListItems_callback in ./markdown.php

File

./markdown.php, line 960

Class

Markdown_Parser

Code

function doLists($text) {

  #

  # Form HTML ordered (numbered) and unordered (bulleted) lists.

  #
  $less_than_tab = $this->tab_width - 1;

  # Re-usable patterns to match list item bullets and number markers:
  $marker_ul_re = '[*+-]';
  $marker_ol_re = '\\d+[\\.]';
  $marker_any_re = "(?:{$marker_ul_re}|{$marker_ol_re})";
  $markers_relist = array(
    $marker_ul_re => $marker_ol_re,
    $marker_ol_re => $marker_ul_re,
  );
  foreach ($markers_relist as $marker_re => $other_marker_re) {

    # Re-usable pattern to match any entirel ul or ol list:
    $whole_list_re = '
				(								# $1 = whole list
				  (								# $2
					([ ]{0,' . $less_than_tab . '})	# $3 = number of spaces
					(' . $marker_re . ')			# $4 = first list item marker
					[ ]+
				  )
				  (?s:.+?)
				  (								# $5
					  \\z
					|
					  \\n{2,}
					  (?=\\S)
					  (?!						# Negative lookahead for another list item marker
						[ ]*
						' . $marker_re . '[ ]+
					  )
					|
					  (?=						# Lookahead for another kind of list
					    \\n
						\\3						# Must have the same indentation
						' . $other_marker_re . '[ ]+
					  )
				  )
				)
			';

    // mx

    # We use a different prefix before nested lists than top-level lists.

    # See extended comment in _ProcessListItems().
    if ($this->list_level) {
      $text = preg_replace_callback('{
						^
						' . $whole_list_re . '
					}mx', array(
        &$this,
        '_doLists_callback',
      ), $text);
    }
    else {
      $text = preg_replace_callback('{
						(?:(?<=\\n)\\n|\\A\\n?) # Must eat the newline
						' . $whole_list_re . '
					}mx', array(
        &$this,
        '_doLists_callback',
      ), $text);
    }
  }
  return $text;
}