PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/vendors/markdown/markdown.php

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