2005-08-21 06:49:02 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
2005-08-21 06:49:02 +00:00
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright (c) 2005, CakePHP Authors/Developers
|
|
|
|
*
|
|
|
|
* Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com>
|
|
|
|
* Larry E. Masters aka PhpNut <nut@phpnut.com>
|
|
|
|
* Kamil Dzielinski aka Brego <brego.dk@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @author CakePHP Authors/Developers
|
|
|
|
* @copyright Copyright (c) 2005, CakePHP Authors/Developers
|
|
|
|
* @link https://trac.cakephp.org/wiki/Authors Authors/Developers
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*/
|
|
|
|
uses('object');
|
|
|
|
|
|
|
|
/**
|
2005-08-21 06:49:02 +00:00
|
|
|
* Short description for class
|
|
|
|
*
|
|
|
|
* Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
*/
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
class Flay extends Object
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
var $text = null;
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
var $allow_html = false;
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param unknown_type $text
|
|
|
|
*/
|
2005-07-16 06:10:56 +00:00
|
|
|
function __construct ($text=null)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
$this->text = $text;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns $text translated to HTML using the Flay syntax.
|
|
|
|
*
|
|
|
|
* @param string $text Text to format
|
|
|
|
* @param boolean $bare
|
|
|
|
* @param boolean $allowHtml Set this to trim whitespace and disable all HTML
|
|
|
|
* @return string Formatted text
|
|
|
|
*/
|
2005-07-16 06:10:56 +00:00
|
|
|
function toHtml ($text=null, $bare=false, $allowHtml=false)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
|
|
|
|
if (empty($text) && empty($this->text))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$text = $text? $text: $this->text;
|
|
|
|
|
|
|
|
// trim whitespace and disable all HTML
|
|
|
|
if ($allowHtml)
|
|
|
|
$text = trim($text);
|
|
|
|
else
|
|
|
|
$text = str_replace('<', '<', str_replace('>', '>', trim($text)));
|
|
|
|
|
2005-07-16 06:10:56 +00:00
|
|
|
if (!$bare)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
// multi-paragraph functions
|
|
|
|
$text = preg_replace('#(?:[\n]{0,2})"""(.*)"""(?:[\n]{0,2})#s', "\n\n%BLOCKQUOTE%\n\n\\1\n\n%ENDBLOCKQUOTE%\n\n", $text);
|
|
|
|
$text = preg_replace('#(?:[\n]{0,2})===(.*)===(?:[\n]{0,2})#s', "\n\n%CENTER%\n\n\\1\n\n%ENDCENTER%\n\n", $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pre-parse newlines
|
|
|
|
$text = preg_replace("#\r\n#", "\n", $text);
|
|
|
|
$text = preg_replace("#[\n]{2,}#", "%PARAGRAPH%", $text);
|
|
|
|
$text = preg_replace('#[\n]{1}#', "%LINEBREAK%", $text);
|
|
|
|
|
|
|
|
// split into paragraphs and parse
|
|
|
|
$out = '';
|
2005-07-16 06:10:56 +00:00
|
|
|
foreach (split('%PARAGRAPH%', $text) as $line)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
|
2005-07-16 06:10:56 +00:00
|
|
|
if ($line)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
|
2005-07-16 06:10:56 +00:00
|
|
|
if (!$bare)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
// pre-parse links
|
|
|
|
$links = array();
|
|
|
|
$regs = null;
|
2005-07-16 06:10:56 +00:00
|
|
|
if (preg_match_all('#\[([^\[]{4,})\]#', $line, $regs))
|
|
|
|
{
|
|
|
|
foreach ($regs[1] as $reg)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
$links[] = $reg;
|
|
|
|
$line = str_replace("[{$reg}]",'%LINK'.(count($links)-1).'%', $line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MAIN TEXT FUNCTIONS
|
|
|
|
// bold
|
|
|
|
$line = ereg_replace("\*([^\*]*)\*", "<strong>\\1</strong>", $line);
|
|
|
|
// italic
|
|
|
|
$line = ereg_replace("_([^_]*)_", "<em>\\1</em>", $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// entities
|
|
|
|
$line = str_replace(' - ', ' – ', $line);
|
|
|
|
$line = str_replace(' -- ', ' — ', $line);
|
|
|
|
$line = str_replace('(C)', '©', $line);
|
|
|
|
$line = str_replace('(R)', '®', $line);
|
|
|
|
$line = str_replace('(TM)', '™', $line);
|
|
|
|
|
|
|
|
// guess e-mails
|
|
|
|
$emails = null;
|
2005-07-16 06:10:56 +00:00
|
|
|
if (preg_match_all("#([_A-Za-z0-9+-+]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#", $line, $emails))
|
|
|
|
{
|
|
|
|
foreach ($emails[1] as $email)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
$line = str_replace($email, "<a href=\"mailto:{$email}\">{$email}</a>", $line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-16 06:10:56 +00:00
|
|
|
if (!$bare)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
// guess links
|
|
|
|
$urls = null;
|
|
|
|
if (preg_match_all("#((?:http|https|ftp|nntp)://[^ ]+)#", $line, $urls))
|
|
|
|
{
|
|
|
|
foreach ($urls[1] as $url)
|
|
|
|
{
|
|
|
|
$line = str_replace($url, "<a href=\"{$url}\">{$url}</a>", $line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (preg_match_all("#(www\.[^\n\%\ ]+[^\n\%\,\.\ ])#", $line, $urls))
|
|
|
|
{
|
|
|
|
foreach ($urls[1] as $url)
|
|
|
|
{
|
|
|
|
$line = str_replace($url, "<a href=\"http://{$url}\">{$url}</a>", $line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-parse links
|
2005-07-16 06:10:56 +00:00
|
|
|
if (count($links))
|
|
|
|
{
|
|
|
|
for ($ii=0; $ii<count($links); $ii++)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
if (preg_match("#^(http|https|ftp|nntp)://#", $links[$ii]))
|
|
|
|
{
|
|
|
|
$prefix = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$prefix = 'http://';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('#^[^\ ]+\.(jpg|jpeg|gif|png)$#', $links[$ii]))
|
|
|
|
{
|
|
|
|
$with = "<img src=\"{$prefix}{$links[$ii]}\" alt=\"\" />";
|
|
|
|
}
|
|
|
|
elseif (preg_match('#^([^\]\ ]+)(?:\ ([^\]]+))?$#', $links[$ii], $regs))
|
|
|
|
{
|
|
|
|
if (isset($regs[2]))
|
|
|
|
{
|
|
|
|
if (preg_match('#\.(jpg|jpeg|gif|png)$#', $regs[2]))
|
|
|
|
$body = "<img src=\"{$prefix}{$regs[2]}\" alt=\"\" />";
|
|
|
|
else
|
|
|
|
$body = $regs[2];
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$body = $links[$ii];
|
|
|
|
}
|
2005-07-16 06:10:56 +00:00
|
|
|
|
|
|
|
$with = "<a href=\"{$prefix}{$regs[1]}\" target=\"_blank\">{$body}</a>";
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$with = $prefix.$links[$ii];
|
|
|
|
}
|
|
|
|
|
|
|
|
$line = str_replace("%LINK{$ii}%", $with, $line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-parse newlines
|
|
|
|
$out .= str_replace('%LINEBREAK%', "<br />\n", "<p>{$line}</p>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-16 06:10:56 +00:00
|
|
|
if (!$bare)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
// re-parse multilines
|
|
|
|
$out = str_replace('<p>%BLOCKQUOTE%</p>', "<blockquote>", $out);
|
|
|
|
$out = str_replace('<p>%ENDBLOCKQUOTE%</p>', "</blockquote>", $out);
|
|
|
|
$out = str_replace('<p>%CENTER%</p>', "<center>", $out);
|
|
|
|
$out = str_replace('<p>%ENDCENTER%</p>', "</center>", $out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $string
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-07-16 06:10:56 +00:00
|
|
|
function extractWords ($string)
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
return preg_split('/[\s,\.:\/="!\(\)<>~\[\]]+/', $string);
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $words
|
|
|
|
* @param unknown_type $string
|
|
|
|
* @param unknown_type $max_snippets
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-07-16 06:10:56 +00:00
|
|
|
function markedSnippets ($words, $string, $max_snippets=5)
|
|
|
|
{
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
$string = strip_tags($string);
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
$snips = array();
|
|
|
|
$rest = $string;
|
2005-07-16 06:10:56 +00:00
|
|
|
foreach ($words as $word)
|
|
|
|
{
|
|
|
|
if (preg_match_all("/[\s,]+.{0,40}{$word}.{0,40}[\s,]+/i", $rest, $r))
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
foreach ($r as $result)
|
|
|
|
$rest = str_replace($result, '', $rest);
|
|
|
|
$snips = array_merge($snips, $r[0]);
|
|
|
|
}
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-07-16 06:10:56 +00:00
|
|
|
if (count($snips) > $max_snippets)
|
|
|
|
{
|
|
|
|
$snips = array_slice($snips, 0, $max_snippets);
|
|
|
|
}
|
2005-07-10 05:08:19 +00:00
|
|
|
$joined = join(' <b>...</b> ', $snips);
|
|
|
|
$snips = $joined? "<b>...</b> {$joined} <b>...</b>": substr($string, 0, 80).'<b>...</b>';
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
return Flay::colorMark($words, $snips);
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $words
|
|
|
|
* @param unknown_type $string
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function colorMark($words, $string)
|
|
|
|
{
|
|
|
|
$colors = array('yl','gr','rd','bl','fu','cy');
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
$nextColorIndex = 0;
|
|
|
|
foreach ($words as $word)
|
|
|
|
{
|
|
|
|
$string = preg_replace("/({$word})/i", '<em class="'.$colors[$nextColorIndex%count($colors)]."\">\\1</em>", $string);
|
|
|
|
$nextColorIndex++;
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
return $string;
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $text
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function toClean ($text)
|
|
|
|
{
|
|
|
|
return strip_tags(html_entity_decode($text, ENT_QUOTES));
|
|
|
|
}
|
|
|
|
|
|
|
|
function toParsedAndClean ($text)
|
|
|
|
{
|
|
|
|
return Flay::toClean(Flay::toHtml($text));
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param unknown_type $text
|
|
|
|
* @param unknown_type $length
|
|
|
|
* @param unknown_type $elipsis
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2005-07-16 06:10:56 +00:00
|
|
|
function fragment ($text, $length, $elipsis='...')
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
$soft=$length-5;
|
|
|
|
$hard=$length+5;
|
|
|
|
$rx = '/(.{'.$soft.','.$hard.'})[\s,\.:\/="!\(\)<>~\[\]]+.*/';
|
2005-07-16 06:10:56 +00:00
|
|
|
if (preg_match($rx, $text, $r))
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
$out = $r[1];
|
|
|
|
}
|
2005-07-16 06:10:56 +00:00
|
|
|
else
|
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
$out = substr($text,0,$length);
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = $out.(strlen($out)<strlen($text)? $elipsis: null);
|
|
|
|
return $out;
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
}
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
?>
|