From d5738d9fcafa831be8a1e709e42fdafb70d4330f Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 14 Nov 2008 16:53:34 +0000 Subject: added some block level tag tracking so we can add inner block level elements when required --- system/libraries/Typography.php | 82 +++++++++++++---------------------------- 1 file changed, 25 insertions(+), 57 deletions(-) (limited to 'system/libraries/Typography.php') diff --git a/system/libraries/Typography.php b/system/libraries/Typography.php index 29b871e23..0502eef1e 100644 --- a/system/libraries/Typography.php +++ b/system/libraries/Typography.php @@ -34,7 +34,13 @@ class CI_Typography { // Tags we want the parser to completely ignore when splitting the string. var $inline_elements = 'a|abbr|acronym|b|bdo|big|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|q|samp|select|small|span|strong|sub|sup|textarea|tt|var'; - + + // array of block level elements that require inner content to be within another block level element + var $inner_block_required = array('blockquote'); + + // the last block element parsed + var $last_block_element = ''; + // whether or not to protect quotes within { curly braces } var $protect_braced_quotes = FALSE; @@ -80,7 +86,7 @@ class CI_Typography { if ($reduce_linebreaks === TRUE) { $str = preg_replace("/\n\n+/", "\n\n", $str); - } + } // Convert quotes within tags to temporary markers. We don't want quotes converted // within tags so we'll temporarily convert them to {@DQ} and {@SQ} @@ -133,37 +139,30 @@ class CI_Typography { { // Are we dealing with a tag? If so, we'll skip the processing for this cycle. // Well also set the "process" flag which allows us to skip
 tags and a few other things.
-			if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunk, $match))
+			if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
 			{
 				if (preg_match("#".$this->skip_elements."#", $match[2]))
 				{
 					$process =  ($match[1] == '/') ? TRUE : FALSE;
 				}
 				
-				$str .= $chunk;
-				continue;
-			}
-			elseif (preg_match('/<(\/?)([a-z]*).*?>/s', $chunk, $tagmatch))
-			{
-				if ($tagmatch[1] == '/' && $tagmatch[2] == $this->last_tag)
+				if ($match[1] == '')
 				{
-					$process = FALSE;
+					$this->last_block_element = $match[2];
 				}
-				else
-				{
-					$process = TRUE;
-					$this->last_tag = $tagmatch[2];					
-				}
-			}
 
+				$str .= $chunk;
+				continue;
+			}
+			
 			if ($process == FALSE)
 			{
 				$str .= $chunk;
 				continue;
 			}
-			
+
 			//  Convert Newlines into 

and
tags - $str .= $this->format_characters($this->_format_newlines($chunk)); + $str .= $this->_format_newlines($chunk); } // is the whole of the content inside a block level element? @@ -171,20 +170,16 @@ class CI_Typography { { $str = "

{$str}

"; } - - - // some special linebreak cleanup - $str = preg_replace_callback('#<(?!/|'.$this->block_elements.')([^>]*)>

(.*?)

<(\w*)#si', array($this, '_linebreak_cleanup'), $str); - - // and cleanup empty paragraph tags sitting between two closing tags - $str = preg_replace('#()

(\s*)

()#si', '$1$2$3', $str); + // Convert quotes, elipsis, and em-dashes + $str = $this->format_characters($str); + // Final clean up $table = array( // If the user submitted their own paragraph tags within the text // we will retain them instead of using our tags. - '/(*?]>)

/' => '$1', // *?]>)

/' => '$1', // )+#' => '

', @@ -192,13 +187,10 @@ class CI_Typography { // Clean up stray paragraph tags that appear before block level elements '#

<('.$this->block_elements.')#' => '<$1', - - // Clean up open paragraph tags that appear before block level elements - '#

(\W)<('.$this->block_elements.')#' => '

$1<$2', // Clean up stray non-breaking spaces preceeding block elements '#[  ]+<('.$this->block_elements.')#' => ' <$1', - + // Replace the temporary markers we added earlier '/\{@TAG\}/' => '<', '/\{@DQ\}/' => '"', @@ -207,7 +199,7 @@ class CI_Typography { '/\{@NBS\}/' => ' ' ); - + // Do we need to reduce empty lines? if ($reduce_linebreaks === TRUE) { @@ -226,30 +218,6 @@ class CI_Typography { // -------------------------------------------------------------------- - /** - * Linebreak Cleanup - * - * Removes paragraph and line break tags inserted inbetween - * inline content and a new opening block level element - * - * @access private - * @param array - * @return string - */ - function _linebreak_cleanup($match) - { - if (in_array($match[3], explode('|', $this->block_elements))) - { - return "<{$match[1]}>".str_replace('
', '', $match[2])."<{$match[3]}"; - } - else - { - return $match[0]; - } - } - - // -------------------------------------------------------------------- - /** * Format Characters * @@ -320,8 +288,8 @@ class CI_Typography { { return $str; } - - if (strpos($str, "\n") === FALSE) + + if (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)) { return $str; } -- cgit v1.2.3-24-g4f1b