PageRenderTime 1513ms CodeModel.GetById 21ms RepoModel.GetById 5ms app.codeStats 1ms

/protected/libs/yii/vendors/markdown/markdown.php

https://bitbucket.org/graaaf/erso
PHP | 2648 lines | 1763 code | 283 blank | 602 comment | 158 complexity | 1ee8f21bc6c177fccdc999c91c48db56 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, BSD-2-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. #
  3. # Markdown Extra - A text-to-HTML conversion tool for web writers
  4. #
  5. # PHP Markdown & Extra
  6. # Copyright (c) 2004-2012 Michel Fortin
  7. # <http://michelf.com/projects/php-markdown/>
  8. #
  9. # Original Markdown
  10. # Copyright (c) 2004-2006 John Gruber
  11. # <http://daringfireball.net/projects/markdown/>
  12. #
  13. #
  14. # Markdown Parser Class
  15. #
  16. class Markdown_Parser {
  17. # Regex to match balanced [brackets].
  18. # Needed to insert a maximum bracked depth while converting to PHP.
  19. public $nested_brackets_depth = 6;
  20. public $nested_brackets_re;
  21. public $nested_url_parenthesis_depth = 4;
  22. public $nested_url_parenthesis_re;
  23. # Table of hash values for escaped characters:
  24. public $escape_chars = '\`*_{}[]()>#+-.!';
  25. public $escape_chars_re;
  26. # Change to ">" for HTML output.
  27. public $empty_element_suffix = ' />';
  28. public $tab_width = 4;
  29. # Change to `true` to disallow markup or entities.
  30. public $no_markup = false;
  31. public $no_entities = false;
  32. # Predefined urls and titles for reference links and images.
  33. public $predef_urls = array();
  34. public $predef_titles = array();
  35. public function __construct() {
  36. #
  37. # Constructor function. Initialize appropriate member variables.
  38. #
  39. $this->_initDetab();
  40. $this->prepareItalicsAndBold();
  41. $this->nested_brackets_re =
  42. str_repeat('(?>[^\[\]]+|\[', $this->nested_brackets_depth).
  43. str_repeat('\])*', $this->nested_brackets_depth);
  44. $this->nested_url_parenthesis_re =
  45. str_repeat('(?>[^()\s]+|\(', $this->nested_url_parenthesis_depth).
  46. str_repeat('(?>\)))*', $this->nested_url_parenthesis_depth);
  47. $this->escape_chars_re = '['.preg_quote($this->escape_chars).']';
  48. # Sort document, block, and span gamut in ascendent priority order.
  49. asort($this->document_gamut);
  50. asort($this->block_gamut);
  51. asort($this->span_gamut);
  52. }
  53. # Internal hashes used during transformation.
  54. public $urls = array();
  55. public $titles = array();
  56. public $html_hashes = array();
  57. # Status flag to avoid invalid nesting.
  58. public $in_anchor = false;
  59. public function setup() {
  60. #
  61. # Called before the transformation process starts to setup parser
  62. # states.
  63. #
  64. # Clear global hashes.
  65. $this->urls = $this->predef_urls;
  66. $this->titles = $this->predef_titles;
  67. $this->html_hashes = array();
  68. $in_anchor = false;
  69. }
  70. public function teardown() {
  71. #
  72. # Called after the transformation process to clear any variable
  73. # which may be taking up memory unnecessarly.
  74. #
  75. $this->urls = array();
  76. $this->titles = array();
  77. $this->html_hashes = array();
  78. }
  79. public function transform($text) {
  80. #
  81. # Main function. Performs some preprocessing on the input text
  82. # and pass it through the document gamut.
  83. #
  84. $this->setup();
  85. # Remove UTF-8 BOM and marker character in input, if present.
  86. $text = preg_replace('{^\xEF\xBB\xBF|\x1A}', '', $text);
  87. # Standardize line endings:
  88. # DOS to Unix and Mac to Unix
  89. $text = preg_replace('{\r\n?}', "\n", $text);
  90. # Make sure $text ends with a couple of newlines:
  91. $text .= "\n\n";
  92. # Convert all tabs to spaces.
  93. $text = $this->detab($text);
  94. # Turn block-level HTML blocks into hash entries
  95. $text = $this->hashHTMLBlocks($text);
  96. # Strip any lines consisting only of spaces and tabs.
  97. # This makes subsequent regexen easier to write, because we can
  98. # match consecutive blank lines with /\n+/ instead of something
  99. # contorted like /[ ]*\n+/ .
  100. $text = preg_replace('/^[ ]+$/m', '', $text);
  101. # Run document gamut methods.
  102. foreach ($this->document_gamut as $method => $priority) {
  103. $text = $this->$method($text);
  104. }
  105. $this->teardown();
  106. return $text . "\n";
  107. }
  108. public $document_gamut = array(
  109. # Strip link definitions, store in hashes.
  110. "stripLinkDefinitions" => 20,
  111. "runBasicBlockGamut" => 30,
  112. );
  113. public function stripLinkDefinitions($text) {
  114. #
  115. # Strips link definitions from text, stores the URLs and titles in
  116. # hash references.
  117. #
  118. $less_than_tab = $this->tab_width - 1;
  119. # Link defs are in the form: ^[id]: url "optional title"
  120. $text = preg_replace_callback('{
  121. ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1
  122. [ ]*
  123. \n? # maybe *one* newline
  124. [ ]*
  125. (?:
  126. <(.+?)> # url = $2
  127. |
  128. (\S+?) # url = $3
  129. )
  130. [ ]*
  131. \n? # maybe one newline
  132. [ ]*
  133. (?:
  134. (?<=\s) # lookbehind for whitespace
  135. ["(]
  136. (.*?) # title = $4
  137. [")]
  138. [ ]*
  139. )? # title is optional
  140. (?:\n+|\Z)
  141. }xm',
  142. array(&$this, '_stripLinkDefinitions_callback'),
  143. $text);
  144. return $text;
  145. }
  146. public function _stripLinkDefinitions_callback($matches) {
  147. $link_id = strtolower($matches[1]);
  148. $url = $matches[2] == '' ? $matches[3] : $matches[2];
  149. $this->urls[$link_id] = $url;
  150. $this->titles[$link_id] =& $matches[4];
  151. return ''; # String that will replace the block
  152. }
  153. public function hashHTMLBlocks($text) {
  154. if ($this->no_markup) return $text;
  155. $less_than_tab = $this->tab_width - 1;
  156. # Hashify HTML blocks:
  157. # We only want to do this for block-level HTML tags, such as headers,
  158. # lists, and tables. That's because we still want to wrap <p>s around
  159. # "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  160. # phrase emphasis, and spans. The list of tags we're looking for is
  161. # hard-coded:
  162. #
  163. # * List "a" is made of tags which can be both inline or block-level.
  164. # These will be treated block-level when the start tag is alone on
  165. # its line, otherwise they're not matched here and will be taken as
  166. # inline later.
  167. # * List "b" is made of tags which are always block-level;
  168. #
  169. $block_tags_a_re = 'ins|del';
  170. $block_tags_b_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|'.
  171. 'script|noscript|form|fieldset|iframe|math';
  172. # Regular expression for the content of a block tag.
  173. $nested_tags_level = 4;
  174. $attr = '
  175. (?> # optional tag attributes
  176. \s # starts with whitespace
  177. (?>
  178. [^>"/]+ # text outside quotes
  179. |
  180. /+(?!>) # slash not followed by ">"
  181. |
  182. "[^"]*" # text inside double quotes (tolerate ">")
  183. |
  184. \'[^\']*\' # text inside single quotes (tolerate ">")
  185. )*
  186. )?
  187. ';
  188. $content =
  189. str_repeat('
  190. (?>
  191. [^<]+ # content without tag
  192. |
  193. <\2 # nested opening tag
  194. '.$attr.' # attributes
  195. (?>
  196. />
  197. |
  198. >', $nested_tags_level). # end of opening tag
  199. '.*?'. # last level nested tag content
  200. str_repeat('
  201. </\2\s*> # closing nested tag
  202. )
  203. |
  204. <(?!/\2\s*> # other tags with a different name
  205. )
  206. )*',
  207. $nested_tags_level);
  208. $content2 = str_replace('\2', '\3', $content);
  209. # First, look for nested blocks, e.g.:
  210. # <div>
  211. # <div>
  212. # tags for inner block must be indented.
  213. # </div>
  214. # </div>
  215. #
  216. # The outermost tags must start at the left margin for this to match, and
  217. # the inner nested divs must be indented.
  218. # We need to do this before the next, more liberal match, because the next
  219. # match will start at the first `<div>` and stop at the first `</div>`.
  220. $text = preg_replace_callback('{(?>
  221. (?>
  222. (?<=\n\n) # Starting after a blank line
  223. | # or
  224. \A\n? # the beginning of the doc
  225. )
  226. ( # save in $1
  227. # Match from `\n<tag>` to `</tag>\n`, handling nested tags
  228. # in between.
  229. [ ]{0,'.$less_than_tab.'}
  230. <('.$block_tags_b_re.')# start tag = $2
  231. '.$attr.'> # attributes followed by > and \n
  232. '.$content.' # content, support nesting
  233. </\2> # the matching end tag
  234. [ ]* # trailing spaces/tabs
  235. (?=\n+|\Z) # followed by a newline or end of document
  236. | # Special version for tags of group a.
  237. [ ]{0,'.$less_than_tab.'}
  238. <('.$block_tags_a_re.')# start tag = $3
  239. '.$attr.'>[ ]*\n # attributes followed by >
  240. '.$content2.' # content, support nesting
  241. </\3> # the matching end tag
  242. [ ]* # trailing spaces/tabs
  243. (?=\n+|\Z) # followed by a newline or end of document
  244. | # Special case just for <hr />. It was easier to make a special
  245. # case than to make the other regex more complicated.
  246. [ ]{0,'.$less_than_tab.'}
  247. <(hr) # start tag = $2
  248. '.$attr.' # attributes
  249. /?> # the matching end tag
  250. [ ]*
  251. (?=\n{2,}|\Z) # followed by a blank line or end of document
  252. | # Special case for standalone HTML comments:
  253. [ ]{0,'.$less_than_tab.'}
  254. (?s:
  255. <!-- .*? -->
  256. )
  257. [ ]*
  258. (?=\n{2,}|\Z) # followed by a blank line or end of document
  259. | # PHP and ASP-style processor instructions (<? and <%)
  260. [ ]{0,'.$less_than_tab.'}
  261. (?s:
  262. <([?%]) # $2
  263. .*?
  264. \2>
  265. )
  266. [ ]*
  267. (?=\n{2,}|\Z) # followed by a blank line or end of document
  268. )
  269. )}Sxmi',
  270. array(&$this, '_hashHTMLBlocks_callback'),
  271. $text);
  272. return $text;
  273. }
  274. public function _hashHTMLBlocks_callback($matches) {
  275. $text = $matches[1];
  276. $key = $this->hashBlock($text);
  277. return "\n\n$key\n\n";
  278. }
  279. public function hashPart($text, $boundary = 'X') {
  280. #
  281. # Called whenever a tag must be hashed when a function insert an atomic
  282. # element in the text stream. Passing $text to through this function gives
  283. # a unique text-token which will be reverted back when calling unhash.
  284. #
  285. # The $boundary argument specify what character should be used to surround
  286. # the token. By convension, "B" is used for block elements that needs not
  287. # to be wrapped into paragraph tags at the end, ":" is used for elements
  288. # that are word separators and "X" is used in the general case.
  289. #
  290. # Swap back any tag hash found in $text so we do not have to `unhash`
  291. # multiple times at the end.
  292. $text = $this->unhash($text);
  293. # Then hash the block.
  294. static $i = 0;
  295. $key = "$boundary\x1A" . ++$i . $boundary;
  296. $this->html_hashes[$key] = $text;
  297. return $key; # String that will replace the tag.
  298. }
  299. public function hashBlock($text) {
  300. #
  301. # Shortcut function for hashPart with block-level boundaries.
  302. #
  303. return $this->hashPart($text, 'B');
  304. }
  305. public $block_gamut = array(
  306. #
  307. # These are all the transformations that form block-level
  308. # tags like paragraphs, headers, and list items.
  309. #
  310. "doHeaders" => 10,
  311. "doHorizontalRules" => 20,
  312. "doLists" => 40,
  313. "doCodeBlocks" => 50,
  314. "doBlockQuotes" => 60,
  315. );
  316. public function runBlockGamut($text) {
  317. #
  318. # Run block gamut tranformations.
  319. #
  320. # We need to escape raw HTML in Markdown source before doing anything
  321. # else. This need to be done for each block, and not only at the
  322. # begining in the Markdown function since hashed blocks can be part of
  323. # list items and could have been indented. Indented blocks would have
  324. # been seen as a code block in a previous pass of hashHTMLBlocks.
  325. $text = $this->hashHTMLBlocks($text);
  326. return $this->runBasicBlockGamut($text);
  327. }
  328. public function runBasicBlockGamut($text) {
  329. #
  330. # Run block gamut tranformations, without hashing HTML blocks. This is
  331. # useful when HTML blocks are known to be already hashed, like in the first
  332. # whole-document pass.
  333. #
  334. foreach ($this->block_gamut as $method => $priority) {
  335. $text = $this->$method($text);
  336. }
  337. # Finally form paragraph and restore hashed blocks.
  338. $text = $this->formParagraphs($text);
  339. return $text;
  340. }
  341. public function doHorizontalRules($text) {
  342. # Do Horizontal Rules:
  343. return preg_replace(
  344. '{
  345. ^[ ]{0,3} # Leading space
  346. ([-*_]) # $1: First marker
  347. (?> # Repeated marker group
  348. [ ]{0,2} # Zero, one, or two spaces.
  349. \1 # Marker character
  350. ){2,} # Group repeated at least twice
  351. [ ]* # Tailing spaces
  352. $ # End of line.
  353. }mx',
  354. "\n".$this->hashBlock("<hr$this->empty_element_suffix")."\n",
  355. $text);
  356. }
  357. public $span_gamut = array(
  358. #
  359. # These are all the transformations that occur *within* block-level
  360. # tags like paragraphs, headers, and list items.
  361. #
  362. # Process character escapes, code spans, and inline HTML
  363. # in one shot.
  364. "parseSpan" => -30,
  365. # Process anchor and image tags. Images must come first,
  366. # because ![foo][f] looks like an anchor.
  367. "doImages" => 10,
  368. "doAnchors" => 20,
  369. # Make links out of things like `<http://example.com/>`
  370. # Must come after doAnchors, because you can use < and >
  371. # delimiters in inline links like [this](<url>).
  372. "doAutoLinks" => 30,
  373. "encodeAmpsAndAngles" => 40,
  374. "doItalicsAndBold" => 50,
  375. "doHardBreaks" => 60,
  376. );
  377. public function runSpanGamut($text) {
  378. #
  379. # Run span gamut tranformations.
  380. #
  381. foreach ($this->span_gamut as $method => $priority) {
  382. $text = $this->$method($text);
  383. }
  384. return $text;
  385. }
  386. public function doHardBreaks($text) {
  387. # Do hard breaks:
  388. return preg_replace_callback('/ {2,}\n/',
  389. array(&$this, '_doHardBreaks_callback'), $text);
  390. }
  391. public function _doHardBreaks_callback($matches) {
  392. return $this->hashPart("<br$this->empty_element_suffix\n");
  393. }
  394. public function doAnchors($text) {
  395. #
  396. # Turn Markdown link shortcuts into XHTML <a> tags.
  397. #
  398. if ($this->in_anchor) return $text;
  399. $this->in_anchor = true;
  400. #
  401. # First, handle reference-style links: [link text] [id]
  402. #
  403. $text = preg_replace_callback('{
  404. ( # wrap whole match in $1
  405. \[
  406. ('.$this->nested_brackets_re.') # link text = $2
  407. \]
  408. [ ]? # one optional space
  409. (?:\n[ ]*)? # one optional newline followed by spaces
  410. \[
  411. (.*?) # id = $3
  412. \]
  413. )
  414. }xs',
  415. array(&$this, '_doAnchors_reference_callback'), $text);
  416. #
  417. # Next, inline-style links: [link text](url "optional title")
  418. #
  419. $text = preg_replace_callback('{
  420. ( # wrap whole match in $1
  421. \[
  422. ('.$this->nested_brackets_re.') # link text = $2
  423. \]
  424. \( # literal paren
  425. [ \n]*
  426. (?:
  427. <(.+?)> # href = $3
  428. |
  429. ('.$this->nested_url_parenthesis_re.') # href = $4
  430. )
  431. [ \n]*
  432. ( # $5
  433. ([\'"]) # quote char = $6
  434. (.*?) # Title = $7
  435. \6 # matching quote
  436. [ \n]* # ignore any spaces/tabs between closing quote and )
  437. )? # title is optional
  438. \)
  439. )
  440. }xs',
  441. array(&$this, '_doAnchors_inline_callback'), $text);
  442. #
  443. # Last, handle reference-style shortcuts: [link text]
  444. # These must come last in case you've also got [link text][1]
  445. # or [link text](/foo)
  446. #
  447. $text = preg_replace_callback('{
  448. ( # wrap whole match in $1
  449. \[
  450. ([^\[\]]+) # link text = $2; can\'t contain [ or ]
  451. \]
  452. )
  453. }xs',
  454. array(&$this, '_doAnchors_reference_callback'), $text);
  455. $this->in_anchor = false;
  456. return $text;
  457. }
  458. public function _doAnchors_reference_callback($matches) {
  459. $whole_match = $matches[1];
  460. $link_text = $matches[2];
  461. $link_id =& $matches[3];
  462. if ($link_id == "") {
  463. # for shortcut links like [this][] or [this].
  464. $link_id = $link_text;
  465. }
  466. # lower-case and turn embedded newlines into spaces
  467. $link_id = strtolower($link_id);
  468. $link_id = preg_replace('{[ ]?\n}', ' ', $link_id);
  469. if (isset($this->urls[$link_id])) {
  470. $url = $this->urls[$link_id];
  471. $url = $this->encodeAttribute($url);
  472. $result = "<a href=\"$url\"";
  473. if ( isset( $this->titles[$link_id] ) ) {
  474. $title = $this->titles[$link_id];
  475. $title = $this->encodeAttribute($title);
  476. $result .= " title=\"$title\"";
  477. }
  478. $link_text = $this->runSpanGamut($link_text);
  479. $result .= ">$link_text</a>";
  480. $result = $this->hashPart($result);
  481. }
  482. else {
  483. $result = $whole_match;
  484. }
  485. return $result;
  486. }
  487. public function _doAnchors_inline_callback($matches) {
  488. $whole_match = $matches[1];
  489. $link_text = $this->runSpanGamut($matches[2]);
  490. $url = $matches[3] == '' ? $matches[4] : $matches[3];
  491. $title =& $matches[7];
  492. $url = $this->encodeAttribute($url);
  493. $result = "<a href=\"$url\"";
  494. if (isset($title)) {
  495. $title = $this->encodeAttribute($title);
  496. $result .= " title=\"$title\"";
  497. }
  498. $link_text = $this->runSpanGamut($link_text);
  499. $result .= ">$link_text</a>";
  500. return $this->hashPart($result);
  501. }
  502. public function doImages($text) {
  503. #
  504. # Turn Markdown image shortcuts into <img> tags.
  505. #
  506. #
  507. # First, handle reference-style labeled images: ![alt text][id]
  508. #
  509. $text = preg_replace_callback('{
  510. ( # wrap whole match in $1
  511. !\[
  512. ('.$this->nested_brackets_re.') # alt text = $2
  513. \]
  514. [ ]? # one optional space
  515. (?:\n[ ]*)? # one optional newline followed by spaces
  516. \[
  517. (.*?) # id = $3
  518. \]
  519. )
  520. }xs',
  521. array(&$this, '_doImages_reference_callback'), $text);
  522. #
  523. # Next, handle inline images: ![alt text](url "optional title")
  524. # Don't forget: encode * and _
  525. #
  526. $text = preg_replace_callback('{
  527. ( # wrap whole match in $1
  528. !\[
  529. ('.$this->nested_brackets_re.') # alt text = $2
  530. \]
  531. \s? # One optional whitespace character
  532. \( # literal paren
  533. [ \n]*
  534. (?:
  535. <(\S*)> # src url = $3
  536. |
  537. ('.$this->nested_url_parenthesis_re.') # src url = $4
  538. )
  539. [ \n]*
  540. ( # $5
  541. ([\'"]) # quote char = $6
  542. (.*?) # title = $7
  543. \6 # matching quote
  544. [ \n]*
  545. )? # title is optional
  546. \)
  547. )
  548. }xs',
  549. array(&$this, '_doImages_inline_callback'), $text);
  550. return $text;
  551. }
  552. public function _doImages_reference_callback($matches) {
  553. $whole_match = $matches[1];
  554. $alt_text = $matches[2];
  555. $link_id = strtolower($matches[3]);
  556. if ($link_id == "") {
  557. $link_id = strtolower($alt_text); # for shortcut links like ![this][].
  558. }
  559. $alt_text = $this->encodeAttribute($alt_text);
  560. if (isset($this->urls[$link_id])) {
  561. $url = $this->encodeAttribute($this->urls[$link_id]);
  562. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  563. if (isset($this->titles[$link_id])) {
  564. $title = $this->titles[$link_id];
  565. $title = $this->encodeAttribute($title);
  566. $result .= " title=\"$title\"";
  567. }
  568. $result .= $this->empty_element_suffix;
  569. $result = $this->hashPart($result);
  570. }
  571. else {
  572. # If there's no such link ID, leave intact:
  573. $result = $whole_match;
  574. }
  575. return $result;
  576. }
  577. public function _doImages_inline_callback($matches) {
  578. $whole_match = $matches[1];
  579. $alt_text = $matches[2];
  580. $url = $matches[3] == '' ? $matches[4] : $matches[3];
  581. $title =& $matches[7];
  582. $alt_text = $this->encodeAttribute($alt_text);
  583. $url = $this->encodeAttribute($url);
  584. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  585. if (isset($title)) {
  586. $title = $this->encodeAttribute($title);
  587. $result .= " title=\"$title\""; # $title already quoted
  588. }
  589. $result .= $this->empty_element_suffix;
  590. return $this->hashPart($result);
  591. }
  592. public function doHeaders($text) {
  593. # Setext-style headers:
  594. # Header 1
  595. # ========
  596. #
  597. # Header 2
  598. # --------
  599. #
  600. $text = preg_replace_callback('{ ^(.+?)[ ]*\n(=+|-+)[ ]*\n+ }mx',
  601. array(&$this, '_doHeaders_callback_setext'), $text);
  602. # atx-style headers:
  603. # # Header 1
  604. # ## Header 2
  605. # ## Header 2 with closing hashes ##
  606. # ...
  607. # ###### Header 6
  608. #
  609. $text = preg_replace_callback('{
  610. ^(\#{1,6}) # $1 = string of #\'s
  611. [ ]*
  612. (.+?) # $2 = Header text
  613. [ ]*
  614. \#* # optional closing #\'s (not counted)
  615. \n+
  616. }xm',
  617. array(&$this, '_doHeaders_callback_atx'), $text);
  618. return $text;
  619. }
  620. public function _doHeaders_callback_setext($matches) {
  621. # Terrible hack to check we haven't found an empty list item.
  622. if ($matches[2] == '-' && preg_match('{^-(?: |$)}', $matches[1]))
  623. return $matches[0];
  624. $level = $matches[2]{0} == '=' ? 1 : 2;
  625. $block = "<h$level>".$this->runSpanGamut($matches[1])."</h$level>";
  626. return "\n" . $this->hashBlock($block) . "\n\n";
  627. }
  628. public function _doHeaders_callback_atx($matches) {
  629. $level = strlen($matches[1]);
  630. $block = "<h$level>".$this->runSpanGamut($matches[2])."</h$level>";
  631. return "\n" . $this->hashBlock($block) . "\n\n";
  632. }
  633. public function doLists($text) {
  634. #
  635. # Form HTML ordered (numbered) and unordered (bulleted) lists.
  636. #
  637. $less_than_tab = $this->tab_width - 1;
  638. # Re-usable patterns to match list item bullets and number markers:
  639. $marker_ul_re = '[*+-]';
  640. $marker_ol_re = '\d+[\.]';
  641. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  642. $markers_relist = array(
  643. $marker_ul_re => $marker_ol_re,
  644. $marker_ol_re => $marker_ul_re,
  645. );
  646. foreach ($markers_relist as $marker_re => $other_marker_re) {
  647. # Re-usable pattern to match any entirel ul or ol list:
  648. $whole_list_re = '
  649. ( # $1 = whole list
  650. ( # $2
  651. ([ ]{0,'.$less_than_tab.'}) # $3 = number of spaces
  652. ('.$marker_re.') # $4 = first list item marker
  653. [ ]+
  654. )
  655. (?s:.+?)
  656. ( # $5
  657. \z
  658. |
  659. \n{2,}
  660. (?=\S)
  661. (?! # Negative lookahead for another list item marker
  662. [ ]*
  663. '.$marker_re.'[ ]+
  664. )
  665. |
  666. (?= # Lookahead for another kind of list
  667. \n
  668. \3 # Must have the same indentation
  669. '.$other_marker_re.'[ ]+
  670. )
  671. )
  672. )
  673. '; // mx
  674. # We use a different prefix before nested lists than top-level lists.
  675. # See extended comment in _ProcessListItems().
  676. if ($this->list_level) {
  677. $text = preg_replace_callback('{
  678. ^
  679. '.$whole_list_re.'
  680. }mx',
  681. array(&$this, '_doLists_callback'), $text);
  682. }
  683. else {
  684. $text = preg_replace_callback('{
  685. (?:(?<=\n)\n|\A\n?) # Must eat the newline
  686. '.$whole_list_re.'
  687. }mx',
  688. array(&$this, '_doLists_callback'), $text);
  689. }
  690. }
  691. return $text;
  692. }
  693. public function _doLists_callback($matches) {
  694. # Re-usable patterns to match list item bullets and number markers:
  695. $marker_ul_re = '[*+-]';
  696. $marker_ol_re = '\d+[\.]';
  697. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  698. $list = $matches[1];
  699. $list_type = preg_match("/$marker_ul_re/", $matches[4]) ? "ul" : "ol";
  700. $marker_any_re = ( $list_type == "ul" ? $marker_ul_re : $marker_ol_re );
  701. $list .= "\n";
  702. $result = $this->processListItems($list, $marker_any_re);
  703. $result = $this->hashBlock("<$list_type>\n" . $result . "</$list_type>");
  704. return "\n". $result ."\n\n";
  705. }
  706. var $list_level = 0;
  707. public function processListItems($list_str, $marker_any_re) {
  708. #
  709. # Process the contents of a single ordered or unordered list, splitting it
  710. # into individual list items.
  711. #
  712. # The $this->list_level global keeps track of when we're inside a list.
  713. # Each time we enter a list, we increment it; when we leave a list,
  714. # we decrement. If it's zero, we're not in a list anymore.
  715. #
  716. # We do this because when we're not inside a list, we want to treat
  717. # something like this:
  718. #
  719. # I recommend upgrading to version
  720. # 8. Oops, now this line is treated
  721. # as a sub-list.
  722. #
  723. # As a single paragraph, despite the fact that the second line starts
  724. # with a digit-period-space sequence.
  725. #
  726. # Whereas when we're inside a list (or sub-list), that line will be
  727. # treated as the start of a sub-list. What a kludge, huh? This is
  728. # an aspect of Markdown's syntax that's hard to parse perfectly
  729. # without resorting to mind-reading. Perhaps the solution is to
  730. # change the syntax rules such that sub-lists must start with a
  731. # starting cardinal number; e.g. "1." or "a.".
  732. $this->list_level++;
  733. # trim trailing blank lines:
  734. $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str);
  735. $list_str = preg_replace_callback('{
  736. (\n)? # leading line = $1
  737. (^[ ]*) # leading whitespace = $2
  738. ('.$marker_any_re.' # list marker and space = $3
  739. (?:[ ]+|(?=\n)) # space only required if item is not empty
  740. )
  741. ((?s:.*?)) # list item text = $4
  742. (?:(\n+(?=\n))|\n) # tailing blank line = $5
  743. (?= \n* (\z | \2 ('.$marker_any_re.') (?:[ ]+|(?=\n))))
  744. }xm',
  745. array(&$this, '_processListItems_callback'), $list_str);
  746. $this->list_level--;
  747. return $list_str;
  748. }
  749. public function _processListItems_callback($matches) {
  750. $item = $matches[4];
  751. $leading_line =& $matches[1];
  752. $leading_space =& $matches[2];
  753. $marker_space = $matches[3];
  754. $tailing_blank_line =& $matches[5];
  755. if ($leading_line || $tailing_blank_line ||
  756. preg_match('/\n{2,}/', $item))
  757. {
  758. # Replace marker with the appropriate whitespace indentation
  759. $item = $leading_space . str_repeat(' ', strlen($marker_space)) . $item;
  760. $item = $this->runBlockGamut($this->outdent($item)."\n");
  761. }
  762. else {
  763. # Recursion for sub-lists:
  764. $item = $this->doLists($this->outdent($item));
  765. $item = preg_replace('/\n+$/', '', $item);
  766. $item = $this->runSpanGamut($item);
  767. }
  768. return "<li>" . $item . "</li>\n";
  769. }
  770. public function doCodeBlocks($text) {
  771. #
  772. # Process Markdown `<pre><code>` blocks.
  773. #
  774. $text = preg_replace_callback('{
  775. (?:\n\n|\A\n?)
  776. ( # $1 = the code block -- one or more lines, starting with a space/tab
  777. (?>
  778. [ ]{'.$this->tab_width.'} # Lines must start with a tab or a tab-width of spaces
  779. .*\n+
  780. )+
  781. )
  782. ((?=^[ ]{0,'.$this->tab_width.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
  783. }xm',
  784. array(&$this, '_doCodeBlocks_callback'), $text);
  785. return $text;
  786. }
  787. public function _doCodeBlocks_callback($matches) {
  788. $codeblock = $matches[1];
  789. $codeblock = $this->outdent($codeblock);
  790. $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
  791. # trim leading newlines and trailing newlines
  792. $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
  793. $codeblock = "<pre><code>$codeblock\n</code></pre>";
  794. return "\n\n".$this->hashBlock($codeblock)."\n\n";
  795. }
  796. public function makeCodeSpan($code) {
  797. #
  798. # Create a code span markup for $code. Called from handleSpanToken.
  799. #
  800. $code = htmlspecialchars(trim($code), ENT_NOQUOTES);
  801. return $this->hashPart("<code>$code</code>");
  802. }
  803. public $em_relist = array(
  804. '' => '(?:(?<!\*)\*(?!\*)|(?<!_)_(?!_))(?=\S|$)(?![\.,:;]\s)',
  805. '*' => '(?<=\S|^)(?<!\*)\*(?!\*)',
  806. '_' => '(?<=\S|^)(?<!_)_(?!_)',
  807. );
  808. public $strong_relist = array(
  809. '' => '(?:(?<!\*)\*\*(?!\*)|(?<!_)__(?!_))(?=\S|$)(?![\.,:;]\s)',
  810. '**' => '(?<=\S|^)(?<!\*)\*\*(?!\*)',
  811. '__' => '(?<=\S|^)(?<!_)__(?!_)',
  812. );
  813. public $em_strong_relist = array(
  814. '' => '(?:(?<!\*)\*\*\*(?!\*)|(?<!_)___(?!_))(?=\S|$)(?![\.,:;]\s)',
  815. '***' => '(?<=\S|^)(?<!\*)\*\*\*(?!\*)',
  816. '___' => '(?<=\S|^)(?<!_)___(?!_)',
  817. );
  818. public $em_strong_prepared_relist;
  819. public function prepareItalicsAndBold() {
  820. #
  821. # Prepare regular expressions for searching emphasis tokens in any
  822. # context.
  823. #
  824. foreach ($this->em_relist as $em => $em_re) {
  825. foreach ($this->strong_relist as $strong => $strong_re) {
  826. # Construct list of allowed token expressions.
  827. $token_relist = array();
  828. if (isset($this->em_strong_relist["$em$strong"])) {
  829. $token_relist[] = $this->em_strong_relist["$em$strong"];
  830. }
  831. $token_relist[] = $em_re;
  832. $token_relist[] = $strong_re;
  833. # Construct master expression from list.
  834. $token_re = '{('. implode('|', $token_relist) .')}';
  835. $this->em_strong_prepared_relist["$em$strong"] = $token_re;
  836. }
  837. }
  838. }
  839. public function doItalicsAndBold($text) {
  840. $token_stack = array('');
  841. $text_stack = array('');
  842. $em = '';
  843. $strong = '';
  844. $tree_char_em = false;
  845. while (1) {
  846. #
  847. # Get prepared regular expression for seraching emphasis tokens
  848. # in current context.
  849. #
  850. $token_re = $this->em_strong_prepared_relist["$em$strong"];
  851. #
  852. # Each loop iteration search for the next emphasis token.
  853. # Each token is then passed to handleSpanToken.
  854. #
  855. $parts = preg_split($token_re, $text, 2, PREG_SPLIT_DELIM_CAPTURE);
  856. $text_stack[0] .= $parts[0];
  857. $token =& $parts[1];
  858. $text =& $parts[2];
  859. if (empty($token)) {
  860. # Reached end of text span: empty stack without emitting.
  861. # any more emphasis.
  862. while ($token_stack[0]) {
  863. $text_stack[1] .= array_shift($token_stack);
  864. $text_stack[0] .= array_shift($text_stack);
  865. }
  866. break;
  867. }
  868. $token_len = strlen($token);
  869. if ($tree_char_em) {
  870. # Reached closing marker while inside a three-char emphasis.
  871. if ($token_len == 3) {
  872. # Three-char closing marker, close em and strong.
  873. array_shift($token_stack);
  874. $span = array_shift($text_stack);
  875. $span = $this->runSpanGamut($span);
  876. $span = "<strong><em>$span</em></strong>";
  877. $text_stack[0] .= $this->hashPart($span);
  878. $em = '';
  879. $strong = '';
  880. } else {
  881. # Other closing marker: close one em or strong and
  882. # change current token state to match the other
  883. $token_stack[0] = str_repeat($token{0}, 3-$token_len);
  884. $tag = $token_len == 2 ? "strong" : "em";
  885. $span = $text_stack[0];
  886. $span = $this->runSpanGamut($span);
  887. $span = "<$tag>$span</$tag>";
  888. $text_stack[0] = $this->hashPart($span);
  889. $$tag = ''; # $$tag stands for $em or $strong
  890. }
  891. $tree_char_em = false;
  892. } else if ($token_len == 3) {
  893. if ($em) {
  894. # Reached closing marker for both em and strong.
  895. # Closing strong marker:
  896. for ($i = 0; $i < 2; ++$i) {
  897. $shifted_token = array_shift($token_stack);
  898. $tag = strlen($shifted_token) == 2 ? "strong" : "em";
  899. $span = array_shift($text_stack);
  900. $span = $this->runSpanGamut($span);
  901. $span = "<$tag>$span</$tag>";
  902. $text_stack[0] .= $this->hashPart($span);
  903. $$tag = ''; # $$tag stands for $em or $strong
  904. }
  905. } else {
  906. # Reached opening three-char emphasis marker. Push on token
  907. # stack; will be handled by the special condition above.
  908. $em = $token{0};
  909. $strong = "$em$em";
  910. array_unshift($token_stack, $token);
  911. array_unshift($text_stack, '');
  912. $tree_char_em = true;
  913. }
  914. } else if ($token_len == 2) {
  915. if ($strong) {
  916. # Unwind any dangling emphasis marker:
  917. if (strlen($token_stack[0]) == 1) {
  918. $text_stack[1] .= array_shift($token_stack);
  919. $text_stack[0] .= array_shift($text_stack);
  920. }
  921. # Closing strong marker:
  922. array_shift($token_stack);
  923. $span = array_shift($text_stack);
  924. $span = $this->runSpanGamut($span);
  925. $span = "<strong>$span</strong>";
  926. $text_stack[0] .= $this->hashPart($span);
  927. $strong = '';
  928. } else {
  929. array_unshift($token_stack, $token);
  930. array_unshift($text_stack, '');
  931. $strong = $token;
  932. }
  933. } else {
  934. # Here $token_len == 1
  935. if ($em) {
  936. if (strlen($token_stack[0]) == 1) {
  937. # Closing emphasis marker:
  938. array_shift($token_stack);
  939. $span = array_shift($text_stack);
  940. $span = $this->runSpanGamut($span);
  941. $span = "<em>$span</em>";
  942. $text_stack[0] .= $this->hashPart($span);
  943. $em = '';
  944. } else {
  945. $text_stack[0] .= $token;
  946. }
  947. } else {
  948. array_unshift($token_stack, $token);
  949. array_unshift($text_stack, '');
  950. $em = $token;
  951. }
  952. }
  953. }
  954. return $text_stack[0];
  955. }
  956. public function doBlockQuotes($text) {
  957. $text = preg_replace_callback('/
  958. ( # Wrap whole match in $1
  959. (?>
  960. ^[ ]*>[ ]? # ">" at the start of a line
  961. .+\n # rest of the first line
  962. (.+\n)* # subsequent consecutive lines
  963. \n* # blanks
  964. )+
  965. )
  966. /xm',
  967. array(&$this, '_doBlockQuotes_callback'), $text);
  968. return $text;
  969. }
  970. public function _doBlockQuotes_callback($matches) {
  971. $bq = $matches[1];
  972. # trim one level of quoting - trim whitespace-only lines
  973. $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq);
  974. $bq = $this->runBlockGamut($bq); # recurse
  975. $bq = preg_replace('/^/m', " ", $bq);
  976. # These leading spaces cause problem with <pre> content,
  977. # so we need to fix that:
  978. $bq = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx',
  979. array(&$this, '_doBlockQuotes_callback2'), $bq);
  980. return "\n". $this->hashBlock("<blockquote>\n$bq\n</blockquote>")."\n\n";
  981. }
  982. public function _doBlockQuotes_callback2($matches) {
  983. $pre = $matches[1];
  984. $pre = preg_replace('/^ /m', '', $pre);
  985. return $pre;
  986. }
  987. public function formParagraphs($text) {
  988. #
  989. # Params:
  990. # $text - string to process with html <p> tags
  991. #
  992. # Strip leading and trailing lines:
  993. $text = preg_replace('/\A\n+|\n+\z/', '', $text);
  994. $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY);
  995. #
  996. # Wrap <p> tags and unhashify HTML blocks
  997. #
  998. foreach ($grafs as $key => $value) {
  999. if (!preg_match('/^B\x1A[0-9]+B$/', $value)) {
  1000. # Is a paragraph.
  1001. $value = $this->runSpanGamut($value);
  1002. $value = preg_replace('/^([ ]*)/', "<p>", $value);
  1003. $value .= "</p>";
  1004. $grafs[$key] = $this->unhash($value);
  1005. }
  1006. else {
  1007. # Is a block.
  1008. # Modify elements of @grafs in-place...
  1009. $graf = $value;
  1010. $block = $this->html_hashes[$graf];
  1011. $graf = $block;
  1012. // if (preg_match('{
  1013. // \A
  1014. // ( # $1 = <div> tag
  1015. // <div \s+
  1016. // [^>]*
  1017. // \b
  1018. // markdown\s*=\s* ([\'"]) # $2 = attr quote char
  1019. // 1
  1020. // \2
  1021. // [^>]*
  1022. // >
  1023. // )
  1024. // ( # $3 = contents
  1025. // .*
  1026. // )
  1027. // (</div>) # $4 = closing tag
  1028. // \z
  1029. // }xs', $block, $matches))
  1030. // {
  1031. // list(, $div_open, , $div_content, $div_close) = $matches;
  1032. //
  1033. // # We can't call Markdown(), because that resets the hash;
  1034. // # that initialization code should be pulled into its own sub, though.
  1035. // $div_content = $this->hashHTMLBlocks($div_content);
  1036. //
  1037. // # Run document gamut methods on the content.
  1038. // foreach ($this->document_gamut as $method => $priority) {
  1039. // $div_content = $this->$method($div_content);
  1040. // }
  1041. //
  1042. // $div_open = preg_replace(
  1043. // '{\smarkdown\s*=\s*([\'"]).+?\1}', '', $div_open);
  1044. //
  1045. // $graf = $div_open . "\n" . $div_content . "\n" . $div_close;
  1046. // }
  1047. $grafs[$key] = $graf;
  1048. }
  1049. }
  1050. return implode("\n\n", $grafs);
  1051. }
  1052. public function encodeAttribute($text) {
  1053. #
  1054. # Encode text for a double-quoted HTML attribute. This function
  1055. # is *not* suitable for attributes enclosed in single quotes.
  1056. #
  1057. $text = $this->encodeAmpsAndAngles($text);
  1058. $text = str_replace('"', '&quot;', $text);
  1059. return $text;
  1060. }
  1061. public function encodeAmpsAndAngles($text) {
  1062. #
  1063. # Smart processing for ampersands and angle brackets that need to
  1064. # be encoded. Valid character entities are left alone unless the
  1065. # no-entities mode is set.
  1066. #
  1067. if ($this->no_entities) {
  1068. $text = str_replace('&', '&amp;', $text);
  1069. } else {
  1070. # Ampersand-encoding based entirely on Nat Irons's Amputator
  1071. # MT plugin: <http://bumppo.net/projects/amputator/>
  1072. $text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/',
  1073. '&amp;', $text);;
  1074. }
  1075. # Encode remaining <'s
  1076. $text = str_replace('<', '&lt;', $text);
  1077. return $text;
  1078. }
  1079. public function doAutoLinks($text) {
  1080. $text = preg_replace_callback('{<((https?|ftp|dict):[^\'">\s]+)>}i',
  1081. array(&$this, '_doAutoLinks_url_callback'), $text);
  1082. # Email addresses: <address@domain.foo>
  1083. $text = preg_replace_callback('{
  1084. <
  1085. (?:mailto:)?
  1086. (
  1087. (?:
  1088. [-!#$%&\'*+/=?^_`.{|}~\w\x80-\xFF]+
  1089. |
  1090. ".*?"
  1091. )
  1092. \@
  1093. (?:
  1094. [-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+
  1095. |
  1096. \[[\d.a-fA-F:]+\] # IPv4 & IPv6
  1097. )
  1098. )
  1099. >
  1100. }xi',
  1101. array(&$this, '_doAutoLinks_email_callback'), $text);
  1102. return $text;
  1103. }
  1104. public function _doAutoLinks_url_callback($matches) {
  1105. $url = $this->encodeAttribute($matches[1]);
  1106. $link = "<a href=\"$url\">$url</a>";
  1107. return $this->hashPart($link);
  1108. }
  1109. public function _doAutoLinks_email_callback($matches) {
  1110. $address = $matches[1];
  1111. $link = $this->encodeEmailAddress($address);
  1112. return $this->hashPart($link);
  1113. }
  1114. public function encodeEmailAddress($addr) {
  1115. #
  1116. # Input: an email address, e.g. "foo@example.com"
  1117. #
  1118. # Output: the email address as a mailto link, with each character
  1119. # of the address encoded as either a decimal or hex entity, in
  1120. # the hopes of foiling most address harvesting spam bots. E.g.:
  1121. #
  1122. # <p><a href="&#109;&#x61;&#105;&#x6c;&#116;&#x6f;&#58;&#x66;o&#111;
  1123. # &#x40;&#101;&#x78;&#97;&#x6d;&#112;&#x6c;&#101;&#46;&#x63;&#111;
  1124. # &#x6d;">&#x66;o&#111;&#x40;&#101;&#x78;&#97;&#x6d;&#112;&#x6c;
  1125. # &#101;&#46;&#x63;&#111;&#x6d;</a></p>
  1126. #
  1127. # Based by a filter by Matthew Wickline, posted to BBEdit-Talk.
  1128. # With some optimizations by Milian Wolff.
  1129. #
  1130. $addr = "mailto:" . $addr;
  1131. $chars = preg_split('/(?<!^)(?!$)/', $addr);
  1132. $seed = (int)abs(crc32($addr) / strlen($addr)); # Deterministic seed.
  1133. foreach ($chars as $key => $char) {
  1134. $ord = ord($char);
  1135. # Ignore non-ascii chars.
  1136. if ($ord < 128) {
  1137. $r = ($seed * (1 + $key)) % 100; # Pseudo-random function.
  1138. # roughly 10% raw, 45% hex, 45% dec
  1139. # '@' *must* be encoded. I insist.
  1140. if ($r > 90 && $char != '@') /* do nothing */;
  1141. else if ($r < 45) $chars[$key] = '&#x'.dechex($ord).';';
  1142. else $chars[$key] = '&#'.$ord.';';
  1143. }
  1144. }
  1145. $addr = implode('', $chars);
  1146. $text = implode('', array_slice($chars, 7)); # text without `mailto:`
  1147. $addr = "<a href=\"$addr\">$text</a>";
  1148. return $addr;
  1149. }
  1150. public function parseSpan($str) {
  1151. #
  1152. # Take the string $str and parse it into tokens, hashing embeded HTML,
  1153. # escaped characters and handling code spans.
  1154. #
  1155. $output = '';
  1156. $span_re = '{
  1157. (
  1158. \\\\'.$this->escape_chars_re.'
  1159. |
  1160. (?<![`\\\\])
  1161. `+ # code span marker
  1162. '.( $this->no_markup ? '' : '
  1163. |
  1164. <!-- .*? --> # comment
  1165. |
  1166. <\?.*?\?> | <%.*?%> # processing instruction
  1167. |
  1168. <[/!$]?[-a-zA-Z0-9:_]+ # regular tags
  1169. (?>
  1170. \s
  1171. (?>[^"\'>]+|"[^"]*"|\'[^\']*\')*
  1172. )?
  1173. >
  1174. ').'
  1175. )
  1176. }xs';
  1177. while (1) {
  1178. #
  1179. # Each loop iteration seach for either the next tag, the next
  1180. # openning code span marker, or the next escaped character.
  1181. # Each token is then passed to handleSpanToken.
  1182. #
  1183. $parts = preg_split($span_re, $str, 2, PREG_SPLIT_DELIM_CAPTURE);
  1184. # Create token from text preceding tag.
  1185. if ($parts[0] != "") {
  1186. $output .= $parts[0];
  1187. }
  1188. # Check if we reach the end.
  1189. if (isset($parts[1])) {
  1190. $output .= $this->handleSpanToken($parts[1], $parts[2]);
  1191. $str = $parts[2];
  1192. }
  1193. else {
  1194. break;
  1195. }
  1196. }
  1197. return $output;
  1198. }
  1199. public function handleSpanToken($token, &$str) {
  1200. #
  1201. # Handle $token provided by parseSpan by determining its nature and
  1202. # returning the corresponding value that should replace it.
  1203. #
  1204. switch ($token{0}) {
  1205. case "\\":
  1206. return $this->hashPart("&#". ord($token{1}). ";");
  1207. case "`":
  1208. # Search for end marker in remaining text.
  1209. if (preg_match('/^(.*?[^`])'.preg_quote($token).'(?!`)(.*)$/sm',
  1210. $str, $matches))
  1211. {
  1212. $str = $matches[2];
  1213. $codespan = $this->makeCodeSpan($matches[1]);
  1214. return $this->hashPart($codespan);
  1215. }
  1216. return $token; // return as text since no ending marker found.
  1217. default:
  1218. return $this->hashPart($token);
  1219. }
  1220. }
  1221. public function outdent($text) {
  1222. #
  1223. # Remove one level of line-leading tabs or spaces
  1224. #
  1225. return preg_replace('/^(\t|[ ]{1,'.$this->tab_width.'})/m', '', $text);
  1226. }
  1227. # String length function for detab. `_initDetab` will create a function to
  1228. # hanlde UTF-8 if the default function does not exist.
  1229. public $utf8_strlen = 'mb_strlen';
  1230. public function detab($text) {
  1231. #
  1232. # Replace tabs with the appropriate amount of space.
  1233. #
  1234. # For each line we separate the line in blocks delemited by
  1235. # tab characters. Then we reconstruct every line by adding the
  1236. # appropriate number of space between each blocks.
  1237. $text = preg_replace_callback('/^.*\t.*$/m',
  1238. array(&$this, '_detab_callback'), $text);
  1239. return $text;
  1240. }
  1241. public function _detab_callback($matches) {
  1242. $line = $matches[0];
  1243. $strlen = $this->utf8_strlen; # strlen function for UTF-8.
  1244. # Split in blocks.
  1245. $blocks = explode("\t", $line);
  1246. # Add each blocks to the line.
  1247. $line = $blocks[0];
  1248. unset($blocks[0]); # Do not add first block twice.
  1249. foreach ($blocks as $block) {
  1250. # Calculate amount of space, insert spaces, insert block.
  1251. $amount = $this->tab_width -
  1252. $strlen($line, 'UTF-8') % $this->tab_width;
  1253. $line .= str_repeat(" ", $amount) . $block;
  1254. }
  1255. return $line;
  1256. }
  1257. public function _initDetab() {
  1258. #
  1259. # Check for the availability of the function in the `utf8_strlen` property
  1260. # (initially `mb_strlen`). If the function is not available, create a
  1261. # function that will loosely count the number of UTF-8 characters with a
  1262. # regular expression.
  1263. #
  1264. if (function_exists($this->utf8_strlen)) return;
  1265. $this->utf8_strlen = create_function('$text', 'return preg_match_all(
  1266. "/[\\\\x00-\\\\xBF]|[\\\\xC0-\\\\xFF][\\\\x80-\\\\xBF]*/",
  1267. $text, $m);');
  1268. }
  1269. public function unhash($text) {
  1270. #
  1271. # Swap back in all the tags hashed by _HashHTMLBlocks.
  1272. #
  1273. return preg_replace_callback('/(.)\x1A[0-9]+\1/',
  1274. array(&$this, '_unhash_callback'), $text);
  1275. }
  1276. public function _unhash_callback($matches) {
  1277. return $this->html_hashes[$matches[0]];
  1278. }
  1279. }
  1280. #
  1281. # Markdown Extra Parser Class
  1282. #
  1283. class MarkdownExtra_Parser extends Markdown_Parser {
  1284. # Prefix for footnote ids.
  1285. public $fn_id_prefix = "";
  1286. # Optional title attribute for footnote links and backlinks.
  1287. public $fn_link_title = '';
  1288. public $fn_backlink_title = '';
  1289. # Optional class attribute for footnote links and backlinks.
  1290. public $fn_link_class = '';
  1291. public $fn_backlink_class = '';
  1292. # Predefined abbreviations.
  1293. public $predef_abbr = array();
  1294. public function __construct() {
  1295. #
  1296. # Constructor function. Initialize the parser object.
  1297. #
  1298. # Add extra escapable characters before parent constructor
  1299. # initialize the table.
  1300. $this->escape_chars .= ':|';
  1301. # Insert extra document, block, and span transformations.
  1302. # Parent constructor will do the sorting.
  1303. $this->document_gamut += array(
  1304. "doFencedCodeBlocks" => 5,
  1305. "stripFootnotes" => 15,
  1306. "stripAbbreviations" => 25,
  1307. "appendFootnotes" => 50,
  1308. );
  1309. $this->block_gamut += array(
  1310. "doFencedCodeBlocks" => 5,
  1311. "doTables" => 15,
  1312. "doDefLists" => 45,
  1313. );
  1314. $this->span_gamut += array(
  1315. "doFootnotes" => 5,
  1316. "doAbbreviations" => 70,
  1317. );
  1318. parent::__construct();
  1319. }
  1320. # Extra variables used during extra transformations.
  1321. public $footnotes = array();
  1322. public $footnotes_ordered = array();
  1323. public $abbr_desciptions = array();
  1324. public $abbr_word_re = '';
  1325. # Give the current footnote number.
  1326. public $footnote_counter = 1;
  1327. public function setup() {
  1328. #
  1329. # Setting up Extra-specific variables.
  1330. #
  1331. parent::setup();
  1332. $this->footnotes = array();
  1333. $this->footnotes_ordered = array();
  1334. $this->abbr_desciptions = array();
  1335. $this->abbr_word_re = '';
  1336. $this->footnote_counter = 1;
  1337. foreach ($this->predef_abbr as $abbr_word => $abbr_desc) {
  1338. if ($this->abbr_word_re)
  1339. $this->abbr_word_re .= '|';
  1340. $this->abbr_word_re .= preg_quote($abbr_word);
  1341. $this->abbr_desciptions[$abbr_word] = trim($abbr_desc);
  1342. }
  1343. }
  1344. public function teardown() {
  1345. #
  1346. # Clearing Extra-specific variables.
  1347. #
  1348. $this->footnotes = array();
  1349. $this->footnotes_ordered = array();
  1350. $this->abbr_desciptions = array();
  1351. $this->abbr_word_re = '';
  1352. parent::teardown();
  1353. }
  1354. ### HTML Block Parser ###
  1355. # Tags that are always treated as block tags:
  1356. public $block_tags_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend';
  1357. # Tags treated as block tags only if the opening tag is alone on it's line:
  1358. public $context_block_tags_re = 'script|noscript|math|ins|del';
  1359. # Tags where markdown="1" default to span mode:
  1360. public $contain_span_tags_re = 'p|h[1-6]|li|dd|dt|td|th|legend|address';
  1361. # Tags which must not have their contents modified, no matter where
  1362. # they appear:
  1363. public $clean_tags_re = 'script|math';
  1364. # Tags that do not need to be closed.
  1365. public $auto_close_tags_re = 'hr|img';
  1366. public function hashHTMLBlocks($text) {
  1367. #
  1368. # Hashify HTML Blocks and "clean tags".
  1369. #
  1370. # We only want to do this for block-level HTML tags, such as headers,
  1371. # lists, and tables. That's because we still want to wrap <p>s around
  1372. # "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  1373. # phrase emphasis, and spans. The list of tags we're looking for is
  1374. # hard-coded.
  1375. #
  1376. # This works by calling _HashHTMLBlocks_InMarkdown, which then calls
  1377. # _HashHTMLBlocks_InHTML when it encounter block tags. When the markdown="1"
  1378. # attribute is found whitin a tag, _HashHTMLBlocks_InHTML calls back
  1379. # _HashHTMLBlocks_InMarkdown to handle the Markdown syntax within the tag.
  1380. # These two functions are calling each other. It's recursive!
  1381. #
  1382. #
  1383. # Call the HTML-in-Markdown hasher.
  1384. #
  1385. list($text, ) = $this->_hashHTMLBlocks_inMarkdown($text);
  1386. return $text;
  1387. }
  1388. public function _hashHTMLBlocks_inMarkdown($text, $indent = 0,
  1389. $enclosing_tag_re = '', $span = false)
  1390. {
  1391. #
  1392. # Parse markdown text, calling _HashHTMLBlocks_InHTML for block tags.
  1393. #
  1394. # * $indent is the number of space to be ignored when checking for code
  1395. # blocks. This is important because if we don't take the indent into
  1396. # account, something like this (which looks right) won't work as expected:
  1397. #
  1398. # <div>
  1399. # <div markdown="1">
  1400. # Hello World. <-- Is this a Markdown code block or text?
  1401. # </div> <-- Is this a Markdown code block or a real tag?
  1402. # <div>
  1403. #
  1404. # If you don't like this, just don't indent the tag on which
  1405. # you apply the markdown="1" attribute.
  1406. #
  1407. # * If $enclosing_tag_re is not empty, stops at the first unmatched closing
  1408. # tag with that name. Nested tags supported.
  1409. #
  1410. # * If $span is true, text inside must treated as span. So any double
  1411. # newline will be replaced by a single newline so that it does not create
  1412. # paragraphs.
  1413. #
  1414. # Returns an array of that form: ( processed text , remaining text )
  1415. #
  1416. if ($text === '') return array('', '');
  1417. # Regex to check for the presense of newlines around a block tag.
  1418. $newline_before_re = '/(?:^\n?|\n\n)*$/';
  1419. $newline_after_re =
  1420. '{
  1421. ^ # Start of text following the tag.
  1422. (?>[ ]*<!--.*?-->)? # Optional comment.
  1423. [ ]*\n # Must be followed by newline.
  1424. }xs';
  1425. # Regex to match any tag.
  1426. $block_tag_re =
  1427. '{
  1428. ( # $2: Capture hole tag.
  1429. </? # Any opening or closing tag.
  1430. (?> # Tag name.
  1431. '.$this->block_tags_re.' |
  1432. '.$this->context_block_tags_re.' |
  1433. '.$this->clean_tags_re.' |
  1434. (?!\s)'.$enclosing_tag_re.'
  1435. )
  1436. (?:
  1437. (?=[\s"\'/a-zA-Z0-9]) # Allowed characters after tag name.
  1438. (?>
  1439. ".*?" | # Double quotes (can contain `>`)
  1440. \'.*?\' | # Single quotes (can contain `>`)
  1441. .+? # Anything but quotes and `>`.
  1442. )*?
  1443. )?
  1444. > # End of tag.
  1445. |
  1446. <!-- .*? --> # HTML Comment
  1447. |
  1448. <\?.*?\?> | <%.*?%> # Processing instruction
  1449. |
  1450. <!\[CDATA\[.*?\]\]> # CData Block
  1451. |
  1452. # Code span marker
  1453. `+
  1454. '. ( !$span ? ' # If not in span.
  1455. |
  1456. # Indented code block
  1457. (?: ^[ ]*\n | ^ | \n[ ]*\n )
  1458. [ ]{'.($indent+4).'}[^\n]* \n
  1459. (?>
  1460. (?: [ ]{'.($indent+4).'}[^\n]* | [ ]* ) \n
  1461. )*
  1462. |
  1463. # Fenced code block marker
  1464. (?> ^ | \n )
  1465. [ ]{0,'.($indent).'}~~~+[ ]*\n
  1466. ' : '' ). ' # End (if not is span).
  1467. )
  1468. }xs';
  1469. $depth = 0; # Current depth inside the tag tree.
  1470. $parsed = ""; # Parsed text that will be returned.
  1471. #
  1472. # Loop through every tag until we find the closing tag of the parent
  1473. # or loop until reaching the end of text if no parent tag specified.
  1474. #
  1475. do {
  1476. #
  1477. # Split the text using the first $tag_match pattern found.
  1478. # Text before pattern will be first in the array, text after
  1479. # pattern will be at the end, and between will be any catches made
  1480. # by the pattern.
  1481. #
  1482. $parts = preg_split($block_tag_re, $text, 2,
  1483. PREG_SPLIT_DELIM_CAPTURE);
  1484. # If in Markdown span mode, add a empty-string span-level hash
  1485. # after each newline to prevent triggering any block element.
  1486. if ($span) {
  1487. $void = $this->hashPart("", ':');
  1488. $newline = "$void\n";
  1489. $parts[0] = $void . str_replace("\n", $newline, $parts[0]) . $void;
  1490. }
  1491. $parsed .= $parts[0]; # Text before current tag.
  1492. # If end of $text has been reached. Stop loop.
  1493. if (count($parts) < 3) {
  1494. $text = "";
  1495. break;
  1496. }
  1497. $tag = $parts[1]; # Tag to handle.
  1498. $text = $parts[2]; # Remaining text after current tag.
  1499. $tag_re = preg_quote($tag); # For use in a regular expression.
  1500. #
  1501. # Check for: Code span marker
  1502. #
  1503. if ($tag{0} == "`") {
  1504. # Find corresponding end marker.
  1505. $tag_re = preg_quote($tag);
  1506. if (preg_match('{^(?>.+?|\n(?!\n))*?(?<!`)'.$tag_re.'(?!`)}',
  1507. $text, $matches))
  1508. {
  1509. # End marker found: pass text unchanged until marker.
  1510. $parsed .= $tag . $matches[0];
  1511. $text = substr($text, strlen($matches[0]));
  1512. }
  1513. else {
  1514. # Unmatched marker: just skip it.
  1515. $parsed .= $tag;
  1516. }
  1517. }
  1518. #
  1519. # Check for: Fenced code block marker.
  1520. #
  1521. else if (preg_match('{^\n?[ ]{0,'.($indent+3).'}~}', $tag)) {
  1522. # Fenced code block marker: find matching end marker.
  1523. $tag_re = preg_quote(trim($tag));
  1524. if (preg_match('{^(?>.*\n)+?[ ]{0,'.($indent).'}'.$tag_re.'[ ]*\n}', $text,
  1525. $matches))
  1526. {
  1527. # End marker found: pass text unchanged until marker.
  1528. $parsed .= $tag . $matches[0];
  1529. $text = substr($text, strlen($matches[0]));
  1530. }
  1531. else {
  1532. # No end marker: just skip it.
  1533. $parsed .= $tag;
  1534. }
  1535. }
  1536. #
  1537. # Check for: Indented code block.
  1538. #
  1539. else if ($tag{0} == "\n" || $tag{0} == " ") {
  1540. # Indented code block: pass it unchanged, will be handled
  1541. # later.
  1542. $parsed .= $tag;
  1543. }
  1544. #
  1545. # Check for: Opening Block level tag or
  1546. # Opening Context Block tag (like ins and del)
  1547. # used as a block tag (tag is alone on it's line).
  1548. #
  1549. else if (preg_match('{^<(?:'.$this->block_tags_re.')\b}', $tag) ||
  1550. ( preg_match('{^<(?:'.$this->context_block_tags_re.')\b}', $tag) &&
  1551. preg_match($newline_before_re, $parsed) &&
  1552. preg_match($newline_after_re, $text) )
  1553. )
  1554. {
  1555. # Need to parse tag and following text using the HTML parser.
  1556. list($block_text, $text) =
  1557. $this->_hashHTMLBlocks_inHTML($tag . $text, "hashBlock", true);
  1558. # Make sure it stays outside of any paragraph by adding newlines.
  1559. $parsed .= "\n\n$b…

Large files files are truncated, but you can click here to view the full file