You are here

function Markdown_Parser::doLists in Markdown 5

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

File

./markdown.php, line 925

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,
  );
  foreach ($markers_relist as $marker_re) {

    # Re-usable pattern to match any entirel ul or ol list:
    $whole_list_re = '
				(								# $1 = whole list
				  (								# $2
					[ ]{0,' . $less_than_tab . '}
					(' . $marker_re . ')			# $3 = first list item marker
					[ ]+
				  )
				  (?s:.+?)
				  (								# $4
					  \\z
					|
					  \\n{2,}
					  (?=\\S)
					  (?!						# Negative lookahead for another list item marker
						[ ]*
						' . $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;
}