Parsedown.php 52 KB

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