Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 31 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 32 | // Elements that should not have <p> and <br /> tags within them. |
Robin Sowell | 8db2d5e | 2009-07-17 22:07:04 +0000 | [diff] [blame] | 33 | var $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 34 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | // Tags we want the parser to completely ignore when splitting the string. |
| 36 | var $inline_elements = 'a|abbr|acronym|b|bdo|big|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|q|samp|select|small|span|strong|sub|sup|textarea|tt|var'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 37 | |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 38 | // array of block level elements that require inner content to be within another block level element |
| 39 | var $inner_block_required = array('blockquote'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 40 | |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 41 | // the last block element parsed |
| 42 | var $last_block_element = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 43 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | // whether or not to protect quotes within { curly braces } |
| 45 | var $protect_braced_quotes = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | * Auto Typography |
| 49 | * |
| 50 | * This function converts text, making it typographically correct: |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | * - Converts double spaces into paragraphs. |
| 52 | * - Converts single line breaks into <br /> tags |
| 53 | * - Converts single and double quotes into correctly facing curly quote entities. |
| 54 | * - Converts three dots into ellipsis. |
| 55 | * - Converts double dashes into em-dashes. |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 56 | * - Converts two spaces into entities |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | * |
| 58 | * @access public |
| 59 | * @param string |
| 60 | * @param bool whether to reduce more then two consecutive newlines to two |
| 61 | * @return string |
| 62 | */ |
| 63 | function auto_typography($str, $reduce_linebreaks = FALSE) |
| 64 | { |
| 65 | if ($str == '') |
| 66 | { |
| 67 | return ''; |
| 68 | } |
| 69 | |
| 70 | // Standardize Newlines to make matching easier |
| 71 | if (strpos($str, "\r") !== FALSE) |
| 72 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 76 | // Reduce line breaks. If there are more than two consecutive linebreaks |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | // we'll compress them down to a maximum of two since there's no benefit to more. |
| 78 | if ($reduce_linebreaks === TRUE) |
| 79 | { |
| 80 | $str = preg_replace("/\n\n+/", "\n\n", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 83 | // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed |
| 84 | $html_comments = array(); |
| 85 | if (strpos($str, '<!--') !== FALSE) |
| 86 | { |
| 87 | if (preg_match_all("#(<!\-\-.*?\-\->)#s", $str, $matches)) |
| 88 | { |
| 89 | for ($i = 0, $total = count($matches[0]); $i < $total; $i++) |
| 90 | { |
| 91 | $html_comments[] = $matches[0][$i]; |
| 92 | $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str); |
| 93 | } |
| 94 | } |
| 95 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 97 | // match and yank <pre> tags if they exist. It's cheaper to do this separately since most content will |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 98 | // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster |
| 99 | if (strpos($str, '<pre') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | { |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 101 | $str = preg_replace_callback("#<pre.*?>.*?</pre>#si", array($this, '_protect_characters'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 104 | // Convert quotes within tags to temporary markers. |
| 105 | $str = preg_replace_callback("#<.+?>#si", array($this, '_protect_characters'), $str); |
| 106 | |
| 107 | // Do the same with braces if necessary |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | if ($this->protect_braced_quotes === TRUE) |
| 109 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | $str = preg_replace_callback("#\{.+?\}#si", array($this, '_protect_characters'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 113 | // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag |
| 114 | // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG} |
| 116 | $str = preg_replace("#<(/*)(".$this->inline_elements.")([ >])#i", "{@TAG}\\1\\2\\3", $str); |
| 117 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 118 | // Split the string at every tag. This expression creates an array with this prototype: |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | // |
| 120 | // [array] |
| 121 | // { |
| 122 | // [0] = <opening tag> |
| 123 | // [1] = Content... |
| 124 | // [2] = <closing tag> |
| 125 | // Etc... |
| 126 | // } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 129 | // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | $str = ''; |
| 131 | $process = TRUE; |
| 132 | $paragraph = FALSE; |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 133 | $current_chunk = 0; |
| 134 | $total_chunks = count($chunks); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | foreach ($chunks as $chunk) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | { |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 138 | $current_chunk++; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | // Are we dealing with a tag? If so, we'll skip the processing for this cycle. |
| 141 | // Well also set the "process" flag which allows us to skip <pre> tags and a few other things. |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 142 | if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunk, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | { |
| 144 | if (preg_match("#".$this->skip_elements."#", $match[2])) |
| 145 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 146 | $process = ($match[1] == '/') ? TRUE : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 149 | if ($match[1] == '') |
| 150 | { |
| 151 | $this->last_block_element = $match[2]; |
| 152 | } |
| 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | $str .= $chunk; |
| 155 | continue; |
| 156 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | if ($process == FALSE) |
| 159 | { |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 160 | $str .= $chunk; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | continue; |
| 162 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 164 | // Force a newline to make sure end tags get processed by _format_newlines() |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 165 | if ($current_chunk == $total_chunks) |
| 166 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | $chunk .= "\n"; |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 168 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 170 | // Convert Newlines into <p> and <br /> tags |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 171 | $str .= $this->_format_newlines($chunk); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 174 | // No opening block level tag? Add it if needed. |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 175 | if ( ! preg_match("/^\s*<(?:".$this->block_elements.")/i", $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | { |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 177 | $str = preg_replace("/^(.*?)<(".$this->block_elements.")/i", '<p>$1</p><$2', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 180 | // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands |
| 181 | $str = $this->format_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 183 | // restore HTML comments |
| 184 | for ($i = 0, $total = count($html_comments); $i < $total; $i++) |
| 185 | { |
Derek Jones | 4777fb8 | 2008-12-11 17:55:42 +0000 | [diff] [blame] | 186 | // remove surrounding paragraph tags, but only if there's an opening paragraph tag |
| 187 | // otherwise HTML comments at the ends of paragraphs will have the closing tag removed |
| 188 | // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment |
| 189 | $str = preg_replace('#(?(?=<p>\{@HC'.$i.'\})<p>\{@HC'.$i.'\}(\s*</p>)|\{@HC'.$i.'\})#s', $html_comments[$i], $str); |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 190 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | // Final clean up |
| 193 | $table = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | // If the user submitted their own paragraph tags within the text |
| 196 | // we will retain them instead of using our tags. |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 197 | '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | // Reduce multiple instances of opening/closing paragraph tags to a single one |
| 200 | '#(</p>)+#' => '</p>', |
| 201 | '/(<p>\W*<p>)+/' => '<p>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | // Clean up stray paragraph tags that appear before block level elements |
| 204 | '#<p></p><('.$this->block_elements.')#' => '<$1', |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 205 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | // Clean up stray non-breaking spaces preceeding block elements |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 207 | '#( \s*)+<('.$this->block_elements.')#' => ' <$2', |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 208 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | // Replace the temporary markers we added earlier |
| 210 | '/\{@TAG\}/' => '<', |
| 211 | '/\{@DQ\}/' => '"', |
| 212 | '/\{@SQ\}/' => "'", |
| 213 | '/\{@DD\}/' => '--', |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 214 | '/\{@NBS\}/' => ' ', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 216 | // An unintended consequence of the _format_newlines function is that |
| 217 | // some of the newlines get truncated, resulting in <p> tags |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | // starting immediately after <block> tags on the same line. |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 219 | // This forces a newline after such occurrences, which looks much nicer. |
| 220 | "/><p>\n/" => ">\n<p>", |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 222 | // Similarly, there might be cases where a closing </block> will follow |
| 223 | // a closing </p> tag, so we'll correct it by adding a newline in between |
| 224 | "#</p></#" => "</p>\n</" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | // Do we need to reduce empty lines? |
| 228 | if ($reduce_linebreaks === TRUE) |
| 229 | { |
| 230 | $table['#<p>\n*</p>#'] = ''; |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | // If we have empty paragraph tags we add a non-breaking space |
| 235 | // otherwise most browsers won't treat them as true paragraphs |
| 236 | $table['#<p></p>#'] = '<p> </p>'; |
| 237 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | return preg_replace(array_keys($table), $table, $str); |
| 240 | |
| 241 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | * Format Characters |
| 247 | * |
| 248 | * This function mainly converts double and single quotes |
| 249 | * to curly entities, but it also converts em-dashes, |
| 250 | * double spaces, and ampersands |
| 251 | * |
| 252 | * @access public |
| 253 | * @param string |
| 254 | * @return string |
| 255 | */ |
| 256 | function format_characters($str) |
| 257 | { |
| 258 | static $table; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | if ( ! isset($table)) |
| 261 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | $table = array( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | // nested smart quotes, opening and closing |
| 264 | // note that rules for grammar (English) allow only for two levels deep |
| 265 | // and that single quotes are _supposed_ to always be on the outside |
| 266 | // but we'll accommodate both |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 267 | // Note that in all cases, whitespace is the primary determining factor |
| 268 | // on which direction to curl, with non-word characters like punctuation |
| 269 | // being a secondary factor only after whitespace is addressed. |
| 270 | '/\'"(\s|$)/' => '’”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 271 | '/(^|\s|<p>)\'"/' => '$1‘“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 272 | '/\'"(\W)/' => '’”$1', |
| 273 | '/(\W)\'"/' => '$1‘“', |
| 274 | '/"\'(\s|$)/' => '”’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 275 | '/(^|\s|<p>)"\'/' => '$1“‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 276 | '/"\'(\W)/' => '”’$1', |
| 277 | '/(\W)"\'/' => '$1“‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | |
| 279 | // single quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 280 | '/\'(\s|$)/' => '’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 281 | '/(^|\s|<p>)\'/' => '$1‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 282 | '/\'(\W)/' => '’$1', |
| 283 | '/(\W)\'/' => '$1‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | |
| 285 | // double quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 286 | '/"(\s|$)/' => '”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 287 | '/(^|\s|<p>)"/' => '$1“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 288 | '/"(\W)/' => '”$1', |
| 289 | '/(\W)"/' => '$1“', |
| 290 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | // apostrophes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 292 | "/(\w)'(\w)/" => '$1’$2', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | |
| 294 | // Em dash and ellipses dots |
| 295 | '/\s?\-\-\s?/' => '—', |
| 296 | '/(\w)\.{3}/' => '$1…', |
| 297 | |
| 298 | // double space after sentences |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 299 | '/(\W) /' => '$1 ', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | |
| 301 | // ampersands, if not a character entity |
| 302 | '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&' |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 303 | ); |
| 304 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | |
| 306 | return preg_replace(array_keys($table), $table, $str); |
| 307 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | // -------------------------------------------------------------------- |
| 310 | |
| 311 | /** |
| 312 | * Format Newlines |
| 313 | * |
| 314 | * Converts newline characters into either <p> tags or <br /> |
| 315 | * |
| 316 | * @access public |
| 317 | * @param string |
| 318 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 319 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | function _format_newlines($str) |
| 321 | { |
| 322 | if ($str == '') |
| 323 | { |
| 324 | return $str; |
| 325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 327 | if (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | { |
| 329 | return $str; |
| 330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | // Convert two consecutive newlines to paragraphs |
| 333 | $str = str_replace("\n\n", "</p>\n\n<p>", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 334 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | // Convert single spaces to <br /> tags |
| 336 | $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | // Wrap the whole enchilada in enclosing paragraphs |
| 339 | if ($str != "\n") |
| 340 | { |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 341 | // We trim off the right-side new line so that the closing </p> tag |
| 342 | // will be positioned immediately following the string, matching |
| 343 | // the behavior of the opening <p> tag |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 344 | $str = '<p>'.rtrim($str).'</p>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // Remove empty paragraphs if they are on the first line, as this |
| 348 | // is a potential unintended consequence of the previous code |
| 349 | $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | return $str; |
| 352 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 355 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | /** |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 357 | * Protect Characters |
| 358 | * |
| 359 | * Protects special characters from being formatted later |
| 360 | * We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ} |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | * and we don't want double dashes converted to emdash entities, so they are marked with {@DD} |
| 362 | * likewise double spaces are converted to {@NBS} to prevent entity conversion |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 363 | * |
| 364 | * @access public |
| 365 | * @param array |
| 366 | * @return string |
| 367 | */ |
| 368 | function _protect_characters($match) |
| 369 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame^] | 370 | return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]); |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 375 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | * Convert newlines to HTML line breaks except within PRE tags |
| 377 | * |
| 378 | * @access public |
| 379 | * @param string |
| 380 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | function nl2br_except_pre($str) |
| 383 | { |
| 384 | $ex = explode("pre>",$str); |
| 385 | $ct = count($ex); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 386 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | $newstr = ""; |
| 388 | for ($i = 0; $i < $ct; $i++) |
| 389 | { |
| 390 | if (($i % 2) == 0) |
| 391 | { |
| 392 | $newstr .= nl2br($ex[$i]); |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | $newstr .= $ex[$i]; |
| 397 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 398 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | if ($ct - 1 != $i) |
| 400 | $newstr .= "pre>"; |
| 401 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | return $newstr; |
| 404 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | } |
| 407 | // END Typography Class |
| 408 | |
| 409 | /* End of file Typography.php */ |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 410 | /* Location: ./system/libraries/Typography.php */ |