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