*/}}

Parsedown.php 52 KB

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