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

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