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