Source for file typography_functions.php
Documentation is available at typography_functions.php
* An open source application development framework for PHP 4.3.2 or newer
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
// ------------------------------------------------------------------------
* CodeIgniter Typography Helpers
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/helpers/typography_helper.html
// ------------------------------------------------------------------------
* Convert newlines to HTML line breaks except within PRE tags
for ($i =
0; $i <
$ct; $i++
)
$newstr .=
nl2br($ex[$i]);
// ------------------------------------------------------------------------
* Auto Typography Wrapper Function
return $TYPE->convert($str);
// ------------------------------------------------------------------------
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/helpers/
// Block level elements that should not be wrapped inside <p> tags
// Elements that should not have <p> and <br /> tags within them.
// Tags we want the parser to completely ignore when splitting the string.
* Main Processing Function
// Standardize Newlines to make matching easier
* If there are more than two consecutive line
* breaks we'll compress them down to a maximum
* of two since there's no benefit to more.
* Convert quotes within tags to temporary marker
* We don't want quotes converted within
* tags so we'll temporarily convert them to
for ($i =
0; $i <
count($matches['0']); $i++
)
str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
* Add closing/opening paragraph tags before/after "block" elements
* Since block elements (like <blockquotes>, <pre>, etc.) do not get
* wrapped in paragraph tags we will add a closing </p> tag just before
* each block element starts and an opening <p> tag right after the block element
* ends. Later on we'll do some further clean up.
* 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 < temporarily to: {@TAG}
* Split the string at every tag
* This creates an array with this prototype:
* [1] = Content contained between the tags
$chunks =
preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -
1, PREG_SPLIT_DELIM_CAPTURE|
PREG_SPLIT_NO_EMPTY);
* Build our finalized string
* We'll cycle through the array, skipping tags,
* and processing the contained text
foreach ($chunks as $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.
$process =
($match['1'] ==
'/') ?
TRUE :
FALSE;
// Convert Newlines into <p> and <br /> tags
// IMPORTANT: DO NOT ALTER THE ORDER OF THE ITEMS BELOW!
* Clean up paragraph tags before/after "block" elements
* Earlier we added <p></p> tags before/after block level elements.
* Then, we added paragraph tags around double line breaks. This
* potentially created incorrectly formatted paragraphs so we'll
// Convert Quotes and other characters
// Fix an artifact that happens during the paragraph replacement
// If the user submitted their own paragraph tags with class data
// in them we will retain them instead of using our tags.
// --------------------------------------------------------------------
* This function mainly converts double and single quotes
* to entities, but since these are directional, it does
* it based on some rules. It also converts em-dashes
* and a couple other things.
// These deal with quotes within quotes, like: "'hi here'"
$space =
array("\n", "\t", " ");
if ($current ===
FALSE) break;
$one_before =
substr($str, $start+
$current-
1, 1);
$one_after =
substr($str, $start+
$current+
2, 1);
if ( ! in_array($one_after, $space, TRUE) &&
$one_after !=
"<")
$one_before.
"“‘".
$one_after,
elseif ( ! in_array($one_before, $space, TRUE) &&
(in_array($one_after, $space, TRUE) OR $one_after ==
'<'))
$one_before.
"”’".
$one_after,
$start =
$start+
$current+
2;
if ($current ===
FALSE) break;
$one_before =
substr($str, $start+
$current-
1, 1);
$one_after =
substr($str, $start+
$current+
2, 1);
if ( in_array($one_before, $space, TRUE) &&
! in_array($one_after, $space, TRUE) &&
$one_after !=
"<")
$one_before.
"‘“".
$one_after,
elseif ( ! in_array($one_before, $space, TRUE) &&
$one_before !=
">")
$one_before.
"’”".
$one_after,
$start =
$start+
$current+
2;
// Are there quotes within a word, as in: ("something")
for ($i=
0, $s=
sizeof($matches['0']); $i <
$s; ++
$i)
if ( ! in_array($matches['1'][$i], $space, TRUE) &&
! in_array($matches['3'][$i], $space, TRUE))
$matches['1'][$i].
"“".
$matches['2'][$i].
"”".
$matches['3'][$i],
for ($i=
0, $s=
sizeof($matches['0']); $i <
$s; ++
$i)
if ( ! in_array($matches['1'][$i], $space, TRUE) &&
! in_array($matches['3'][$i], $space, TRUE))
$matches['1'][$i].
"‘".
$matches['2'][$i].
"’".
$matches['3'][$i],
// How about one apostrophe, as in Rick's
if ($current ===
FALSE) break;
$one_before =
substr($str, $start+
$current-
1, 1);
$one_after =
substr($str, $start+
$current+
1, 1);
if ( ! in_array($one_before, $space, TRUE) &&
! in_array($one_after, $space, TRUE))
$one_before.
"’".
$one_after,
$start =
$start+
$current+
2;
if ($current ===
FALSE) break;
$one_before =
substr($str, $start+
$current-
1, 1);
$one_after =
substr($str, $start+
$current+
2, 1);
$two_before =
substr($str, $start+
$current-
2, 1);
$two_after =
substr($str, $start+
$current+
3, 1);
if (( ! in_array($one_before, $space, TRUE) &&
! in_array($one_after, $space, TRUE))
( ! in_array($two_before, $space, TRUE) &&
! in_array($two_after, $space, TRUE) &&
$one_before ==
' ' &&
$one_after ==
' ')
$str =
str_replace( $two_before.
$one_before.
"--".
$one_after.
$two_after,
$two_before.
trim($one_before).
"—".
trim($one_after).
$two_after,
$start =
$start+
$current+
2;
$str =
preg_replace("#(\w)\.\.\.(\s|<br />|</p>)#", "\\1…\\2", $str);
$str =
preg_replace("#(\s|<br />|</p>)\.\.\.(\w)#", "\\1…\\2", $str);
// Run the translation array we defined above
// If there are any stray double quotes we'll catch them here
if ($current ===
FALSE) break;
$one_before =
substr($str, $start+
$current-
1, 1);
$one_after =
substr($str, $start+
$current+
1, 1);
if ( ! in_array($one_after, $space, TRUE))
$one_before.
"“".
$one_after,
elseif( ! in_array($one_before, $space, TRUE))
$one_before.
"”".
$one_after,
$start =
$start+
$current+
2;
if ($current ===
FALSE) break;
$one_before =
substr($str, $start+
$current-
1, 1);
$one_after =
substr($str, $start+
$current+
1, 1);
if ( ! in_array($one_after, $space, TRUE))
$one_before.
"‘".
$one_after,
elseif( ! in_array($one_before, $space, TRUE))
$one_before.
"’".
$one_after,
$start =
$start+
$current+
2;
// --------------------------------------------------------------------
* Converts newline characters into either <p> tags or <br />
if (strpos($str, "\n") ===
FALSE)
return '<p>'.
$str.
'</p>';
$str =
preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
return '<p>'.
$str.
'</p>';
Documentation generated on Tue, 22 Nov 2011 13:29:01 -0200 by phpDocumentor 1.4.3