A tumblelog CMS built on AJAX, PHP and MySQL.

textile.class.php 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. <?php
  2. if (!defined('entry') || !entry) {
  3. die('Not a valid page');
  4. }
  5. /**
  6. * Example: get XHTML from a given Textile-markup string ($string)
  7. *
  8. * $textile = new Textile;
  9. * echo $textile->TextileThis($string);
  10. *
  11. */
  12. /*
  13. $Id: classTextile.php 216 2006-10-17 22:31:53Z zem $
  14. $LastChangedRevision: 216 $
  15. */
  16. /*
  17. _____________
  18. T E X T I L E
  19. A Humane Web Text Generator
  20. Version 2.0
  21. Copyright (c) 2003-2004, Dean Allen <dean@textism.com>
  22. All rights reserved.
  23. Thanks to Carlo Zottmann <carlo@g-blog.net> for refactoring
  24. Textile's procedural code into a class framework
  25. Additions and fixes Copyright (c) 2006 Alex Shiels http://thresholdstate.com/
  26. _____________
  27. L I C E N S E
  28. Redistribution and use in source and binary forms, with or without
  29. modification, are permitted provided that the following conditions are met:
  30. * Redistributions of source code must retain the above copyright notice,
  31. this list of conditions and the following disclaimer.
  32. * Redistributions in binary form must reproduce the above copyright notice,
  33. this list of conditions and the following disclaimer in the documentation
  34. and/or other materials provided with the distribution.
  35. * Neither the name Textile nor the names of its contributors may be used to
  36. endorse or promote products derived from this software without specific
  37. prior written permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  42. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  43. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  44. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  45. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  46. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48. POSSIBILITY OF SUCH DAMAGE.
  49. _________
  50. U S A G E
  51. Block modifier syntax:
  52. Header: h(1-6).
  53. Paragraphs beginning with 'hn. ' (where n is 1-6) are wrapped in header tags.
  54. Example: h1. Header... -> <h1>Header...</h1>
  55. Paragraph: p. (also applied by default)
  56. Example: p. Text -> <p>Text</p>
  57. Blockquote: bq.
  58. Example: bq. Block quotation... -> <blockquote>Block quotation...</blockquote>
  59. Blockquote with citation: bq.:http://citation.url
  60. Example: bq.:http://textism.com/ Text...
  61. -> <blockquote cite="http://textism.com">Text...</blockquote>
  62. Footnote: fn(1-100).
  63. Example: fn1. Footnote... -> <p id="fn1">Footnote...</p>
  64. Numeric list: #, ##
  65. Consecutive paragraphs beginning with # are wrapped in ordered list tags.
  66. Example: <ol><li>ordered list</li></ol>
  67. Bulleted list: *, **
  68. Consecutive paragraphs beginning with * are wrapped in unordered list tags.
  69. Example: <ul><li>unordered list</li></ul>
  70. Phrase modifier syntax:
  71. _emphasis_ -> <em>emphasis</em>
  72. __italic__ -> <i>italic</i>
  73. *strong* -> <strong>strong</strong>
  74. **bold** -> <b>bold</b>
  75. ??citation?? -> <cite>citation</cite>
  76. -deleted text- -> <del>deleted</del>
  77. +inserted text+ -> <ins>inserted</ins>
  78. ^superscript^ -> <sup>superscript</sup>
  79. ~subscript~ -> <sub>subscript</sub>
  80. @code@ -> <code>computer code</code>
  81. %(bob)span% -> <span class="bob">span</span>
  82. ==notextile== -> leave text alone (do not format)
  83. "linktext":url -> <a href="url">linktext</a>
  84. "linktext(title)":url -> <a href="url" title="title">linktext</a>
  85. !imageurl! -> <img src="imageurl" />
  86. !imageurl(alt text)! -> <img src="imageurl" alt="alt text" />
  87. !imageurl!:linkurl -> <a href="linkurl"><img src="imageurl" /></a>
  88. ABC(Always Be Closing) -> <acronym title="Always Be Closing">ABC</acronym>
  89. Table syntax:
  90. Simple tables:
  91. |a|simple|table|row|
  92. |And|Another|table|row|
  93. |_. A|_. table|_. header|_.row|
  94. |A|simple|table|row|
  95. Tables with attributes:
  96. table{border:1px solid black}.
  97. {background:#ddd;color:red}. |{}| | | |
  98. Applying Attributes:
  99. Most anywhere Textile code is used, attributes such as arbitrary css style,
  100. css classes, and ids can be applied. The syntax is fairly consistent.
  101. The following characters quickly alter the alignment of block elements:
  102. < -> left align ex. p<. left-aligned para
  103. > -> right align h3>. right-aligned header 3
  104. = -> centred h4=. centred header 4
  105. <> -> justified p<>. justified paragraph
  106. These will change vertical alignment in table cells:
  107. ^ -> top ex. |^. top-aligned table cell|
  108. - -> middle |-. middle aligned|
  109. ~ -> bottom |~. bottom aligned cell|
  110. Plain (parentheses) inserted between block syntax and the closing dot-space
  111. indicate classes and ids:
  112. p(hector). paragraph -> <p class="hector">paragraph</p>
  113. p(#fluid). paragraph -> <p id="fluid">paragraph</p>
  114. (classes and ids can be combined)
  115. p(hector#fluid). paragraph -> <p class="hector" id="fluid">paragraph</p>
  116. Curly {brackets} insert arbitrary css style
  117. p{line-height:18px}. paragraph -> <p style="line-height:18px">paragraph</p>
  118. h3{color:red}. header 3 -> <h3 style="color:red">header 3</h3>
  119. Square [brackets] insert language attributes
  120. p[no]. paragraph -> <p lang="no">paragraph</p>
  121. %[fr]phrase% -> <span lang="fr">phrase</span>
  122. Usually Textile block element syntax requires a dot and space before the block
  123. begins, but since lists don't, they can be styled just using braces
  124. #{color:blue} one -> <ol style="color:blue">
  125. # big <li>one</li>
  126. # list <li>big</li>
  127. <li>list</li>
  128. </ol>
  129. Using the span tag to style a phrase
  130. It goes like this, %{color:red}the fourth the fifth%
  131. -> It goes like this, <span style="color:red">the fourth the fifth</span>
  132. */
  133. // define these before including this file to override the standard glyphs
  134. @define('txt_quote_single_open', '&#8216;');
  135. @define('txt_quote_single_close', '&#8217;');
  136. @define('txt_quote_double_open', '&#8220;');
  137. @define('txt_quote_double_close', '&#8221;');
  138. @define('txt_apostrophe', '&#8217;');
  139. @define('txt_prime', '&#8242;');
  140. @define('txt_prime_double', '&#8243;');
  141. @define('txt_ellipsis', '&#8230;');
  142. @define('txt_emdash', '&#8212;');
  143. @define('txt_endash', '&#8211;');
  144. @define('txt_dimension', '&#215;');
  145. @define('txt_trademark', '&#8482;');
  146. @define('txt_registered', '&#174;');
  147. @define('txt_copyright', '&#169;');
  148. class Textile
  149. {
  150. public $hlgn;
  151. public $vlgn;
  152. public $clas;
  153. public $lnge;
  154. public $styl;
  155. public $cspn;
  156. public $rspn;
  157. public $a;
  158. public $s;
  159. public $c;
  160. public $pnct;
  161. public $rel;
  162. public $fn;
  163. public $shelf = array();
  164. public $restricted = false;
  165. public $noimage = false;
  166. public $lite = false;
  167. public $url_schemes = array();
  168. public $glyph = array();
  169. public $hu = '';
  170. public $ver = '2.0.0';
  171. public $rev = '$Rev: 216 $';
  172. // -------------------------------------------------------------
  173. public function Textile()
  174. {
  175. $this->hlgn = "(?:\<(?!>)|(?<!<)\>|\<\>|\=|[()]+(?! ))";
  176. $this->vlgn = "[\-^~]";
  177. $this->clas = "(?:\([^)]+\))";
  178. $this->lnge = "(?:\[[^]]+\])";
  179. $this->styl = "(?:\{[^}]+\})";
  180. $this->cspn = "(?:\\\\\d+)";
  181. $this->rspn = "(?:\/\d+)";
  182. $this->a = "(?:{$this->hlgn}|{$this->vlgn})*";
  183. $this->s = "(?:{$this->cspn}|{$this->rspn})*";
  184. $this->c = "(?:{$this->clas}|{$this->styl}|{$this->lnge}|{$this->hlgn})*";
  185. $this->pnct = '[\!"#\$%&\'()\*\+,\-\./:;<=>\?@\[\\\]\^_`{\|}\~]';
  186. $this->urlch = '[\w"$\-_.+!*\'(),";\/?:@=&%#{}|\\^~\[\]`]';
  187. $this->url_schemes = array('http','https','ftp','mailto');
  188. $this->btag = array('bq', 'bc', 'notextile', 'pre', 'h[1-6]', 'fn\d+', 'p');
  189. $this->glyph = array(
  190. 'quote_single_open' => txt_quote_single_open,
  191. 'quote_single_close' => txt_quote_single_close,
  192. 'quote_double_open' => txt_quote_double_open,
  193. 'quote_double_close' => txt_quote_double_close,
  194. 'apostrophe' => txt_apostrophe,
  195. 'prime' => txt_prime,
  196. 'prime_double' => txt_prime_double,
  197. 'ellipsis' => txt_ellipsis,
  198. 'emdash' => txt_emdash,
  199. 'endash' => txt_endash,
  200. 'dimension' => txt_dimension,
  201. 'trademark' => txt_trademark,
  202. 'registered' => txt_registered,
  203. 'copyright' => txt_copyright,
  204. );
  205. if (defined('hu')) {
  206. $this->hu = hu;
  207. }
  208. }
  209. // -------------------------------------------------------------
  210. public function TextileThis($text, $lite='', $encode='', $noimage='', $strict='', $rel='')
  211. {
  212. if ($rel) {
  213. $this->rel = ' rel="'.$rel.'" ';
  214. }
  215. $this->lite = $lite;
  216. $this->noimage = $noimage;
  217. if ($encode) {
  218. $text = $this->incomingEntities($text);
  219. $text = str_replace("x%x%", "&#38;", $text);
  220. return $text;
  221. } else {
  222. if (!$strict) {
  223. $text = $this->cleanWhiteSpace($text);
  224. }
  225. $text = $this->getRefs($text);
  226. if (!$lite) {
  227. $text = $this->block($text);
  228. }
  229. $text = $this->retrieve($text);
  230. // just to be tidy
  231. $text = str_replace("<br />", "<br />\n", $text);
  232. return $text;
  233. }
  234. }
  235. // -------------------------------------------------------------
  236. public function TextileRestricted($text, $lite=1, $noimage=1, $rel='nofollow')
  237. {
  238. $this->restricted = true;
  239. $this->lite = $lite;
  240. $this->noimage = $noimage;
  241. if ($rel) {
  242. $this->rel = ' rel="'.$rel.'" ';
  243. }
  244. // escape any raw html
  245. $text = $this->encode_html($text, 0);
  246. $text = $this->cleanWhiteSpace($text);
  247. $text = $this->getRefs($text);
  248. if ($lite) {
  249. $text = $this->blockLite($text);
  250. } else {
  251. $text = $this->block($text);
  252. }
  253. $text = $this->retrieve($text);
  254. // just to be tidy
  255. $text = str_replace("<br />", "<br />\n", $text);
  256. return $text;
  257. }
  258. // -------------------------------------------------------------
  259. public function pba($in, $element = "") // "parse block attributes"
  260. {
  261. $style = '';
  262. $class = '';
  263. $lang = '';
  264. $colspan = '';
  265. $rowspan = '';
  266. $id = '';
  267. $atts = '';
  268. if (!empty($in)) {
  269. $matched = $in;
  270. if ($element == 'td') {
  271. if (preg_match("/\\\\(\d+)/", $matched, $csp)) {
  272. $colspan = $csp[1];
  273. }
  274. if (preg_match("/\/(\d+)/", $matched, $rsp)) {
  275. $rowspan = $rsp[1];
  276. }
  277. }
  278. if ($element == 'td' or $element == 'tr') {
  279. if (preg_match("/($this->vlgn)/", $matched, $vert)) {
  280. $style[] = "vertical-align:" . $this->vAlign($vert[1]) . ";";
  281. }
  282. }
  283. if (preg_match("/\{([^}]*)\}/", $matched, $sty)) {
  284. $style[] = rtrim($sty[1], ';') . ';';
  285. $matched = str_replace($sty[0], '', $matched);
  286. }
  287. if (preg_match("/\[([^]]+)\]/U", $matched, $lng)) {
  288. $lang = $lng[1];
  289. $matched = str_replace($lng[0], '', $matched);
  290. }
  291. if (preg_match("/\(([^()]+)\)/U", $matched, $cls)) {
  292. $class = $cls[1];
  293. $matched = str_replace($cls[0], '', $matched);
  294. }
  295. if (preg_match("/([(]+)/", $matched, $pl)) {
  296. $style[] = "padding-left:" . strlen($pl[1]) . "em;";
  297. $matched = str_replace($pl[0], '', $matched);
  298. }
  299. if (preg_match("/([)]+)/", $matched, $pr)) {
  300. // $this->dump($pr);
  301. $style[] = "padding-right:" . strlen($pr[1]) . "em;";
  302. $matched = str_replace($pr[0], '', $matched);
  303. }
  304. if (preg_match("/($this->hlgn)/", $matched, $horiz)) {
  305. $style[] = "text-align:" . $this->hAlign($horiz[1]) . ";";
  306. }
  307. if (preg_match("/^(.*)#(.*)$/", $class, $ids)) {
  308. $id = $ids[2];
  309. $class = $ids[1];
  310. }
  311. if ($this->restricted) {
  312. return ($lang) ? ' lang="' . $lang .'"':'';
  313. }
  314. return join('', array(
  315. ($style) ? ' style="' . join("", $style) .'"':'',
  316. ($class) ? ' class="' . $class .'"':'',
  317. ($lang) ? ' lang="' . $lang .'"':'',
  318. ($id) ? ' id="' . $id .'"':'',
  319. ($colspan) ? ' colspan="' . $colspan .'"':'',
  320. ($rowspan) ? ' rowspan="' . $rowspan .'"':''
  321. ));
  322. }
  323. return '';
  324. }
  325. // -------------------------------------------------------------
  326. public function hasRawText($text)
  327. {
  328. // checks whether the text has text not already enclosed by a block tag
  329. $r = trim(preg_replace('@<(p|blockquote|div|form|table|ul|ol|pre|h\d)[^>]*?>.*</\1>@s', '', trim($text)));
  330. $r = trim(preg_replace('@<(hr|br)[^>]*?/>@', '', $r));
  331. return '' != $r;
  332. }
  333. // -------------------------------------------------------------
  334. public function table($text)
  335. {
  336. $text = $text . "\n\n";
  337. return preg_replace_callback("/^(?:table(_?{$this->s}{$this->a}{$this->c})\. ?\n)?^({$this->a}{$this->c}\.? ?\|.*\|)\n\n/smU",
  338. array(&$this, "fTable"), $text);
  339. }
  340. // -------------------------------------------------------------
  341. public function fTable($matches)
  342. {
  343. $tatts = $this->pba($matches[1], 'table');
  344. foreach (preg_split("/\|$/m", $matches[2], -1, PREG_SPLIT_NO_EMPTY) as $row) {
  345. if (preg_match("/^($this->a$this->c\. )(.*)/m", ltrim($row), $rmtch)) {
  346. $ratts = $this->pba($rmtch[1], 'tr');
  347. $row = $rmtch[2];
  348. } else {
  349. $ratts = '';
  350. }
  351. $cells = array();
  352. foreach (explode("|", $row) as $cell) {
  353. $ctyp = "d";
  354. if (preg_match("/^_/", $cell)) {
  355. $ctyp = "h";
  356. }
  357. if (preg_match("/^(_?$this->s$this->a$this->c\. )(.*)/", $cell, $cmtch)) {
  358. $catts = $this->pba($cmtch[1], 'td');
  359. $cell = $cmtch[2];
  360. } else {
  361. $catts = '';
  362. }
  363. $cell = $this->graf($this->span($cell));
  364. if (trim($cell) != '') {
  365. $cells[] = "\t\t\t<t$ctyp$catts>$cell</t$ctyp>";
  366. }
  367. }
  368. $rows[] = "\t\t<tr$ratts>\n" . join("\n", $cells) . ($cells ? "\n" : "") . "\t\t</tr>";
  369. unset($cells, $catts);
  370. }
  371. return "\t<table$tatts>\n" . join("\n", $rows) . "\n\t</table>\n\n";
  372. }
  373. // -------------------------------------------------------------
  374. public function lists($text)
  375. {
  376. return preg_replace_callback("/^([#*]+$this->c .*)$(?![^#*])/smU", array(&$this, "fList"), $text);
  377. }
  378. // -------------------------------------------------------------
  379. public function fList($m)
  380. {
  381. $text = explode("\n", $m[0]);
  382. foreach ($text as $line) {
  383. $nextline = next($text);
  384. if (preg_match("/^([#*]+)($this->a$this->c) (.*)$/s", $line, $m)) {
  385. list(, $tl, $atts, $content) = $m;
  386. $nl = '';
  387. if (preg_match("/^([#*]+)\s.*/", $nextline, $nm)) {
  388. $nl = $nm[1];
  389. }
  390. if (!isset($lists[$tl])) {
  391. $lists[$tl] = true;
  392. $atts = $this->pba($atts);
  393. $line = "\t<" . $this->lT($tl) . "l$atts>\n\t\t<li>" . $this->graf($content);
  394. } else {
  395. $line = "\t\t<li>" . $this->graf($content);
  396. }
  397. if (strlen($nl) <= strlen($tl)) {
  398. $line .= "</li>";
  399. }
  400. foreach (array_reverse($lists) as $k => $v) {
  401. if (strlen($k) > strlen($nl)) {
  402. $line .= "\n\t</" . $this->lT($k) . "l>";
  403. if (strlen($k) > 1) {
  404. $line .= "</li>";
  405. }
  406. unset($lists[$k]);
  407. }
  408. }
  409. }
  410. $out[] = $line;
  411. }
  412. return join("\n", $out);
  413. }
  414. // -------------------------------------------------------------
  415. public function lT($in)
  416. {
  417. return preg_match("/^#+/", $in) ? 'o' : 'u';
  418. }
  419. // -------------------------------------------------------------
  420. public function doPBr($in)
  421. {
  422. return preg_replace_callback('@<(p)([^>]*?)>(.*)(</\1>)@s', array(&$this, 'doBr'), $in);
  423. }
  424. // -------------------------------------------------------------
  425. public function doBr($m)
  426. {
  427. $content = preg_replace("@(.+)(?<!<br>|<br />)\n(?![#*\s|])@", '$1<br />', $m[3]);
  428. return '<'.$m[1].$m[2].'>'.$content.$m[4];
  429. }
  430. // -------------------------------------------------------------
  431. public function block($text)
  432. {
  433. $find = $this->btag;
  434. $tre = join('|', $find);
  435. $text = explode("\n\n", $text);
  436. $tag = 'p';
  437. $atts = $cite = $graf = $ext = '';
  438. foreach ($text as $line) {
  439. $anon = 0;
  440. if (preg_match("/^($tre)($this->a$this->c)\.(\.?)(?::(\S+))? (.*)$/s", $line, $m)) {
  441. // last block was extended, so close it
  442. if ($ext) {
  443. $out[count($out)-1] .= $c1;
  444. }
  445. // new block
  446. list(, $tag, $atts, $ext, $cite, $graf) = $m;
  447. list($o1, $o2, $content, $c2, $c1) = $this->fBlock(array(0,$tag,$atts,$ext,$cite,$graf));
  448. // leave off c1 if this block is extended, we'll close it at the start of the next block
  449. if ($ext) {
  450. $line = $o1.$o2.$content.$c2;
  451. } else {
  452. $line = $o1.$o2.$content.$c2.$c1;
  453. }
  454. } else {
  455. // anonymous block
  456. $anon = 1;
  457. if ($ext or !preg_match('/^ /', $line)) {
  458. list($o1, $o2, $content, $c2, $c1) = $this->fBlock(array(0,$tag,$atts,$ext,$cite,$line));
  459. // skip $o1/$c1 because this is part of a continuing extended block
  460. if ($tag == 'p' and !$this->hasRawText($content)) {
  461. $line = $content;
  462. } else {
  463. $line = $o2.$content.$c2;
  464. }
  465. } else {
  466. $line = $this->graf($line);
  467. }
  468. }
  469. $line = $this->doPBr($line);
  470. $line = preg_replace('/<br>/', '<br />', $line);
  471. if ($ext and $anon) {
  472. $out[count($out)-1] .= "\n".$line;
  473. } else {
  474. $out[] = $line;
  475. }
  476. if (!$ext) {
  477. $tag = 'p';
  478. $atts = '';
  479. $cite = '';
  480. $graf = '';
  481. }
  482. }
  483. if ($ext) {
  484. $out[count($out)-1] .= $c1;
  485. }
  486. return join("\n\n", $out);
  487. }
  488. // -------------------------------------------------------------
  489. public function fBlock($m)
  490. {
  491. // $this->dump($m);
  492. list(, $tag, $atts, $ext, $cite, $content) = $m;
  493. $atts = $this->pba($atts);
  494. $o1 = $o2 = $c2 = $c1 = '';
  495. if (preg_match("/fn(\d+)/", $tag, $fns)) {
  496. $tag = 'p';
  497. $fnid = empty($this->fn[$fns[1]]) ? $fns[1] : $this->fn[$fns[1]];
  498. $atts .= ' id="fn' . $fnid . '"';
  499. if (strpos($atts, 'class=') === false) {
  500. $atts .= ' class="footnote"';
  501. }
  502. $content = '<sup>' . $fns[1] . '</sup> ' . $content;
  503. }
  504. if ($tag == "bq") {
  505. $cite = $this->checkRefs($cite);
  506. $cite = ($cite != '') ? ' cite="' . $cite . '"' : '';
  507. $o1 = "\t<blockquote$cite$atts>\n";
  508. $o2 = "\t\t<p$atts>";
  509. $c2 = "</p>";
  510. $c1 = "\n\t</blockquote>";
  511. } elseif ($tag == 'bc') {
  512. $o1 = "<pre$atts>";
  513. $o2 = "<code$atts>";
  514. $c2 = "</code>";
  515. $c1 = "</pre>";
  516. $content = $this->shelve($this->encode_html(rtrim($content, "\n")."\n"));
  517. } elseif ($tag == 'notextile') {
  518. $content = $this->shelve($content);
  519. $o1 = $o2 = '';
  520. $c1 = $c2 = '';
  521. } elseif ($tag == 'pre') {
  522. $content = $this->shelve($this->encode_html(rtrim($content, "\n")."\n"));
  523. $o1 = "<pre$atts>";
  524. $o2 = $c2 = '';
  525. $c1 = "</pre>";
  526. } else {
  527. $o2 = "\t<$tag$atts>";
  528. $c2 = "</$tag>";
  529. }
  530. $content = $this->graf($content);
  531. return array($o1, $o2, $content, $c2, $c1);
  532. }
  533. // -------------------------------------------------------------
  534. public function graf($text)
  535. {
  536. // handle normal paragraph text
  537. if (!$this->lite) {
  538. $text = $this->noTextile($text);
  539. $text = $this->code($text);
  540. }
  541. $text = $this->links($text);
  542. if (!$this->noimage) {
  543. $text = $this->image($text);
  544. }
  545. if (!$this->lite) {
  546. $text = $this->lists($text);
  547. $text = $this->table($text);
  548. }
  549. $text = $this->span($text);
  550. $text = $this->footnoteRef($text);
  551. $text = $this->glyphs($text);
  552. return rtrim($text, "\n");
  553. }
  554. // -------------------------------------------------------------
  555. public function span($text)
  556. {
  557. $qtags = array('\*\*','\*','\?\?','-','__','_','%','\+','~','\^');
  558. $pnct = ".,\"'?!;:";
  559. foreach ($qtags as $f) {
  560. $text = preg_replace_callback("/
  561. (?:^|(?<=[\s>$pnct])|([{[]))
  562. ($f)(?!$f)
  563. ({$this->c})
  564. (?::(\S+))?
  565. ([^\s$f]+|\S[^$f\n]*[^\s$f\n])
  566. ([$pnct]*)
  567. $f
  568. (?:$|([\]}])|(?=[[:punct:]]{1,2}|\s))
  569. /x", array(&$this, "fSpan"), $text);
  570. }
  571. return $text;
  572. }
  573. // -------------------------------------------------------------
  574. public function fSpan($m)
  575. {
  576. $qtags = array(
  577. '*' => 'strong',
  578. '**' => 'b',
  579. '??' => 'cite',
  580. '_' => 'em',
  581. '__' => 'i',
  582. '-' => 'del',
  583. '%' => 'span',
  584. '+' => 'ins',
  585. '~' => 'sub',
  586. '^' => 'sup',
  587. );
  588. list(, , $tag, $atts, $cite, $content, $end) = $m;
  589. $tag = $qtags[$tag];
  590. $atts = $this->pba($atts);
  591. $atts .= ($cite != '') ? 'cite="' . $cite . '"' : '';
  592. $out = "<$tag$atts>$content$end</$tag>";
  593. // $this->dump($out);
  594. return $out;
  595. }
  596. // -------------------------------------------------------------
  597. public function links($text)
  598. {
  599. return preg_replace_callback('/
  600. (?:^|(?<=[\s>.$pnct\(])|([{[])) # $pre
  601. " # start
  602. (' . $this->c . ') # $atts
  603. ([^"]+) # $text
  604. \s?
  605. (?:\(([^)]+)\)(?="))? # $title
  606. ":
  607. ('.$this->urlch.'+) # $url
  608. (\/)? # $slash
  609. ([^\w\/;]*) # $post
  610. (?:([\]}])|(?=\s|$|\)))
  611. /Ux', array(&$this, "fLink"), $text);
  612. }
  613. // -------------------------------------------------------------
  614. public function fLink($m)
  615. {
  616. list(, $pre, $atts, $text, $title, $url, $slash, $post) = $m;
  617. $url = $this->checkRefs($url);
  618. $atts = $this->pba($atts);
  619. $atts .= ($title != '') ? ' title="' . $this->encode_html($title) . '"' : '';
  620. if (!$this->noimage) {
  621. $text = $this->image($text);
  622. }
  623. $text = $this->span($text);
  624. $text = $this->glyphs($text);
  625. $url = $this->relURL($url);
  626. $out = '<a href="' . $this->encode_html($url . $slash) . '"' . $atts . $this->rel . '>' . $text . '</a>' . $post;
  627. // $this->dump($out);
  628. return $this->shelve($out);
  629. }
  630. // -------------------------------------------------------------
  631. public function getRefs($text)
  632. {
  633. return preg_replace_callback("/(?<=^|\s)\[(.+)\]((?:http:\/\/|\/)\S+)(?=\s|$)/U",
  634. array(&$this, "refs"), $text);
  635. }
  636. // -------------------------------------------------------------
  637. public function refs($m)
  638. {
  639. list(, $flag, $url) = $m;
  640. $this->urlrefs[$flag] = $url;
  641. return '';
  642. }
  643. // -------------------------------------------------------------
  644. public function checkRefs($text)
  645. {
  646. return (isset($this->urlrefs[$text])) ? $this->urlrefs[$text] : $text;
  647. }
  648. // -------------------------------------------------------------
  649. public function relURL($url)
  650. {
  651. $parts = parse_url($url);
  652. if ((empty($parts['scheme']) or @$parts['scheme'] == 'http') and
  653. empty($parts['host']) and
  654. preg_match('/^\w/', @$parts['path'])) {
  655. $url = $this->hu.$url;
  656. }
  657. if ($this->restricted and !empty($parts['scheme']) and
  658. !in_array($parts['scheme'], $this->url_schemes)) {
  659. return '#';
  660. }
  661. return $url;
  662. }
  663. // -------------------------------------------------------------
  664. public function image($text)
  665. {
  666. return preg_replace_callback("/
  667. (?:[[{])? # pre
  668. \! # opening !
  669. (\<|\=|\>)?? # optional alignment atts
  670. ($this->c) # optional style,class atts
  671. (?:\. )? # optional dot-space
  672. ([^\s(!]+) # presume this is the src
  673. \s? # optional space
  674. (?:\(([^\)]+)\))? # optional title
  675. \! # closing
  676. (?::(\S+))? # optional href
  677. (?:[\]}]|(?=\s|$)) # lookahead: space or end of string
  678. /Ux", array(&$this, "fImage"), $text);
  679. }
  680. // -------------------------------------------------------------
  681. public function fImage($m)
  682. {
  683. list(, $algn, $atts, $url) = $m;
  684. $atts = $this->pba($atts);
  685. $atts .= ($algn != '') ? ' align="' . $this->iAlign($algn) . '"' : '';
  686. $atts .= (isset($m[4])) ? ' title="' . $m[4] . '"' : '';
  687. $atts .= (isset($m[4])) ? ' alt="' . $m[4] . '"' : ' alt=""';
  688. $size = @getimagesize($url);
  689. if ($size) {
  690. $atts .= " $size[3]";
  691. }
  692. $href = (isset($m[5])) ? $this->checkRefs($m[5]) : '';
  693. $url = $this->checkRefs($url);
  694. $url = $this->relURL($url);
  695. $out = array(
  696. ($href) ? '<a href="' . $href . '">' : '',
  697. '<img src="' . $url . '"' . $atts . ' />',
  698. ($href) ? '</a>' : ''
  699. );
  700. return join('', $out);
  701. }
  702. // -------------------------------------------------------------
  703. public function code($text)
  704. {
  705. $text = $this->doSpecial($text, '<code>', '</code>', 'fCode');
  706. $text = $this->doSpecial($text, '@', '@', 'fCode');
  707. $text = $this->doSpecial($text, '<pre>', '</pre>', 'fPre');
  708. return $text;
  709. }
  710. // -------------------------------------------------------------
  711. public function fCode($m)
  712. {
  713. @list(, $before, $text, $after) = $m;
  714. if ($this->restricted) {
  715. // $text is already escaped
  716. return $before.$this->shelve('<code>'.$text.'</code>').$after;
  717. } else {
  718. return $before.$this->shelve('<code>'.$this->encode_html($text).'</code>').$after;
  719. }
  720. }
  721. // -------------------------------------------------------------
  722. public function fPre($m)
  723. {
  724. @list(, $before, $text, $after) = $m;
  725. if ($this->restricted) {
  726. // $text is already escaped
  727. return $before.'<pre>'.$this->shelve($text).'</pre>'.$after;
  728. } else {
  729. return $before.'<pre>'.$this->shelve($this->encode_html($text)).'</pre>'.$after;
  730. }
  731. }
  732. // -------------------------------------------------------------
  733. public function shelve($val)
  734. {
  735. $i = uniqid(rand());
  736. $this->shelf[$i] = $val;
  737. return $i;
  738. }
  739. // -------------------------------------------------------------
  740. public function retrieve($text)
  741. {
  742. if (is_array($this->shelf)) {
  743. do {
  744. $old = $text;
  745. $text = strtr($text, $this->shelf);
  746. } while ($text != $old);
  747. }
  748. return $text;
  749. }
  750. // -------------------------------------------------------------
  751. // NOTE: deprecated
  752. public function incomingEntities($text)
  753. {
  754. return preg_replace("/&(?![#a-z0-9]+;)/i", "x%x%", $text);
  755. }
  756. // -------------------------------------------------------------
  757. // NOTE: deprecated
  758. public function encodeEntities($text)
  759. {
  760. return (function_exists('mb_encode_numericentity'))
  761. ? $this->encode_high($text)
  762. : htmlentities($text, ENT_NOQUOTES, "utf-8");
  763. }
  764. // -------------------------------------------------------------
  765. // NOTE: deprecated
  766. public function fixEntities($text)
  767. {
  768. /* de-entify any remaining angle brackets or ampersands */
  769. return str_replace(array("&gt;", "&lt;", "&amp;"),
  770. array(">", "<", "&"), $text);
  771. }
  772. // -------------------------------------------------------------
  773. public function cleanWhiteSpace($text)
  774. {
  775. $out = str_replace("\r\n", "\n", $text);
  776. $out = preg_replace("/\n{3,}/", "\n\n", $out);
  777. $out = preg_replace("/\n *\n/", "\n\n", $out);
  778. $out = preg_replace('/"$/', "\" ", $out);
  779. return $out;
  780. }
  781. // -------------------------------------------------------------
  782. public function doSpecial($text, $start, $end, $method='fSpecial')
  783. {
  784. return preg_replace_callback('/(^|\s|[[({>])'.preg_quote($start, '/').'(.*?)'.preg_quote($end, '/').'(\s|$|[\])}])?/ms',
  785. array(&$this, $method), $text);
  786. }
  787. // -------------------------------------------------------------
  788. public function fSpecial($m)
  789. {
  790. // A special block like notextile or code
  791. @list(, $before, $text, $after) = $m;
  792. return $before.$this->shelve($this->encode_html($text)).$after;
  793. }
  794. // -------------------------------------------------------------
  795. public function noTextile($text)
  796. {
  797. $text = $this->doSpecial($text, '<notextile>', '</notextile>', 'fTextile');
  798. return $this->doSpecial($text, '==', '==', 'fTextile');
  799. }
  800. // -------------------------------------------------------------
  801. public function fTextile($m)
  802. {
  803. @list(, $before, $notextile, $after) = $m;
  804. #$notextile = str_replace(array_keys($modifiers), array_values($modifiers), $notextile);
  805. return $before.$this->shelve($notextile).$after;
  806. }
  807. // -------------------------------------------------------------
  808. public function footnoteRef($text)
  809. {
  810. return preg_replace('/\b\[([0-9]+)\](\s)?/Ue',
  811. '$this->footnoteID(\'\1\',\'\2\')', $text);
  812. }
  813. // -------------------------------------------------------------
  814. public function footnoteID($id, $t)
  815. {
  816. if (empty($this->fn[$id])) {
  817. $this->fn[$id] = uniqid(rand());
  818. }
  819. $fnid = $this->fn[$id];
  820. return '<sup class="footnote"><a href="#fn'.$fnid.'">'.$id.'</a></sup>'.$t;
  821. }
  822. // -------------------------------------------------------------
  823. public function glyphs($text)
  824. {
  825. // fix: hackish
  826. $text = preg_replace('/"\z/', "\" ", $text);
  827. $pnc = '[[:punct:]]';
  828. $glyph_search = array(
  829. '/(\w)\'(\w)/', // apostrophe's
  830. '/(\s)\'(\d+\w?)\b(?!\')/', // back in '88
  831. '/(\S)\'(?=\s|'.$pnc.'|<|$)/', // single closing
  832. '/\'/', // single opening
  833. '/(\S)\"(?=\s|'.$pnc.'|<|$)/', // double closing
  834. '/"/', // double opening
  835. '/\b([A-Z][A-Z0-9]{2,})\b(?:[(]([^)]*)[)])/', // 3+ uppercase acronym
  836. '/\b([A-Z][A-Z\'\-]+[A-Z])(?=[\s.,\)>])/', // 3+ uppercase
  837. '/\b( )?\.{3}/', // ellipsis
  838. '/(\s?)--(\s?)/', // em dash
  839. '/\s-(?:\s|$)/', // en dash
  840. '/(\d+)( ?)x( ?)(?=\d+)/', // dimension sign
  841. '/\b ?[([]TM[])]/i', // trademark
  842. '/\b ?[([]R[])]/i', // registered
  843. '/\b ?[([]C[])]/i', // copyright
  844. );
  845. extract($this->glyph, EXTR_PREFIX_ALL, 'txt');
  846. $glyph_replace = array(
  847. '$1'.$txt_apostrophe.'$2', // apostrophe's
  848. '$1'.$txt_apostrophe.'$2', // back in '88
  849. '$1'.$txt_quote_single_close, // single closing
  850. $txt_quote_single_open, // single opening
  851. '$1'.$txt_quote_double_close, // double closing
  852. $txt_quote_double_open, // double opening
  853. '<acronym title="$2">$1</acronym>', // 3+ uppercase acronym
  854. '<span class="caps">$1</span>', // 3+ uppercase
  855. '$1'.$txt_ellipsis, // ellipsis
  856. '$1'.$txt_emdash.'$2', // em dash
  857. ' '.$txt_endash.' ', // en dash
  858. '$1$2'.$txt_dimension.'$3', // dimension sign
  859. $txt_trademark, // trademark
  860. $txt_registered, // registered
  861. $txt_copyright, // copyright
  862. );
  863. $text = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  864. foreach ($text as $line) {
  865. if (!preg_match("/<.*>/", $line)) {
  866. $line = preg_replace($glyph_search, $glyph_replace, $line);
  867. }
  868. $glyph_out[] = $line;
  869. }
  870. return join('', $glyph_out);
  871. }
  872. // -------------------------------------------------------------
  873. public function iAlign($in)
  874. {
  875. $vals = array(
  876. '<' => 'left',
  877. '=' => 'center',
  878. '>' => 'right');
  879. return (isset($vals[$in])) ? $vals[$in] : '';
  880. }
  881. // -------------------------------------------------------------
  882. public function hAlign($in)
  883. {
  884. $vals = array(
  885. '<' => 'left',
  886. '=' => 'center',
  887. '>' => 'right',
  888. '<>' => 'justify');
  889. return (isset($vals[$in])) ? $vals[$in] : '';
  890. }
  891. // -------------------------------------------------------------
  892. public function vAlign($in)
  893. {
  894. $vals = array(
  895. '^' => 'top',
  896. '-' => 'middle',
  897. '~' => 'bottom');
  898. return (isset($vals[$in])) ? $vals[$in] : '';
  899. }
  900. // -------------------------------------------------------------
  901. // NOTE: deprecated
  902. public function encode_high($text, $charset = "UTF-8")
  903. {
  904. return mb_encode_numericentity($text, $this->cmap(), $charset);
  905. }
  906. // -------------------------------------------------------------
  907. // NOTE: deprecated
  908. public function decode_high($text, $charset = "UTF-8")
  909. {
  910. return mb_decode_numericentity($text, $this->cmap(), $charset);
  911. }
  912. // -------------------------------------------------------------
  913. // NOTE: deprecated
  914. public function cmap()
  915. {
  916. $f = 0xffff;
  917. $cmap = array(
  918. 0x0080, 0xffff, 0, $f);
  919. return $cmap;
  920. }
  921. // -------------------------------------------------------------
  922. public function encode_html($str, $quotes=1)
  923. {
  924. $a = array(
  925. '&' => '&#38;',
  926. '<' => '&#60;',
  927. '>' => '&#62;',
  928. );
  929. if ($quotes) {
  930. $a = $a + array(
  931. "'" => '&#39;',
  932. '"' => '&#34;',
  933. );
  934. }
  935. return strtr($str, $a);
  936. }
  937. // -------------------------------------------------------------
  938. public function textile_popup_help($name, $helpvar, $windowW, $windowH)
  939. {
  940. return ' <a target="_blank" href="http://www.textpattern.com/help/?item=' . $helpvar . '" onclick="window.open(this.href, \'popupwindow\', \'width=' . $windowW . ',height=' . $windowH . ',scrollbars,resizable\'); return false;">' . $name . '</a><br />';
  941. return $out;
  942. }
  943. // -------------------------------------------------------------
  944. // NOTE: deprecated
  945. public function txtgps($thing)
  946. {
  947. if (isset($_POST[$thing])) {
  948. if (get_magic_quotes_gpc()) {
  949. return stripslashes($_POST[$thing]);
  950. } else {
  951. return $_POST[$thing];
  952. }
  953. } else {
  954. return '';
  955. }
  956. }
  957. // -------------------------------------------------------------
  958. // NOTE: deprecated
  959. public function dump()
  960. {
  961. foreach (func_get_args() as $a) {
  962. echo "\n<pre>",(is_array($a)) ? print_r($a) : $a, "</pre>\n";
  963. }
  964. }
  965. // -------------------------------------------------------------
  966. public function blockLite($text)
  967. {
  968. $this->btag = array('bq', 'p');
  969. return $this->block($text."\n\n");
  970. }
  971. } // end class