1
0

ParsedownLegacy.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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.5.0';
  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. #
  57. # Lines
  58. #
  59. protected $BlockTypes = array(
  60. '#' => array('Header'),
  61. '*' => array('Rule', 'List'),
  62. '+' => array('List'),
  63. '-' => array('SetextHeader', 'Table', 'Rule', 'List'),
  64. '0' => array('List'),
  65. '1' => array('List'),
  66. '2' => array('List'),
  67. '3' => array('List'),
  68. '4' => array('List'),
  69. '5' => array('List'),
  70. '6' => array('List'),
  71. '7' => array('List'),
  72. '8' => array('List'),
  73. '9' => array('List'),
  74. ':' => array('Table'),
  75. '<' => array('Comment', 'Markup'),
  76. '=' => array('SetextHeader'),
  77. '>' => array('Quote'),
  78. '[' => array('Reference'),
  79. '_' => array('Rule'),
  80. '`' => array('FencedCode'),
  81. '|' => array('Table'),
  82. '~' => array('FencedCode'),
  83. );
  84. # ~
  85. protected $DefinitionTypes = array(
  86. '[' => array('Reference'),
  87. );
  88. # ~
  89. protected $unmarkedBlockTypes = array(
  90. 'Code',
  91. );
  92. #
  93. # Blocks
  94. #
  95. private function lines(array $lines)
  96. {
  97. $CurrentBlock = null;
  98. foreach ($lines as $line)
  99. {
  100. if (chop($line) === '')
  101. {
  102. if (isset($CurrentBlock))
  103. {
  104. $CurrentBlock['interrupted'] = true;
  105. }
  106. continue;
  107. }
  108. if (strpos($line, "\t") !== false)
  109. {
  110. $parts = explode("\t", $line);
  111. $line = $parts[0];
  112. unset($parts[0]);
  113. foreach ($parts as $part)
  114. {
  115. $shortage = 4 - mb_strlen($line, 'utf-8') % 4;
  116. $line .= str_repeat(' ', $shortage);
  117. $line .= $part;
  118. }
  119. }
  120. $indent = 0;
  121. while (isset($line[$indent]) and $line[$indent] === ' ')
  122. {
  123. $indent ++;
  124. }
  125. $text = $indent > 0 ? substr($line, $indent) : $line;
  126. # ~
  127. $Line = array('body' => $line, 'indent' => $indent, 'text' => $text);
  128. # ~
  129. if (isset($CurrentBlock['incomplete']))
  130. {
  131. $Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock);
  132. if (isset($Block))
  133. {
  134. $CurrentBlock = $Block;
  135. continue;
  136. }
  137. else
  138. {
  139. if (method_exists($this, 'block'.$CurrentBlock['type'].'Complete'))
  140. {
  141. $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
  142. }
  143. unset($CurrentBlock['incomplete']);
  144. }
  145. }
  146. # ~
  147. $marker = $text[0];
  148. # ~
  149. $blockTypes = $this->unmarkedBlockTypes;
  150. if (isset($this->BlockTypes[$marker]))
  151. {
  152. foreach ($this->BlockTypes[$marker] as $blockType)
  153. {
  154. $blockTypes []= $blockType;
  155. }
  156. }
  157. #
  158. # ~
  159. foreach ($blockTypes as $blockType)
  160. {
  161. $Block = $this->{'block'.$blockType}($Line, $CurrentBlock);
  162. if (isset($Block))
  163. {
  164. $Block['type'] = $blockType;
  165. if ( ! isset($Block['identified']))
  166. {
  167. $Blocks []= $CurrentBlock;
  168. $Block['identified'] = true;
  169. }
  170. if (method_exists($this, 'block'.$blockType.'Continue'))
  171. {
  172. $Block['incomplete'] = true;
  173. }
  174. $CurrentBlock = $Block;
  175. continue 2;
  176. }
  177. }
  178. # ~
  179. if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted']))
  180. {
  181. $CurrentBlock['element']['text'] .= "\n".$text;
  182. }
  183. else
  184. {
  185. $Blocks []= $CurrentBlock;
  186. $CurrentBlock = $this->paragraph($Line);
  187. $CurrentBlock['identified'] = true;
  188. }
  189. }
  190. # ~
  191. if (isset($CurrentBlock['incomplete']) and method_exists($this, 'block'.$CurrentBlock['type'].'Complete'))
  192. {
  193. $CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock);
  194. }
  195. # ~
  196. $Blocks []= $CurrentBlock;
  197. unset($Blocks[0]);
  198. # ~
  199. $markup = '';
  200. foreach ($Blocks as $Block)
  201. {
  202. if (isset($Block['hidden']))
  203. {
  204. continue;
  205. }
  206. $markup .= "\n";
  207. $markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']);
  208. }
  209. $markup .= "\n";
  210. # ~
  211. return $markup;
  212. }
  213. #
  214. # Code
  215. protected function blockCode($Line, $Block = null)
  216. {
  217. if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted']))
  218. {
  219. return;
  220. }
  221. if ($Line['indent'] >= 4)
  222. {
  223. $text = substr($Line['body'], 4);
  224. $Block = array(
  225. 'element' => array(
  226. 'name' => 'pre',
  227. 'handler' => 'element',
  228. 'text' => array(
  229. 'name' => 'code',
  230. 'text' => $text,
  231. ),
  232. ),
  233. );
  234. return $Block;
  235. }
  236. }
  237. protected function blockCodeContinue($Line, $Block)
  238. {
  239. if ($Line['indent'] >= 4)
  240. {
  241. if (isset($Block['interrupted']))
  242. {
  243. $Block['element']['text']['text'] .= "\n";
  244. unset($Block['interrupted']);
  245. }
  246. $Block['element']['text']['text'] .= "\n";
  247. $text = substr($Line['body'], 4);
  248. $Block['element']['text']['text'] .= $text;
  249. return $Block;
  250. }
  251. }
  252. protected function blockCodeComplete($Block)
  253. {
  254. $text = $Block['element']['text']['text'];
  255. $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
  256. $Block['element']['text']['text'] = $text;
  257. return $Block;
  258. }
  259. #
  260. # Comment
  261. protected function blockComment($Line)
  262. {
  263. if ($this->markupEscaped)
  264. {
  265. return;
  266. }
  267. if (isset($Line['text'][3]) and $Line['text'][3] === '-' and $Line['text'][2] === '-' and $Line['text'][1] === '!')
  268. {
  269. $Block = array(
  270. 'markup' => $Line['body'],
  271. );
  272. if (preg_match('/-->$/', $Line['text']))
  273. {
  274. $Block['closed'] = true;
  275. }
  276. return $Block;
  277. }
  278. }
  279. protected function blockCommentContinue($Line, array $Block)
  280. {
  281. if (isset($Block['closed']))
  282. {
  283. return;
  284. }
  285. $Block['markup'] .= "\n" . $Line['body'];
  286. if (preg_match('/-->$/', $Line['text']))
  287. {
  288. $Block['closed'] = true;
  289. }
  290. return $Block;
  291. }
  292. #
  293. # Fenced Code
  294. protected function blockFencedCode($Line)
  295. {
  296. if (preg_match('/^(['.$Line['text'][0].']{3,})[ ]*([\w-]+)?[ ]*$/', $Line['text'], $matches))
  297. {
  298. $Element = array(
  299. 'name' => 'code',
  300. 'text' => '',
  301. );
  302. if (isset($matches[2]))
  303. {
  304. $class = 'language-'.$matches[2];
  305. $Element['attributes'] = array(
  306. 'class' => $class,
  307. );
  308. }
  309. $Block = array(
  310. 'char' => $Line['text'][0],
  311. 'element' => array(
  312. 'name' => 'pre',
  313. 'handler' => 'element',
  314. 'text' => $Element,
  315. ),
  316. );
  317. return $Block;
  318. }
  319. }
  320. protected function blockFencedCodeContinue($Line, $Block)
  321. {
  322. if (isset($Block['complete']))
  323. {
  324. return;
  325. }
  326. if (isset($Block['interrupted']))
  327. {
  328. $Block['element']['text']['text'] .= "\n";
  329. unset($Block['interrupted']);
  330. }
  331. if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
  332. {
  333. $Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1);
  334. $Block['complete'] = true;
  335. return $Block;
  336. }
  337. $Block['element']['text']['text'] .= "\n".$Line['body'];;
  338. return $Block;
  339. }
  340. protected function blockFencedCodeComplete($Block)
  341. {
  342. $text = $Block['element']['text']['text'];
  343. $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
  344. $Block['element']['text']['text'] = $text;
  345. return $Block;
  346. }
  347. #
  348. # Header
  349. protected function blockHeader($Line)
  350. {
  351. if (isset($Line['text'][1]))
  352. {
  353. $level = 1;
  354. while (isset($Line['text'][$level]) and $Line['text'][$level] === '#')
  355. {
  356. $level ++;
  357. }
  358. if ($level > 6)
  359. {
  360. return;
  361. }
  362. $text = trim($Line['text'], '# ');
  363. $Block = array(
  364. 'element' => array(
  365. 'name' => 'h' . min(6, $level),
  366. 'text' => $text,
  367. 'handler' => 'line',
  368. ),
  369. );
  370. return $Block;
  371. }
  372. }
  373. #
  374. # List
  375. protected function blockList($Line)
  376. {
  377. list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]');
  378. if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches))
  379. {
  380. $Block = array(
  381. 'indent' => $Line['indent'],
  382. 'pattern' => $pattern,
  383. 'element' => array(
  384. 'name' => $name,
  385. 'handler' => 'elements',
  386. ),
  387. );
  388. $Block['li'] = array(
  389. 'name' => 'li',
  390. 'handler' => 'li',
  391. 'text' => array(
  392. $matches[2],
  393. ),
  394. );
  395. $Block['element']['text'] []= & $Block['li'];
  396. return $Block;
  397. }
  398. }
  399. protected function blockListContinue($Line, array $Block)
  400. {
  401. if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'(?:[ ]+(.*)|$)/', $Line['text'], $matches))
  402. {
  403. if (isset($Block['interrupted']))
  404. {
  405. $Block['li']['text'] []= '';
  406. unset($Block['interrupted']);
  407. }
  408. unset($Block['li']);
  409. $text = isset($matches[1]) ? $matches[1] : '';
  410. $Block['li'] = array(
  411. 'name' => 'li',
  412. 'handler' => 'li',
  413. 'text' => array(
  414. $text,
  415. ),
  416. );
  417. $Block['element']['text'] []= & $Block['li'];
  418. return $Block;
  419. }
  420. if ($Line['text'][0] === '[' and $this->blockReference($Line))
  421. {
  422. return $Block;
  423. }
  424. if ( ! isset($Block['interrupted']))
  425. {
  426. $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
  427. $Block['li']['text'] []= $text;
  428. return $Block;
  429. }
  430. if ($Line['indent'] > 0)
  431. {
  432. $Block['li']['text'] []= '';
  433. $text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
  434. $Block['li']['text'] []= $text;
  435. unset($Block['interrupted']);
  436. return $Block;
  437. }
  438. }
  439. #
  440. # Quote
  441. protected function blockQuote($Line)
  442. {
  443. if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
  444. {
  445. $Block = array(
  446. 'element' => array(
  447. 'name' => 'blockquote',
  448. 'handler' => 'lines',
  449. 'text' => (array) $matches[1],
  450. ),
  451. );
  452. return $Block;
  453. }
  454. }
  455. protected function blockQuoteContinue($Line, array $Block)
  456. {
  457. if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches))
  458. {
  459. if (isset($Block['interrupted']))
  460. {
  461. $Block['element']['text'] []= '';
  462. unset($Block['interrupted']);
  463. }
  464. $Block['element']['text'] []= $matches[1];
  465. return $Block;
  466. }
  467. if ( ! isset($Block['interrupted']))
  468. {
  469. $Block['element']['text'] []= $Line['text'];
  470. return $Block;
  471. }
  472. }
  473. #
  474. # Rule
  475. protected function blockRule($Line)
  476. {
  477. if (preg_match('/^(['.$Line['text'][0].'])([ ]*\1){2,}[ ]*$/', $Line['text']))
  478. {
  479. $Block = array(
  480. 'element' => array(
  481. 'name' => 'hr'
  482. ),
  483. );
  484. return $Block;
  485. }
  486. }
  487. #
  488. # Setext
  489. protected function blockSetextHeader($Line, array $Block = null)
  490. {
  491. if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
  492. {
  493. return;
  494. }
  495. if (chop($Line['text'], $Line['text'][0]) === '')
  496. {
  497. $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2';
  498. return $Block;
  499. }
  500. }
  501. #
  502. # Markup
  503. protected function blockMarkup($Line)
  504. {
  505. if ($this->markupEscaped)
  506. {
  507. return;
  508. }
  509. if (preg_match('/^<(\w*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches))
  510. {
  511. if (in_array($matches[1], $this->textLevelElements))
  512. {
  513. return;
  514. }
  515. $Block = array(
  516. 'name' => $matches[1],
  517. 'depth' => 0,
  518. 'markup' => $Line['text'],
  519. );
  520. $length = strlen($matches[0]);
  521. $remainder = substr($Line['text'], $length);
  522. if (trim($remainder) === '')
  523. {
  524. if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
  525. {
  526. $Block['closed'] = true;
  527. $Block['void'] = true;
  528. }
  529. }
  530. else
  531. {
  532. if (isset($matches[2]) or in_array($matches[1], $this->voidElements))
  533. {
  534. return;
  535. }
  536. if (preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder))
  537. {
  538. $Block['closed'] = true;
  539. }
  540. }
  541. return $Block;
  542. }
  543. }
  544. protected function blockMarkupContinue($Line, array $Block)
  545. {
  546. if (isset($Block['closed']))
  547. {
  548. return;
  549. }
  550. if (preg_match('/^<'.$Block['name'].'(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*>/i', $Line['text'])) # open
  551. {
  552. $Block['depth'] ++;
  553. }
  554. if (preg_match('/(.*?)<\/'.$Block['name'].'>[ ]*$/i', $Line['text'], $matches)) # close
  555. {
  556. if ($Block['depth'] > 0)
  557. {
  558. $Block['depth'] --;
  559. }
  560. else
  561. {
  562. $Block['closed'] = true;
  563. }
  564. $Block['markup'] .= $matches[1];
  565. }
  566. if (isset($Block['interrupted']))
  567. {
  568. $Block['markup'] .= "\n";
  569. unset($Block['interrupted']);
  570. }
  571. $Block['markup'] .= "\n".$Line['body'];
  572. return $Block;
  573. }
  574. #
  575. # Reference
  576. protected function blockReference($Line)
  577. {
  578. if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches))
  579. {
  580. $id = strtolower($matches[1]);
  581. $Data = array(
  582. 'url' => $matches[2],
  583. 'title' => null,
  584. );
  585. if (isset($matches[3]))
  586. {
  587. $Data['title'] = $matches[3];
  588. }
  589. $this->DefinitionData['Reference'][$id] = $Data;
  590. $Block = array(
  591. 'hidden' => true,
  592. );
  593. return $Block;
  594. }
  595. }
  596. #
  597. # Table
  598. protected function blockTable($Line, array $Block = null)
  599. {
  600. if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted']))
  601. {
  602. return;
  603. }
  604. if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '')
  605. {
  606. $alignments = array();
  607. $divider = $Line['text'];
  608. $divider = trim($divider);
  609. $divider = trim($divider, '|');
  610. $dividerCells = explode('|', $divider);
  611. foreach ($dividerCells as $dividerCell)
  612. {
  613. $dividerCell = trim($dividerCell);
  614. if ($dividerCell === '')
  615. {
  616. continue;
  617. }
  618. $alignment = null;
  619. if ($dividerCell[0] === ':')
  620. {
  621. $alignment = 'left';
  622. }
  623. if (substr($dividerCell, - 1) === ':')
  624. {
  625. $alignment = $alignment === 'left' ? 'center' : 'right';
  626. }
  627. $alignments []= $alignment;
  628. }
  629. # ~
  630. $HeaderElements = array();
  631. $header = $Block['element']['text'];
  632. $header = trim($header);
  633. $header = trim($header, '|');
  634. $headerCells = explode('|', $header);
  635. foreach ($headerCells as $index => $headerCell)
  636. {
  637. $headerCell = trim($headerCell);
  638. $HeaderElement = array(
  639. 'name' => 'th',
  640. 'text' => $headerCell,
  641. 'handler' => 'line',
  642. );
  643. if (isset($alignments[$index]))
  644. {
  645. $alignment = $alignments[$index];
  646. $HeaderElement['attributes'] = array(
  647. 'style' => 'text-align: '.$alignment.';',
  648. );
  649. }
  650. $HeaderElements []= $HeaderElement;
  651. }
  652. # ~
  653. $Block = array(
  654. 'alignments' => $alignments,
  655. 'identified' => true,
  656. 'element' => array(
  657. 'name' => 'table',
  658. 'handler' => 'elements',
  659. ),
  660. );
  661. $Block['element']['text'] []= array(
  662. 'name' => 'thead',
  663. 'handler' => 'elements',
  664. );
  665. $Block['element']['text'] []= array(
  666. 'name' => 'tbody',
  667. 'handler' => 'elements',
  668. 'text' => array(),
  669. );
  670. $Block['element']['text'][0]['text'] []= array(
  671. 'name' => 'tr',
  672. 'handler' => 'elements',
  673. 'text' => $HeaderElements,
  674. );
  675. return $Block;
  676. }
  677. }
  678. protected function blockTableContinue($Line, array $Block)
  679. {
  680. if (isset($Block['interrupted']))
  681. {
  682. return;
  683. }
  684. if ($Line['text'][0] === '|' or strpos($Line['text'], '|'))
  685. {
  686. $Elements = array();
  687. $row = $Line['text'];
  688. $row = trim($row);
  689. $row = trim($row, '|');
  690. preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]+`|`)+/', $row, $matches);
  691. foreach ($matches[0] as $index => $cell)
  692. {
  693. $cell = trim($cell);
  694. $Element = array(
  695. 'name' => 'td',
  696. 'handler' => 'line',
  697. 'text' => $cell,
  698. );
  699. if (isset($Block['alignments'][$index]))
  700. {
  701. $Element['attributes'] = array(
  702. 'style' => 'text-align: '.$Block['alignments'][$index].';',
  703. );
  704. }
  705. $Elements []= $Element;
  706. }
  707. $Element = array(
  708. 'name' => 'tr',
  709. 'handler' => 'elements',
  710. 'text' => $Elements,
  711. );
  712. $Block['element']['text'][1]['text'] []= $Element;
  713. return $Block;
  714. }
  715. }
  716. #
  717. # ~
  718. #
  719. protected function paragraph($Line)
  720. {
  721. $Block = array(
  722. 'element' => array(
  723. 'name' => 'p',
  724. 'text' => $Line['text'],
  725. 'handler' => 'line',
  726. ),
  727. );
  728. return $Block;
  729. }
  730. #
  731. # Inline Elements
  732. #
  733. protected $InlineTypes = array(
  734. '"' => array('SpecialCharacter'),
  735. '!' => array('Image'),
  736. '&' => array('SpecialCharacter'),
  737. '*' => array('Emphasis'),
  738. ':' => array('Url'),
  739. '<' => array('UrlTag', 'EmailTag', 'Markup', 'SpecialCharacter'),
  740. '>' => array('SpecialCharacter'),
  741. '[' => array('Link'),
  742. '_' => array('Emphasis'),
  743. '`' => array('Code'),
  744. '~' => array('Strikethrough'),
  745. '\\' => array('EscapeSequence'),
  746. );
  747. # ~
  748. protected $inlineMarkerList = '!"*_&[:<>`~\\';
  749. #
  750. # ~
  751. #
  752. public function line($text)
  753. {
  754. $markup = '';
  755. $unexaminedText = $text;
  756. $markerPosition = 0;
  757. while ($excerpt = strpbrk($unexaminedText, $this->inlineMarkerList))
  758. {
  759. $marker = $excerpt[0];
  760. $markerPosition += strpos($unexaminedText, $marker);
  761. $Excerpt = array('text' => $excerpt, 'context' => $text);
  762. foreach ($this->InlineTypes[$marker] as $inlineType)
  763. {
  764. $Inline = $this->{'inline'.$inlineType}($Excerpt);
  765. if ( ! isset($Inline))
  766. {
  767. continue;
  768. }
  769. if (isset($Inline['position']) and $Inline['position'] > $markerPosition) # position is ahead of marker
  770. {
  771. continue;
  772. }
  773. if ( ! isset($Inline['position']))
  774. {
  775. $Inline['position'] = $markerPosition;
  776. }
  777. $unmarkedText = substr($text, 0, $Inline['position']);
  778. $markup .= $this->unmarkedText($unmarkedText);
  779. $markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']);
  780. $text = substr($text, $Inline['position'] + $Inline['extent']);
  781. $unexaminedText = $text;
  782. $markerPosition = 0;
  783. continue 2;
  784. }
  785. $unexaminedText = substr($excerpt, 1);
  786. $markerPosition ++;
  787. }
  788. $markup .= $this->unmarkedText($text);
  789. return $markup;
  790. }
  791. #
  792. # ~
  793. #
  794. protected function inlineCode($Excerpt)
  795. {
  796. $marker = $Excerpt['text'][0];
  797. if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches))
  798. {
  799. $text = $matches[2];
  800. $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
  801. $text = preg_replace("/[ ]*\n/", ' ', $text);
  802. return array(
  803. 'extent' => strlen($matches[0]),
  804. 'element' => array(
  805. 'name' => 'code',
  806. 'text' => $text,
  807. ),
  808. );
  809. }
  810. }
  811. protected function inlineEmailTag($Excerpt)
  812. {
  813. if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches))
  814. {
  815. $url = $matches[1];
  816. if ( ! isset($matches[2]))
  817. {
  818. $url = 'mailto:' . $url;
  819. }
  820. return array(
  821. 'extent' => strlen($matches[0]),
  822. 'element' => array(
  823. 'name' => 'a',
  824. 'text' => $matches[1],
  825. 'attributes' => array(
  826. 'href' => $url,
  827. ),
  828. ),
  829. );
  830. }
  831. }
  832. protected function inlineEmphasis($Excerpt)
  833. {
  834. if ( ! isset($Excerpt['text'][1]))
  835. {
  836. return;
  837. }
  838. $marker = $Excerpt['text'][0];
  839. if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches))
  840. {
  841. $emphasis = 'strong';
  842. }
  843. elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches))
  844. {
  845. $emphasis = 'em';
  846. }
  847. else
  848. {
  849. return;
  850. }
  851. return array(
  852. 'extent' => strlen($matches[0]),
  853. 'element' => array(
  854. 'name' => $emphasis,
  855. 'handler' => 'line',
  856. 'text' => $matches[1],
  857. ),
  858. );
  859. }
  860. protected function inlineEscapeSequence($Excerpt)
  861. {
  862. if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters))
  863. {
  864. return array(
  865. 'markup' => $Excerpt['text'][1],
  866. 'extent' => 2,
  867. );
  868. }
  869. }
  870. protected function inlineImage($Excerpt)
  871. {
  872. if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
  873. {
  874. return;
  875. }
  876. $Excerpt['text']= substr($Excerpt['text'], 1);
  877. $Link = $this->inlineLink($Excerpt);
  878. if ($Link === null)
  879. {
  880. return;
  881. }
  882. $Inline = array(
  883. 'extent' => $Link['extent'] + 1,
  884. 'element' => array(
  885. 'name' => 'img',
  886. 'attributes' => array(
  887. 'src' => $Link['element']['attributes']['href'],
  888. 'alt' => $Link['element']['text'],
  889. ),
  890. ),
  891. );
  892. $Inline['element']['attributes'] += $Link['element']['attributes'];
  893. unset($Inline['element']['attributes']['href']);
  894. return $Inline;
  895. }
  896. protected function inlineLink($Excerpt)
  897. {
  898. $Element = array(
  899. 'name' => 'a',
  900. 'handler' => 'line',
  901. 'text' => null,
  902. 'attributes' => array(
  903. 'href' => null,
  904. 'title' => null,
  905. ),
  906. );
  907. $extent = 0;
  908. $remainder = $Excerpt['text'];
  909. if (preg_match('/\[((?:[^][]|(?R))*)\]/', $remainder, $matches))
  910. {
  911. $Element['text'] = $matches[1];
  912. $extent += strlen($matches[0]);
  913. $remainder = substr($remainder, $extent);
  914. }
  915. else
  916. {
  917. return;
  918. }
  919. if (preg_match('/^[(]((?:[^ (]|[(][^ )]+[)])+)(?:[ ]+("[^"]+"|\'[^\']+\'))?[)]/', $remainder, $matches))
  920. {
  921. $Element['attributes']['href'] = $matches[1];
  922. if (isset($matches[2]))
  923. {
  924. $Element['attributes']['title'] = substr($matches[2], 1, - 1);
  925. }
  926. $extent += strlen($matches[0]);
  927. }
  928. else
  929. {
  930. if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches))
  931. {
  932. $definition = $matches[1] ?: $Element['text'];
  933. $definition = strtolower($definition);
  934. $extent += strlen($matches[0]);
  935. }
  936. else
  937. {
  938. $definition = strtolower($Element['text']);
  939. }
  940. if ( ! isset($this->DefinitionData['Reference'][$definition]))
  941. {
  942. return;
  943. }
  944. $Definition = $this->DefinitionData['Reference'][$definition];
  945. $Element['attributes']['href'] = $Definition['url'];
  946. $Element['attributes']['title'] = $Definition['title'];
  947. }
  948. $Element['attributes']['href'] = str_replace(array('&', '<'), array('&amp;', '&lt;'), $Element['attributes']['href']);
  949. return array(
  950. 'extent' => $extent,
  951. 'element' => $Element,
  952. );
  953. }
  954. protected function inlineMarkup($Excerpt)
  955. {
  956. if ($this->markupEscaped or strpos($Excerpt['text'], '>') === false)
  957. {
  958. return;
  959. }
  960. if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w*[ ]*>/s', $Excerpt['text'], $matches))
  961. {
  962. return array(
  963. 'markup' => $matches[0],
  964. 'extent' => strlen($matches[0]),
  965. );
  966. }
  967. if ($Excerpt['text'][1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $Excerpt['text'], $matches))
  968. {
  969. return array(
  970. 'markup' => $matches[0],
  971. 'extent' => strlen($matches[0]),
  972. );
  973. }
  974. if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $Excerpt['text'], $matches))
  975. {
  976. return array(
  977. 'markup' => $matches[0],
  978. 'extent' => strlen($matches[0]),
  979. );
  980. }
  981. }
  982. protected function inlineSpecialCharacter($Excerpt)
  983. {
  984. if ($Excerpt['text'][0] === '&' and ! preg_match('/^&#?\w+;/', $Excerpt['text']))
  985. {
  986. return array(
  987. 'markup' => '&amp;',
  988. 'extent' => 1,
  989. );
  990. }
  991. $SpecialCharacter = array('>' => 'gt', '<' => 'lt', '"' => 'quot');
  992. if (isset($SpecialCharacter[$Excerpt['text'][0]]))
  993. {
  994. return array(
  995. 'markup' => '&'.$SpecialCharacter[$Excerpt['text'][0]].';',
  996. 'extent' => 1,
  997. );
  998. }
  999. }
  1000. protected function inlineStrikethrough($Excerpt)
  1001. {
  1002. if ( ! isset($Excerpt['text'][1]))
  1003. {
  1004. return;
  1005. }
  1006. if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
  1007. {
  1008. return array(
  1009. 'extent' => strlen($matches[0]),
  1010. 'element' => array(
  1011. 'name' => 'del',
  1012. 'text' => $matches[1],
  1013. 'handler' => 'line',
  1014. ),
  1015. );
  1016. }
  1017. }
  1018. protected function inlineUrl($Excerpt)
  1019. {
  1020. if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/')
  1021. {
  1022. return;
  1023. }
  1024. if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE))
  1025. {
  1026. $Inline = array(
  1027. 'extent' => strlen($matches[0][0]),
  1028. 'position' => $matches[0][1],
  1029. 'element' => array(
  1030. 'name' => 'a',
  1031. 'text' => $matches[0][0],
  1032. 'attributes' => array(
  1033. 'href' => $matches[0][0],
  1034. ),
  1035. ),
  1036. );
  1037. return $Inline;
  1038. }
  1039. }
  1040. protected function inlineUrlTag($Excerpt)
  1041. {
  1042. if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $Excerpt['text'], $matches))
  1043. {
  1044. $url = str_replace(array('&', '<'), array('&amp;', '&lt;'), $matches[1]);
  1045. return array(
  1046. 'extent' => strlen($matches[0]),
  1047. 'element' => array(
  1048. 'name' => 'a',
  1049. 'text' => $url,
  1050. 'attributes' => array(
  1051. 'href' => $url,
  1052. ),
  1053. ),
  1054. );
  1055. }
  1056. }
  1057. #
  1058. # ~
  1059. protected $unmarkedInlineTypes = array("\n" => 'Break', '://' => 'Url');
  1060. # ~
  1061. protected function unmarkedText($text)
  1062. {
  1063. if ($this->breaksEnabled)
  1064. {
  1065. $text = preg_replace('/[ ]*\n/', "<br />\n", $text);
  1066. }
  1067. else
  1068. {
  1069. $text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "<br />\n", $text);
  1070. $text = str_replace(" \n", "\n", $text);
  1071. }
  1072. return $text;
  1073. }
  1074. #
  1075. # Handlers
  1076. #
  1077. protected function element(array $Element)
  1078. {
  1079. $markup = '<'.$Element['name'];
  1080. if (isset($Element['attributes']))
  1081. {
  1082. foreach ($Element['attributes'] as $name => $value)
  1083. {
  1084. if ($value === null)
  1085. {
  1086. continue;
  1087. }
  1088. $markup .= ' '.$name.'="'.$value.'"';
  1089. }
  1090. }
  1091. if (isset($Element['text']))
  1092. {
  1093. $markup .= '>';
  1094. if (isset($Element['handler']))
  1095. {
  1096. $markup .= $this->{$Element['handler']}($Element['text']);
  1097. }
  1098. else
  1099. {
  1100. $markup .= $Element['text'];
  1101. }
  1102. $markup .= '</'.$Element['name'].'>';
  1103. }
  1104. else
  1105. {
  1106. $markup .= ' />';
  1107. }
  1108. return $markup;
  1109. }
  1110. protected function elements(array $Elements)
  1111. {
  1112. $markup = '';
  1113. foreach ($Elements as $Element)
  1114. {
  1115. $markup .= "\n" . $this->element($Element);
  1116. }
  1117. $markup .= "\n";
  1118. return $markup;
  1119. }
  1120. # ~
  1121. protected function li($lines)
  1122. {
  1123. $markup = $this->lines($lines);
  1124. $trimmedMarkup = trim($markup);
  1125. if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>')
  1126. {
  1127. $markup = $trimmedMarkup;
  1128. $markup = substr($markup, 3);
  1129. $position = strpos($markup, "</p>");
  1130. $markup = substr_replace($markup, '', $position, 4);
  1131. }
  1132. return $markup;
  1133. }
  1134. #
  1135. # Deprecated Methods
  1136. #
  1137. function parse($text)
  1138. {
  1139. $markup = $this->text($text);
  1140. return $markup;
  1141. }
  1142. #
  1143. # Static Methods
  1144. #
  1145. static function instance($name = 'default')
  1146. {
  1147. if (isset(self::$instances[$name]))
  1148. {
  1149. return self::$instances[$name];
  1150. }
  1151. $instance = new self();
  1152. self::$instances[$name] = $instance;
  1153. return $instance;
  1154. }
  1155. private static $instances = array();
  1156. #
  1157. # Fields
  1158. #
  1159. protected $DefinitionData;
  1160. #
  1161. # Read-Only
  1162. protected $specialCharacters = array(
  1163. '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|',
  1164. );
  1165. protected $StrongRegex = array(
  1166. '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
  1167. '_' => '/^__((?:\\\\_|[^_]|_[^_]*_)+?)__(?!_)/us',
  1168. );
  1169. protected $EmRegex = array(
  1170. '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
  1171. '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
  1172. );
  1173. protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*(?:\s*=\s*(?:[^"\'=<>`\s]+|"[^"]*"|\'[^\']*\'))?';
  1174. protected $voidElements = array(
  1175. 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source',
  1176. );
  1177. protected $textLevelElements = array(
  1178. 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
  1179. 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
  1180. 'i', 'rp', 'del', 'code', 'strike', 'marquee',
  1181. 'q', 'rt', 'ins', 'font', 'strong',
  1182. 's', 'tt', 'sub', 'mark',
  1183. 'u', 'xm', 'sup', 'nobr',
  1184. 'var', 'ruby',
  1185. 'wbr', 'span',
  1186. 'time',
  1187. );
  1188. }