From d7b511270aa561732822e8ec3683da6c469da79d Mon Sep 17 00:00:00 2001 From: 8n-tech <8n-tech@users.noreply.github.com> Date: Mon, 20 Apr 2015 20:44:57 +1000 Subject: [PATCH] Completely rebuild paragraph parser. The paragraph parser will now encapsulate entire blocks in

tags. #471 - Code tags are fixed. Old posts will need to be regenerated. :( #458 - Parser improved. Signed-off-by: 8n-tech <8n-tech@users.noreply.github.com> --- inc/functions.php | 74 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index b7da4c7f..675e5932 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -2076,7 +2076,7 @@ function markup(&$body, $track_cites = false, $op = false) { if (mysql_version() < 50503) $body = mb_encode_numericentity($body, array(0x010000, 0xffffff, 0, 0xffffff), 'UTF-8'); - + foreach ($config['markup'] as $markup) { if (is_string($markup[1])) { $body = preg_replace($markup[0], $markup[1], $body); @@ -2273,17 +2273,26 @@ function markup(&$body, $track_cites = false, $op = false) { } $tracked_cites = array_unique($tracked_cites, SORT_REGULAR); - - //$body = preg_replace("/^\s*>.*$/m", '$0', $body); - + if ($config['strip_superfluous_returns']) $body = preg_replace('/\s+$/', '', $body); if ($config['markup_paragraphs']) { $paragraphs = explode("\n", $body); - $bodyNew = ""; + $bodyNew = ""; + $tagsOpen = false; + + // Matches , , but not and returns a + $matchOpen = "#<([A-Z][A-Z0-9]*)+(?:(?:\s+\w+(?:\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)>#i"; + // Matches returns a + $matchClose = "##i"; + $tagsOpened = array(); + $tagsClosed = array(); foreach ($paragraphs as $paragraph) { + + + // Determine if RTL based on content of line. if (strlen(trim($paragraph)) > 0) { $paragraphDirection = is_rtl($paragraph) ? "rtl" : "ltr"; } @@ -2291,6 +2300,8 @@ function markup(&$body, $track_cites = false, $op = false) { $paragraphDirection = "empty"; } + + // Add in a quote class for >quotes. if (strpos($paragraph, ">")===0) { $quoteClass = "quote"; } @@ -2298,12 +2309,63 @@ function markup(&$body, $track_cites = false, $op = false) { $quoteClass = ""; } - $bodyNew .= "

" . $paragraph . "

"; + // If tags are closed, start a new line. + if ($tagsOpen === false) { + $bodyNew .= "

"; + } + + // If tags are open, add the paragraph to our temporary holder instead. + if ($tagsOpen !== false) { + $tagsOpen .= $paragraph; + + // Recheck tags to see if we've formed a complete tag with this latest line. + if (preg_match_all($matchOpen, $tagsOpen, $tagsOpened) === preg_match_all($matchClose, $tagsOpen, $tagsClosed)) { + sort($tagsOpened[1]); + sort($tagsClosed[1]); + + // Double-check to make sure these are the same tags. + if (count(array_diff_assoc($tagsOpened[1], $tagsClosed[1])) === 0) { + // Tags are closed! \o/ + $bodyNew .= $tagsOpen; + $tagsOpen = false; + } + } + + if ($tagsOpen !== false) { + $tagsOpen .= "
"; + } + } + // If tags are closed, check to see if they are now open. + // This counts the number of open tags (that are not self-closing) against the number of complete tags. + // If they match completely, we are closed. + else if (preg_match_all($matchOpen, $paragraph, $tagsOpened) === preg_match_all($matchClose, $paragraph, $tagsClosed)) { + sort($tagsOpened[1]); + sort($tagsClosed[1]); + + // Double-check to make sure these are the same tags. + if (count(array_diff_assoc($tagsOpened[1], $tagsClosed[1])) === 0) { + $bodyNew .= $paragraph; + } + } + else { + // Tags are open! + $tagsOpen = $paragraph; + } + + // If tags are open, do not close it. + if (!$tagsOpen) { + $bodyNew .= "

"; + } + } + + if ($tagsOpen !== false) { + $bodyNew .= $tagsOpen; } $body = $bodyNew; } else { + $body = preg_replace("/^\s*>.*$/m", '$0', $body); $body = preg_replace("/\n/", '
', $body); }