Advertisement
Guest User

Untitled

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