diff options
author | Rick Ellis <rick.ellis@ellislab.com> | 2008-09-13 01:15:52 +0200 |
---|---|---|
committer | Rick Ellis <rick.ellis@ellislab.com> | 2008-09-13 01:15:52 +0200 |
commit | 3bc791f34c4b71955b4a34d123c187958f29b7c2 (patch) | |
tree | 8efc5bb20615fbb19388902b1fbd384e0cdb7846 | |
parent | 7eb56d6d10e9ed500053a835480cd56a0d41b454 (diff) |
typography tweaks
-rw-r--r-- | system/helpers/typography_helper.php | 11 | ||||
-rw-r--r-- | system/libraries/Typography.php | 57 | ||||
-rw-r--r-- | user_guide/libraries/typography.html | 26 |
3 files changed, 45 insertions, 49 deletions
diff --git a/system/helpers/typography_helper.php b/system/helpers/typography_helper.php index 11acea1f7..5c5fffa80 100644 --- a/system/helpers/typography_helper.php +++ b/system/helpers/typography_helper.php @@ -60,16 +60,11 @@ if ( ! function_exists('nl2br_except_pre')) */
if ( ! function_exists('auto_typography'))
{
- function auto_typography($str, $allow_event_handlers = FALSE, $reduce_empty_lines = FALSE)
+ function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE)
{
- $CI =& get_instance();
-
+ $CI =& get_instance();
$CI->load->library('typography');
-
- $CI->typography->allow_js_event_handlers($allow_event_handlers);
- $CI->typography->reduce_empty_lines($reduce_empty_lines);
-
- return $CI->typography->auto_typography($str);
+ return $CI->typography->auto_typography($str, $strip_js_event_handlers, $reduce_linebreaks);
}
}
diff --git a/system/libraries/Typography.php b/system/libraries/Typography.php index dd5f928d9..27604421d 100644 --- a/system/libraries/Typography.php +++ b/system/libraries/Typography.php @@ -35,12 +35,6 @@ class CI_Typography { // Tags we want the parser to completely ignore when splitting the string. var $ignore_elements = 'a|b|i|em|strong|span|img|li'; - // Whether to allow Javascript event handlers to be sumitted inside tags - var $allow_js_event_handlers = FALSE; - - // Whether to reduce more than two consecutive empty lines to a maximum of two - var $reduce_empty_lines = FALSE; - /** * Nothing to do here... * @@ -59,14 +53,20 @@ class CI_Typography { * - Converts three dots into ellipsis. * - Converts double dashes into em-dashes. * - Converts two spaces into entities + * + * @access public + * @param string + * @param bool whether to strip javascript event handlers for security + * @param bool whether to reduce more then two consecutive newlines to two + * @return string */ - function auto_typography($str) + function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE) { if ($str == '') { return ''; } - + // Standardize Newlines to make matching easier if (strpos($str, "\r") !== FALSE) { @@ -75,13 +75,13 @@ class CI_Typography { // 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 ($this->reduce_empty_lines == TRUE) + if ($reduce_linebreaks === TRUE) { $str = preg_replace("/\n\n+/", "\n\n", $str); } // Do we allow JavaScript event handlers? If not, we strip them from within all tags - if ($this->allow_js_event_handlers == FALSE) + if ($strip_js_event_handlers === TRUE) { $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str); } @@ -140,7 +140,7 @@ class CI_Typography { } // Convert Newlines into <p> and <br /> tags - $str .= $this->format_newlines($chunk); + $str .= $this->_format_newlines($chunk); } // Convert quotes, elipsis, and em-dashes @@ -168,7 +168,7 @@ class CI_Typography { ); // Do we need to reduce empty lines? - if ($this->reduce_empty_lines == TRUE) + if ($reduce_linebreaks === TRUE) { $table['#<p>\n*</p>#'] = ''; } @@ -191,6 +191,10 @@ class CI_Typography { * This function mainly converts double and single quotes * to curly entities, but it also converts em-dashes, * double spaces, and ampersands + * + * @access public + * @param string + * @return string */ function format_characters($str) { @@ -242,7 +246,7 @@ class CI_Typography { * Converts newline characters into either <p> tags or <br /> * */ - function format_newlines($str) + function _format_newlines($str) { if ($str == '') { @@ -260,33 +264,6 @@ class CI_Typography { return '<p>'.$str.'</p>'; } - // -------------------------------------------------------------------- - - /** - * Allow JavaScript Event Handlers? - * - * For security reasons, by default we disallow JS event handlers - * - */ - function allow_js_event_handlers($val = FALSE) - { - $this->allow_js_event_handlers = ($val === FALSE) ? FALSE : TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Reduce empty lines - * - * Sets a flag that tells the parser to reduce any instances of more than - * two consecutive linebreaks down to two - * - */ - function reduce_empty_lines($val = FALSE) - { - $this->reduce_empty_lines = ($val === FALSE) ? FALSE : TRUE; - } - // ------------------------------------------------------------------------ /** diff --git a/user_guide/libraries/typography.html b/user_guide/libraries/typography.html index 91b205bb2..359c590ce 100644 --- a/user_guide/libraries/typography.html +++ b/user_guide/libraries/typography.html @@ -89,7 +89,21 @@ the following formatting:</p> <code>$string = $this->typography->auto_typography($string);</code>
-<p><strong>Note:</strong> Typographic formatting can be processor intensive, particularly if you have a lot of content being formatted.
+<h3>Parameters</h3>
+
+<p>There are two optional parameters:</p>
+
+<ol>
+ <li><strong>Strip JavaScript Event Handlers</strong>. Determines whether the parser should strip all JavaScript event handlers for security. Use bolean <kbd>TRUE</kbd> or <kbd>FALSE</kbd>.</li>
+ <li><strong>Reduce Linebreaks</strong>. Determines whether the parser should reduce more then two consecutive linebreaks down to two. Use bolean <kbd>TRUE</kbd> or <kbd>FALSE</kbd>.</li>
+</ol>
+
+<p>By default the parser strips JS Event handlers and does not reduce line breaks. In other words, if no parameters are submitted, it is the same as doing this:</p>
+
+<code>$string = $this->typography->auto_typography($string, <kbd>TRUE</kbd>, <kbd>FALSE</kbd>);</code>
+
+
+<p class="important"><strong>Note:</strong> Typographic formatting can be processor intensive, particularly if you have a lot of content being formatted.
If you choose to use this function you may want to consider
<a href="../general/caching.html">caching</a> your pages.</p>
@@ -97,9 +111,19 @@ If you choose to use this function you may want to consider <h2>convert_characters()</h2>
+<p>This function is similiar to the <dfn>auto_typography</dfn> function above, except that it only does character conversion:</p>
+<ul>
+<li>Quotes are converted to correctly facing curly quote entities, except those that appear within tags.</li>
+<li>Apostrophes are converted to curly apostrophy entities.</li>
+<li>Double dashes (either like -- this or like--this) are converted to em—dashes.</li>
+<li>Three consecutive periods either preceding or following a word are converted to ellipsis…</li>
+<li>Double spaces following sentences are converted to non-breaking spaces to mimic double spacing.</li>
+</ul>
+<p>Usage example:</p>
+<code>$string = $this->typography->convert_characters($string);</code>
<h2>nl2br_except_pre()</h2>
|