From 86237084481ae3dc2078ff9dd1bc03e81e36141f Mon Sep 17 00:00:00 2001 From: Michel Fortin Date: Mon, 29 Aug 2016 16:19:22 -0400 Subject: [PATCH] Fixing handling of list items with span-level content so that nested lists aren't passed through the span gamut. This more principled approach should have no effect on the output other than prevent line breaks from appearing between the text of a list item and its nested list. It has become somewhat necessary after adding `hard_wrap`. --- Michelf/Markdown.php | 14 ++++++++------ Michelf/MarkdownExtra.php | 7 ++++--- Readme.md | 5 +++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Michelf/Markdown.php b/Michelf/Markdown.php index ce51d53..3acb100 100644 --- a/Michelf/Markdown.php +++ b/Michelf/Markdown.php @@ -1175,8 +1175,7 @@ class Markdown implements MarkdownInterface { } else { // Recursion for sub-lists: $item = $this->doLists($this->outdent($item)); - $item = preg_replace('/\n+$/', '', $item); - $item = $this->runSpanGamut($item); + $item = $this->formParagraphs($item, false); } return "
  • " . $item . "
  • \n"; @@ -1476,10 +1475,11 @@ class Markdown implements MarkdownInterface { /** * Parse paragraphs * - * @param string $text String to process with HTML

    tags + * @param string $text String to process in paragraphs + * @param boolean $wrap_in_p Whether paragraphs should be wrapped in

    tags * @return string */ - protected function formParagraphs($text) { + protected function formParagraphs($text, $wrap_in_p = true) { // Strip leading and trailing lines: $text = preg_replace('/\A\n+|\n+\z/', '', $text); @@ -1490,8 +1490,10 @@ class Markdown implements MarkdownInterface { if (!preg_match('/^B\x1A[0-9]+B$/', $value)) { // Is a paragraph. $value = $this->runSpanGamut($value); - $value = preg_replace('/^([ ]*)/', "

    ", $value); - $value .= "

    "; + if ($wrap_in_p) { + $value = preg_replace('/^([ ]*)/', "

    ", $value); + $value .= "

    "; + } $grafs[$key] = $this->unhash($value); } else { // Is a block. diff --git a/Michelf/MarkdownExtra.php b/Michelf/MarkdownExtra.php index 48edcea..9b47a26 100644 --- a/Michelf/MarkdownExtra.php +++ b/Michelf/MarkdownExtra.php @@ -1505,10 +1505,11 @@ class MarkdownExtra extends \Michelf\Markdown { /** * Parse text into paragraphs - * @param string $text String to process with html

    tags + * @param string $text String to process in paragraphs + * @param boolean $wrap_in_p Whether paragraphs should be wrapped in

    tags * @return string HTML output */ - protected function formParagraphs($text) { + protected function formParagraphs($text, $wrap_in_p = true) { // Strip leading and trailing lines: $text = preg_replace('/\A\n+|\n+\z/', '', $text); @@ -1520,7 +1521,7 @@ class MarkdownExtra extends \Michelf\Markdown { // Check if this should be enclosed in a paragraph. // Clean tag hashes & block tag hashes are left alone. - $is_p = !preg_match('/^B\x1A[0-9]+B|^C\x1A[0-9]+C$/', $value); + $is_p = $wrap_in_p && !preg_match('/^B\x1A[0-9]+B|^C\x1A[0-9]+C$/', $value); if ($is_p) { $value = "

    $value

    "; diff --git a/Readme.md b/Readme.md index 418571a..fd8809f 100644 --- a/Readme.md +++ b/Readme.md @@ -181,6 +181,11 @@ Current Version: to the standard Markdown syntax these newlines are ignored unless they a preceded by two spaces. Thanks to Jonathan Cohlmeyer for the implementation. +* Improved the parsing of list items to fix problematic cases that came to + light with the addition of `hard_wrap`. This should have no effect on the + output except span-level list items that ended with two spaces (and thus + ended with a line break). + * Added a `code_span_content_func` configuration variable which takes a function that will convert the content of the code span to HTML. This can be useful to implement syntax highlighting. Although contrary to its