BioTorrents.de’s version of Gazelle
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.

Parsedown.php 51KB

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