blob: 27604421d1d61c32c703fb8450274bce03548b43 [file] [log] [blame]
Rick Ellis4c938ae2008-09-10 22:58:38 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Rick Ellis08e38122008-09-12 06:21:25 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Rick Ellis4c938ae2008-09-10 22:58:38 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Typography Class
20 *
21 *
22 * @access private
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/
26 */
27class CI_Typography {
28
29 // Block level elements that should not be wrapped inside <p> tags
Rick Ellis16e3c8c2008-09-11 20:12:26 +000030 var $block_elements = 'p|div|blockquote|pre|code|h\d|script|ol|ul';
Rick Ellis4c938ae2008-09-10 22:58:38 +000031
32 // Elements that should not have <p> and <br /> tags within them.
Rick Ellis3ef59b72008-09-11 21:35:39 +000033 var $skip_elements = 'p|pre|ol|ul';
Rick Ellis4c938ae2008-09-10 22:58:38 +000034
35 // Tags we want the parser to completely ignore when splitting the string.
36 var $ignore_elements = 'a|b|i|em|strong|span|img|li';
37
Rick Ellis4c938ae2008-09-10 22:58:38 +000038 /**
Rick Ellis142f8ed2008-09-12 06:14:52 +000039 * Nothing to do here...
40 *
41 */
42 function CI_Typography()
43 {
44 }
45
46 /**
Rick Ellis08e38122008-09-12 06:21:25 +000047 * Auto Typography
Rick Ellis4c938ae2008-09-10 22:58:38 +000048 *
Rick Ellis08e38122008-09-12 06:21:25 +000049 * This function converts text, making it typographically correct:
50 * - Converts double spaces into paragraphs.
51 * - Converts single line breaks into <br /> tags
52 * - Converts single and double quotes into correctly facing curly quote entities.
53 * - Converts three dots into ellipsis.
54 * - Converts double dashes into em-dashes.
55 * - Converts two spaces into entities
Rick Ellis3bc791f2008-09-12 23:15:52 +000056 *
57 * @access public
58 * @param string
59 * @param bool whether to strip javascript event handlers for security
60 * @param bool whether to reduce more then two consecutive newlines to two
61 * @return string
Rick Ellis4c938ae2008-09-10 22:58:38 +000062 */
Rick Ellis3bc791f2008-09-12 23:15:52 +000063 function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE)
Rick Ellis4c938ae2008-09-10 22:58:38 +000064 {
65 if ($str == '')
66 {
67 return '';
68 }
Rick Ellis3bc791f2008-09-12 23:15:52 +000069
Rick Ellis4c938ae2008-09-10 22:58:38 +000070 // Standardize Newlines to make matching easier
71 if (strpos($str, "\r") !== FALSE)
72 {
73 $str = str_replace(array("\r\n", "\r"), "\n", $str);
74 }
Rick Ellis16e3c8c2008-09-11 20:12:26 +000075
76 // Reduce line breaks. If there are more than two consecutive linebreaks
77 // we'll compress them down to a maximum of two since there's no benefit to more.
Rick Ellis3bc791f2008-09-12 23:15:52 +000078 if ($reduce_linebreaks === TRUE)
Rick Ellis16e3c8c2008-09-11 20:12:26 +000079 {
80 $str = preg_replace("/\n\n+/", "\n\n", $str);
81 }
Rick Ellis4c938ae2008-09-10 22:58:38 +000082
Rick Ellis16e3c8c2008-09-11 20:12:26 +000083 // Do we allow JavaScript event handlers? If not, we strip them from within all tags
Rick Ellis3bc791f2008-09-12 23:15:52 +000084 if ($strip_js_event_handlers === TRUE)
Rick Ellis18bd8b52008-09-10 23:40:35 +000085 {
Rick Ellise320bc82008-09-11 18:11:31 +000086 $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
Rick Ellis18bd8b52008-09-10 23:40:35 +000087 }
88
Rick Ellis339fab72008-09-12 06:04:39 +000089 // Convert quotes within tags to temporary markers. We don't want quotes converted
90 // within tags so we'll temporarily convert them to {@DQ} and {@SQ}
Rick Ellis4c938ae2008-09-10 22:58:38 +000091 if (preg_match_all("#\<.+?>#si", $str, $matches))
92 {
93 for ($i = 0; $i < count($matches['0']); $i++)
94 {
95 $str = str_replace($matches['0'][$i],
96 str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
97 $str);
98 }
99 }
Rick Ellis4c938ae2008-09-10 22:58:38 +0000100
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000101 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
102 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Rick Ellis339fab72008-09-12 06:04:39 +0000103 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
Rick Ellis4c938ae2008-09-10 22:58:38 +0000104 $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str);
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000105
106 // Split the string at every tag. This expression creates an array with this prototype:
107 //
108 // [array]
109 // {
110 // [0] = <opening tag>
111 // [1] = Content...
112 // [2] = <closing tag>
113 // Etc...
114 // }
Rick Ellis4c938ae2008-09-10 22:58:38 +0000115 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
116
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000117 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Rick Ellis4c938ae2008-09-10 22:58:38 +0000118 $str = '';
119 $process = TRUE;
Rick Ellis3ef59b72008-09-11 21:35:39 +0000120 $paragraph = FALSE;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000121 foreach ($chunks as $chunk)
122 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000123 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
124 // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
Rick Ellis4c938ae2008-09-10 22:58:38 +0000125 if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
126 {
Rick Ellis3ef59b72008-09-11 21:35:39 +0000127 if (preg_match("#".$this->skip_elements."#", $match[2]))
Rick Ellis4c938ae2008-09-10 22:58:38 +0000128 {
Rick Ellis3ef59b72008-09-11 21:35:39 +0000129 $process = ($match[1] == '/') ? TRUE : FALSE;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000130 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000131
Rick Ellis4c938ae2008-09-10 22:58:38 +0000132 $str .= $chunk;
133 continue;
134 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000135
Rick Ellis4c938ae2008-09-10 22:58:38 +0000136 if ($process == FALSE)
137 {
138 $str .= $chunk;
139 continue;
140 }
141
142 // Convert Newlines into <p> and <br /> tags
Rick Ellis3bc791f2008-09-12 23:15:52 +0000143 $str .= $this->_format_newlines($chunk);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000144 }
145
Rick Ellis339fab72008-09-12 06:04:39 +0000146 // Convert quotes, elipsis, and em-dashes
Rick Ellis4c938ae2008-09-10 22:58:38 +0000147 $str = $this->format_characters($str);
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000148
Rick Ellis4c938ae2008-09-10 22:58:38 +0000149 // Final clean up
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000150 $table = array(
Rick Ellis4c938ae2008-09-10 22:58:38 +0000151
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000152 // If the user submitted their own paragraph tags within the text
153 // we will retain them instead of using our tags.
154 '/(<p.*?>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
155
Rick Ellis25319a32008-09-11 20:13:25 +0000156 // Reduce multiple instances of opening/closing paragraph tags to a single one
Rick Ellis339fab72008-09-12 06:04:39 +0000157 '#(</p>)+#' => '</p>',
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000158 '/(<p><p>)+/' => '<p>',
159
160 // Clean up stray paragraph tags that appear before block level elements
Rick Ellis339fab72008-09-12 06:04:39 +0000161 '#<p></p><('.$this->block_elements.')#' => '<$1',
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000162
163 // Replace the temporary markers we added earlier
164 '/\{@TAG\}/' => '<',
165 '/\{@DQ\}/' => '"',
166 '/\{@SQ\}/' => "'"
167
168 );
169
Rick Ellis339fab72008-09-12 06:04:39 +0000170 // Do we need to reduce empty lines?
Rick Ellis3bc791f2008-09-12 23:15:52 +0000171 if ($reduce_linebreaks === TRUE)
Rick Ellis339fab72008-09-12 06:04:39 +0000172 {
173 $table['#<p>\n*</p>#'] = '';
174 }
175 else
176 {
177 // If we have empty paragraph tags we add a non-breaking space
178 // otherwise most browsers won't treat them as true paragraphs
179 $table['#<p></p>#'] = '<p>&nbsp;</p>';
180 }
181
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000182 return preg_replace(array_keys($table), $table, $str);
183
Rick Ellis4c938ae2008-09-10 22:58:38 +0000184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Format Characters
190 *
191 * This function mainly converts double and single quotes
Derek Jonesab504b82008-09-11 17:04:30 +0000192 * to curly entities, but it also converts em-dashes,
193 * double spaces, and ampersands
Rick Ellis3bc791f2008-09-12 23:15:52 +0000194 *
195 * @access public
196 * @param string
197 * @return string
Rick Ellis4c938ae2008-09-10 22:58:38 +0000198 */
199 function format_characters($str)
Derek Jonesab504b82008-09-11 17:04:30 +0000200 {
201 static $table;
202
203 if ( ! isset($table))
204 {
205 $table = array(
206 // nested smart quotes, opening and closing
207 // note that rules for grammar (English) allow only for two levels deep
208 // and that single quotes are _supposed_ to always be on the outside
209 // but we'll accommodate both
210 '/(^|\W|\s)\'"/' => '$1&#8216;&#8220;',
211 '/\'"(\s|\W|$)/' => '&#8217;&#8221;$1',
212 '/(^|\W|\s)"\'/' => '$1&#8220;&#8216;',
213 '/"\'(\s|\W|$)/' => '&#8221;&#8217;$1',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000214
Derek Jonesab504b82008-09-11 17:04:30 +0000215 // single quote smart quotes
216 '/\'(\s|\W|$)/' => '&#8217;$1',
217 '/(^|\W|\s)\'/' => '$1&#8216;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000218
Derek Jonesab504b82008-09-11 17:04:30 +0000219 // double quote smart quotes
220 '/"(\s|\W|$)/' => '&#8221;$1',
221 '/(^|\W|\s)"/' => '$1&#8220;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000222
Derek Jonesab504b82008-09-11 17:04:30 +0000223 // apostrophes
224 "/(\w)'(\w)/" => '$1&#8217;$2',
225
226 // Em dash and ellipses dots
227 '/\s?\-\-\s?/' => '&#8212;',
228 '/\w\.{3}/' => '&#8230;',
229
230 // double space after sentences
Derek Jones50500a22008-09-11 18:21:18 +0000231 '/(\W) /' => '$1&nbsp; ',
Derek Jonesab504b82008-09-11 17:04:30 +0000232
233 // ampersands, if not a character entity
234 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
235 );
236 }
237
238 return preg_replace(array_keys($table), $table, $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Format Newlines
245 *
246 * Converts newline characters into either <p> tags or <br />
247 *
248 */
Rick Ellis3bc791f2008-09-12 23:15:52 +0000249 function _format_newlines($str)
Rick Ellis4c938ae2008-09-10 22:58:38 +0000250 {
251 if ($str == '')
252 {
253 return $str;
254 }
255
256 if (strpos($str, "\n") === FALSE)
257 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000258 return $str;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000259 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000260
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000261 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000262 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
263
264 return '<p>'.$str.'</p>';
Rick Ellis18bd8b52008-09-10 23:40:35 +0000265 }
266
Rick Ellis9907df42008-09-12 07:22:21 +0000267 // ------------------------------------------------------------------------
268
269 /**
270 * Convert newlines to HTML line breaks except within PRE tags
271 *
272 * @access public
273 * @param string
274 * @return string
275 */
276 function nl2br_except_pre($str)
277 {
278 $ex = explode("pre>",$str);
279 $ct = count($ex);
280
281 $newstr = "";
282 for ($i = 0; $i < $ct; $i++)
283 {
284 if (($i % 2) == 0)
285 {
286 $newstr .= nl2br($ex[$i]);
287 }
288 else
289 {
290 $newstr .= $ex[$i];
291 }
292
293 if ($ct - 1 != $i)
294 $newstr .= "pre>";
295 }
296
297 return $newstr;
298 }
299
Rick Ellis4c938ae2008-09-10 22:58:38 +0000300}
301// END Typography Class
302
303/* End of file Typography.php */
304/* Location: ./system/libraries/Typography.php */