Parsedown.php 50 KB

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