Take two:
[www-register-wizard.git] / libraries / Typography.php
1 <?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) 2008, 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  */
27 class CI_Typography {
28
29         // Block level elements that should not be wrapped inside <p> tags
30         var $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
31         
32         // Elements that should not have <p> and <br /> tags within them.
33         var $skip_elements      = 'p|pre|ol|ul|dl|object|table';
34         
35         // Tags we want the parser to completely ignore when splitting the string.
36         var $inline_elements = 'a|abbr|acronym|b|bdo|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|samp|select|span|strong|sub|sup|textarea|var';
37
38         // whether or not to protect quotes within { curly braces }
39         var $protect_braced_quotes = FALSE;
40         
41         /**
42          * Nothing to do here...
43          *
44          */
45         function CI_Typography()
46         {
47         }
48
49         /**
50          * Auto Typography
51          *
52          * This function converts text, making it typographically correct:
53          *      - Converts double spaces into paragraphs.
54          *      - Converts single line breaks into <br /> tags
55          *      - Converts single and double quotes into correctly facing curly quote entities.
56          *      - Converts three dots into ellipsis.
57          *      - Converts double dashes into em-dashes.
58          *  - Converts two spaces into entities
59          *
60          * @access      public
61          * @param       string
62          * @param       bool    whether to strip javascript event handlers for security
63          * @param       bool    whether to reduce more then two consecutive newlines to two
64          * @return      string
65          */
66         function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE)
67         {
68                 if ($str == '')
69                 {
70                         return '';
71                 }
72
73                 // Standardize Newlines to make matching easier
74                 if (strpos($str, "\r") !== FALSE)
75                 {
76                         $str = str_replace(array("\r\n", "\r"), "\n", $str);                    
77                 }
78                         
79                 // Reduce line breaks.  If there are more than two consecutive linebreaks
80                 // we'll compress them down to a maximum of two since there's no benefit to more.
81                 if ($reduce_linebreaks === TRUE)
82                 {
83                         $str = preg_replace("/\n\n+/", "\n\n", $str);
84                 }
85                 
86                  // Do we allow JavaScript event handlers? If not, we strip them from within all tags
87                 if ($strip_js_event_handlers === TRUE)
88                 {
89                         $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
90                 }       
91
92                 // Convert quotes within tags to temporary markers. We don't want quotes converted 
93                 // within tags so we'll temporarily convert them to {@DQ} and {@SQ}
94                 if (preg_match_all("#\<.+?>#si", $str, $matches))
95                 {
96                         for ($i = 0; $i < count($matches['0']); $i++)
97                         {
98                                 $str = str_replace($matches['0'][$i],
99                                                                         str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
100                                                                         $str);
101                         }
102                 }
103
104                 if ($this->protect_braced_quotes === TRUE)
105                 {
106                         if (preg_match_all("#\{.+?}#si", $str, $matches))
107                         {
108                                 for ($i = 0; $i < count($matches['0']); $i++)
109                                 {
110                                         $str = str_replace($matches['0'][$i],
111                                                                                 str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
112                                                                                 $str);
113                                 }
114                         }                       
115                 }
116                         
117                 // Convert "ignore" tags to temporary marker.  The parser splits out the string at every tag 
118                 // it encounters.  Certain inline tags, like image tags, links, span tags, etc. will be 
119                 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
120                 $str = preg_replace("#<(/*)(".$this->inline_elements.")([ >])#i", "{@TAG}\\1\\2\\3", $str);
121
122                 // Split the string at every tag.  This expression creates an array with this prototype:
123                 // 
124                 //      [array]
125                 //      {
126                 //              [0] = <opening tag>
127                 //              [1] = Content...
128                 //              [2] = <closing tag>
129                 //              Etc...
130                 //      }       
131                 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
132                 
133                 // Build our finalized string.  We cycle through the array, skipping tags, and processing the contained text    
134                 $str = '';
135                 $process = TRUE;
136                 $paragraph = FALSE;
137                 foreach ($chunks as $chunk)
138                 {
139                         // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
140                         // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
141                         if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
142                         {
143                                 if (preg_match("#".$this->skip_elements."#", $match[2]))
144                                 {
145                                         $process =  ($match[1] == '/') ? TRUE : FALSE;
146                                 }
147                                 
148                                 $str .= $chunk;
149                                 continue;
150                         }
151
152                         if ($process == FALSE)
153                         {
154                                 $str .= $chunk;
155                                 continue;
156                         }
157                         
158                         //  Convert Newlines into <p> and <br /> tags
159                         $str .= $this->_format_newlines($chunk);
160                 }
161
162                 // is the whole of the content inside a block level element?
163                 if ( ! preg_match("/^<(?:".$this->block_elements.")/i", $str, $match))
164                 {
165                         $str = "<p>{$str}</p>";
166                 }
167
168                 // Convert quotes, elipsis, and em-dashes
169                 $str = $this->format_characters($str);
170         
171                 // Final clean up
172                 $table = array(
173                 
174                                                 // If the user submitted their own paragraph tags within the text
175                                                 // we will retain them instead of using our tags.
176                                                 '/(<p.*?>)<p>/'         => '$1', // <?php BBEdit syntax coloring bug fix
177                                                 
178                                                 // Reduce multiple instances of opening/closing paragraph tags to a single one
179                                                 '#(</p>)+#'                     => '</p>',
180                                                 '/(<p><p>)+/'           => '<p>',
181                                                 
182                                                 // Clean up stray paragraph tags that appear before block level elements
183                                                 '#<p></p><('.$this->block_elements.')#' => '<$1',
184                         
185                                                 // Replace the temporary markers we added earlier
186                                                 '/\{@TAG\}/'            => '<',
187                                                 '/\{@DQ\}/'                     => '"',
188                                                 '/\{@SQ\}/'                     => "'"
189
190                                                 );
191         
192                 // Do we need to reduce empty lines?
193                 if ($reduce_linebreaks === TRUE)
194                 {
195                         $table['#<p>\n*</p>#'] = '';
196                 }
197                 else
198                 {
199                         // If we have empty paragraph tags we add a non-breaking space
200                         // otherwise most browsers won't treat them as true paragraphs
201                         $table['#<p></p>#'] = '<p>&nbsp;</p>';
202                 }
203         
204                 return preg_replace(array_keys($table), $table, $str);
205
206         }
207         
208         // --------------------------------------------------------------------
209
210         /**
211          * Format Characters
212          *
213          * This function mainly converts double and single quotes
214          * to curly entities, but it also converts em-dashes,
215          * double spaces, and ampersands
216          *
217          * @access      public
218          * @param       string
219          * @return      string
220          */
221         function format_characters($str)
222         {
223                 static $table;
224                 
225                 if ( ! isset($table))
226                 {
227                 $table = array(                                 
228                                                         // nested smart quotes, opening and closing
229                                                         // note that rules for grammar (English) allow only for two levels deep
230                                                         // and that single quotes are _supposed_ to always be on the outside
231                                                         // but we'll accommodate both
232                                                         '/(^|\W|\s)\'"/'                                => '$1&#8216;&#8220;',
233                                                         '/\'"(\s|\W|$)/'                                => '&#8217;&#8221;$1',
234                                                         '/(^|\W|\s)"\'/'                                => '$1&#8220;&#8216;',
235                                                         '/"\'(\s|\W|$)/'                                => '&#8221;&#8217;$1',
236
237                                                         // single quote smart quotes
238                                                         '/\'(\s|\W|$)/'                                 => '&#8217;$1',
239                                                         '/(^|\W|\s)\'/'                                 => '$1&#8216;',
240
241                                                         // double quote smart quotes
242                                                         '/"(\s|\W|$)/'                                  => '&#8221;$1',
243                                                         '/(^|\W|\s)"/'                                  => '$1&#8220;',
244
245                                                         // apostrophes
246                                                         "/(\w)'(\w)/"                   => '$1&#8217;$2',
247
248                                                         // Em dash and ellipses dots
249                                                         '/\s?\-\-\s?/'                                  => '&#8212;',
250                                                         '/(\w)\.{3}/'                                   => '$1&#8230;',
251
252                                                         // double space after sentences
253                                                         '/(\W)  /'                                              => '$1&nbsp; ',
254
255                                                         // ampersands, if not a character entity
256                                                         '/&(?!#?[a-zA-Z0-9]{2,};)/'             => '&amp;'
257                                         );                      
258                 }       
259
260                 return preg_replace(array_keys($table), $table, $str);
261         }
262         
263         // --------------------------------------------------------------------
264
265         /**
266          * Format Newlines
267          *
268          * Converts newline characters into either <p> tags or <br />
269          *
270          * @access      public
271          * @param       string
272          * @return      string
273          */     
274         function _format_newlines($str)
275         {
276                 if ($str == '')
277                 {
278                         return $str;
279                 }
280
281                 if (strpos($str, "\n") === FALSE)
282                 {
283                         return $str;
284                 }
285                 
286                 // Convert two consecutive newlines to paragraphs
287                 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
288                 
289                 // Convert single spaces to <br /> tags
290                 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
291                 
292                 // Wrap the whole enchilada in enclosing paragraphs
293                 if ($str != "\n")
294                 {
295                         $str =  '<p>'.$str.'</p>';
296                 }
297
298                 // Remove empty paragraphs if they are on the first line, as this
299                 // is a potential unintended consequence of the previous code
300                 $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1);
301                 
302                 return $str;
303         }
304         
305         // ------------------------------------------------------------------------
306         
307         /**
308          * Convert newlines to HTML line breaks except within PRE tags
309          *
310          * @access      public
311          * @param       string
312          * @return      string
313          */             
314         function nl2br_except_pre($str)
315         {
316                 $ex = explode("pre>",$str);
317                 $ct = count($ex);
318         
319                 $newstr = "";
320                 for ($i = 0; $i < $ct; $i++)
321                 {
322                         if (($i % 2) == 0)
323                         {
324                                 $newstr .= nl2br($ex[$i]);
325                         }
326                         else
327                         {
328                                 $newstr .= $ex[$i];
329                         }
330                 
331                         if ($ct - 1 != $i)
332                                 $newstr .= "pre>";
333                 }
334         
335                 return $newstr;
336         }
337         
338 }
339 // END Typography Class
340
341 /* End of file Typography.php */
342 /* Location: ./system/libraries/Typography.php */