blob: 93e40b94ab233c717cad5e6ecc3bd4d0033a3f6e [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 /**
Rick Ellis142f8ed2008-09-12 06:14:52 +000045 * Nothing to do here...
46 *
47 */
48 function CI_Typography()
49 {
50 }
51
52 /**
Rick Ellis4c938ae2008-09-10 22:58:38 +000053 * Main Processing Function
54 *
55 */
56 function convert($str)
57 {
58 if ($str == '')
59 {
60 return '';
61 }
62
Rick Ellis4c938ae2008-09-10 22:58:38 +000063 // Standardize Newlines to make matching easier
64 if (strpos($str, "\r") !== FALSE)
65 {
66 $str = str_replace(array("\r\n", "\r"), "\n", $str);
67 }
Rick Ellis16e3c8c2008-09-11 20:12:26 +000068
69 // Reduce line breaks. If there are more than two consecutive linebreaks
70 // we'll compress them down to a maximum of two since there's no benefit to more.
71 if ($this->reduce_empty_lines == TRUE)
72 {
73 $str = preg_replace("/\n\n+/", "\n\n", $str);
74 }
Rick Ellis4c938ae2008-09-10 22:58:38 +000075
Rick Ellis16e3c8c2008-09-11 20:12:26 +000076 // Do we allow JavaScript event handlers? If not, we strip them from within all tags
Rick Ellis18bd8b52008-09-10 23:40:35 +000077 if ($this->allow_js_event_handlers == FALSE)
78 {
Rick Ellise320bc82008-09-11 18:11:31 +000079 $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
Rick Ellis18bd8b52008-09-10 23:40:35 +000080 }
81
Rick Ellis339fab72008-09-12 06:04:39 +000082 // Convert quotes within tags to temporary markers. We don't want quotes converted
83 // within tags so we'll temporarily convert them to {@DQ} and {@SQ}
Rick Ellis4c938ae2008-09-10 22:58:38 +000084 if (preg_match_all("#\<.+?>#si", $str, $matches))
85 {
86 for ($i = 0; $i < count($matches['0']); $i++)
87 {
88 $str = str_replace($matches['0'][$i],
89 str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
90 $str);
91 }
92 }
Rick Ellis4c938ae2008-09-10 22:58:38 +000093
Rick Ellis16e3c8c2008-09-11 20:12:26 +000094 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
95 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Rick Ellis339fab72008-09-12 06:04:39 +000096 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
Rick Ellis4c938ae2008-09-10 22:58:38 +000097 $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str);
Rick Ellis16e3c8c2008-09-11 20:12:26 +000098
99 // Split the string at every tag. This expression creates an array with this prototype:
100 //
101 // [array]
102 // {
103 // [0] = <opening tag>
104 // [1] = Content...
105 // [2] = <closing tag>
106 // Etc...
107 // }
Rick Ellis4c938ae2008-09-10 22:58:38 +0000108 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
109
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000110 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Rick Ellis4c938ae2008-09-10 22:58:38 +0000111 $str = '';
112 $process = TRUE;
Rick Ellis3ef59b72008-09-11 21:35:39 +0000113 $paragraph = FALSE;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000114 foreach ($chunks as $chunk)
115 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000116 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
117 // 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 +0000118 if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
119 {
Rick Ellis3ef59b72008-09-11 21:35:39 +0000120 if (preg_match("#".$this->skip_elements."#", $match[2]))
Rick Ellis4c938ae2008-09-10 22:58:38 +0000121 {
Rick Ellis3ef59b72008-09-11 21:35:39 +0000122 $process = ($match[1] == '/') ? TRUE : FALSE;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000123 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000124
Rick Ellis4c938ae2008-09-10 22:58:38 +0000125 $str .= $chunk;
126 continue;
127 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000128
Rick Ellis4c938ae2008-09-10 22:58:38 +0000129 if ($process == FALSE)
130 {
131 $str .= $chunk;
132 continue;
133 }
134
135 // Convert Newlines into <p> and <br /> tags
136 $str .= $this->format_newlines($chunk);
137 }
138
Rick Ellis339fab72008-09-12 06:04:39 +0000139 // Convert quotes, elipsis, and em-dashes
Rick Ellis4c938ae2008-09-10 22:58:38 +0000140 $str = $this->format_characters($str);
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000141
Rick Ellis4c938ae2008-09-10 22:58:38 +0000142 // Final clean up
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000143 $table = array(
Rick Ellis4c938ae2008-09-10 22:58:38 +0000144
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000145 // If the user submitted their own paragraph tags within the text
146 // we will retain them instead of using our tags.
147 '/(<p.*?>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
148
Rick Ellis25319a32008-09-11 20:13:25 +0000149 // Reduce multiple instances of opening/closing paragraph tags to a single one
Rick Ellis339fab72008-09-12 06:04:39 +0000150 '#(</p>)+#' => '</p>',
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000151 '/(<p><p>)+/' => '<p>',
152
153 // Clean up stray paragraph tags that appear before block level elements
Rick Ellis339fab72008-09-12 06:04:39 +0000154 '#<p></p><('.$this->block_elements.')#' => '<$1',
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000155
156 // Replace the temporary markers we added earlier
157 '/\{@TAG\}/' => '<',
158 '/\{@DQ\}/' => '"',
159 '/\{@SQ\}/' => "'"
160
161 );
162
Rick Ellis339fab72008-09-12 06:04:39 +0000163 // Do we need to reduce empty lines?
164 if ($this->reduce_empty_lines == TRUE)
165 {
166 $table['#<p>\n*</p>#'] = '';
167 }
168 else
169 {
170 // If we have empty paragraph tags we add a non-breaking space
171 // otherwise most browsers won't treat them as true paragraphs
172 $table['#<p></p>#'] = '<p>&nbsp;</p>';
173 }
174
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000175 return preg_replace(array_keys($table), $table, $str);
176
Rick Ellis4c938ae2008-09-10 22:58:38 +0000177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Format Characters
183 *
184 * This function mainly converts double and single quotes
Derek Jonesab504b82008-09-11 17:04:30 +0000185 * to curly entities, but it also converts em-dashes,
186 * double spaces, and ampersands
Rick Ellis4c938ae2008-09-10 22:58:38 +0000187 */
188 function format_characters($str)
Derek Jonesab504b82008-09-11 17:04:30 +0000189 {
190 static $table;
191
192 if ( ! isset($table))
193 {
194 $table = array(
195 // nested smart quotes, opening and closing
196 // note that rules for grammar (English) allow only for two levels deep
197 // and that single quotes are _supposed_ to always be on the outside
198 // but we'll accommodate both
199 '/(^|\W|\s)\'"/' => '$1&#8216;&#8220;',
200 '/\'"(\s|\W|$)/' => '&#8217;&#8221;$1',
201 '/(^|\W|\s)"\'/' => '$1&#8220;&#8216;',
202 '/"\'(\s|\W|$)/' => '&#8221;&#8217;$1',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000203
Derek Jonesab504b82008-09-11 17:04:30 +0000204 // single quote smart quotes
205 '/\'(\s|\W|$)/' => '&#8217;$1',
206 '/(^|\W|\s)\'/' => '$1&#8216;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000207
Derek Jonesab504b82008-09-11 17:04:30 +0000208 // double quote smart quotes
209 '/"(\s|\W|$)/' => '&#8221;$1',
210 '/(^|\W|\s)"/' => '$1&#8220;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000211
Derek Jonesab504b82008-09-11 17:04:30 +0000212 // apostrophes
213 "/(\w)'(\w)/" => '$1&#8217;$2',
214
215 // Em dash and ellipses dots
216 '/\s?\-\-\s?/' => '&#8212;',
217 '/\w\.{3}/' => '&#8230;',
218
219 // double space after sentences
Derek Jones50500a22008-09-11 18:21:18 +0000220 '/(\W) /' => '$1&nbsp; ',
Derek Jonesab504b82008-09-11 17:04:30 +0000221
222 // ampersands, if not a character entity
223 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
224 );
225 }
226
227 return preg_replace(array_keys($table), $table, $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Format Newlines
234 *
235 * Converts newline characters into either <p> tags or <br />
236 *
237 */
238 function format_newlines($str)
239 {
240 if ($str == '')
241 {
242 return $str;
243 }
244
245 if (strpos($str, "\n") === FALSE)
246 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000247 return $str;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000248 }
Rick Ellis3ef59b72008-09-11 21:35:39 +0000249
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000250 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000251 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
252
253 return '<p>'.$str.'</p>';
Rick Ellis18bd8b52008-09-10 23:40:35 +0000254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Allow JavaScript Event Handlers?
260 *
261 * For security reasons, by default we disallow JS event handlers
262 *
263 */
264 function allow_js_event_handlers($val = FALSE)
265 {
266 $this->allow_js_event_handlers = ($val === FALSE) ? FALSE : TRUE;
267 }
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000268
269 // --------------------------------------------------------------------
270
271 /**
272 * Reduce empty lines
273 *
274 * Sets a flag that tells the parser to reduce any instances of more than
275 * two consecutive linebreaks down to two
276 *
277 */
278 function reduce_empty_lines($val = FALSE)
279 {
280 $this->reduce_empty_lines = ($val === FALSE) ? FALSE : TRUE;
281 }
Rick Ellis4c938ae2008-09-10 22:58:38 +0000282}
283// END Typography Class
284
285/* End of file Typography.php */
286/* Location: ./system/libraries/Typography.php */