Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: flay.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Text-to-HTML parser.
5
 *
6
 * Text-to-html parser, similar to {@link http://textism.com/tools/textile/ Textile} or {@link http://www.whytheluckystiff.net/ruby/redcloth/ RedCloth}.
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.libs
21
 * @since         CakePHP(tm) v 0.2.9
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
/**
28
 * Included libraries.
29
 *
30
 */
31
if (!class_exists('Object')) {
32
	uses('object');
33
}
34
/**
35
 * Text-to-HTML parser.
36
 *
37
 * Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.
38
 *
39
 * @package       cake
40
 * @subpackage    cake.cake.libs
41
 */
42
class Flay extends Object{
43
/**
44
 * Text to be parsed.
45
 *
46
 * @var string
47
 * @access public
48
 */
49
	var $text = null;
50
/**
51
 * Set this to allow HTML in the markup.
52
 *
53
 * @var boolean
54
 * @access public
55
 */
56
	var $allow_html = false;
57
/**
58
 * Constructor.
59
 *
60
 * @param string $text Text to transform
61
 */
62
	function __construct($text = null) {
63
		$this->text = $text;
64
		parent::__construct();
65
	}
66
/**
67
 * Returns given text translated to HTML using the Flay syntax.
68
 *
69
 * @param string $text String to format
70
 * @param boolean $bare	Set this to only do <p> transforms and > to &gt;, no typography additions.
71
 * @param boolean $allowHtml Set this to trim whitespace and disable all HTML
72
 * @return string Formatted text
73
 * @access public
74
 */
75
	function toHtml($text = null, $bare = false, $allowHtml = false) {
76
		if (empty($text) && empty($this->text)) {
77
			return false;
78
		}
79
		$text = $text ? $text : $this->text;
80
		// trim whitespace and disable all HTML
81
		if ($allowHtml) {
82
			$text = trim($text);
83
		} else {
84
			$text = str_replace('<', '&lt;', str_replace('>', '&gt;', trim($text)));
85
		}
86
 
87
		if (!$bare) {
88
			// multi-paragraph functions
89
			$text=preg_replace('#(?:[\n]{0,2})"""(.*)"""(?:[\n]{0,2})#s', "\n\n%BLOCKQUOTE%\n\n\\1\n\n%ENDBLOCKQUOTE%\n\n", $text);
90
			$text=preg_replace('#(?:[\n]{0,2})===(.*)===(?:[\n]{0,2})#s', "\n\n%CENTER%\n\n\\1\n\n%ENDCENTER%\n\n", $text);
91
		}
92
 
93
		// pre-parse newlines
94
		$text=preg_replace("#\r\n#", "\n", $text);
95
		$text=preg_replace("#[\n]{2,}#", "%PARAGRAPH%", $text);
96
		$text=preg_replace('#[\n]{1}#', "%LINEBREAK%", $text);
97
		$out ='';
98
 
99
		foreach (split('%PARAGRAPH%', $text)as $line) {
100
			if ($line) {
101
				if (!$bare) {
102
					$links = array();
103
					$regs = null;
104
 
105
					if (preg_match_all('#\[([^\[]{4,})\]#', $line, $regs)) {
106
						foreach ($regs[1] as $reg) {
107
							$links[] = $reg;
108
							$line = str_replace("[{$reg}]", '%LINK' . (count($links) - 1) . '%', $line);
109
						}
110
					}
111
					// bold
112
					$line = ereg_replace("\*([^\*]*)\*", "<strong>\\1</strong>", $line);
113
					// italic
114
					$line = ereg_replace("_([^_]*)_", "<em>\\1</em>", $line);
115
				}
116
				// entities
117
				$line = str_replace(' - ', ' &ndash; ', $line);
118
				$line = str_replace(' -- ', ' &mdash; ', $line);
119
				$line = str_replace('(C)', '&copy;', $line);
120
				$line = str_replace('(R)', '&reg;', $line);
121
				$line = str_replace('(TM)', '&trade;', $line);
122
				// guess e-mails
123
				$emails = null;
124
				if (preg_match_all("#([_A-Za-z0-9+-+]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#", $line, $emails)) {
125
					foreach ($emails[1] as $email) {
126
						$line = str_replace($email, "<a href=\"mailto:{$email}\">{$email}</a>", $line);
127
					}
128
				}
129
 
130
				if (!$bare) {
131
					$urls = null;
132
					if (preg_match_all("#((?:http|https|ftp|nntp)://[^ ]+)#", $line, $urls)) {
133
						foreach ($urls[1] as $url) {
134
							$line = str_replace($url, "<a href=\"{$url}\">{$url}</a>", $line);
135
						}
136
					}
137
 
138
					if (preg_match_all("#(www\.[^\n\%\ ]+[^\n\%\,\.\ ])#", $line, $urls)) {
139
						foreach ($urls[1] as $url) {
140
							$line = str_replace($url, "<a href=\"http://{$url}\">{$url}</a>", $line);
141
						}
142
					}
143
 
144
					if ($count = count($links)) {
145
						for ($ii = 0; $ii < $count; $ii++) {
146
							if (preg_match("#^(http|https|ftp|nntp)://#", $links[$ii])) {
147
								$prefix = null;
148
							} else {
149
								$prefix = 'http://';
150
							}
151
							if (preg_match('#^[^\ ]+\.(jpg|jpeg|gif|png)$#', $links[$ii])) {
152
								$with = "<img src=\"{$prefix}{$links[$ii]}\" alt=\"\" />";
153
							} elseif (preg_match('#^([^\]\ ]+)(?:\ ([^\]]+))?$#', $links[$ii], $regs)) {
154
								if (isset($regs[2])) {
155
									if (preg_match('#\.(jpg|jpeg|gif|png)$#', $regs[2])) {
156
										$body = "<img src=\"{$prefix}{$regs[2]}\" alt=\"\" />";
157
									} else {
158
										$body = $regs[2];
159
									}
160
								} else {
161
									$body = $links[$ii];
162
								}
163
								$with = "<a href=\"{$prefix}{$regs[1]}\" target=\"_blank\">{$body}</a>";
164
							} else {
165
								$with = $prefix . $links[$ii];
166
							}
167
							$line = str_replace("%LINK{$ii}%", $with, $line);
168
						}
169
					}
170
				}
171
				$out .= str_replace('%LINEBREAK%', "<br />\n", "<p>{$line}</p>\n");
172
			}
173
		}
174
 
175
		if (!$bare) {
176
			$out = str_replace('<p>%BLOCKQUOTE%</p>', "<blockquote>", $out);
177
			$out = str_replace('<p>%ENDBLOCKQUOTE%</p>', "</blockquote>", $out);
178
			$out = str_replace('<p>%CENTER%</p>', "<center>", $out);
179
			$out = str_replace('<p>%ENDCENTER%</p>', "</center>", $out);
180
		}
181
		return $out;
182
	}
183
/**
184
 * Return the words of the string as an array.
185
 *
186
 * @param string $string
187
 * @return array Array of words
188
 * @access public
189
 */
190
	function extractWords($string) {
191
		$split = preg_split('/[\s,\.:\/="!\(\)<>~\[\]]+/', $string);
192
		return $split;
193
	 }
194
/**
195
 * Return given string with words in array colorMarked, up to a number of times (defaults to 5).
196
 *
197
 * @param array $words			Words to look for and markup
198
 * @param string $string		String to look in
199
 * @param integer $max_snippets	Max number of snippets to extract
200
 * @return string String with words marked
201
 * @see colorMark
202
 * @access public
203
 */
204
	function markedSnippets($words, $string, $max_snippets = 5) {
205
		$string = strip_tags($string);
206
		$snips = array();
207
		$rest = $string;
208
		foreach ($words as $word) {
209
			if (preg_match_all("/[\s,]+.{0,40}{$word}.{0,40}[\s,]+/i", $rest, $r)) {
210
				foreach ($r as $result) {
211
					$rest = str_replace($result, '', $rest);
212
				}
213
				$snips = array_merge($snips, $r[0]);
214
			}
215
		}
216
 
217
		if (count($snips) > $max_snippets) {
218
			$snips = array_slice($snips, 0, $max_snippets);
219
		}
220
		$joined = join(' <b>...</b> ', $snips);
221
		$snips = $joined ? "<b>...</b> {$joined} <b>...</b>" : substr($string, 0, 80) . '<b>...</b>';
222
		return $this->colorMark($words, $snips);
223
	}
224
/**
225
 * Returns string with EM elements with color classes added.
226
 *
227
 * @param array $words Array of words to be colorized
228
 * @param string $string Text in which the words might be found
229
 * @return string String with words colorized
230
 * @access public
231
 */
232
	function colorMark($words, $string) {
233
		$colors=array('yl', 'gr', 'rd', 'bl', 'fu', 'cy');
234
		$nextColorIndex = 0;
235
		foreach ($words as $word) {
236
			$string = preg_replace("/({$word})/i", '<em class="' . $colors[$nextColorIndex % count($colors)] . "\">\\1</em>", $string);
237
			$nextColorIndex++;
238
		}
239
		return $string;
240
	}
241
/**
242
 * Returns given text with tags stripped out.
243
 *
244
 * @param string $text Text to clean
245
 * @return string Cleaned text
246
 * @access public
247
 */
248
	function toClean($text) {
249
		$strip = strip_tags(html_entity_decode($text, ENT_QUOTES));
250
		return $strip;
251
	}
252
/**
253
 * Return parsed text with tags stripped out.
254
 *
255
 * @param string $text Text to parse and clean
256
 * @return string Cleaned text
257
 * @access public
258
 */
259
	function toParsedAndClean($text) {
260
		return $this->toClean(Flay::toHtml($text));
261
	}
262
/**
263
 * Return a fragment of a text, up to $length characters long, with an ellipsis after it.
264
 *
265
 * @param string $text		Text to be truncated.
266
 * @param integer $length	Max length of text.
267
 * @param string $ellipsis	Sign to print after truncated text.
268
 * @return string Fragment
269
 * @access public
270
 */
271
	function fragment($text, $length, $ellipsis = '...') {
272
		$soft = $length - 5;
273
		$hard = $length + 5;
274
		$rx = '/(.{' . $soft . ',' . $hard . '})[\s,\.:\/="!\(\)<>~\[\]]+.*/';
275
 
276
		if (preg_match($rx, $text, $r)) {
277
			$out = $r[1];
278
		} else {
279
			$out = substr($text, 0, $length);
280
		}
281
		$out = $out . (strlen($out) < strlen($text) ? $ellipsis : null);
282
		return $out;
283
	}
284
}
285
?>