tags var $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 and {$str} *?]>) /' => '$1', // )+#' => ' )+/' => ' ',
// Clean up stray paragraph tags that appear before block level elements
'# \n* tags or ", $str);
// Convert single spaces to '.$str.' <\/p>(.*)/", "\\1", $str, 1);
return $str;
}
// ------------------------------------------------------------------------
/**
* Convert newlines to HTML line breaks except within PRE tags
*
* @access public
* @param string
* @return string
*/
function nl2br_except_pre($str)
{
$ex = explode("pre>",$str);
$ct = count($ex);
$newstr = "";
for ($i = 0; $i < $ct; $i++)
{
if (($i % 2) == 0)
{
$newstr .= nl2br($ex[$i]);
}
else
{
$newstr .= $ex[$i];
}
if ($ct - 1 != $i)
$newstr .= "pre>";
}
return $newstr;
}
}
// END Typography Class
/* End of file Typography.php */
/* Location: ./system/libraries/Typography.php */
tags within them.
var $skip_elements = 'p|pre|ol|ul|dl|object|table';
// Tags we want the parser to completely ignore when splitting the string.
var $inline_elements = 'a|abbr|acronym|b|bdo|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|samp|select|span|strong|sub|sup|textarea|var';
// whether or not to protect quotes within { curly braces }
var $protect_braced_quotes = FALSE;
/**
* Nothing to do here...
*
*/
function CI_Typography()
{
}
/**
* Auto Typography
*
* This function converts text, making it typographically correct:
* - Converts double spaces into paragraphs.
* - Converts single line breaks into
tags
* - Converts single and double quotes into correctly facing curly quote entities.
* - Converts three dots into ellipsis.
* - Converts double dashes into em-dashes.
* - Converts two spaces into entities
*
* @access public
* @param string
* @param bool whether to reduce more then two consecutive newlines to two
* @return string
*/
function auto_typography($str, $reduce_linebreaks = FALSE)
{
if ($str == '')
{
return '';
}
// Standardize Newlines to make matching easier
if (strpos($str, "\r") !== FALSE)
{
$str = str_replace(array("\r\n", "\r"), "\n", $str);
}
// Reduce line breaks. If there are more than two consecutive linebreaks
// we'll compress them down to a maximum of two since there's no benefit to more.
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}
// 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
if (preg_match_all("#\<.+?>#si", $str, $matches))
{
for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
{
$str = str_replace($matches[0][$i],
str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $matches[0][$i]),
$str);
}
}
if ($this->protect_braced_quotes === TRUE)
{
if (preg_match_all("#\{.+?}#si", $str, $matches))
{
for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
{
$str = str_replace($matches[0][$i],
str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches[0][$i]),
$str);
}
}
}
// Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
// it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
// 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] = tags and a few other things.
if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
{
if (preg_match("#".$this->skip_elements."#", $match[2]))
{
$process = ($match[1] == '/') ? TRUE : FALSE;
}
$str .= $chunk;
continue;
}
if ($process == FALSE)
{
$str .= $chunk;
continue;
}
// Convert Newlines into
tags
$str .= $this->_format_newlines($chunk);
}
// is the whole of the content inside a block level element?
if ( ! preg_match("/^<(?:".$this->block_elements.")/i", $str, $match))
{
$str = "
*
* @access public
* @param string
* @return string
*/
function _format_newlines($str)
{
if ($str == '')
{
return $str;
}
if (strpos($str, "\n") === FALSE)
{
return $str;
}
// Convert two consecutive newlines to paragraphs
$str = str_replace("\n\n", "
tags
$str = preg_replace("/([^\n])(\n)([^\n])/", "\\1
\\2\\3", $str);
// Wrap the whole enchilada in enclosing paragraphs
if ($str != "\n")
{
$str = '