blob: 9d28a7525040a14bfcff0cf0445d407cbeb2ad6f [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 Ellis16e3c8c2008-09-11 20:12:26 +000033 var $skip_elements = 'pre|ol|ul|p';
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;
105 foreach ($chunks as $chunk)
106 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000107 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
108 // 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 +0000109 if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
110 {
111 if (preg_match("#".$this->skip_elements."#", $match['2']))
112 {
113 $process = ($match['1'] == '/') ? TRUE : FALSE;
114 }
115
116 $str .= $chunk;
117 continue;
118 }
119
120 if ($process == FALSE)
121 {
122 $str .= $chunk;
123 continue;
124 }
125
126 // Convert Newlines into <p> and <br /> tags
127 $str .= $this->format_newlines($chunk);
128 }
129
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000130 // Convert Quotes, elipsis, and em-dashes
Rick Ellis4c938ae2008-09-10 22:58:38 +0000131 $str = $this->format_characters($str);
132
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000133 // Do we need to reduce empty lines?
134 if ($this->reduce_empty_lines == TRUE)
135 {
136 $str = preg_replace('#(<p>\n*</p>)#', '', $str);
137 }
138
Rick Ellis4c938ae2008-09-10 22:58:38 +0000139 // Final clean up
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000140 $table = array(
Rick Ellis4c938ae2008-09-10 22:58:38 +0000141
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000142 // If the user submitted their own paragraph tags within the text
143 // we will retain them instead of using our tags.
144 '/(<p.*?>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
145
146 // Reduce multiple paragraphs to a single one
147 '/(<\/p>)+/' => '</p>',
148 '/(<p><p>)+/' => '<p>',
149
150 // Clean up stray paragraph tags that appear before block level elements
151 '/<p><\/p><('.$this->block_elements.')/' => '<$1',
152
153 // Replace the temporary markers we added earlier
154 '/\{@TAG\}/' => '<',
155 '/\{@DQ\}/' => '"',
156 '/\{@SQ\}/' => "'"
157
158 );
159
160 return preg_replace(array_keys($table), $table, $str);
161
Rick Ellis4c938ae2008-09-10 22:58:38 +0000162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Format Characters
168 *
169 * This function mainly converts double and single quotes
Derek Jonesab504b82008-09-11 17:04:30 +0000170 * to curly entities, but it also converts em-dashes,
171 * double spaces, and ampersands
Rick Ellis4c938ae2008-09-10 22:58:38 +0000172 */
173 function format_characters($str)
Derek Jonesab504b82008-09-11 17:04:30 +0000174 {
175 static $table;
176
177 if ( ! isset($table))
178 {
179 $table = array(
180 // nested smart quotes, opening and closing
181 // note that rules for grammar (English) allow only for two levels deep
182 // and that single quotes are _supposed_ to always be on the outside
183 // but we'll accommodate both
184 '/(^|\W|\s)\'"/' => '$1&#8216;&#8220;',
185 '/\'"(\s|\W|$)/' => '&#8217;&#8221;$1',
186 '/(^|\W|\s)"\'/' => '$1&#8220;&#8216;',
187 '/"\'(\s|\W|$)/' => '&#8221;&#8217;$1',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000188
Derek Jonesab504b82008-09-11 17:04:30 +0000189 // single quote smart quotes
190 '/\'(\s|\W|$)/' => '&#8217;$1',
191 '/(^|\W|\s)\'/' => '$1&#8216;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000192
Derek Jonesab504b82008-09-11 17:04:30 +0000193 // double quote smart quotes
194 '/"(\s|\W|$)/' => '&#8221;$1',
195 '/(^|\W|\s)"/' => '$1&#8220;',
Rick Ellis4c938ae2008-09-10 22:58:38 +0000196
Derek Jonesab504b82008-09-11 17:04:30 +0000197 // apostrophes
198 "/(\w)'(\w)/" => '$1&#8217;$2',
199
200 // Em dash and ellipses dots
201 '/\s?\-\-\s?/' => '&#8212;',
202 '/\w\.{3}/' => '&#8230;',
203
204 // double space after sentences
Derek Jones50500a22008-09-11 18:21:18 +0000205 '/(\W) /' => '$1&nbsp; ',
Derek Jonesab504b82008-09-11 17:04:30 +0000206
207 // ampersands, if not a character entity
208 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
209 );
210 }
211
212 return preg_replace(array_keys($table), $table, $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Format Newlines
219 *
220 * Converts newline characters into either <p> tags or <br />
221 *
222 */
223 function format_newlines($str)
224 {
225 if ($str == '')
226 {
227 return $str;
228 }
229
230 if (strpos($str, "\n") === FALSE)
231 {
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000232 return $str;
Rick Ellis4c938ae2008-09-10 22:58:38 +0000233 }
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000234
235 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Rick Ellis4c938ae2008-09-10 22:58:38 +0000236 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
237
238 return '<p>'.$str.'</p>';
Rick Ellis18bd8b52008-09-10 23:40:35 +0000239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Allow JavaScript Event Handlers?
245 *
246 * For security reasons, by default we disallow JS event handlers
247 *
248 */
249 function allow_js_event_handlers($val = FALSE)
250 {
251 $this->allow_js_event_handlers = ($val === FALSE) ? FALSE : TRUE;
252 }
Rick Ellis16e3c8c2008-09-11 20:12:26 +0000253
254 // --------------------------------------------------------------------
255
256 /**
257 * Reduce empty lines
258 *
259 * Sets a flag that tells the parser to reduce any instances of more than
260 * two consecutive linebreaks down to two
261 *
262 */
263 function reduce_empty_lines($val = FALSE)
264 {
265 $this->reduce_empty_lines = ($val === FALSE) ? FALSE : TRUE;
266 }
Rick Ellis4c938ae2008-09-10 22:58:38 +0000267}
268// END Typography Class
269
270/* End of file Typography.php */
271/* Location: ./system/libraries/Typography.php */