summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Sturgeon <email@philsturgeon.co.uk>2011-12-26 18:56:19 +0100
committerPhil Sturgeon <email@philsturgeon.co.uk>2011-12-26 18:56:19 +0100
commitd9f0a1496af1dfca7c317028708b2cd1e5727cea (patch)
tree3b338bdf1a5e7ffa822d59bbe2672d5ba6b1bdd7
parent2d91ef7e274eb5bd7663349d14b8dc9ce3629431 (diff)
parent43aa8e4f66e0a109cfb80a8ea424edb04b9feecc (diff)
Merge pull request #829 from narfbg/develop-typography
Improve the Typography library
-rw-r--r--system/libraries/Typography.php101
1 files changed, 42 insertions, 59 deletions
diff --git a/system/libraries/Typography.php b/system/libraries/Typography.php
index af6ca2bf2..651ba7bff 100644
--- a/system/libraries/Typography.php
+++ b/system/libraries/Typography.php
@@ -1,13 +1,13 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* NOTICE OF LICENSE
- *
+ *
* Licensed under the Open Software License version 3.0
- *
+ *
* This source file is subject to the Open Software License (OSL 3.0) that is
* bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
@@ -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
@@ -39,22 +39,22 @@
class CI_Typography {
// Block level elements that should not be wrapped inside <p> 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 <p> and <br /> 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] = <opening tag>
- // [1] = Content...
- // [2] = <closing tag>
- // Etc...
- // }
+ /* Split the string at every tag. This expression creates an array with this prototype:
+ *
+ * [array]
+ * {
+ * [0] = <opening tag>
+ * [1] = Content...
+ * [2] = <closing tag>
+ * 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 <pre> 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 <p> and <br /> 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 <p> tags or <br />
*
- * @access public
+ * @access protected
* @param string
* @return string
*/
- function _format_newlines($str)
+ protected 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 protected
* @param array
* @return string
*/
- function _protect_characters($match)
+ protected 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;