Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1693 lines
41 KiB

5 years ago
  1. <?php
  2. #
  3. #
  4. # Parsedown
  5. # http://parsedown.org
  6. #
  7. # (c) Emanuil Rusev
  8. # http://erusev.com
  9. #
  10. # For the full license information, view the LICENSE file that was distributed
  11. # with this source code.
  12. #
  13. #
  14. class Parsedown
  15. {
  16. # ~
  17. const version = '1.7.3';
  18. # ~
  19. function text($text)
  20. {
  21. # make sure no definitions are set
  22. $this->DefinitionData = array();
  23. # standardize line breaks
  24. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  25. # remove surrounding line breaks
  26. $text = trim($text, "\n");
  27. # split text into lines
  28. $lines = explode("\n", $text);
  29. # iterate through lines to identify blocks
  30. $markup = $this->lines($lines);
  31. # trim line breaks
  32. $markup = trim($markup, "\n");
  33. return $markup;
  34. }
  35. #
  36. # Setters
  37. #
  38. function setBreaksEnabled($breaksEnabled)
  39. {
  40. $this->breaksEnabled = $breaksEnabled;
  41. return $this;
  42. }
  43. protected $breaksEnabled;
  44. function setMarkupEscaped($markupEscaped)
  45. {
  46. $this->markupEscaped = $markupEscaped;
  47. return $this;
  48. }
  49. protected $markupEscaped;
  50. function setUrlsLinked($urlsLinked)
  51. {
  52. $this->urlsLinked = $urlsLinked;
  53. return $this;
  54. }
  55. protected $urlsLinked = true;
  56. function setSafeMode($safeMode)
  57. {
  58. $this->safeMode = (bool) $safeMode;
  59. return $this;
  60. }
  61. protected $safeMode;
  62. protected $safeLinksWhitelist = array(
  63. 'http://',
  64. 'https://',
  65. 'ftp://',
  66. 'ftps://',
  67. 'mailto:',
  68. 'data:image/png;base64,',
  69. 'data:image/gif;base64,',
  70. 'data:image/jpeg;base64,',
  71. 'irc:',
  72. 'ircs:',
  73. 'git:',
  74. 'ssh:',
  75. 'news:',
  76. 'steam:',
  77. );
  78. #
  79. # Lines
  80. #
  81. protected $BlockTypes = array(
  82. '#' => array('Header'),
  83. '*' => array('Rule', 'List'),
  84. '+' => array('List'),
  85. '-' => array('SetextHeader', 'Table', 'Rule', 'List'),
  86. '0' => array('List'),
  87. '1' => array('List'),
  88. '2' => array('List'),
  89. '3' => array('List'),
  90. '4' => array('List'),
  91. '5' => array('List'),
  92. '6' => array('List'),
  93. '7' => array('List'),
  94. '8' => array('List'),
  95. '9' => array('List'),
  96. ':' => array('Table'),
  97. '<' => array('Comment', 'Markup'),
  98. '=' => array('SetextHeader'),
  99. '>' => array('Quote'),
  100. '[' => array('Reference'),
  101. '_' => array('Rule'),
  102. '`' => array('FencedCode'),
  103. '|' => array('Table'),
  104. '~' => array('FencedCode'),
  105. );
  106. # ~
  107. protected $unmarkedBlockTypes = array(
  108. 'Code',
  109. );
  110. #
  111. # Blocks
  112. #
  113. protected function lines(array $lines)
  114. {
  115. $CurrentBlock = null;
  116. foreach ($lines as $line)
  117. {
  118. if (chop($line) === '')
  119. {
  120. if (isset($CurrentBlock))
  121. {
  122. $CurrentBlock['interrupted'] = true;
  123. }
  124. continue;
  125. }
  126. if (strpos($line, "\t") !== false)
  127. {
  128. $parts = explode("\t", $line);
  129. $line = $parts[0];
  130. unset($parts[0]);
  131. foreach ($parts as $part)
  132. {
  133. $shortage = 4 - mb_strlen($line, 'utf-8') % 4;
  134. $line .= str_repeat(' ', $shortage);
  135. $line .= $part;
  136. }
  137. }
  138. $indent = 0;
  139. while (isset($line[$indent]) and $line[$indent] === ' ')
  140. {
  141. $indent ++;
  142. }
  143. $text = $indent > 0 ? substr($line, $indent) : $line;
  144. # ~
  145. $Line = array('body' => $line, 'indent' => $indent, 'text' => $text);
  146. # ~
  147. if (isset($CurrentBlock['continuable']))
  148. {
  149. $Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock);
  150. if (isset($Block))
  151. {
  152. $CurrentBlock = $Block;
  153. continue;
  154. }
  155. else
  156. {
  157. if ($this->isBlockCompletable($CurrentBlock['type']))
  158. {
  159. $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
  160. }
  161. }
  162. }
  163. # ~
  164. $marker = $text[0];
  165. # ~
  166. $blockTypes = $this->unmarkedBlockTypes;
  167. if (isset($this->BlockTypes[$marker]))
  168. {
  169. foreach ($this->BlockTypes[$marker] as $blockType)
  170. {
  171. $blockTypes []= $blockType;
  172. }
  173. }
  174. #
  175. # ~
  176. foreach ($blockTypes as $blockType)
  177. {
  178. $Block = $this->{'block'.$blockType}($Line, $CurrentBlock);
  179. if (isset($Block))
  180. {
  181. $Block['type'] = $blockType;
  182. if ( ! isset($Block['identified']))
  183. {
  184. $Blocks []= $CurrentBlock;
  185. $Block['identified'] = true;
  186. }
  187. if ($this->isBlockContinuable($blockType))
  188. {
  189. $Block['continuable'] = true;
  190. }
  191. $CurrentBlock = $Block;
  192. continue 2;
  193. }
  194. }
  195. # ~
  196. if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted']))
  197. {
  198. $CurrentBlock['element']['text'] .= "\n".$text;
  199. }
  200. else
  201. {
  202. $Blocks []= $CurrentBlock;
  203. $CurrentBlock = $this->paragraph($Line);
  204. $CurrentBlock['identified'] = true;
  205. }
  206. }
  207. # ~
  208. if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type']))
  209. {
  210. $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
  211. }
  212. # ~
  213. $Blocks []= $CurrentBlock;
  214. unset($Blocks[0]);
  215. # ~
  216. $markup = '';
  217. foreach ($Blocks as $Block)
  218. {
  219. if (isset($Block['hidden']))
  220. {
  221. continue;
  222. }
  223. $markup .= "\n";
  224. $markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']);
  225. }
  226. $markup .= "\n";
  227. # ~
  228. return $markup;
  229. }
  230. protected function isBlockContinuable($Type)
  231. {
  232. return method_exists($this, 'block'.$Type.'Continue');
  233. }
  234. protected function isBlockCompletable($Type)
  235. {
  236. return method_exists($this, 'block'.$Type.'Complete');
  237. }
  238. #
  239. # Code
  240. protected function blockCode($Line, $Block = null)
  241. {
  242. if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted']))
  243. {
  244. return;
  245. }
  246. if ($Line['indent'] >= 4)
  247. {
  248. $text = substr($Line['body'], 4);
  249. $Block = array(
  250. 'element' => array(
  251. 'name' => 'pre',
  252. 'handler' => 'element',
  253. 'text' => array(
  254. 'name' => 'code',
  255. 'text' => $text,
  256. ),
  257. ),
  258. );
  259. return $Block;
  260. }
  261. }
  262. protected function blockCodeContinue($Line, $Block)
  263. {
  264. if ($Line['indent'] >= 4)
  265. {
  266. if (isset($Block['interrupted']))
  267. {
  268. $Block['element']['text']['text'] .= "\n";
  269. unset($Block['interrupted']);
  270. }
  271. $Block['element']['text']['text'] .= "\n";
  272. $text = substr($Line['body'], 4);
  273. $Block['element']['text']['text'] .= $text;
  274. return $Block;
  275. }
  276. }
  277. protected function blockCodeComplete($Block)
  278. {
  279. $text = $Block['element']['text']['text'];
  280. $Block['element']['text']['text'] = $text;
  281. return $Block;
  282. }
  283. #
  284. # Comment
  285. protected function blockComment($Line)
  286. {
  287. if ($this->markupEscaped or $this->safeMode)
  288. {
  289. return;
  290. }
  291. if (isset($Line['text'][3]) and $Line['text'][3] === '-' and $Line['text'][2] === '-' and $Line['text'][1] === '!')
  292. {
  293. $Block = array(
  294. 'markup' => $Line['body'],
  295. );
  296. if (preg_match('/-->$/', $Line['text']))
  297. {
  298. $Block['closed'] = true;
  299. }
  300. return $Block;
  301. }
  302. }
  303. protected function blockCommentContinue($Line, array $Block)
  304. {
  305. if (isset($Block['closed']))
  306. {
  307. return;
  308. }
  309. $Block['markup'] .= "\n" . $Line['body'];
  310. if (preg_match('/-->$/', $Line['text']))
  311. {
  312. $Block['closed'] = true;
  313. }
  314. return $Block;
  315. }
  316. #
  317. # Fenced Code
  318. protected function blockFencedCode($Line)
  319. {
  320. if (preg_match('/^['.$Line['text'][0].']{3,}[ ]*([^`]+)?[ ]*$/', $Line['text'], $matches))
  321. {
  322. $Element = array(
  323. 'name' => 'code',
  324. 'text' => '',
  325. );
  326. if (isset($matches[1]))
  327. {
  328. /**
  329. * https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#classes
  330. * Every HTML element may have a class attribute specified.
  331. * The attribute, if specified, must have a value that is a set
  332. * of space-separated tokens representing the various classes
  333. * that the element belongs to.
  334. * [...]
  335. * The space characters, for the purposes of this specification,
  336. * are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab),
  337. * U+000A LINE FEED (LF), U+000C FORM FEED (FF), and
  338. * U+000D CARRIAGE RETURN (CR).
  339. */
  340. $language = substr($matches[1], 0, strcspn($matches[1], " \t\n\f\r"));
  341. $class = 'language-'.$language;
  342. $Element['attributes'] = array(
  343. 'class' => $class,
  344. );
  345. }
  346. $Block = array(
  347. 'char' => $Line['text'][0],
  348. 'element' => array(
  349. 'name' => 'pre',
  350. 'handler' => 'element',
  351. 'text' => $Element,
  352. ),
  353. );
  354. return $Block;
  355. }
  356. }
  357. protected function blockFencedCodeContinue($Line, $Block)
  358. {
  359. if (isset($Block['complete']))
  360. {
  361. return;
  362. }
  363. if (isset($Block['interrupted']))
  364. {
  365. $Block['element']['text']['text'] .= "\n";
  366. unset($Block['interrupted']);
  367. }
  368. if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
  369. {
  370. $Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1);
  371. $Block['complete'] = true;
  372. return $Block;
  373. }
  374. $Block['element']['text']['text'] .= "\n".$Line['body'];
  375. return $Block;
  376. }
  377. protected function blockFencedCodeComplete($Block)
  378. {
  379. $text = $Block['element']['text']['text'];
  380. $Block['element']['text']['text'] = $text;
  381. return $Block;
  382. }
  383. #
  384. # Header
  385. protected function blockHeader($Line)
  386. {
  387. if (isset($Line['text'][1]))
  388. {
  389. $level = 1;
  390. while (isset($Line['text'][$level]) and $Line['text'][$level] === '#')
  391. {
  392. $level ++;
  393. }
  394. if ($level > 6)
  395. {
  396. return;
  397. }
  398. $text = trim($Line['text'], '# ');
  399. $Block = array(
  400. 'element' => array(
  401. 'name' => 'h' . min(6, $level),
  402. 'text' => $text,
  403. 'handler' => 'line',
  404. ),
  405. );
  406. return $Block;
  407. }
  408. }
  409. #
  410. # List
  411. protected function blockList($Line)
  412. {
  413. list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
  414. if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
  415. {
  416. $Block = array(
  417. 'indent' => $Line['indent'],
  418. 'pattern' => $pattern,
  419. 'element' => array(
  420. 'name' => $name,
  421. 'handler' => 'elements',
  422. ),
  423. );
  424. if($name === 'ol')
  425. {
  426. $listStart = stristr($matches[0], '.', true);
  427. if($listStart !== '1')
  428. {
  429. $Block['element']['attributes'] = array('start' => $listStart);
  430. }
  431. }
  432. $Block['li'] = array(
  433. 'name' => 'li',
  434. 'handler' => 'li',
  435. 'text' => array(
  436. $matches[2],
  437. ),
  438. );
  439. $Block['element']['text'] []= & $Block['li'];
  440. return $Block;
  441. }
  442. }
  443. protected function blockListContinue($Line, array $Block)
  444. {
  445. if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'(?:[ ]+(.*)|$)/', $Line['text'], $matches))
  446. {
  447. if (isset($Block['interrupted']))
  448. {
  449. $Block['li']['text'] []= '';
  450. $Block['loose'] = true;
  451. unset($Block['interrupted']);
  452. }
  453. unset($Block['li']);
  454. $text = isset($matches[1]) ? $matches[1] : '';
  455. $Block['li'] = array(
  456. 'name' => 'li',
  457. 'handler' => 'li',
  458. 'text' => array(
  459. $text,
  460. ),
  461. );
  462. $Block['element']['text'] []= & $Block['li'];
  463. return $Block;
  464. }
  465. if ($Line['text'][0] === '[' and $this->blockReference($Line))
  466. {
  467. return $Block;
  468. }
  469. if ( ! isset($Block['interrupted']))
  470. {
  471. $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
  472. $Block['li']['text'] []= $text;
  473. return $Block;
  474. }
  475. if ($Line['indent'] > 0)
  476. {
  477. $Block['li']['text'] []= '';
  478. $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
  479. $Block['li']['text'] []= $text;
  480. unset($Block['interrupted']);
  481. return $Block;
  482. }
  483. }
  484. protected function blockListComplete(array $Block)
  485. {
  486. if (isset($Block['loose']))
  487. {
  488. foreach ($Block['element']['text'] as &$li)
  489. {
  490. if (end($li['text']) !== '')
  491. {
  492. $li['text'] []= '';
  493. }
  494. }
  495. }
  496. return $Block;
  497. }
  498. #
  499. # Quote
  500. protected function blockQuote($Line)
  501. {
  502. if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
  503. {
  504. $Block = array(
  505. 'element' => array(
  506. 'name' => 'blockquote',
  507. 'handler' => 'lines',
  508. 'text' => (array) $matches[1],
  509. ),
  510. );
  511. return $Block;
  512. }
  513. }
  514. protected function blockQuoteContinue($Line, array $Block)
  515. {
  516. if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
  517. {
  518. if (isset($Block['interrupted']))
  519. {
  520. $Block['element']['text'] []= '';
  521. unset($Block['interrupted']);
  522. }
  523. $Block['element']['text'] []= $matches[1];
  524. return $Block;
  525. }
  526. if ( ! isset($Block['interrupted']))
  527. {
  528. $Block['element']['text'] []= $Line['text'];
  529. return $Block;
  530. }
  531. }
  532. #
  533. # Rule
  534. protected function blockRule($Line)
  535. {
  536. if (preg_match('/^(['.$Line['text'][0].'])([ ]*\1){2,}[ ]*$/', $Line['text']))
  537. {
  538. $Block = array(
  539. 'element' => array(
  540. 'name' => 'hr'
  541. ),
  542. );
  543. return $Block;
  544. }
  545. }
  546. #
  547. # Setext
  548. protected function blockSetextHeader($Line, array $Block = null)
  549. {
  550. if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
  551. {
  552. return;
  553. }
  554. if (chop($Line['text'], $Line['text'][0]) === '')
  555. {
  556. $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2';
  557. return $Block;
  558. }
  559. }
  560. #
  561. # Markup
  562. protected function blockMarkup($Line)
  563. {
  564. if ($this->markupEscaped or $this->safeMode)
  565. {
  566. return;
  567. }
  568. if (preg_match('/^<(\w[\w-]*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches))
  569. {
  570. $element = strtolower($matches[1]);
  571. if (in_array($element, $this->textLevelElements))
  572. {
  573. return;
  574. }
  575. $Block = array(
  576. 'name' => $matches[1],
  577. 'depth' => 0,
  578. 'markup' => $Line['text'],
  579. );
  580. $length = strlen($matches[0]);
  581. $remainder = substr($Line['text'], $length);
  582. if (trim($remainder) === '')
  583. {
  584. if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
  585. {
  586. $Block['closed'] = true;
  587. $Block['void'] = true;
  588. }
  589. }
  590. else
  591. {
  592. if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
  593. {
  594. return;
  595. }
  596. if (preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder))
  597. {
  598. $Block['closed'] = true;
  599. }
  600. }
  601. return $Block;
  602. }
  603. }
  604. protected function blockMarkupContinue($Line, array $Block)
  605. {
  606. if (isset($Block['closed']))
  607. {
  608. return;
  609. }
  610. if (preg_match('/^<'.$Block['name'].'(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*>/i', $Line['text'])) # open
  611. {
  612. $Block['depth'] ++;
  613. }
  614. if (preg_match('/(.*?)<\/'.$Block['name'].'>[ ]*$/i', $Line['text'], $matches)) # close
  615. {
  616. if ($Block['depth'] > 0)
  617. {
  618. $Block['depth'] --;
  619. }
  620. else
  621. {
  622. $Block['closed'] = true;
  623. }
  624. }
  625. if (isset($Block['interrupted']))
  626. {
  627. $Block['markup'] .= "\n";
  628. unset($Block['interrupted']);
  629. }
  630. $Block['markup'] .= "\n".$Line['body'];
  631. return $Block;
  632. }
  633. #
  634. # Reference
  635. protected function blockReference($Line)
  636. {
  637. if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
  638. {
  639. $id = strtolower($matches[1]);
  640. $Data = array(
  641. 'url' => $matches[2],
  642. 'title' => null,
  643. );
  644. if (isset($matches[3]))
  645. {
  646. $Data['title'] = $matches[3];
  647. }
  648. $this->DefinitionData['Reference'][$id] = $Data;
  649. $Block = array(
  650. 'hidden' => true,
  651. );
  652. return $Block;
  653. }
  654. }
  655. #
  656. # Table
  657. protected function blockTable($Line, array $Block = null)
  658. {
  659. if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
  660. {
  661. return;
  662. }
  663. if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '')
  664. {
  665. $alignments = array();
  666. $divider = $Line['text'];
  667. $divider = trim($divider);
  668. $divider = trim($divider, '|');
  669. $dividerCells = explode('|', $divider);
  670. foreach ($dividerCells as $dividerCell)
  671. {
  672. $dividerCell = trim($dividerCell);
  673. if ($dividerCell === '')
  674. {
  675. continue;
  676. }
  677. $alignment = null;
  678. if ($dividerCell[0] === ':')
  679. {
  680. $alignment = 'left';
  681. }
  682. if (substr($dividerCell, - 1) === ':')
  683. {
  684. $alignment = $alignment === 'left' ? 'center' : 'right';
  685. }
  686. $alignments []= $alignment;
  687. }
  688. # ~
  689. $HeaderElements = array();
  690. $header = $Block['element']['text'];
  691. $header = trim($header);
  692. $header = trim($header, '|');
  693. $headerCells = explode('|', $header);
  694. foreach ($headerCells as $index => $headerCell)
  695. {
  696. $headerCell = trim($headerCell);
  697. $HeaderElement = array(
  698. 'name' => 'th',
  699. 'text' => $headerCell,
  700. 'handler' => 'line',
  701. );
  702. if (isset($alignments[$index]))
  703. {
  704. $alignment = $alignments[$index];
  705. $HeaderElement['attributes'] = array(
  706. 'style' => 'text-align: '.$alignment.';',
  707. );
  708. }
  709. $HeaderElements []= $HeaderElement;
  710. }
  711. # ~
  712. $Block = array(
  713. 'alignments' => $alignments,
  714. 'identified' => true,
  715. 'element' => array(
  716. 'name' => 'table',
  717. 'handler' => 'elements',
  718. ),
  719. );
  720. $Block['element']['text'] []= array(
  721. 'name' => 'thead',
  722. 'handler' => 'elements',
  723. );
  724. $Block['element']['text'] []= array(
  725. 'name' => 'tbody',
  726. 'handler' => 'elements',
  727. 'text' => array(),
  728. );
  729. $Block['element']['text'][0]['text'] []= array(
  730. 'name' => 'tr',
  731. 'handler' => 'elements',
  732. 'text' => $HeaderElements,
  733. );
  734. return $Block;
  735. }
  736. }
  737. protected function blockTableContinue($Line, array $Block)
  738. {
  739. if (isset($Block['interrupted']))
  740. {
  741. return;
  742. }
  743. if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
  744. {
  745. $Elements = array();
  746. $row = $Line['text'];
  747. $row = trim($row);
  748. $row = trim($row, '|');
  749. preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]+`|`)+/', $row, $matches);
  750. foreach ($matches[0] as $index => $cell)
  751. {
  752. $cell = trim($cell);
  753. $Element = array(
  754. 'name' => 'td',
  755. 'handler' => 'line',
  756. 'text' => $cell,
  757. );
  758. if (isset($Block['alignments'][$index]))
  759. {
  760. $Element['attributes'] = array(
  761. 'style' => 'text-align: '.$Block['alignments'][$index].';',
  762. );
  763. }
  764. $Elements []= $Element;
  765. }
  766. $Element = array(
  767. 'name' => 'tr',
  768. 'handler' => 'elements',
  769. 'text' => $Elements,
  770. );
  771. $Block['element']['text'][1]['text'] []= $Element;
  772. return $Block;
  773. }
  774. }
  775. #
  776. # ~
  777. #
  778. protected function paragraph($Line)
  779. {
  780. $Block = array(
  781. 'element' => array(
  782. 'name' => 'p',
  783. 'text' => $Line['text'],
  784. 'handler' => 'line',
  785. ),
  786. );
  787. return $Block;
  788. }
  789. #
  790. # Inline Elements
  791. #
  792. protected $InlineTypes = array(
  793. '"' => array('SpecialCharacter'),
  794. '!' => array('Image'),
  795. '&' => array('SpecialCharacter'),
  796. '*' => array('Emphasis'),
  797. ':' => array('Url'),
  798. '<' => array('UrlTag', 'EmailTag', 'Markup', 'SpecialCharacter'),
  799. '>' => array('SpecialCharacter'),
  800. '[' => array('Link'),
  801. '_' => array('Emphasis'),
  802. '`' => array('Code'),
  803. '~' => array('Strikethrough'),
  804. '\\' => array('EscapeSequence'),
  805. );
  806. # ~
  807. protected $inlineMarkerList = '!"*_&[:<>`~\\';
  808. #
  809. # ~
  810. #
  811. public function line($text, $nonNestables=array())
  812. {
  813. $markup = '';
  814. # $excerpt is based on the first occurrence of a marker
  815. while ($excerpt = strpbrk($text, $this->inlineMarkerList))
  816. {
  817. $marker = $excerpt[0];
  818. $markerPosition = strpos($text, $marker);
  819. $Excerpt = array('text' => $excerpt, 'context' => $text);
  820. foreach ($this->InlineTypes[$marker] as $inlineType)
  821. {
  822. # check to see if the current inline type is nestable in the current context
  823. if ( ! empty($nonNestables) and in_array($inlineType, $nonNestables))
  824. {
  825. continue;
  826. }
  827. $Inline = $this->{'inline'.$inlineType}($Excerpt);
  828. if ( ! isset($Inline))
  829. {
  830. continue;
  831. }
  832. # makes sure that the inline belongs to "our" marker
  833. if (isset($Inline['position']) and $Inline['position'] > $markerPosition)
  834. {
  835. continue;
  836. }
  837. # sets a default inline position
  838. if ( ! isset($Inline['position']))
  839. {
  840. $Inline['position'] = $markerPosition;
  841. }
  842. # cause the new element to 'inherit' our non nestables
  843. foreach ($nonNestables as $non_nestable)
  844. {
  845. $Inline['element']['nonNestables'][] = $non_nestable;
  846. }
  847. # the text that comes before the inline
  848. $unmarkedText = substr($text, 0, $Inline['position']);
  849. # compile the unmarked text
  850. $markup .= $this->unmarkedText($unmarkedText);
  851. # compile the inline
  852. $markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']);
  853. # remove the examined text
  854. $text = substr($text, $Inline['position'] + $Inline['extent']);
  855. continue 2;
  856. }
  857. # the marker does not belong to an inline
  858. $unmarkedText = substr($text, 0, $markerPosition + 1);
  859. $markup .= $this->unmarkedText($unmarkedText);
  860. $text = substr($text, $markerPosition + 1);
  861. }
  862. $markup .= $this->unmarkedText($text);
  863. return $markup;
  864. }
  865. #
  866. # ~
  867. #
  868. protected function inlineCode($Excerpt)
  869. {
  870. $marker = $Excerpt['text'][0];
  871. if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches))
  872. {
  873. $text = $matches[2];
  874. $text = preg_replace("/[ ]*\n/", ' ', $text);
  875. return array(
  876. 'extent' => strlen($matches[0]),
  877. 'element' => array(
  878. 'name' => 'code',
  879. 'text' => $text,
  880. ),
  881. );
  882. }
  883. }
  884. protected function inlineEmailTag($Excerpt)
  885. {
  886. if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches))
  887. {
  888. $url = $matches[1];
  889. if ( ! isset($matches[2]))
  890. {
  891. $url = 'mailto:' . $url;
  892. }
  893. return array(
  894. 'extent' => strlen($matches[0]),
  895. 'element' => array(
  896. 'name' => 'a',
  897. 'text' => $matches[1],
  898. 'attributes' => array(
  899. 'href' => $url,
  900. ),
  901. ),
  902. );
  903. }
  904. }
  905. protected function inlineEmphasis($Excerpt)
  906. {
  907. if ( ! isset($Excerpt['text'][1]))
  908. {
  909. return;
  910. }
  911. $marker = $Excerpt['text'][0];
  912. if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches))
  913. {
  914. $emphasis = 'strong';
  915. }
  916. elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches))
  917. {
  918. $emphasis = 'em';
  919. }
  920. else
  921. {
  922. return;
  923. }
  924. return array(
  925. 'extent' => strlen($matches[0]),
  926. 'element' => array(
  927. 'name' => $emphasis,
  928. 'handler' => 'line',
  929. 'text' => $matches[1],
  930. ),
  931. );
  932. }
  933. protected function inlineEscapeSequence($Excerpt)
  934. {
  935. if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters))
  936. {
  937. return array(
  938. 'markup' => $Excerpt['text'][1],
  939. 'extent' => 2,
  940. );
  941. }
  942. }
  943. protected function inlineImage($Excerpt)
  944. {
  945. if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
  946. {
  947. return;
  948. }
  949. $Excerpt['text']= substr($Excerpt['text'], 1);
  950. $Link = $this->inlineLink($Excerpt);
  951. if ($Link === null)
  952. {
  953. return;
  954. }
  955. $Inline = array(
  956. 'extent' => $Link['extent'] + 1,
  957. 'element' => array(
  958. 'name' => 'img',
  959. 'attributes' => array(
  960. 'src' => $Link['element']['attributes']['href'],
  961. 'alt' => $Link['element']['text'],
  962. ),
  963. ),
  964. );
  965. $Inline['element']['attributes'] += $Link['element']['attributes'];
  966. unset($Inline['element']['attributes']['href']);
  967. return $Inline;
  968. }
  969. protected function inlineLink($Excerpt)
  970. {
  971. $Element = array(
  972. 'name' => 'a',
  973. 'handler' => 'line',
  974. 'nonNestables' => array('Url', 'Link'),
  975. 'text' => null,
  976. 'attributes' => array(
  977. 'href' => null,
  978. 'title' => null,
  979. ),
  980. );
  981. $extent = 0;
  982. $remainder = $Excerpt['text'];
  983. if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches))
  984. {
  985. $Element['text'] = $matches[1];
  986. $extent += strlen($matches[0]);
  987. $remainder = substr($remainder, $extent);
  988. }
  989. else
  990. {
  991. return;
  992. }
  993. if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches))
  994. {
  995. $Element['attributes']['href'] = $matches[1];
  996. if (isset($matches[2]))
  997. {
  998. $Element['attributes']['title'] = substr($matches[2], 1, - 1);
  999. }
  1000. $extent += strlen($matches[0]);
  1001. }
  1002. else
  1003. {
  1004. if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
  1005. {
  1006. $definition = strlen($matches[1]) ? $matches[1] : $Element['text'];
  1007. $definition = strtolower($definition);
  1008. $extent += strlen($matches[0]);
  1009. }
  1010. else
  1011. {
  1012. $definition = strtolower($Element['text']);
  1013. }
  1014. if ( ! isset($this->DefinitionData['Reference'][$definition]))
  1015. {
  1016. return;
  1017. }
  1018. $Definition = $this->DefinitionData['Reference'][$definition];
  1019. $Element['attributes']['href'] = $Definition['url'];
  1020. $Element['attributes']['title'] = $Definition['title'];
  1021. }
  1022. return array(
  1023. 'extent' => $extent,
  1024. 'element' => $Element,
  1025. );
  1026. }
  1027. protected function inlineMarkup($Excerpt)
  1028. {
  1029. if ($this->markupEscaped or $this->safeMode or strpos($Excerpt['text'], '>') === false)
  1030. {
  1031. return;
  1032. }
  1033. if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w[\w-]*[ ]*>/s', $Excerpt['text'], $matches))
  1034. {
  1035. return array(
  1036. 'markup' => $matches[0],
  1037. 'extent' => strlen($matches[0]),
  1038. );
  1039. }
  1040. if ($Excerpt['text'][1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $Excerpt['text'], $matches))
  1041. {
  1042. return array(
  1043. 'markup' => $matches[0],
  1044. 'extent' => strlen($matches[0]),
  1045. );
  1046. }
  1047. if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w[\w-]*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $Excerpt['text'], $matches))
  1048. {
  1049. return array(
  1050. 'markup' => $matches[0],
  1051. 'extent' => strlen($matches[0]),
  1052. );
  1053. }
  1054. }
  1055. protected function inlineSpecialCharacter($Excerpt)
  1056. {
  1057. if ($Excerpt['text'][0] === '&' and ! preg_match('/^&#?\w+;/', $Excerpt['text']))
  1058. {
  1059. return array(
  1060. 'markup' => '&amp;',
  1061. 'extent' => 1,
  1062. );
  1063. }
  1064. $SpecialCharacter = array('>' => 'gt', '<' => 'lt', '"' => 'quot');
  1065. if (isset($SpecialCharacter[$Excerpt['text'][0]]))
  1066. {
  1067. return array(
  1068. 'markup' => '&'.$SpecialCharacter[$Excerpt['text'][0]].';',
  1069. 'extent' => 1,
  1070. );
  1071. }
  1072. }
  1073. protected function inlineStrikethrough($Excerpt)
  1074. {
  1075. if ( ! isset($Excerpt['text'][1]))
  1076. {
  1077. return;
  1078. }
  1079. if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
  1080. {
  1081. return array(
  1082. 'extent' => strlen($matches[0]),
  1083. 'element' => array(
  1084. 'name' => 'del',
  1085. 'text' => $matches[1],
  1086. 'handler' => 'line',
  1087. ),
  1088. );
  1089. }
  1090. }
  1091. protected function inlineUrl($Excerpt)
  1092. {
  1093. if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/')
  1094. {
  1095. return;
  1096. }
  1097. if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE))
  1098. {
  1099. $url = $matches[0][0];
  1100. $Inline = array(
  1101. 'extent' => strlen($matches[0][0]),
  1102. 'position' => $matches[0][1],
  1103. 'element' => array(
  1104. 'name' => 'a',
  1105. 'text' => $url,
  1106. 'attributes' => array(
  1107. 'href' => $url,
  1108. ),
  1109. ),
  1110. );
  1111. return $Inline;
  1112. }
  1113. }
  1114. protected function inlineUrlTag($Excerpt)
  1115. {
  1116. if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $Excerpt['text'], $matches))
  1117. {
  1118. $url = $matches[1];
  1119. return array(
  1120. 'extent' => strlen($matches[0]),
  1121. 'element' => array(
  1122. 'name' => 'a',
  1123. 'text' => $url,
  1124. 'attributes' => array(
  1125. 'href' => $url,
  1126. ),
  1127. ),
  1128. );
  1129. }
  1130. }
  1131. # ~
  1132. protected function unmarkedText($text)
  1133. {
  1134. if ($this->breaksEnabled)
  1135. {
  1136. $text = preg_replace('/[ ]*\n/', "<br />\n", $text);
  1137. }
  1138. else
  1139. {
  1140. $text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "<br />\n", $text);
  1141. $text = str_replace(" \n", "\n", $text);
  1142. }
  1143. return $text;
  1144. }
  1145. #
  1146. # Handlers
  1147. #
  1148. protected function element(array $Element)
  1149. {
  1150. if ($this->safeMode)
  1151. {
  1152. $Element = $this->sanitiseElement($Element);
  1153. }
  1154. $markup = '<'.$Element['name'];
  1155. if (isset($Element['attributes']))
  1156. {
  1157. foreach ($Element['attributes'] as $name => $value)
  1158. {
  1159. if ($value === null)
  1160. {
  1161. continue;
  1162. }
  1163. $markup .= ' '.$name.'="'.self::escape($value).'"';
  1164. }
  1165. }
  1166. if (isset($Element['text']))
  1167. {
  1168. $markup .= '>';
  1169. if (!isset($Element['nonNestables']))
  1170. {
  1171. $Element['nonNestables'] = array();
  1172. }
  1173. if (isset($Element['handler']))
  1174. {
  1175. $markup .= $this->{$Element['handler']}($Element['text'], $Element['nonNestables']);
  1176. }
  1177. else
  1178. {
  1179. $markup .= self::escape($Element['text'], true);
  1180. }
  1181. $markup .= '</'.$Element['name'].'>';
  1182. }
  1183. else
  1184. {
  1185. $markup .= ' />';
  1186. }
  1187. return $markup;
  1188. }
  1189. protected function elements(array $Elements)
  1190. {
  1191. $markup = '';
  1192. foreach ($Elements as $Element)
  1193. {
  1194. $markup .= "\n" . $this->element($Element);
  1195. }
  1196. $markup .= "\n";
  1197. return $markup;
  1198. }
  1199. # ~
  1200. protected function li($lines)
  1201. {
  1202. $markup = $this->lines($lines);
  1203. $trimmedMarkup = trim($markup);
  1204. if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>')
  1205. {
  1206. $markup = $trimmedMarkup;
  1207. $markup = substr($markup, 3);
  1208. $position = strpos($markup, "</p>");
  1209. $markup = substr_replace($markup, '', $position, 4);
  1210. }
  1211. return $markup;
  1212. }
  1213. #
  1214. # Deprecated Methods
  1215. #
  1216. function parse($text)
  1217. {
  1218. $markup = $this->text($text);
  1219. return $markup;
  1220. }
  1221. protected function sanitiseElement(array $Element)
  1222. {
  1223. static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/';
  1224. static $safeUrlNameToAtt = array(
  1225. 'a' => 'href',
  1226. 'img' => 'src',
  1227. );
  1228. if (isset($safeUrlNameToAtt[$Element['name']]))
  1229. {
  1230. $Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]);
  1231. }
  1232. if ( ! empty($Element['attributes']))
  1233. {
  1234. foreach ($Element['attributes'] as $att => $val)
  1235. {
  1236. # filter out badly parsed attribute
  1237. if ( ! preg_match($goodAttribute, $att))
  1238. {
  1239. unset($Element['attributes'][$att]);
  1240. }
  1241. # dump onevent attribute
  1242. elseif (self::striAtStart($att, 'on'))
  1243. {
  1244. unset($Element['attributes'][$att]);
  1245. }
  1246. }
  1247. }
  1248. return $Element;
  1249. }
  1250. protected function filterUnsafeUrlInAttribute(array $Element, $attribute)
  1251. {
  1252. foreach ($this->safeLinksWhitelist as $scheme)
  1253. {
  1254. if (self::striAtStart($Element['attributes'][$attribute], $scheme))
  1255. {
  1256. return $Element;
  1257. }
  1258. }
  1259. $Element['attributes'][$attribute] = str_replace(':', '%3A', $Element['attributes'][$attribute]);
  1260. return $Element;
  1261. }
  1262. #
  1263. # Static Methods
  1264. #
  1265. protected static function escape($text, $allowQuotes = false)
  1266. {
  1267. return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8');
  1268. }
  1269. protected static function striAtStart($string, $needle)
  1270. {
  1271. $len = strlen($needle);
  1272. if ($len > strlen($string))
  1273. {
  1274. return false;
  1275. }
  1276. else
  1277. {
  1278. return strtolower(substr($string, 0, $len)) === strtolower($needle);
  1279. }
  1280. }
  1281. static function instance($name = 'default')
  1282. {
  1283. if (isset(self::$instances[$name]))
  1284. {
  1285. return self::$instances[$name];
  1286. }
  1287. $instance = new static();
  1288. self::$instances[$name] = $instance;
  1289. return $instance;
  1290. }
  1291. private static $instances = array();
  1292. #
  1293. # Fields
  1294. #
  1295. protected $DefinitionData;
  1296. #
  1297. # Read-Only
  1298. protected $specialCharacters = array(
  1299. '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|',
  1300. );
  1301. protected $StrongRegex = array(
  1302. '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
  1303. '_' => '/^__((?:\\\\_|[^_]|_[^_]*_)+?)__(?!_)/us',
  1304. );
  1305. protected $EmRegex = array(
  1306. '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
  1307. '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
  1308. );
  1309. protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*(?:\s*=\s*(?:[^"\'=<>`\s]+|"[^"]*"|\'[^\']*\'))?';
  1310. protected $voidElements = array(
  1311. 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source',
  1312. );
  1313. protected $textLevelElements = array(
  1314. 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
  1315. 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
  1316. 'i', 'rp', 'del', 'code', 'strike', 'marquee',
  1317. 'q', 'rt', 'ins', 'font', 'strong',
  1318. 's', 'tt', 'kbd', 'mark',
  1319. 'u', 'xm', 'sub', 'nobr',
  1320. 'sup', 'ruby',
  1321. 'var', 'span',
  1322. 'wbr', 'time',
  1323. );
  1324. }