Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [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 |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 42 | public $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. |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 45 | public $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. |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 48 | public $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 |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 51 | public $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 |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 54 | public $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 } |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 57 | public $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 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 75 | public function auto_typography($str, $reduce_linebreaks = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 130 | /* Split the string at every tag. This expression creates an array with this prototype: |
| 131 | * |
| 132 | * [array] |
| 133 | * { |
| 134 | * [0] = <opening tag> |
| 135 | * [1] = Content... |
| 136 | * [2] = <closing tag> |
| 137 | * Etc... |
| 138 | * } |
| 139 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | $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] | 141 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 142 | // 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] | 143 | $str = ''; |
| 144 | $process = TRUE; |
| 145 | $paragraph = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 147 | for ($i = 1, $c = count($chunks); $i <= $c; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | // Are we dealing with a tag? If so, we'll skip the processing for this cycle. |
| 150 | // Well also set the "process" flag which allows us to skip <pre> tags and a few other things. |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 151 | if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunks[$i], $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | { |
| 153 | if (preg_match("#".$this->skip_elements."#", $match[2])) |
| 154 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 155 | $process = ($match[1] === '/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 158 | if ($match[1] == '') |
| 159 | { |
| 160 | $this->last_block_element = $match[2]; |
| 161 | } |
| 162 | |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 163 | $str .= $chunks[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | continue; |
| 165 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 167 | if ($process === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 169 | $str .= $chunks[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | continue; |
| 171 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 173 | // Force a newline to make sure end tags get processed by _format_newlines() |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 174 | if ($i === $c) |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 175 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 176 | $chunks[$i] .= "\n"; |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 177 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 179 | // Convert Newlines into <p> and <br /> tags |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 180 | $str .= $this->_format_newlines($chunks[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 183 | // No opening block level tag? Add it if needed. |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 184 | if ( ! preg_match("/^\s*<(?:".$this->block_elements.")/i", $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | { |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 186 | $str = preg_replace("/^(.*?)<(".$this->block_elements.")/i", '<p>$1</p><$2', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 189 | // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands |
| 190 | $str = $this->format_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 192 | // restore HTML comments |
| 193 | for ($i = 0, $total = count($html_comments); $i < $total; $i++) |
| 194 | { |
Derek Jones | 4777fb8 | 2008-12-11 17:55:42 +0000 | [diff] [blame] | 195 | // remove surrounding paragraph tags, but only if there's an opening paragraph tag |
| 196 | // otherwise HTML comments at the ends of paragraphs will have the closing tag removed |
| 197 | // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment |
| 198 | $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] | 199 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | // Final clean up |
| 202 | $table = array( |
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 | // If the user submitted their own paragraph tags within the text |
| 205 | // we will retain them instead of using our tags. |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 206 | '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | // Reduce multiple instances of opening/closing paragraph tags to a single one |
| 209 | '#(</p>)+#' => '</p>', |
| 210 | '/(<p>\W*<p>)+/' => '<p>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | // Clean up stray paragraph tags that appear before block level elements |
| 213 | '#<p></p><('.$this->block_elements.')#' => '<$1', |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 214 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | // Clean up stray non-breaking spaces preceeding block elements |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 216 | '#( \s*)+<('.$this->block_elements.')#' => ' <$2', |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | // Replace the temporary markers we added earlier |
| 219 | '/\{@TAG\}/' => '<', |
| 220 | '/\{@DQ\}/' => '"', |
| 221 | '/\{@SQ\}/' => "'", |
| 222 | '/\{@DD\}/' => '--', |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 223 | '/\{@NBS\}/' => ' ', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 225 | // An unintended consequence of the _format_newlines function is that |
| 226 | // some of the newlines get truncated, resulting in <p> tags |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | // starting immediately after <block> tags on the same line. |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 228 | // This forces a newline after such occurrences, which looks much nicer. |
| 229 | "/><p>\n/" => ">\n<p>", |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 231 | // Similarly, there might be cases where a closing </block> will follow |
| 232 | // a closing </p> tag, so we'll correct it by adding a newline in between |
| 233 | "#</p></#" => "</p>\n</" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | // Do we need to reduce empty lines? |
| 237 | if ($reduce_linebreaks === TRUE) |
| 238 | { |
| 239 | $table['#<p>\n*</p>#'] = ''; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | // If we have empty paragraph tags we add a non-breaking space |
| 244 | // otherwise most browsers won't treat them as true paragraphs |
| 245 | $table['#<p></p>#'] = '<p> </p>'; |
| 246 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | return preg_replace(array_keys($table), $table, $str); |
| 249 | |
| 250 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | * Format Characters |
| 256 | * |
| 257 | * This function mainly converts double and single quotes |
| 258 | * to curly entities, but it also converts em-dashes, |
| 259 | * double spaces, and ampersands |
| 260 | * |
| 261 | * @access public |
| 262 | * @param string |
| 263 | * @return string |
| 264 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 265 | public function format_characters($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | { |
| 267 | static $table; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 268 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | if ( ! isset($table)) |
| 270 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 271 | $table = array( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | // nested smart quotes, opening and closing |
| 273 | // note that rules for grammar (English) allow only for two levels deep |
| 274 | // and that single quotes are _supposed_ to always be on the outside |
| 275 | // but we'll accommodate both |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 276 | // Note that in all cases, whitespace is the primary determining factor |
| 277 | // on which direction to curl, with non-word characters like punctuation |
| 278 | // being a secondary factor only after whitespace is addressed. |
| 279 | '/\'"(\s|$)/' => '’”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 280 | '/(^|\s|<p>)\'"/' => '$1‘“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 281 | '/\'"(\W)/' => '’”$1', |
| 282 | '/(\W)\'"/' => '$1‘“', |
| 283 | '/"\'(\s|$)/' => '”’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 284 | '/(^|\s|<p>)"\'/' => '$1“‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 285 | '/"\'(\W)/' => '”’$1', |
| 286 | '/(\W)"\'/' => '$1“‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | |
| 288 | // single quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 289 | '/\'(\s|$)/' => '’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 290 | '/(^|\s|<p>)\'/' => '$1‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 291 | '/\'(\W)/' => '’$1', |
| 292 | '/(\W)\'/' => '$1‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | |
| 294 | // double quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 295 | '/"(\s|$)/' => '”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 296 | '/(^|\s|<p>)"/' => '$1“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 297 | '/"(\W)/' => '”$1', |
| 298 | '/(\W)"/' => '$1“', |
| 299 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | // apostrophes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 301 | "/(\w)'(\w)/" => '$1’$2', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | |
| 303 | // Em dash and ellipses dots |
| 304 | '/\s?\-\-\s?/' => '—', |
| 305 | '/(\w)\.{3}/' => '$1…', |
| 306 | |
| 307 | // double space after sentences |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 308 | '/(\W) /' => '$1 ', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | |
| 310 | // ampersands, if not a character entity |
| 311 | '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&' |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 312 | ); |
| 313 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | |
| 315 | return preg_replace(array_keys($table), $table, $str); |
| 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | // -------------------------------------------------------------------- |
| 319 | |
| 320 | /** |
| 321 | * Format Newlines |
| 322 | * |
| 323 | * Converts newline characters into either <p> tags or <br /> |
| 324 | * |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 325 | * @access private |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | * @param string |
| 327 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 328 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 329 | private function _format_newlines($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 331 | if ($str == '' OR (strpos($str, "\n") === FALSE AND ! in_array($this->last_block_element, $this->inner_block_required))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | { |
| 333 | return $str; |
| 334 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | // Convert two consecutive newlines to paragraphs |
| 337 | $str = str_replace("\n\n", "</p>\n\n<p>", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | // Convert single spaces to <br /> tags |
| 340 | $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 341 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | // Wrap the whole enchilada in enclosing paragraphs |
| 343 | if ($str != "\n") |
| 344 | { |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 345 | // We trim off the right-side new line so that the closing </p> tag |
| 346 | // will be positioned immediately following the string, matching |
| 347 | // the behavior of the opening <p> tag |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 348 | $str = '<p>'.rtrim($str).'</p>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Remove empty paragraphs if they are on the first line, as this |
| 352 | // is a potential unintended consequence of the previous code |
| 353 | $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 354 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | return $str; |
| 356 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | /** |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 361 | * Protect Characters |
| 362 | * |
| 363 | * Protects special characters from being formatted later |
| 364 | * 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] | 365 | * and we don't want double dashes converted to emdash entities, so they are marked with {@DD} |
| 366 | * likewise double spaces are converted to {@NBS} to prevent entity conversion |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 367 | * |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 368 | * @access private |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 369 | * @param array |
| 370 | * @return string |
| 371 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 372 | private function _protect_characters($match) |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 373 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 374 | return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]); |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 379 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | * Convert newlines to HTML line breaks except within PRE tags |
| 381 | * |
| 382 | * @access public |
| 383 | * @param string |
| 384 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 385 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 386 | public function nl2br_except_pre($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 388 | $newstr = ''; |
| 389 | for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 391 | $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i]; |
| 392 | if ($ct - 1 !== $i) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame^] | 394 | $newstr .= 'pre>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | return $newstr; |
| 399 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | } |
| 402 | // END Typography Class |
| 403 | |
| 404 | /* End of file Typography.php */ |
Gerry | 6f2b264 | 2011-09-25 00:30:52 +0800 | [diff] [blame] | 405 | /* Location: ./system/libraries/Typography.php */ |