Parsedown.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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';
  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. }
  1185. protected function inlineStrikethrough($Excerpt)
  1186. {
  1187. if ( ! isset($Excerpt['text'][1]))
  1188. {
  1189. return;
  1190. }
  1191. if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
  1192. {
  1193. return array(
  1194. 'extent' => strlen($matches[0]),
  1195. 'element' => array(
  1196. 'name' => 'del',
  1197. 'handler' => array(
  1198. 'function' => 'lineElements',
  1199. 'argument' => $matches[1],
  1200. 'destination' => 'elements',
  1201. )
  1202. ),
  1203. );
  1204. }
  1205. }
  1206. protected function inlineUrl($Excerpt)
  1207. {
  1208. if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/')
  1209. {
  1210. return;
  1211. }
  1212. if (strpos($Excerpt['context'], 'http') !== false
  1213. and preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE)
  1214. ) {
  1215. $url = $matches[0][0];
  1216. $Inline = array(
  1217. 'extent' => strlen($matches[0][0]),
  1218. 'position' => $matches[0][1],
  1219. 'element' => array(
  1220. 'name' => 'a',
  1221. 'text' => $url,
  1222. 'attributes' => array(
  1223. 'href' => $url,
  1224. ),
  1225. ),
  1226. );
  1227. return $Inline;
  1228. }
  1229. }
  1230. protected function inlineUrlTag($Excerpt)
  1231. {
  1232. if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches))
  1233. {
  1234. $url = $matches[1];
  1235. return array(
  1236. 'extent' => strlen($matches[0]),
  1237. 'element' => array(
  1238. 'name' => 'a',
  1239. 'text' => $url,
  1240. 'attributes' => array(
  1241. 'href' => $url,
  1242. ),
  1243. ),
  1244. );
  1245. }
  1246. }
  1247. # ~
  1248. protected function unmarkedText($text)
  1249. {
  1250. $Inline = $this->inlineText($text);
  1251. return $this->element($Inline['element']);
  1252. }
  1253. #
  1254. # Handlers
  1255. #
  1256. protected function handle(array $Element)
  1257. {
  1258. if (isset($Element['handler']))
  1259. {
  1260. if (!isset($Element['nonNestables']))
  1261. {
  1262. $Element['nonNestables'] = array();
  1263. }
  1264. if (is_string($Element['handler']))
  1265. {
  1266. $function = $Element['handler'];
  1267. $argument = $Element['text'];
  1268. unset($Element['text']);
  1269. $destination = 'rawHtml';
  1270. }
  1271. else
  1272. {
  1273. $function = $Element['handler']['function'];
  1274. $argument = $Element['handler']['argument'];
  1275. $destination = $Element['handler']['destination'];
  1276. }
  1277. $Element[$destination] = $this->{$function}($argument, $Element['nonNestables']);
  1278. if ($destination === 'handler')
  1279. {
  1280. $Element = $this->handle($Element);
  1281. }
  1282. unset($Element['handler']);
  1283. }
  1284. return $Element;
  1285. }
  1286. protected function handleElementRecursive(array $Element)
  1287. {
  1288. return $this->elementApplyRecursive(array($this, 'handle'), $Element);
  1289. }
  1290. protected function handleElementsRecursive(array $Elements)
  1291. {
  1292. return $this->elementsApplyRecursive(array($this, 'handle'), $Elements);
  1293. }
  1294. protected function elementApplyRecursive($closure, array $Element)
  1295. {
  1296. $Element = call_user_func($closure, $Element);
  1297. if (isset($Element['elements']))
  1298. {
  1299. $Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']);
  1300. }
  1301. elseif (isset($Element['element']))
  1302. {
  1303. $Element['element'] = $this->elementApplyRecursive($closure, $Element['element']);
  1304. }
  1305. return $Element;
  1306. }
  1307. protected function elementApplyRecursiveDepthFirst($closure, array $Element)
  1308. {
  1309. if (isset($Element['elements']))
  1310. {
  1311. $Element['elements'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['elements']);
  1312. }
  1313. elseif (isset($Element['element']))
  1314. {
  1315. $Element['element'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['element']);
  1316. }
  1317. $Element = call_user_func($closure, $Element);
  1318. return $Element;
  1319. }
  1320. protected function elementsApplyRecursive($closure, array $Elements)
  1321. {
  1322. foreach ($Elements as &$Element)
  1323. {
  1324. $Element = $this->elementApplyRecursive($closure, $Element);
  1325. }
  1326. return $Elements;
  1327. }
  1328. protected function elementsApplyRecursiveDepthFirst($closure, array $Elements)
  1329. {
  1330. foreach ($Elements as &$Element)
  1331. {
  1332. $Element = $this->elementApplyRecursiveDepthFirst($closure, $Element);
  1333. }
  1334. return $Elements;
  1335. }
  1336. protected function element(array $Element)
  1337. {
  1338. if ($this->safeMode)
  1339. {
  1340. $Element = $this->sanitiseElement($Element);
  1341. }
  1342. # identity map if element has no handler
  1343. $Element = $this->handle($Element);
  1344. $hasName = isset($Element['name']);
  1345. $markup = '';
  1346. if ($hasName)
  1347. {
  1348. $markup .= '<' . $Element['name'];
  1349. if (isset($Element['attributes']))
  1350. {
  1351. foreach ($Element['attributes'] as $name => $value)
  1352. {
  1353. if ($value === null)
  1354. {
  1355. continue;
  1356. }
  1357. $markup .= " $name=\"".self::escape($value).'"';
  1358. }
  1359. }
  1360. }
  1361. $permitRawHtml = false;
  1362. if (isset($Element['text']))
  1363. {
  1364. $text = $Element['text'];
  1365. }
  1366. // very strongly consider an alternative if you're writing an
  1367. // extension
  1368. elseif (isset($Element['rawHtml']))
  1369. {
  1370. $text = $Element['rawHtml'];
  1371. $allowRawHtmlInSafeMode = isset($Element['allowRawHtmlInSafeMode']) && $Element['allowRawHtmlInSafeMode'];
  1372. $permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode;
  1373. }
  1374. $hasContent = isset($text) || isset($Element['element']) || isset($Element['elements']);
  1375. if ($hasContent)
  1376. {
  1377. $markup .= $hasName ? '>' : '';
  1378. if (isset($Element['elements']))
  1379. {
  1380. $markup .= $this->elements($Element['elements']);
  1381. }
  1382. elseif (isset($Element['element']))
  1383. {
  1384. $markup .= $this->element($Element['element']);
  1385. }
  1386. else
  1387. {
  1388. if (!$permitRawHtml)
  1389. {
  1390. $markup .= self::escape($text, true);
  1391. }
  1392. else
  1393. {
  1394. $markup .= $text;
  1395. }
  1396. }
  1397. $markup .= $hasName ? '</' . $Element['name'] . '>' : '';
  1398. }
  1399. elseif ($hasName)
  1400. {
  1401. $markup .= ' />';
  1402. }
  1403. return $markup;
  1404. }
  1405. protected function elements(array $Elements)
  1406. {
  1407. $markup = '';
  1408. $autoBreak = true;
  1409. foreach ($Elements as $Element)
  1410. {
  1411. if (empty($Element))
  1412. {
  1413. continue;
  1414. }
  1415. $autoBreakNext = (isset($Element['autobreak'])
  1416. ? $Element['autobreak'] : isset($Element['name'])
  1417. );
  1418. // (autobreak === false) covers both sides of an element
  1419. $autoBreak = !$autoBreak ? $autoBreak : $autoBreakNext;
  1420. $markup .= ($autoBreak ? "\n" : '') . $this->element($Element);
  1421. $autoBreak = $autoBreakNext;
  1422. }
  1423. $markup .= $autoBreak ? "\n" : '';
  1424. return $markup;
  1425. }
  1426. # ~
  1427. protected function li($lines)
  1428. {
  1429. $Elements = $this->linesElements($lines);
  1430. if ( ! in_array('', $lines)
  1431. and isset($Elements[0]) and isset($Elements[0]['name'])
  1432. and $Elements[0]['name'] === 'p'
  1433. ) {
  1434. unset($Elements[0]['name']);
  1435. }
  1436. return $Elements;
  1437. }
  1438. #
  1439. # AST Convenience
  1440. #
  1441. /**
  1442. * Replace occurrences $regexp with $Elements in $text. Return an array of
  1443. * elements representing the replacement.
  1444. */
  1445. protected static function pregReplaceElements($regexp, $Elements, $text)
  1446. {
  1447. $newElements = array();
  1448. while (preg_match($regexp, $text, $matches, PREG_OFFSET_CAPTURE))
  1449. {
  1450. $offset = $matches[0][1];
  1451. $before = substr($text, 0, $offset);
  1452. $after = substr($text, $offset + strlen($matches[0][0]));
  1453. $newElements[] = array('text' => $before);
  1454. foreach ($Elements as $Element)
  1455. {
  1456. $newElements[] = $Element;
  1457. }
  1458. $text = $after;
  1459. }
  1460. $newElements[] = array('text' => $text);
  1461. return $newElements;
  1462. }
  1463. #
  1464. # Deprecated Methods
  1465. #
  1466. /**
  1467. * @deprecated use text() instead
  1468. */
  1469. function parse($text)
  1470. {
  1471. $markup = $this->text($text);
  1472. return $markup;
  1473. }
  1474. protected function sanitiseElement(array $Element)
  1475. {
  1476. static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/';
  1477. static $safeUrlNameToAtt = array(
  1478. 'a' => 'href',
  1479. 'img' => 'src',
  1480. );
  1481. if ( ! isset($Element['name']))
  1482. {
  1483. unset($Element['attributes']);
  1484. return $Element;
  1485. }
  1486. if (isset($safeUrlNameToAtt[$Element['name']]))
  1487. {
  1488. $Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]);
  1489. }
  1490. if ( ! empty($Element['attributes']))
  1491. {
  1492. foreach ($Element['attributes'] as $att => $val)
  1493. {
  1494. # filter out badly parsed attribute
  1495. if ( ! preg_match($goodAttribute, $att))
  1496. {
  1497. unset($Element['attributes'][$att]);
  1498. }
  1499. # dump onevent attribute
  1500. elseif (self::striAtStart($att, 'on'))
  1501. {
  1502. unset($Element['attributes'][$att]);
  1503. }
  1504. }
  1505. }
  1506. return $Element;
  1507. }
  1508. protected function filterUnsafeUrlInAttribute(array $Element, $attribute)
  1509. {
  1510. foreach ($this->safeLinksWhitelist as $scheme)
  1511. {
  1512. if (self::striAtStart($Element['attributes'][$attribute], $scheme))
  1513. {
  1514. return $Element;
  1515. }
  1516. }
  1517. $Element['attributes'][$attribute] = str_replace(':', '%3A', $Element['attributes'][$attribute]);
  1518. return $Element;
  1519. }
  1520. #
  1521. # Static Methods
  1522. #
  1523. protected static function escape($text, $allowQuotes = false)
  1524. {
  1525. return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8');
  1526. }
  1527. protected static function striAtStart($string, $needle)
  1528. {
  1529. $len = strlen($needle);
  1530. if ($len > strlen($string))
  1531. {
  1532. return false;
  1533. }
  1534. else
  1535. {
  1536. return strtolower(substr($string, 0, $len)) === strtolower($needle);
  1537. }
  1538. }
  1539. static function instance($name = 'default')
  1540. {
  1541. if (isset(self::$instances[$name]))
  1542. {
  1543. return self::$instances[$name];
  1544. }
  1545. $instance = new static();
  1546. self::$instances[$name] = $instance;
  1547. return $instance;
  1548. }
  1549. private static $instances = array();
  1550. #
  1551. # Fields
  1552. #
  1553. protected $DefinitionData;
  1554. #
  1555. # Read-Only
  1556. protected $specialCharacters = array(
  1557. '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', '~'
  1558. );
  1559. protected $StrongRegex = array(
  1560. '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s',
  1561. '_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us',
  1562. );
  1563. protected $EmRegex = array(
  1564. '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
  1565. '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
  1566. );
  1567. protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+';
  1568. protected $voidElements = array(
  1569. 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source',
  1570. );
  1571. protected $textLevelElements = array(
  1572. 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont',
  1573. 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing',
  1574. 'i', 'rp', 'del', 'code', 'strike', 'marquee',
  1575. 'q', 'rt', 'ins', 'font', 'strong',
  1576. 's', 'tt', 'kbd', 'mark',
  1577. 'u', 'xm', 'sub', 'nobr',
  1578. 'sup', 'ruby',
  1579. 'var', 'span',
  1580. 'wbr', 'time',
  1581. );
  1582. }