Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 10
 *
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:
17
 *
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
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * Typography Class
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Libraries
45
 * @category	Helpers
46
 * @author		EllisLab Dev Team
47
 * @link		https://codeigniter.com/user_guide/libraries/typography.html
48
 */
49
class CI_Typography {
50
 
51
	/**
52
	 * Block level elements that should not be wrapped inside <p> tags
53
	 *
54
	 * @var string
55
	 */
56
	public $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
57
 
58
	/**
59
	 * Elements that should not have <p> and <br /> tags within them.
60
	 *
61
	 * @var string
62
	 */
63
	public $skip_elements	= 'p|pre|ol|ul|dl|object|table|h\d';
64
 
65
	/**
66
	 * Tags we want the parser to completely ignore when splitting the string.
67
	 *
68
	 * @var string
69
	 */
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';
71
 
72
	/**
73
	 * array of block level elements that require inner content to be within another block level element
74
	 *
75
	 * @var array
76
	 */
77
	public $inner_block_required = array('blockquote');
78
 
79
	/**
80
	 * the last block element parsed
81
	 *
82
	 * @var string
83
	 */
84
	public $last_block_element = '';
85
 
86
	/**
87
	 * whether or not to protect quotes within { curly braces }
88
	 *
89
	 * @var bool
90
	 */
91
	public $protect_braced_quotes = FALSE;
92
 
93
	/**
94
	 * Auto Typography
95
	 *
96
	 * This function converts text, making it typographically correct:
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.
102
	 *  - Converts two spaces into entities
103
	 *
104
	 * @param	string
105
	 * @param	bool	whether to reduce more then two consecutive newlines to two
106
	 * @return	string
107
	 */
108
	public function auto_typography($str, $reduce_linebreaks = FALSE)
109
	{
110
		if ($str === '')
111
		{
112
			return '';
113
		}
114
 
115
		// Standardize Newlines to make matching easier
116
		if (strpos($str, "\r") !== FALSE)
117
		{
118
			$str = str_replace(array("\r\n", "\r"), "\n", $str);
119
		}
120
 
121
		// Reduce line breaks.  If there are more than two consecutive linebreaks
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);
126
		}
127
 
128
		// HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
129
		$html_comments = array();
130
		if (strpos($str, '<!--') !== FALSE && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches))
131
		{
132
			for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
133
			{
134
				$html_comments[] = $matches[0][$i];
135
				$str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
136
			}
137
		}
138
 
139
		// match and yank <pre> tags if they exist.  It's cheaper to do this separately since most content will
140
		// not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
141
		if (strpos($str, '<pre') !== FALSE)
142
		{
143
			$str = preg_replace_callback('#<pre.*?>.*?</pre>#si', array($this, '_protect_characters'), $str);
144
		}
145
 
146
		// Convert quotes within tags to temporary markers.
147
		$str = preg_replace_callback('#<.+?>#si', array($this, '_protect_characters'), $str);
148
 
149
		// Do the same with braces if necessary
150
		if ($this->protect_braced_quotes === TRUE)
151
		{
152
			$str = preg_replace_callback('#\{.+?\}#si', array($this, '_protect_characters'), $str);
153
		}
154
 
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
157
		// adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
158
		$str = preg_replace('#<(/*)('.$this->inline_elements.')([ >])#i', '{@TAG}\\1\\2\\3', $str);
159
 
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
		 */
170
		$chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
171
 
172
		// Build our finalized string.  We cycle through the array, skipping tags, and processing the contained text
173
		$str = '';
174
		$process = TRUE;
175
 
176
		for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++)
177
		{
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.
180
			if (preg_match('#<(/*)('.$this->block_elements.').*?>#', $chunks[$i], $match))
181
			{
182
				if (preg_match('#'.$this->skip_elements.'#', $match[2]))
183
				{
184
					$process = ($match[1] === '/');
185
				}
186
 
187
				if ($match[1] === '')
188
				{
189
					$this->last_block_element = $match[2];
190
				}
191
 
192
				$str .= $chunks[$i];
193
				continue;
194
			}
195
 
196
			if ($process === FALSE)
197
			{
198
				$str .= $chunks[$i];
199
				continue;
200
			}
201
 
202
			//  Force a newline to make sure end tags get processed by _format_newlines()
203
			if ($i === $c)
204
			{
205
				$chunks[$i] .= "\n";
206
			}
207
 
208
			//  Convert Newlines into <p> and <br /> tags
209
			$str .= $this->_format_newlines($chunks[$i]);
210
		}
211
 
212
		// No opening block level tag? Add it if needed.
213
		if ( ! preg_match('/^\s*<(?:'.$this->block_elements.')/i', $str))
214
		{
215
			$str = preg_replace('/^(.*?)<('.$this->block_elements.')/i', '<p>$1</p><$2', $str);
216
		}
217
 
218
		// Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
219
		$str = $this->format_characters($str);
220
 
221
		// restore HTML comments
222
		for ($i = 0, $total = count($html_comments); $i < $total; $i++)
223
		{
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);
228
		}
229
 
230
		// Final clean up
231
		$table = array(
232
 
233
						// If the user submitted their own paragraph tags within the text
234
						// we will retain them instead of using our tags.
235
						'/(<p[^>*?]>)<p>/'	=> '$1', // <?php BBEdit syntax coloring bug fix
236
 
237
						// Reduce multiple instances of opening/closing paragraph tags to a single one
238
						'#(</p>)+#'			=> '</p>',
239
						'/(<p>\W*<p>)+/'	=> '<p>',
240
 
241
						// Clean up stray paragraph tags that appear before block level elements
242
						'#<p></p><('.$this->block_elements.')#'	=> '<$1',
243
 
2107 lars 244
						// Clean up stray non-breaking spaces preceding block elements
68 lars 245
						'#(&nbsp;\s*)+<('.$this->block_elements.')#'	=> '  <$2',
246
 
247
						// Replace the temporary markers we added earlier
248
						'/\{@TAG\}/'		=> '<',
249
						'/\{@DQ\}/'			=> '"',
250
						'/\{@SQ\}/'			=> "'",
251
						'/\{@DD\}/'			=> '--',
252
						'/\{@NBS\}/'		=> '  ',
253
 
254
						// An unintended consequence of the _format_newlines function is that
255
						// some of the newlines get truncated, resulting in <p> tags
256
						// starting immediately after <block> tags on the same line.
257
						// This forces a newline after such occurrences, which looks much nicer.
258
						"/><p>\n/"			=> ">\n<p>",
259
 
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
262
						'#</p></#'			=> "</p>\n</"
263
						);
264
 
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>&nbsp;</p>';
275
		}
276
 
277
		return preg_replace(array_keys($table), $table, $str);
278
 
279
	}
280
 
281
	// --------------------------------------------------------------------
282
 
283
	/**
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
	 *
290
	 * @param	string
291
	 * @return	string
292
	 */
293
	public function format_characters($str)
294
	{
295
		static $table;
296
 
297
		if ( ! isset($table))
298
		{
299
			$table = array(
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
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|$)/'					=> '&#8217;&#8221;$1',
308
							'/(^|\s|<p>)\'"/'				=> '$1&#8216;&#8220;',
309
							'/\'"(\W)/'						=> '&#8217;&#8221;$1',
310
							'/(\W)\'"/'						=> '$1&#8216;&#8220;',
311
							'/"\'(\s|$)/'					=> '&#8221;&#8217;$1',
312
							'/(^|\s|<p>)"\'/'				=> '$1&#8220;&#8216;',
313
							'/"\'(\W)/'						=> '&#8221;&#8217;$1',
314
							'/(\W)"\'/'						=> '$1&#8220;&#8216;',
315
 
316
							// single quote smart quotes
317
							'/\'(\s|$)/'					=> '&#8217;$1',
318
							'/(^|\s|<p>)\'/'				=> '$1&#8216;',
319
							'/\'(\W)/'						=> '&#8217;$1',
320
							'/(\W)\'/'						=> '$1&#8216;',
321
 
322
							// double quote smart quotes
323
							'/"(\s|$)/'						=> '&#8221;$1',
324
							'/(^|\s|<p>)"/'					=> '$1&#8220;',
325
							'/"(\W)/'						=> '&#8221;$1',
326
							'/(\W)"/'						=> '$1&#8220;',
327
 
328
							// apostrophes
329
							"/(\w)'(\w)/"					=> '$1&#8217;$2',
330
 
331
							// Em dash and ellipses dots
332
							'/\s?\-\-\s?/'					=> '&#8212;',
333
							'/(\w)\.{3}/'					=> '$1&#8230;',
334
 
335
							// double space after sentences
336
							'/(\W)  /'						=> '$1&nbsp; ',
337
 
338
							// ampersands, if not a character entity
339
							'/&(?!#?[a-zA-Z0-9]{2,};)/'		=> '&amp;'
340
						);
341
		}
342
 
343
		return preg_replace(array_keys($table), $table, $str);
344
	}
345
 
346
	// --------------------------------------------------------------------
347
 
348
	/**
349
	 * Format Newlines
350
	 *
351
	 * Converts newline characters into either <p> tags or <br />
352
	 *
353
	 * @param	string
354
	 * @return	string
355
	 */
356
	protected function _format_newlines($str)
357
	{
358
		if ($str === '' OR (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)))
359
		{
360
			return $str;
361
		}
362
 
363
		// Convert two consecutive newlines to paragraphs
364
		$str = str_replace("\n\n", "</p>\n\n<p>", $str);
365
 
366
		// Convert single spaces to <br /> tags
367
		$str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str);
368
 
369
		// Wrap the whole enchilada in enclosing paragraphs
370
		if ($str !== "\n")
371
		{
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
375
			$str =  '<p>'.rtrim($str).'</p>';
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
380
		return preg_replace('/<p><\/p>(.*)/', '\\1', $str, 1);
381
	}
382
 
383
	// ------------------------------------------------------------------------
384
 
385
	/**
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}
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
392
	 *
393
	 * @param	array
394
	 * @return	string
395
	 */
396
	protected function _protect_characters($match)
397
	{
398
		return str_replace(array("'",'"','--','  '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
399
	}
400
 
401
	// --------------------------------------------------------------------
402
 
403
	/**
404
	 * Convert newlines to HTML line breaks except within PRE tags
405
	 *
406
	 * @param	string
407
	 * @return	string
408
	 */
409
	public function nl2br_except_pre($str)
410
	{
411
		$newstr = '';
412
		for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++)
413
		{
414
			$newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
415
			if ($ct - 1 !== $i)
416
			{
417
				$newstr .= 'pre>';
418
			}
419
		}
420
 
421
		return $newstr;
422
	}
423
 
424
}