Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Typography Class |
| 42 | * |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/typography.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | class CI_Typography { |
| 50 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 51 | /** |
| 52 | * Block level elements that should not be wrapped inside <p> tags |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 53 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 54 | * @var string |
| 55 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 56 | 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] | 57 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 58 | /** |
| 59 | * Elements that should not have <p> and <br /> tags within them. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 63 | public $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 65 | /** |
| 66 | * Tags we want the parser to completely ignore when splitting the string. |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 67 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 68 | * @var string |
| 69 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 70 | 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] | 71 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 72 | /** |
| 73 | * array of block level elements that require inner content to be within another block level element |
| 74 | * |
| 75 | * @var array |
| 76 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 77 | public $inner_block_required = array('blockquote'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 79 | /** |
| 80 | * the last block element parsed |
| 81 | * |
| 82 | * @var string |
| 83 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 84 | public $last_block_element = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 86 | /** |
| 87 | * whether or not to protect quotes within { curly braces } |
| 88 | * |
| 89 | * @var bool |
| 90 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 91 | public $protect_braced_quotes = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | * Auto Typography |
| 95 | * |
| 96 | * This function converts text, making it typographically correct: |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | * - Converts double spaces into paragraphs. |
| 98 | * - Converts single line breaks into <br /> tags |
| 99 | * - Converts single and double quotes into correctly facing curly quote entities. |
| 100 | * - Converts three dots into ellipsis. |
| 101 | * - Converts double dashes into em-dashes. |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 102 | * - Converts two spaces into entities |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | * @param string |
| 105 | * @param bool whether to reduce more then two consecutive newlines to two |
| 106 | * @return string |
| 107 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 108 | public function auto_typography($str, $reduce_linebreaks = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 110 | if ($str === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | { |
| 112 | return ''; |
| 113 | } |
| 114 | |
| 115 | // Standardize Newlines to make matching easier |
| 116 | if (strpos($str, "\r") !== FALSE) |
| 117 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 118 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 121 | // Reduce line breaks. If there are more than two consecutive linebreaks |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | // we'll compress them down to a maximum of two since there's no benefit to more. |
| 123 | if ($reduce_linebreaks === TRUE) |
| 124 | { |
| 125 | $str = preg_replace("/\n\n+/", "\n\n", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 128 | // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed |
| 129 | $html_comments = array(); |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 130 | if (strpos($str, '<!--') !== FALSE && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches)) |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 131 | { |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 132 | for ($i = 0, $total = count($matches[0]); $i < $total; $i++) |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 133 | { |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 134 | $html_comments[] = $matches[0][$i]; |
| 135 | $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str); |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 139 | // 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] | 140 | // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster |
| 141 | if (strpos($str, '<pre') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 143 | $str = preg_replace_callback('#<pre.*?>.*?</pre>#si', array($this, '_protect_characters'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 146 | // Convert quotes within tags to temporary markers. |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 147 | $str = preg_replace_callback('#<.+?>#si', array($this, '_protect_characters'), $str); |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 148 | |
| 149 | // Do the same with braces if necessary |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | if ($this->protect_braced_quotes === TRUE) |
| 151 | { |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 152 | $str = preg_replace_callback('#\{.+?\}#si', array($this, '_protect_characters'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 155 | // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag |
| 156 | // 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] | 157 | // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG} |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 158 | $str = preg_replace('#<(/*)('.$this->inline_elements.')([ >])#i', '{@TAG}\\1\\2\\3', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 160 | /* Split the string at every tag. This expression creates an array with this prototype: |
| 161 | * |
| 162 | * [array] |
| 163 | * { |
| 164 | * [0] = <opening tag> |
| 165 | * [1] = Content... |
| 166 | * [2] = <closing tag> |
| 167 | * Etc... |
| 168 | * } |
| 169 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | $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] | 171 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 172 | // 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] | 173 | $str = ''; |
| 174 | $process = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Ronald Beilsma | 64b0136 | 2011-12-28 12:59:45 +0100 | [diff] [blame] | 176 | for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | // Are we dealing with a tag? If so, we'll skip the processing for this cycle. |
| 179 | // Well also set the "process" flag which allows us to skip <pre> tags and a few other things. |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 180 | if (preg_match('#<(/*)('.$this->block_elements.').*?>#', $chunks[$i], $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | { |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 182 | if (preg_match('#'.$this->skip_elements.'#', $match[2])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 184 | $process = ($match[1] === '/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 187 | if ($match[1] === '') |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 188 | { |
| 189 | $this->last_block_element = $match[2]; |
| 190 | } |
| 191 | |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 192 | $str .= $chunks[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | continue; |
| 194 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 196 | if ($process === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 198 | $str .= $chunks[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | continue; |
| 200 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 202 | // 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] | 203 | if ($i === $c) |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 204 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 205 | $chunks[$i] .= "\n"; |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 206 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 208 | // Convert Newlines into <p> and <br /> tags |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 209 | $str .= $this->_format_newlines($chunks[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 212 | // No opening block level tag? Add it if needed. |
| 213 | if ( ! preg_match('/^\s*<(?:'.$this->block_elements.')/i', $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | { |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 215 | $str = preg_replace('/^(.*?)<('.$this->block_elements.')/i', '<p>$1</p><$2', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 218 | // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands |
| 219 | $str = $this->format_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 221 | // restore HTML comments |
| 222 | for ($i = 0, $total = count($html_comments); $i < $total; $i++) |
| 223 | { |
Derek Jones | 4777fb8 | 2008-12-11 17:55:42 +0000 | [diff] [blame] | 224 | // remove surrounding paragraph tags, but only if there's an opening paragraph tag |
| 225 | // otherwise HTML comments at the ends of paragraphs will have the closing tag removed |
| 226 | // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment |
| 227 | $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] | 228 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | // Final clean up |
| 231 | $table = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | // If the user submitted their own paragraph tags within the text |
| 234 | // we will retain them instead of using our tags. |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 235 | '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | // Reduce multiple instances of opening/closing paragraph tags to a single one |
| 238 | '#(</p>)+#' => '</p>', |
| 239 | '/(<p>\W*<p>)+/' => '<p>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | // Clean up stray paragraph tags that appear before block level elements |
| 242 | '#<p></p><('.$this->block_elements.')#' => '<$1', |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 243 | |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 244 | // Clean up stray non-breaking spaces preceding block elements |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 245 | '#( \s*)+<('.$this->block_elements.')#' => ' <$2', |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 246 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | // Replace the temporary markers we added earlier |
| 248 | '/\{@TAG\}/' => '<', |
| 249 | '/\{@DQ\}/' => '"', |
| 250 | '/\{@SQ\}/' => "'", |
| 251 | '/\{@DD\}/' => '--', |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 252 | '/\{@NBS\}/' => ' ', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 254 | // An unintended consequence of the _format_newlines function is that |
| 255 | // some of the newlines get truncated, resulting in <p> tags |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 256 | // starting immediately after <block> tags on the same line. |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 257 | // This forces a newline after such occurrences, which looks much nicer. |
| 258 | "/><p>\n/" => ">\n<p>", |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 260 | // Similarly, there might be cases where a closing </block> will follow |
| 261 | // a closing </p> tag, so we'll correct it by adding a newline in between |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 262 | '#</p></#' => "</p>\n</" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | // Do we need to reduce empty lines? |
| 266 | if ($reduce_linebreaks === TRUE) |
| 267 | { |
| 268 | $table['#<p>\n*</p>#'] = ''; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | // If we have empty paragraph tags we add a non-breaking space |
| 273 | // otherwise most browsers won't treat them as true paragraphs |
| 274 | $table['#<p></p>#'] = '<p> </p>'; |
| 275 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 276 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | return preg_replace(array_keys($table), $table, $str); |
| 278 | |
| 279 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | * Format Characters |
| 285 | * |
| 286 | * This function mainly converts double and single quotes |
| 287 | * to curly entities, but it also converts em-dashes, |
| 288 | * double spaces, and ampersands |
| 289 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | * @param string |
| 291 | * @return string |
| 292 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 293 | public function format_characters($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | { |
| 295 | static $table; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | if ( ! isset($table)) |
| 298 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | $table = array( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | // nested smart quotes, opening and closing |
| 301 | // note that rules for grammar (English) allow only for two levels deep |
| 302 | // and that single quotes are _supposed_ to always be on the outside |
| 303 | // but we'll accommodate both |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 304 | // Note that in all cases, whitespace is the primary determining factor |
| 305 | // on which direction to curl, with non-word characters like punctuation |
| 306 | // being a secondary factor only after whitespace is addressed. |
| 307 | '/\'"(\s|$)/' => '’”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 308 | '/(^|\s|<p>)\'"/' => '$1‘“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 309 | '/\'"(\W)/' => '’”$1', |
| 310 | '/(\W)\'"/' => '$1‘“', |
| 311 | '/"\'(\s|$)/' => '”’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 312 | '/(^|\s|<p>)"\'/' => '$1“‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 313 | '/"\'(\W)/' => '”’$1', |
| 314 | '/(\W)"\'/' => '$1“‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | |
| 316 | // single quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 317 | '/\'(\s|$)/' => '’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 318 | '/(^|\s|<p>)\'/' => '$1‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 319 | '/\'(\W)/' => '’$1', |
| 320 | '/(\W)\'/' => '$1‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | |
| 322 | // double quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 323 | '/"(\s|$)/' => '”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 324 | '/(^|\s|<p>)"/' => '$1“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 325 | '/"(\W)/' => '”$1', |
| 326 | '/(\W)"/' => '$1“', |
| 327 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | // apostrophes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 329 | "/(\w)'(\w)/" => '$1’$2', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | |
| 331 | // Em dash and ellipses dots |
| 332 | '/\s?\-\-\s?/' => '—', |
| 333 | '/(\w)\.{3}/' => '$1…', |
| 334 | |
| 335 | // double space after sentences |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 336 | '/(\W) /' => '$1 ', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | |
| 338 | // ampersands, if not a character entity |
| 339 | '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&' |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 340 | ); |
| 341 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | |
| 343 | return preg_replace(array_keys($table), $table, $str); |
| 344 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 345 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | // -------------------------------------------------------------------- |
| 347 | |
| 348 | /** |
| 349 | * Format Newlines |
| 350 | * |
| 351 | * Converts newline characters into either <p> tags or <br /> |
| 352 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | * @param string |
| 354 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 355 | */ |
Andrey Andreev | 43aa8e4 | 2011-12-26 16:53:18 +0200 | [diff] [blame] | 356 | protected function _format_newlines($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 358 | if ($str === '' OR (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] | 359 | { |
| 360 | return $str; |
| 361 | } |
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 | // Convert two consecutive newlines to paragraphs |
| 364 | $str = str_replace("\n\n", "</p>\n\n<p>", $str); |
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 | // Convert single spaces to <br /> tags |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 367 | $str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 368 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | // Wrap the whole enchilada in enclosing paragraphs |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 370 | if ($str !== "\n") |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | { |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame] | 372 | // We trim off the right-side new line so that the closing </p> tag |
| 373 | // will be positioned immediately following the string, matching |
| 374 | // the behavior of the opening <p> tag |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 375 | $str = '<p>'.rtrim($str).'</p>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // Remove empty paragraphs if they are on the first line, as this |
| 379 | // is a potential unintended consequence of the previous code |
Andrey Andreev | d69082b | 2012-03-26 16:14:26 +0300 | [diff] [blame] | 380 | return preg_replace('/<p><\/p>(.*)/', '\\1', $str, 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 382 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | /** |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 386 | * Protect Characters |
| 387 | * |
| 388 | * Protects special characters from being formatted later |
| 389 | * 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] | 390 | * and we don't want double dashes converted to emdash entities, so they are marked with {@DD} |
| 391 | * likewise double spaces are converted to {@NBS} to prevent entity conversion |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 392 | * |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 393 | * @param array |
| 394 | * @return string |
| 395 | */ |
Andrey Andreev | 43aa8e4 | 2011-12-26 16:53:18 +0200 | [diff] [blame] | 396 | protected function _protect_characters($match) |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 397 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 398 | return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]); |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 403 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | * Convert newlines to HTML line breaks except within PRE tags |
| 405 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | * @param string |
| 407 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | */ |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 409 | public function nl2br_except_pre($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 411 | $newstr = ''; |
| 412 | 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] | 413 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 414 | $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i]; |
| 415 | if ($ct - 1 !== $i) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
Andrey Andreev | 7dc8c44 | 2011-12-25 16:52:37 +0200 | [diff] [blame] | 417 | $newstr .= 'pre>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 420 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | return $newstr; |
| 422 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | } |