blob: 66c21853d119fd85b364afbac823b9c338b3e848 [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
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
10 * @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 Ellis18bd8b52008-09-10 23:40:35 +000038 // Whether to allow Javascript event handlers to be sumitted inside tags
39 var $allow_js_event_handlers = FALSE;
Rick Ellis4c938ae2008-09-10 22:58:38 +000040
Rick Ellis16e3c8c2008-09-11 20:12:26 +000041 // Whether to reduce more than two consecutive empty lines to a maximum of two
42 var $reduce_empty_lines = FALSE;
43
Rick Ellis4c938ae2008-09-10 22:58:38 +000044 /**
45 * Main Processing Function
46 *
47 */
48 function convert($str)
49 {
50 if ($str == '')
51 {
52 return '';
53 }
54
Rick Ellis4c938ae2008-09-10 22:58:38 +000055 // Standardize Newlines to make matching easier
56 if (strpos($str, "\r") !== FALSE)
57 {
58 $str = str_replace(array("\r\n", "\r"), "\n", $str);
59 }
Rick Ellis16e3c8c2008-09-11 20:12:26 +000060
61 // Reduce line breaks. If there are more than two consecutive linebreaks
62 // we'll compress them down to a maximum of two since there's no benefit to more.
63 if ($this->reduce_empty_lines == TRUE)
64 {
65 $str = preg_replace("/\n\n+/", "\n\n", $str);
66 }
Rick Ellis4c938ae2008-09-10 22:58:38 +000067
Rick Ellis16e3c8c2008-09-11 20:12:26 +000068 // Do we allow JavaScript event handlers? If not, we strip them from within all tags
Rick Ellis18bd8b52008-09-10 23:40:35 +000069 if ($this->allow_js_event_handlers == FALSE)
70 {
Rick Ellise320bc82008-09-11 18:11:31 +000071 $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
Rick Ellis18bd8b52008-09-10 23:40:35 +000072 }
73
Rick Ellis16e3c8c2008-09-11 20:12:26 +000074 // Convert quotes within tags to temporary marker.
75 // We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ}
Rick Ellis4c938ae2008-09-10 22:58:38 +000076 if (preg_match_all("#\<.+?>#si", $str, $matches))
77 {
78 for ($i = 0; $i < count($matches['0']); $i++)
79 {
80 $str = str_replace($matches['0'][$i],
81 str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
82 $str);
83 }
84 }
Rick Ellis4c938ae2008-09-10 22:58:38 +000085
Rick Ellis16e3c8c2008-09-11 20:12:26 +000086 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
87 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
88 // adversely affected if they are split out so we'll convert the opening < temporarily to: {@TAG}
Rick Ellis4c938ae2008-09-10 22:58:38 +000089 $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str);
Rick Ellis16e3c8c2008-09-11 20:12:26 +000090
91 // Split the string at every tag. This expression creates an array with this prototype:
92 //
93 // [array]
94 // {
95 // [0] = <opening tag>
96 // [1] = Content...
97 // [2] = <closing tag>
98 // Etc...
99 // }
Rick Ellis4c938ae2008-09-10 22:58:38 +0000100 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
101
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000102 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Rick Ellis4c938ae2008-09-10 22:58:38 +0000103 $str = '';
104 $process = TRUE;
Rick Ellis3ef59b72008-09-11 21:35:39 +0000105 $paragraph = FALSE;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000106 foreach ($chunks as $chunk)
107 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000108 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
109 // 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 +0000110 if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
111 {
Rick Ellis3ef59b72008-09-11 21:35:39 +0000112 if (preg_match("#".$this->skip_elements."#", $match[2]))
Rick Ellis4c938ae2008-09-10 22:58:38 +0000113 {
Rick Ellis3ef59b72008-09-11 21:35:39 +0000114 $process = ($match[1] == '/') ? TRUE : FALSE;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000115 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000116
Rick Ellis4c938ae2008-09-10 22:58:38 +0000117 $str .= $chunk;
118 continue;
119 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000120
Rick Ellis4c938ae2008-09-10 22:58:38 +0000121 if ($process == FALSE)
122 {
123 $str .= $chunk;
124 continue;
125 }
126
127 // Convert Newlines into <p> and <br /> tags
128 $str .= $this->format_newlines($chunk);
129 }
130
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000131 // Convert Quotes, elipsis, and em-dashes
Rick Ellis4c938ae2008-09-10 22:58:38 +0000132 $str = $this->format_characters($str);
Rick Ellis3ef59b72008-09-11 21:35:39 +0000133
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000134 // Do we need to reduce empty lines?
135 if ($this->reduce_empty_lines == TRUE)
136 {
137 $str = preg_replace('#(<p>\n*</p>)#', '', $str);
138 }
139
Rick Ellis4c938ae2008-09-10 22:58:38 +0000140 // Final clean up
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000141 $table = array(
Rick Ellis4c938ae2008-09-10 22:58:38 +0000142
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000143 // If the user submitted their own paragraph tags within the text
144 // we will retain them instead of using our tags.
145 '/(<p.*?>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
146
Rick Ellis25319a32008-09-11 20:13:25 +0000147 // Reduce multiple instances of opening/closing paragraph tags to a single one
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000148 '/(<\/p>)+/' => '</p>',
149 '/(<p><p>)+/' => '<p>',
150
151 // Clean up stray paragraph tags that appear before block level elements
152 '/<p><\/p><('.$this->block_elements.')/' => '<$1',
153
154 // Replace the temporary markers we added earlier
155 '/\{@TAG\}/' => '<',
156 '/\{@DQ\}/' => '"',
157 '/\{@SQ\}/' => "'"
158
159 );
160
161 return preg_replace(array_keys($table), $table, $str);
162
Rick Ellis4c938ae2008-09-10 22:58:38 +0000163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Format Characters
169 *
170 * This function mainly converts double and single quotes
Derek Jonesab504b82008-09-11 17:04:30 +0000171 * to curly entities, but it also converts em-dashes,
172 * double spaces, and ampersands
Rick Ellis4c938ae2008-09-10 22:58:38 +0000173 */
174 function format_characters($str)
Derek Jonesab504b82008-09-11 17:04:30 +0000175 {
176 static $table;
177
178 if ( ! isset($table))
179 {
180 $table = array(
181 // nested smart quotes, opening and closing
182 // note that rules for grammar (English) allow only for two levels deep
183 // and that single quotes are _supposed_ to always be on the outside
184 // but we'll accommodate both
185 '/(^|\W|\s)\'"/' => '$1&#8216;&#8220;',
186 '/\'"(\s|\W|$)/' => '&#8217;&#8221;$1',
187 '/(^|\W|\s)"\'/' => '$1&#8220;&#8216;',
188 '/"\'(\s|\W|$)/' => '&#8221;&#8217;$1',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000189
Derek Jonesab504b82008-09-11 17:04:30 +0000190 // single quote smart quotes
191 '/\'(\s|\W|$)/' => '&#8217;$1',
192 '/(^|\W|\s)\'/' => '$1&#8216;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000193
Derek Jonesab504b82008-09-11 17:04:30 +0000194 // double quote smart quotes
195 '/"(\s|\W|$)/' => '&#8221;$1',
196 '/(^|\W|\s)"/' => '$1&#8220;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000197
Derek Jonesab504b82008-09-11 17:04:30 +0000198 // apostrophes
199 "/(\w)'(\w)/" => '$1&#8217;$2',
200
201 // Em dash and ellipses dots
202 '/\s?\-\-\s?/' => '&#8212;',
203 '/\w\.{3}/' => '&#8230;',
204
205 // double space after sentences
Derek Jones50500a22008-09-11 18:21:18 +0000206 '/(\W) /' => '$1&nbsp; ',
Derek Jonesab504b82008-09-11 17:04:30 +0000207
208 // ampersands, if not a character entity
209 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
210 );
211 }
212
213 return preg_replace(array_keys($table), $table, $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Format Newlines
220 *
221 * Converts newline characters into either <p> tags or <br />
222 *
223 */
224 function format_newlines($str)
225 {
226 if ($str == '')
227 {
228 return $str;
229 }
230
231 if (strpos($str, "\n") === FALSE)
232 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000233 return $str;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000234 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000235
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000236 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000237 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
238
239 return '<p>'.$str.'</p>';
Rick Ellis18bd8b52008-09-10 23:40:35 +0000240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Allow JavaScript Event Handlers?
246 *
247 * For security reasons, by default we disallow JS event handlers
248 *
249 */
250 function allow_js_event_handlers($val = FALSE)
251 {
252 $this->allow_js_event_handlers = ($val === FALSE) ? FALSE : TRUE;
253 }
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000254
255 // --------------------------------------------------------------------
256
257 /**
258 * Reduce empty lines
259 *
260 * Sets a flag that tells the parser to reduce any instances of more than
261 * two consecutive linebreaks down to two
262 *
263 */
264 function reduce_empty_lines($val = FALSE)
265 {
266 $this->reduce_empty_lines = ($val === FALSE) ? FALSE : TRUE;
267 }
Rick Ellis4c938ae2008-09-10 22:58:38 +0000268}
269// END Typography Class
270
271/* End of file Typography.php */
272/* Location: ./system/libraries/Typography.php */