Parsedown.php 50 KB

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