📄 textile2.php
字号:
<?php
/*
Plugin Name: Textile 2
Version: 2.0 Beta
Plugin URI: http://www.huddledmasses.org/
Description: This is a simple wrapper for <a href="http://textism.com/?wp">Dean Allen's</a> Humane Web Text Generator, also known as <a href="http://www.textism.com/tools/textile/">Textile</a>. Version 2 adds a lot of flexibility that makes it almost a HTML meta-language. As a cost, it's slower. If you use this plugin you should disable Textile 1 and Markdown, as they don't play well together.
Author: Dean Allen
Author URI: http://www.textism.com/?wp
*/
/*
This is Textile
A Humane Web Text Generator
Version 2.0 beta
8 July, 2003
Copyright (c) 2003, Dean Allen, www.textism.com
All rights reserved.
_______
LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name Textile nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
$hlgn = "(?:\<(?!>)|(?<!<)\>|\<\>|\=|[()]+)";
$vlgn = "[\-^~]";
$clas = "(?:\([^)]+\))";
$lnge = "(?:\[[^]]+\])";
$styl = "(?:\{[^}]+\})";
$cspn = "(?:\\\\\d+)";
$rspn = "(?:\/\d+)";
$textile_a = "(?:$hlgn?$vlgn?|$vlgn?$hlgn?)";
$textile_s = "(?:$cspn?$rspn?|$rspn?$cspn?)";
$textile_c = "(?:$clas?$styl?$lnge?|$styl?$lnge?$clas?|$lnge?$styl?$clas?)";
$pnct = '[\!"#\$%&\'()\*\+,\-\./:;<=>\?@\[\\\]\^_`{\|}\~]';
function textile($text,$lite='') {
if (get_magic_quotes_gpc()==1)
$text = stripslashes($text);
$text = incomingEntities($text);
$text = encodeEntities($text);
$text = fixEntities($text);
$text = cleanWhiteSpace($text);
$text = getRefs($text);
$text = noTextile($text);
$text = image($text);
$text = links($text);
$text = span($text);
$text = superscript($text);
$text = footnoteRef($text);
$text = code($text);
$text = glyphs($text);
$text = retrieve($text);
if ($lite=='') {
$text = lists($text);
$text = table($text);
$text = block($text);
}
/* clean up <notextile> */
$text = preg_replace('/<\/?notextile>/', "",$text);
/* turn the temp char back to an ampersand entity */
$text = str_replace("x%x%","&",$text);
$text = str_replace("<br />","<br />\n",$text);
return trim($text);
}
function pba($in,$element="") // "parse block attributes"
{
global $hlgn,$vlgn,$clas,$styl,$cspn,$rspn,$textile_a,$textile_s,$textile_c;
$style=''; $class=''; $language=''; $colspan=''; $rowspan=''; $id=''; $atts='';
if (!empty($in)) {
$matched = $in;
if($element=='td'){
if(preg_match("/\\\\(\d+)/",$matched,$csp)) $colspan=$csp[1];
if(preg_match("/\/(\d+)/",$matched,$rsp)) $rowspan=$rsp[1];
if (preg_match("/($vlgn)/",$matched,$vert))
$style[] = "vertical-align:".vAlign($vert[1]).";";
}
if(preg_match("/\{([^}]*)\}/",$matched,$sty)) {
$style[]=$sty[1].';';
$matched = str_replace($sty[0],'',$matched);
}
if(preg_match("/\[([^)]+)\]/U",$matched,$lng)) {
$language=$lng[1];
$matched = str_replace($lng[0],'',$matched);
}
if(preg_match("/\(([^()]+)\)/U",$matched,$cls)) {
$class=$cls[1];
$matched = str_replace($cls[0],'',$matched);
}
if(preg_match("/([(]+)/",$matched,$pl)) {
$style[] = "padding-left:".strlen($pl[1])."em;";
$matched = str_replace($pl[0],'',$matched);
}
if(preg_match("/([)]+)/",$matched,$pr)) {
dump($pr);
$style[] = "padding-right:".strlen($pr[1])."em;";
$matched = str_replace($pr[0],'',$matched);
}
if (preg_match("/($hlgn)/",$matched,$horiz))
$style[] = "text-align:".hAlign($horiz[1]).";";
if (preg_match("/^(.*)#(.*)$/",$class,$ids)) {
$id = $ids[2];
$class = $ids[1];
}
if($style) $atts.=' style="'.join("",$style).'"';
if($class) $atts.=' class="'.$class.'"';
if($language) $atts.=' lang="'.$language.'"';
if($id) $atts.=' id="'.$id.'"';
if($colspan) $atts.=' colspan="'.$colspan.'"';
if($rowspan) $atts.=' rowspan="'.$rowspan.'"';
return $atts;
} else {
return '';
}
}
// -------------------------------------------------------------
function table($text)
{
global $textile_a,$textile_c,$textile_s;
$text = $text."\n\n";
return preg_replace_callback("/^(?:table(_?$textile_s$textile_a$textile_c)\. ?\n)?^($textile_a$textile_c\.? ?\|.*\|)\n\n/smU",
"fTable",$text);
}
// -------------------------------------------------------------
function fTable($matches)
{
global $textile_s,$textile_a,$textile_c;
$tatts = pba($matches[1],'table');
foreach(preg_split("/\|$/m",$matches[2],-1,PREG_SPLIT_NO_EMPTY) as $row){
if (preg_match("/^($textile_a$textile_c\. )(.*)/m",$row,$rmtch)) {
$ratts = pba($rmtch[1],'tr');
$row = $rmtch[2];
} else $ratts = '';
foreach(explode("|",$row) as $cell){
$ctyp = "d";
if (preg_match("/^_/",$cell)) $ctyp = "h";
if (preg_match("/^(_?$textile_s$textile_a$textile_c\. )(.*)/",$cell,$cmtch)) {
$catts = pba($cmtch[1],'td');
$cell = $cmtch[2];
} else $catts = '';
if(trim($cell)!='')
$cells[] = "\t\t\t<t$ctyp$catts>$cell</t$ctyp>";
}
$rows[] = "\t\t<tr$ratts>\n".join("\n",$cells)."\n\t\t</tr>";
unset($cells,$catts);
}
return "\t<table$tatts>\n".join("\n",$rows)."\n\t</table>\n\n";
}
// -------------------------------------------------------------
function lists($text)
{
global $textile_a,$textile_c;
return preg_replace_callback("/^([#*]+$textile_c .*)$(?![^#*])/smU","fList",$text);
}
// -------------------------------------------------------------
function fList($m)
{
global $textile_a,$textile_c;
$text = explode("\n",$m[0]);
foreach($text as $line){
$nextline = next($text);
if(preg_match("/^([#*]+)($textile_a$textile_c) (.*)$/s",$line,$m)) {
list(,$tl,$atts,$content) = $m;
$nl = preg_replace("/^([#*]+)\s.*/","$1",$nextline);
if(!isset($lists[$tl])){
$lists[$tl] = true;
$atts = pba($atts);
$line = "\t<".lT($tl)."l$atts>\n\t<li>".$content;
} else {
$line = "\t\t<li>".$content;
}
if ($nl===$tl){
$line .= "</li>";
} elseif($nl=="*" or $nl=="#") {
$line .= "</li>\n\t</".lT($tl)."l>\n\t</li>";
unset($lists[$tl]);
}
if (!$nl) {
foreach($lists as $k=>$v){
$line .= "</li>\n\t</".lT($k)."l>";
unset($lists[$k]);
}
}
}
$out[] = $line;
}
return join("\n",$out);
}
// -------------------------------------------------------------
function lT($in)
{
return preg_match("/^#+/",$in) ? 'o' : 'u';
}
// -------------------------------------------------------------
function block($text)
{
global $textile_a,$textile_c;
$pre = false;
$find = array('bq','h[1-6]','fn\d+','p');
$text = preg_replace("/(.+)\n(?![#*\s|])/",
"$1<br />", $text);
$text = explode("\n",$text);
array_push($text," ");
foreach($text as $line) {
if (preg_match('/<pre>/i',$line)) { $pre = true; }
foreach($find as $tag){
$line = ($pre==false)
? preg_replace_callback("/^($tag)($textile_a$textile_c)\.(?::(\S+))? (.*)$/",
"fBlock",$line)
: $line;
}
$line = preg_replace('/^(?!\t|<\/?pre|<\/?code|$| )(.*)/',"\t<p>$1</p>",$line);
$line=($pre==true) ? str_replace("<br />","\n",$line):$line;
if (preg_match('/<\/pre>/i',$line)) { $pre = false; }
$out[] = $line;
}
return join("\n",$out);
}
// -------------------------------------------------------------
function fBlock($m)
{
# dump($m);
list(,$tag,$atts,$cite,$content) = $m;
$atts = pba($atts);
if(preg_match("/fn(\d+)/",$tag,$fns)){
$tag = 'p';
$atts.= ' id="fn'.$fns[1].'"';
$content = '<sup>'.$fns[1].'</sup> '.$content;
}
$start = "\t<$tag";
$end = "</$tag>";
if ($tag=="bq") {
$cite = checkRefs($cite);
$cite = ($cite!='') ? ' cite="'.$cite.'"' : '';
$start = "\t<blockquote$cite>\n\t\t<p";
$end = "</p>\n\t</blockquote>";
}
return "$start$atts>$content$end";
}
// -------------------------------------------------------------
function span($text)
{
global $textile_c,$pnct;
$qtags = array('\*\*','\*','\?\?','-','__','_','%','\+','~');
foreach($qtags as $f) {
$text = preg_replace_callback(
"/(?<=^|\s|\>|[[:punct:]]|[{(\[])
($f)
($textile_c)
(?::(\S+))?
(\w.+\w)
([[:punct:]]*)
$f
(?=[])}]|[[:punct:]]+|\s|$)
/xmU","fSpan",$text);
}
return $text;
}
// -------------------------------------------------------------
function fSpan($m)
{
# dump($m);
global $textile_c;
$qtags = array(
'*' => 'b',
'**' => 'strong',
'??' => 'cite',
'_' => 'em',
'__' => 'i',
'-' => 'del',
'%' => 'span',
'+' => 'ins',
'~' => 'sub');
list(,$tag,$atts,$cite,$content,$end) = $m;
$tag = $qtags[$tag];
$atts = pba($atts);
$atts.= ($cite!='') ? 'cite="'.$cite.'"' : '';
return "<$tag$atts>$content$end</$tag>";
}
// -------------------------------------------------------------
function links($text)
{
global $textile_c;
return preg_replace_callback('/
([\s[{(]|[[:punct:]])? # $pre
" # start
('.$textile_c.') # $atts
([^"]+) # $text
\s?
(?:\(([^)]+)\)(?="))? # $title
":
(\S+\b) # $url
(\/)? # $slash
([^\w\/;]*) # $post
(?=\s|$)
/Ux',"fLink",$text);
}
// -------------------------------------------------------------
function fLink($m)
{
list(,$pre,$atts,$text,$title,$url,$slash,$post) = $m;
$url = checkRefs($url);
$atts = pba($atts);
$atts.= ($title!='') ? ' title="'.$title.'"' : '';
$atts = ($atts!='') ? shelve($atts) : '';
return $pre.'<a href="'.$url.$slash.'"'.$atts.'>'.$text.'</a>'.$post;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -