From 7dc8c442be1122f4fa71f7f53efe4d837b555f2f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 25 Dec 2011 16:52:37 +0200 Subject: Improve the Typography library --- system/libraries/Typography.php | 99 +++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 58 deletions(-) (limited to 'system/libraries') diff --git a/system/libraries/Typography.php b/system/libraries/Typography.php index af6ca2bf2..0f48f32ad 100644 --- a/system/libraries/Typography.php +++ b/system/libraries/Typography.php @@ -1,13 +1,13 @@ - tags - var $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul'; + public $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul'; // Elements that should not have

and
tags within them. - var $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d'; + public $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d'; // 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'; + public $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'); + public $inner_block_required = array('blockquote'); // the last block element parsed - var $last_block_element = ''; + public $last_block_element = ''; // whether or not to protect quotes within { curly braces } - var $protect_braced_quotes = FALSE; + public $protect_braced_quotes = FALSE; /** * Auto Typography @@ -72,7 +72,7 @@ class CI_Typography { * @param bool whether to reduce more then two consecutive newlines to two * @return string */ - function auto_typography($str, $reduce_linebreaks = FALSE) + public function auto_typography($str, $reduce_linebreaks = FALSE) { if ($str == '') { @@ -127,35 +127,32 @@ class CI_Typography { // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG} $str = preg_replace("#<(/*)(".$this->inline_elements.")([ >])#i", "{@TAG}\\1\\2\\3", $str); - // Split the string at every tag. This expression creates an array with this prototype: - // - // [array] - // { - // [0] = - // [1] = Content... - // [2] = - // Etc... - // } + /* Split the string at every tag. This expression creates an array with this prototype: + * + * [array] + * { + * [0] = + * [1] = Content... + * [2] = + * Etc... + * } + */ $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text $str = ''; $process = TRUE; $paragraph = FALSE; - $current_chunk = 0; - $total_chunks = count($chunks); - foreach ($chunks as $chunk) + for ($i = 1, $c = count($chunks); $i <= $c; $i++) { - $current_chunk++; - // 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.").*?>#", $chunks[$i], $match))
 			{
 				if (preg_match("#".$this->skip_elements."#", $match[2]))
 				{
-					$process =  ($match[1] == '/') ? TRUE : FALSE;
+					$process = ($match[1] === '/');
 				}
 
 				if ($match[1] == '')
@@ -163,24 +160,24 @@ class CI_Typography {
 					$this->last_block_element = $match[2];
 				}
 
-				$str .= $chunk;
+				$str .= $chunks[$i];
 				continue;
 			}
 
-			if ($process == FALSE)
+			if ($process === FALSE)
 			{
-				$str .= $chunk;
+				$str .= $chunks[$i];
 				continue;
 			}
 
 			//  Force a newline to make sure end tags get processed by _format_newlines()
-			if ($current_chunk == $total_chunks)
+			if ($i === $c)
 			{
-				$chunk .= "\n";
+				$chunks[$i] .= "\n";
 			}
 
 			//  Convert Newlines into 

and
tags - $str .= $this->_format_newlines($chunk); + $str .= $this->_format_newlines($chunks[$i]); } // No opening block level tag? Add it if needed. @@ -265,7 +262,7 @@ class CI_Typography { * @param string * @return string */ - function format_characters($str) + public function format_characters($str) { static $table; @@ -325,18 +322,13 @@ class CI_Typography { * * Converts newline characters into either

tags or
* - * @access public + * @access private * @param string * @return string */ - function _format_newlines($str) + private function _format_newlines($str) { - if ($str == '') - { - return $str; - } - - if (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)) + if ($str == '' OR (strpos($str, "\n") === FALSE AND ! in_array($this->last_block_element, $this->inner_block_required))) { return $str; } @@ -373,11 +365,11 @@ class CI_Typography { * and we don't want double dashes converted to emdash entities, so they are marked with {@DD} * likewise double spaces are converted to {@NBS} to prevent entity conversion * - * @access public + * @access private * @param array * @return string */ - function _protect_characters($match) + private function _protect_characters($match) { return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]); } @@ -391,25 +383,16 @@ class CI_Typography { * @param string * @return string */ - function nl2br_except_pre($str) + public function nl2br_except_pre($str) { - $ex = explode("pre>",$str); - $ct = count($ex); - - $newstr = ""; - for ($i = 0; $i < $ct; $i++) + $newstr = ''; + for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++) { - if (($i % 2) == 0) + $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i]; + if ($ct - 1 !== $i) { - $newstr .= nl2br($ex[$i]); + $newstr .= 'pre>'; } - else - { - $newstr .= $ex[$i]; - } - - if ($ct - 1 != $i) - $newstr .= "pre>"; } return $newstr; -- cgit v1.2.3-24-g4f1b From 43aa8e4f66e0a109cfb80a8ea424edb04b9feecc Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 26 Dec 2011 16:53:18 +0200 Subject: Replace private with protected, so the class can be easily extended --- system/libraries/Typography.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system/libraries') diff --git a/system/libraries/Typography.php b/system/libraries/Typography.php index 0f48f32ad..651ba7bff 100644 --- a/system/libraries/Typography.php +++ b/system/libraries/Typography.php @@ -31,7 +31,7 @@ * Typography Class * * - * @access private + * @access protected * @category Helpers * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/libraries/typography.html @@ -322,11 +322,11 @@ class CI_Typography { * * Converts newline characters into either

tags or
* - * @access private + * @access protected * @param string * @return string */ - private function _format_newlines($str) + protected function _format_newlines($str) { if ($str == '' OR (strpos($str, "\n") === FALSE AND ! in_array($this->last_block_element, $this->inner_block_required))) { @@ -365,11 +365,11 @@ class CI_Typography { * and we don't want double dashes converted to emdash entities, so they are marked with {@DD} * likewise double spaces are converted to {@NBS} to prevent entity conversion * - * @access private + * @access protected * @param array * @return string */ - private function _protect_characters($match) + protected function _protect_characters($match) { return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]); } -- cgit v1.2.3-24-g4f1b