Text
Blockquote: bq. Example: bq. Block quotation... ->Block quotation...Blockquote with citation: bq.:http://citation.url Example: bq.:http://textism.com/ Text... ->
Text...Footnote: fn(1-100). Example: fn1. Footnote... ->
Footnote...
Numeric list: #, ## Consecutive paragraphs beginning with # are wrapped in ordered list tags. Example:computer code
%(bob)span% -> span
==notextile== -> leave text alone (do not format)
"linktext":url -> linktext
"linktext(title)":url -> linktext
!imageurl! -> paragraph
p(#fluid). paragraph ->paragraph
(classes and ids can be combined) p(hector#fluid). paragraph ->paragraph
Curly {brackets} insert arbitrary css style p{line-height:18px}. paragraph ->paragraph
h3{color:red}. header 3 ->paragraph
%[fr]phrase% -> phrase Usually Textile block element syntax requires a dot and space before the block begins, but since lists don't, they can be styled just using braces #{color:blue} one ->/i', $line)) {
$pre = true;
}
elseif (preg_match('//i', $line)) {
$php = true;
}
elseif (preg_match('/^\s*a$this->c)\.(?::(\S+))? (.*)$/s",
array(&$this, "fBlock"), $line)
: $line;
}
$hasraw = $this->hasRawText($line);
if (!$php and !$txp and $hasraw)
$line = preg_replace('/^(?!\t|<\/?pre|<\/?code|$| )(.*)/s', "\t$1
", $line);
$line = $this->doPBr($line);
$line = preg_replace('/
/', '
', $line);
if (preg_match('/<\/pre>/i', $line)) {
$pre = false;
}
elseif (preg_match('/<\/txp:php>/i', $line)) {
$php = false;
}
if ($txp == true) $txp = false;
$out[] = $line;
}
return join("\n", $out);
}
// -------------------------------------------------------------
function fBlock($m)
{
// $this->dump($m);
list(, $tag, $atts, $cite, $content) = $m;
$atts = $this->pba($atts);
if (preg_match("/fn(\d+)/", $tag, $fns)) {
$tag = 'p';
$fnid = empty($this->fn[$fns[1]]) ? $fns[1] : $this->fn[$fns[1]];
$atts .= ' id="fn' . $fnid . '"';
$content = '' . $fns[1] . ' ' . $content;
}
$start = "\t<$tag";
$end = "$tag>";
if ($tag == "bq") {
$cite = $this->checkRefs($cite);
$cite = ($cite != '') ? ' cite="' . $cite . '"' : '';
$start = "\t\n\t\t\n\t
";
}
return "$start$atts>$content$end";
}
// -------------------------------------------------------------
function span($text)
{
$qtags = array('\*\*','\*','\?\?','-','__','_','%','\+','~','\^');
$pnct = ".,\"'?!;:";
foreach($qtags as $f) {
$text = preg_replace_callback("/
(?:^|(?<=[\s>$pnct])|([{[]))
($f)
($this->c)
(?::(\S+))?
([^\s$f]+|\S[^$f]*[^\s$f])
([$pnct]*)
$f
(?:$|([\]}])|(?=[[:punct:]]{1,2}|\s))
/xU", array(&$this, "fSpan"), $text);
}
return $text;
}
// -------------------------------------------------------------
function fSpan($m)
{
$qtags = array(
'*' => 'strong',
'**' => 'b',
'??' => 'cite',
'_' => 'em',
'__' => 'i',
'-' => 'del',
'%' => 'span',
'+' => 'ins',
'~' => 'sub',
'^' => 'sup',
);
list(,, $tag, $atts, $cite, $content, $end) = $m;
$tag = $qtags[$tag];
$atts = $this->pba($atts);
$atts .= ($cite != '') ? 'cite="' . $cite . '"' : '';
$out = "<$tag$atts>$content$end$tag>";
// $this->dump($out);
return $out;
}
// -------------------------------------------------------------
function links($text)
{
return preg_replace_callback('/
([\s[{(]|[[:punct:]])? # $pre
" # start
(' . $this->c . ') # $atts
([^"]+) # $text
\s?
(?:\(([^)]+)\)(?="))? # $title
":
('.$this->urlch.'+) # $url
(\/)? # $slash
([^\w\/;]*) # $post
(?=\s|$)
/Ux', array(&$this, "fLink"), $text);
}
// -------------------------------------------------------------
function fLink($m)
{
list(, $pre, $atts, $text, $title, $url, $slash, $post) = $m;
$url = $this->checkRefs($url);
$atts = $this->pba($atts);
$atts .= ($title != '') ? 'title="' . htmlspecialchars($title) . '"' : '';
$atts = ($atts) ? $this->shelve($atts) : '';
$url = $this->relURL($url);
$out = $pre . 'rel . '>' . $text . '' . $post;
// $this->dump($out);
return $out;
}
// -------------------------------------------------------------
function getRefs($text)
{
return preg_replace_callback("/(?<=^|\s)\[(.+)\]((?:http:\/\/|\/)\S+)(?=\s|$)/U",
array(&$this, "refs"), $text);
}
// -------------------------------------------------------------
function refs($m)
{
list(, $flag, $url) = $m;
$this->urlrefs[$flag] = $url;
return '';
}
// -------------------------------------------------------------
function checkRefs($text)
{
return (isset($this->urlrefs[$text])) ? $this->urlrefs[$text] : $text;
}
// -------------------------------------------------------------
function relURL($url)
{
$parts = parse_url($url);
if ((empty($parts['scheme']) or @$parts['scheme'] == 'http') and
empty($parts['host']) and
preg_match('/^\w/', @$parts['path']))
$url = hu.$url;
return $url;
}
// -------------------------------------------------------------
function image($text)
{
return preg_replace_callback("/
\! # opening !
(\<|\=|\>)?? # optional alignment atts
($this->c) # optional style,class atts
(?:\. )? # optional dot-space
([^\s(!]+) # presume this is the src
\s? # optional space
(?:\(([^\)]+)\))? # optional title
\! # closing
(?::(\S+))? # optional href
(?=\s|$|[\]})]) # lookahead: space or end of string
/Ux", array(&$this, "fImage"), $text);
}
// -------------------------------------------------------------
function fImage($m)
{
list(, $algn, $atts, $url) = $m;
$atts = $this->pba($atts);
$atts .= ($algn != '') ? ' align="' . $this->iAlign($algn) . '"' : '';
$atts .= (isset($m[4])) ? ' title="' . $m[4] . '"' : '';
$atts .= (isset($m[4])) ? ' alt="' . $m[4] . '"' : ' alt=""';
$size = @getimagesize($url);
if ($size) $atts .= " $size[3]";
$href = (isset($m[5])) ? $this->checkRefs($m[5]) : '';
$url = $this->checkRefs($url);
$url = $this->relURL($url);
$out = array(
($href) ? '' : '',
'
',
($href) ? '' : ''
);
return join('',$out);
}
// -------------------------------------------------------------
function code($text)
{
return preg_replace_callback("/
(?:^|(?<=[\s\(])|([[{])) # before
@
(?:\|(\w+)\|)? # lang
(.+) # code
@
(?:$|([\]}])|
(?=[[:punct:]]{1,2}|
\s|$)) # after
/Ux", array(&$this, "fCode"), $text);
}
// -------------------------------------------------------------
function fCode($m)
{
@list(, $before, $lang, $code, $after) = $m;
$lang = ($lang) ? ' language="' . $lang . '"' : '';
return $before . '' . $code . '' . $after;
}
// -------------------------------------------------------------
function shelve($val)
{
$this->shelf[] = $val;
return ' <' . count($this->shelf) . '>';
}
// -------------------------------------------------------------
function retrieve($text)
{
$i = 0;
if (isset($this->shelf) && is_array($this->shelf)) {
foreach($this->shelf as $r) {
$i++;
$text = str_replace("<$i>", $r, $text);
}
}
return $text;
}
// -------------------------------------------------------------
function incomingEntities($text)
{
return preg_replace("/&(?![#a-z0-9]+;)/i", "x%x%", $text);
}
// -------------------------------------------------------------
function encodeEntities($text)
{
return (function_exists('mb_encode_numericentity'))
? $this->encode_high($text)
: htmlentities($text, ENT_NOQUOTES, "utf-8");
}
// -------------------------------------------------------------
function fixEntities($text)
{
/* de-entify any remaining angle brackets or ampersands */
return str_replace(array(">", "<", "&"),
array(">", "<", "&"), $text);
}
// -------------------------------------------------------------
function cleanWhiteSpace($text)
{
$out = str_replace(array("\r\n", "\t"), array("\n", ''), $text);
$out = preg_replace("/\n{3,}/", "\n\n", $out);
$out = preg_replace("/\n *\n/", "\n\n", $out);
$out = preg_replace('/"$/', "\" ", $out);
return $out;
}
// -------------------------------------------------------------
function noTextile($text)
{
$text = preg_replace_callback('/(^|\s)(.*)<\/notextile>(\s|$)?/msU',
array(&$this, "fTextile"), $text);
return preg_replace_callback('/(^|\s)==(.*)==(\s|$)?/msU',
array(&$this, "fTextile"), $text);
}
// -------------------------------------------------------------
function fTextile($m)
{
$modifiers = array(
'"' => '"',
'%' => '%',
'*' => '*',
'+' => '+',
'-' => '-',
'<' => '<',
'=' => '=',
'>' => '>',
'?' => '?',
'^' => '^',
'_' => '_',
'~' => '~',
);
@list(, $before, $notextile, $after) = $m;
$notextile = str_replace(array_keys($modifiers), array_values($modifiers), $notextile);
return $before . '' . $notextile . ' ' . $after;
}
// -------------------------------------------------------------
function footnoteRef($text)
{
return preg_replace('/\b\[([0-9]+)\](\s)?/Ue',
'$this->footnoteID(\'\1\',\'\2\')', $text);
}
// -------------------------------------------------------------
function footnoteID($id, $t)
{
if (empty($this->fn[$id]))
$this->fn[$id] = uniqid(rand());
$fnid = $this->fn[$id];
return ''.$id.''.$t;
}
// -------------------------------------------------------------
function glyphs($text)
{
// fix: hackish
$text = preg_replace('/"\z/', "\" ", $text);
$pnc = '[[:punct:]]';
$glyph_search = array(
'/([^\s[{(>_*])?\'(?(1)|(?=\s|s\b|'.$pnc.'))/', // single closing
'/\'/', // single opening
'/([^\s[{(>_*])?"(?(1)|(?=\s|'.$pnc.'))/', // double closing
'/"/', // double opening
'/\b( )?\.{3}/', // ellipsis
'/\b([A-Z][A-Z0-9]{2,})\b(?:[(]([^)]*)[)])/', // 3+ uppercase acronym
'/\s?--\s?/', // em dash
'/\s-\s/', // en dash
'/(\d+) ?x ?(\d+)/', // dimension sign
'/\b ?[([]TM[])]/i', // trademark
'/\b ?[([]R[])]/i', // registered
'/\b ?[([]C[])]/i'); // copyright
$glyph_replace = array('$1’$2', // single closing
'‘', // single opening
'$1”', // double closing
'“', // double opening
'$1…', // ellipsis
'$1', // 3+ uppercase acronym
'—', // em dash
' – ', // en dash
'$1×$2', // dimension sign
'™', // trademark
'®', // registered
'©'); // copyright
$codepre = false;
/* if no html, do a simple search and replace... */
if (!preg_match("/<.*>/", $text)) {
$text = preg_replace($glyph_search, $glyph_replace, $text);
return $text;
}
else {
$text = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach($text as $line) {
$offtags = ('code|pre|kbd|notextile|txp:php');
/* matches are off if we're between , etc. */
if (preg_match('/<(' . $offtags . ')>/i', $line)) $codepre = true;
if (preg_match('/<\/(' . $offtags . ')>/i', $line)) $codepre = false;
if (!preg_match("/<.*>/", $line) && $codepre == false) {
$line = preg_replace($glyph_search, $glyph_replace, $line);
}
/* do htmlspecial if between */
if ($codepre == true) {
$line = htmlspecialchars($line, ENT_NOQUOTES, "UTF-8");
$line = preg_replace('/<(\/?' . $offtags . ')>/', "<$1>", $line);
$line = str_replace("&#","",$line);
}
$glyph_out[] = $line;
}
return join('', $glyph_out);
}
}
// -------------------------------------------------------------
function iAlign($in)
{
$vals = array(
'<' => 'left',
'=' => 'center',
'>' => 'right');
return (isset($vals[$in])) ? $vals[$in] : '';
}
// -------------------------------------------------------------
function hAlign($in)
{
$vals = array(
'<' => 'left',
'=' => 'center',
'>' => 'right',
'<>' => 'justify');
return (isset($vals[$in])) ? $vals[$in] : '';
}
// -------------------------------------------------------------
function vAlign($in)
{
$vals = array(
'^' => 'top',
'-' => 'middle',
'~' => 'bottom');
return (isset($vals[$in])) ? $vals[$in] : '';
}
// -------------------------------------------------------------
function encode_high($text, $charset = "UTF-8")
{
return mb_encode_numericentity($text, $this->cmap(), $charset);
}
// -------------------------------------------------------------
function decode_high($text, $charset = "UTF-8")
{
return mb_decode_numericentity($text, $this->cmap(), $charset);
}
// -------------------------------------------------------------
function cmap()
{
$f = 0xffff;
$cmap = array(
0x0080, 0xffff, 0, $f);
return $cmap;
}
// -------------------------------------------------------------
function textile_popup_help($name, $helpvar, $windowW, $windowH)
{
return ' ' . $name . '
';
return $out;
}
// -------------------------------------------------------------
function txtgps($thing)
{
if (isset($_POST[$thing])) {
if (get_magic_quotes_gpc()) {
return stripslashes($_POST[$thing]);
}
else {
return $_POST[$thing];
}
}
else {
return '';
}
}
// -------------------------------------------------------------
function dump()
{
foreach (func_get_args() as $a)
echo "\n",(is_array($a)) ? print_r($a) : $a, "
\n";
}
// -------------------------------------------------------------
function blockLite($text)
{
$find = array('bq', 'p');
$text = preg_replace("/(.+)\n(?![#*\s|])/",
"$1
", $text);
$text = explode("\n", $text);
array_push($text, " ");
foreach($text as $line) {
foreach($find as $tag) {
$line = preg_replace_callback("/^($tag)($this->a$this->c)\.(?::(\S+))? (.*)$/",
array(&$this, "fBlock"), $line);
}
$line = preg_replace('/^(?!\t|<\/?pre|<\/?code|$| )(.*)/', "\t$1
", $line);
$out[] = $line;
}
return join("\n", $out);
}
} // end class
?>