PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Markdown.php

https://github.com/MHordecki/milionkostek
PHP | 2789 lines | 1608 code | 364 blank | 817 comment | 134 complexity | 897c99061ba8b17bc0b39a8f64382ca8 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-2007 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. define( 'MARKDOWN_VERSION', "1.0.1h" ); # Fri 3 Aug 2007
  14. define( 'MARKDOWNEXTRA_VERSION', "1.1.4" ); # Fri 3 Aug 2007
  15. #
  16. # Global default settings:
  17. #
  18. # Change to ">" for HTML output
  19. define( 'MARKDOWN_EMPTY_ELEMENT_SUFFIX', " />");
  20. # Define the width of a tab for code blocks.
  21. define( 'MARKDOWN_TAB_WIDTH', 4 );
  22. # Optional title attribute for footnote links and backlinks.
  23. define( 'MARKDOWN_FN_LINK_TITLE', "" );
  24. define( 'MARKDOWN_FN_BACKLINK_TITLE', "" );
  25. # Optional class attribute for footnote links and backlinks.
  26. define( 'MARKDOWN_FN_LINK_CLASS', "" );
  27. define( 'MARKDOWN_FN_BACKLINK_CLASS', "" );
  28. #
  29. # WordPress settings:
  30. #
  31. # Change to false to remove Markdown from posts and/or comments.
  32. define( 'MARKDOWN_WP_POSTS', true );
  33. define( 'MARKDOWN_WP_COMMENTS', true );
  34. ### Standard Function Interface ###
  35. define( 'MARKDOWN_PARSER_CLASS', 'MarkdownExtra_Parser' );
  36. function Markdown($text) {
  37. #
  38. # Initialize the parser and return the result of its transform method.
  39. #
  40. # Setup static parser variable.
  41. static $parser;
  42. if (!isset($parser)) {
  43. $parser_class = MARKDOWN_PARSER_CLASS;
  44. $parser = new $parser_class;
  45. }
  46. # Transform text using parser.
  47. return $parser->transform($text);
  48. }
  49. ### WordPress Plugin Interface ###
  50. /*
  51. Plugin Name: Markdown Extra
  52. Plugin URI: http://www.michelf.com/projects/php-markdown/
  53. Description: <a href="http://daringfireball.net/projects/markdown/syntax">Markdown syntax</a> allows you to write using an easy-to-read, easy-to-write plain text format. Based on the original Perl version by <a href="http://daringfireball.net/">John Gruber</a>. <a href="http://www.michelf.com/projects/php-markdown/">More...</a>
  54. Version: 1.1.4
  55. Author: Michel Fortin
  56. Author URI: http://www.michelf.com/
  57. */
  58. if (isset($wp_version)) {
  59. # More details about how it works here:
  60. # <http://www.michelf.com/weblog/2005/wordpress-text-flow-vs-markdown/>
  61. # Post content and excerpts
  62. # - Remove WordPress paragraph generator.
  63. # - Run Markdown on excerpt, then remove all tags.
  64. # - Add paragraph tag around the excerpt, but remove it for the excerpt rss.
  65. if (MARKDOWN_WP_POSTS) {
  66. remove_filter('the_content', 'wpautop');
  67. remove_filter('the_content_rss', 'wpautop');
  68. remove_filter('the_excerpt', 'wpautop');
  69. add_filter('the_content', 'Markdown', 6);
  70. add_filter('the_content_rss', 'Markdown', 6);
  71. add_filter('get_the_excerpt', 'Markdown', 6);
  72. add_filter('get_the_excerpt', 'trim', 7);
  73. add_filter('the_excerpt', 'mdwp_add_p');
  74. add_filter('the_excerpt_rss', 'mdwp_strip_p');
  75. remove_filter('content_save_pre', 'balanceTags', 50);
  76. remove_filter('excerpt_save_pre', 'balanceTags', 50);
  77. add_filter('the_content', 'balanceTags', 50);
  78. add_filter('get_the_excerpt', 'balanceTags', 9);
  79. }
  80. # Comments
  81. # - Remove WordPress paragraph generator.
  82. # - Remove WordPress auto-link generator.
  83. # - Scramble important tags before passing them to the kses filter.
  84. # - Run Markdown on excerpt then remove paragraph tags.
  85. if (MARKDOWN_WP_COMMENTS) {
  86. remove_filter('comment_text', 'wpautop', 30);
  87. remove_filter('comment_text', 'make_clickable');
  88. add_filter('pre_comment_content', 'Markdown', 6);
  89. add_filter('pre_comment_content', 'mdwp_hide_tags', 8);
  90. add_filter('pre_comment_content', 'mdwp_show_tags', 12);
  91. add_filter('get_comment_text', 'Markdown', 6);
  92. add_filter('get_comment_excerpt', 'Markdown', 6);
  93. add_filter('get_comment_excerpt', 'mdwp_strip_p', 7);
  94. global $mdwp_hidden_tags, $mdwp_placeholders;
  95. $mdwp_hidden_tags = explode(' ',
  96. '<p> </p> <pre> </pre> <ol> </ol> <ul> </ul> <li> </li>');
  97. $mdwp_placeholders = explode(' ', str_rot13(
  98. 'pEj07ZbbBZ U1kqgh4w4p pre2zmeN6K QTi31t9pre ol0MP1jzJR '.
  99. 'ML5IjmbRol ulANi1NsGY J7zRLJqPul liA8ctl16T K9nhooUHli'));
  100. }
  101. function mdwp_add_p($text) {
  102. if (!preg_match('{^$|^<(p|ul|ol|dl|pre|blockquote)>}i', $text)) {
  103. $text = '<p>'.$text.'</p>';
  104. $text = preg_replace('{\n{2,}}', "</p>\n\n<p>", $text);
  105. }
  106. return $text;
  107. }
  108. function mdwp_strip_p($t) { return preg_replace('{</?p>}i', '', $t); }
  109. function mdwp_hide_tags($text) {
  110. global $mdwp_hidden_tags, $mdwp_placeholders;
  111. return str_replace($mdwp_hidden_tags, $mdwp_placeholders, $text);
  112. }
  113. function mdwp_show_tags($text) {
  114. global $mdwp_hidden_tags, $mdwp_placeholders;
  115. return str_replace($mdwp_placeholders, $mdwp_hidden_tags, $text);
  116. }
  117. }
  118. ### bBlog Plugin Info ###
  119. function identify_modifier_markdown() {
  120. return array(
  121. 'name' => 'markdown',
  122. 'type' => 'modifier',
  123. 'nicename' => 'PHP Markdown Extra',
  124. 'description' => 'A text-to-HTML conversion tool for web writers',
  125. 'authors' => 'Michel Fortin and John Gruber',
  126. 'licence' => 'GPL',
  127. 'version' => MARKDOWNEXTRA_VERSION,
  128. 'help' => '<a href="http://daringfireball.net/projects/markdown/syntax">Markdown syntax</a> allows you to write using an easy-to-read, easy-to-write plain text format. Based on the original Perl version by <a href="http://daringfireball.net/">John Gruber</a>. <a href="http://www.michelf.com/projects/php-markdown/">More...</a>',
  129. );
  130. }
  131. ### Smarty Modifier Interface ###
  132. function smarty_modifier_markdown($text) {
  133. return Markdown($text);
  134. }
  135. ### Textile Compatibility Mode ###
  136. # Rename this file to "classTextile.php" and it can replace Textile everywhere.
  137. if (strcasecmp(substr(__FILE__, -16), "classTextile.php") == 0) {
  138. # Try to include PHP SmartyPants. Should be in the same directory.
  139. @include_once 'smartypants.php';
  140. # Fake Textile class. It calls Markdown instead.
  141. class Textile {
  142. function TextileThis($text, $lite='', $encode='') {
  143. if ($lite == '' && $encode == '') $text = Markdown($text);
  144. if (function_exists('SmartyPants')) $text = SmartyPants($text);
  145. return $text;
  146. }
  147. # Fake restricted version: restrictions are not supported for now.
  148. function TextileRestricted($text, $lite='', $noimage='') {
  149. return $this->TextileThis($text, $lite);
  150. }
  151. # Workaround to ensure compatibility with TextPattern 4.0.3.
  152. function blockLite($text) { return $text; }
  153. }
  154. }
  155. #
  156. # Markdown Parser Class
  157. #
  158. class Markdown_Parser {
  159. # Regex to match balanced [brackets].
  160. # Needed to insert a maximum bracked depth while converting to PHP.
  161. var $nested_brackets_depth = 6;
  162. var $nested_brackets;
  163. var $nested_url_parenthesis_depth = 4;
  164. var $nested_url_parenthesis;
  165. # Table of hash values for escaped characters:
  166. var $escape_chars = '\`*_{}[]()>#+-.!';
  167. // var $escape_table = array();
  168. var $backslash_escape_table = array();
  169. # Change to ">" for HTML output.
  170. var $empty_element_suffix = MARKDOWN_EMPTY_ELEMENT_SUFFIX;
  171. var $tab_width = MARKDOWN_TAB_WIDTH;
  172. # Change to `true` to disallow markup or entities.
  173. var $no_markup = false;
  174. var $no_entities = false;
  175. function Markdown_Parser() {
  176. #
  177. # Constructor function. Initialize appropriate member variables.
  178. #
  179. $this->_initDetab();
  180. $this->nested_brackets =
  181. str_repeat('(?>[^\[\]]+|\[', $this->nested_brackets_depth).
  182. str_repeat('\])*', $this->nested_brackets_depth);
  183. $this->nested_url_parenthesis =
  184. str_repeat('(?>[^()\s]+|\(', $this->nested_url_parenthesis_depth).
  185. str_repeat('(?>\)))*', $this->nested_url_parenthesis_depth);
  186. # Create an identical table but for escaped characters.
  187. foreach (preg_split('/(?!^|$)/', $this->escape_chars) as $char) {
  188. $entity = "&#". ord($char). ";";
  189. // $this->escape_table[$char] = $entity;
  190. $this->backslash_escape_table["\\$char"] = $entity;
  191. }
  192. # Sort document, block, and span gamut in ascendent priority order.
  193. asort($this->document_gamut);
  194. asort($this->block_gamut);
  195. asort($this->span_gamut);
  196. }
  197. # Internal hashes used during transformation.
  198. var $urls = array();
  199. var $titles = array();
  200. var $html_blocks = array();
  201. var $html_hashes = array(); # Contains both blocks and span hashes.
  202. # Status flag to avoid invalid nesting.
  203. var $in_anchor = false;
  204. function transform($text) {
  205. #
  206. # Main function. The order in which other subs are called here is
  207. # essential. Link and image substitutions need to happen before
  208. # _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a>
  209. # and <img> tags get encoded.
  210. #
  211. # Clear the global hashes. If we don't clear these, you get conflicts
  212. # from other articles when generating a page which contains more than
  213. # one article (e.g. an index page that shows the N most recent
  214. # articles):
  215. $this->urls = array();
  216. $this->titles = array();
  217. $this->html_blocks = array();
  218. $this->html_hashes = array();
  219. # Standardize line endings:
  220. # DOS to Unix and Mac to Unix
  221. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  222. # Make sure $text ends with a couple of newlines:
  223. $text .= "\n\n";
  224. # Convert all tabs to spaces.
  225. $text = $this->detab($text);
  226. # Turn block-level HTML blocks into hash entries
  227. $text = $this->hashHTMLBlocks($text);
  228. # Strip any lines consisting only of spaces and tabs.
  229. # This makes subsequent regexen easier to write, because we can
  230. # match consecutive blank lines with /\n+/ instead of something
  231. # contorted like /[ ]*\n+/ .
  232. $text = preg_replace('/^[ ]+$/m', '', $text);
  233. # Run document gamut methods.
  234. foreach ($this->document_gamut as $method => $priority) {
  235. $text = $this->$method($text);
  236. }
  237. return $text . "\n";
  238. }
  239. var $document_gamut = array(
  240. # Strip link definitions, store in hashes.
  241. "stripLinkDefinitions" => 20,
  242. "runBasicBlockGamut" => 30,
  243. );
  244. function stripLinkDefinitions($text) {
  245. #
  246. # Strips link definitions from text, stores the URLs and titles in
  247. # hash references.
  248. #
  249. $less_than_tab = $this->tab_width - 1;
  250. # Link defs are in the form: ^[id]: url "optional title"
  251. $text = preg_replace_callback('{
  252. ^[ ]{0,'.$less_than_tab.'}\[(.+)\][ ]?: # id = $1
  253. [ ]*
  254. \n? # maybe *one* newline
  255. [ ]*
  256. <?(\S+?)>? # url = $2
  257. [ ]*
  258. \n? # maybe one newline
  259. [ ]*
  260. (?:
  261. (?<=\s) # lookbehind for whitespace
  262. ["(]
  263. (.*?) # title = $3
  264. [")]
  265. [ ]*
  266. )? # title is optional
  267. (?:\n+|\Z)
  268. }xm',
  269. array(&$this, '_stripLinkDefinitions_callback'),
  270. $text);
  271. return $text;
  272. }
  273. function _stripLinkDefinitions_callback($matches) {
  274. $link_id = strtolower($matches[1]);
  275. $this->urls[$link_id] = $this->encodeAmpsAndAngles($matches[2]);
  276. if (isset($matches[3]))
  277. $this->titles[$link_id] = str_replace('"', '&quot;', $matches[3]);
  278. return ''; # String that will replace the block
  279. }
  280. function hashHTMLBlocks($text) {
  281. if ($this->no_markup) return $text;
  282. $less_than_tab = $this->tab_width - 1;
  283. # Hashify HTML blocks:
  284. # We only want to do this for block-level HTML tags, such as headers,
  285. # lists, and tables. That's because we still want to wrap <p>s around
  286. # "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  287. # phrase emphasis, and spans. The list of tags we're looking for is
  288. # hard-coded:
  289. $block_tags_a = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|'.
  290. 'script|noscript|form|fieldset|iframe|math|ins|del';
  291. $block_tags_b = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|'.
  292. 'script|noscript|form|fieldset|iframe|math';
  293. # Regular expression for the content of a block tag.
  294. $nested_tags_level = 4;
  295. $attr = '
  296. (?> # optional tag attributes
  297. \s # starts with whitespace
  298. (?>
  299. [^>"/]+ # text outside quotes
  300. |
  301. /+(?!>) # slash not followed by ">"
  302. |
  303. "[^"]*" # text inside double quotes (tolerate ">")
  304. |
  305. \'[^\']*\' # text inside single quotes (tolerate ">")
  306. )*
  307. )?
  308. ';
  309. $content =
  310. str_repeat('
  311. (?>
  312. [^<]+ # content without tag
  313. |
  314. <\2 # nested opening tag
  315. '.$attr.' # attributes
  316. (?:
  317. />
  318. |
  319. >', $nested_tags_level). # end of opening tag
  320. '.*?'. # last level nested tag content
  321. str_repeat('
  322. </\2\s*> # closing nested tag
  323. )
  324. |
  325. <(?!/\2\s*> # other tags with a different name
  326. )
  327. )*',
  328. $nested_tags_level);
  329. # First, look for nested blocks, e.g.:
  330. # <div>
  331. # <div>
  332. # tags for inner block must be indented.
  333. # </div>
  334. # </div>
  335. #
  336. # The outermost tags must start at the left margin for this to match, and
  337. # the inner nested divs must be indented.
  338. # We need to do this before the next, more liberal match, because the next
  339. # match will start at the first `<div>` and stop at the first `</div>`.
  340. $text = preg_replace_callback('{
  341. ( # save in $1
  342. ^ # start of line (with /m)
  343. <('.$block_tags_a.')# start tag = $2
  344. '.$attr.'>\n # attributes followed by > and \n
  345. '.$content.' # content, support nesting
  346. </\2> # the matching end tag
  347. [ ]* # trailing spaces/tabs
  348. (?=\n+|\Z) # followed by a newline or end of document
  349. )
  350. }xmi',
  351. array(&$this, '_hashHTMLBlocks_callback'),
  352. $text);
  353. #
  354. # Match from `\n<tag>` to `</tag>\n`, handling nested tags in between.
  355. #
  356. $text = preg_replace_callback('{
  357. ( # save in $1
  358. ^ # start of line (with /m)
  359. <('.$block_tags_b.')# start tag = $2
  360. '.$attr.'> # attributes followed by >
  361. '.$content.' # content, support nesting
  362. </\2> # the matching end tag
  363. [ ]* # trailing spaces/tabs
  364. (?=\n+|\Z) # followed by a newline or end of document
  365. )
  366. }xmi',
  367. array(&$this, '_hashHTMLBlocks_callback'),
  368. $text);
  369. # Special case just for <hr />. It was easier to make a special case than
  370. # to make the other regex more complicated.
  371. $text = preg_replace_callback('{
  372. (?:
  373. (?<=\n\n) # Starting after a blank line
  374. | # or
  375. \A\n? # the beginning of the doc
  376. )
  377. ( # save in $1
  378. [ ]{0,'.$less_than_tab.'}
  379. <(hr) # start tag = $2
  380. \b # word break
  381. ([^<>])*? #
  382. /?> # the matching end tag
  383. [ ]*
  384. (?=\n{2,}|\Z) # followed by a blank line or end of document
  385. )
  386. }xi',
  387. array(&$this, '_hashHTMLBlocks_callback'),
  388. $text);
  389. # Special case for standalone HTML comments:
  390. $text = preg_replace_callback('{
  391. (?:
  392. (?<=\n\n) # Starting after a blank line
  393. | # or
  394. \A\n? # the beginning of the doc
  395. )
  396. ( # save in $1
  397. [ ]{0,'.$less_than_tab.'}
  398. (?s:
  399. <!-- .*? -->
  400. )
  401. [ ]*
  402. (?=\n{2,}|\Z) # followed by a blank line or end of document
  403. )
  404. }x',
  405. array(&$this, '_hashHTMLBlocks_callback'),
  406. $text);
  407. # PHP and ASP-style processor instructions (<? and <%)
  408. $text = preg_replace_callback('{
  409. (?:
  410. (?<=\n\n) # Starting after a blank line
  411. | # or
  412. \A\n? # the beginning of the doc
  413. )
  414. ( # save in $1
  415. [ ]{0,'.$less_than_tab.'}
  416. (?s:
  417. <([?%]) # $2
  418. .*?
  419. \2>
  420. )
  421. [ ]*
  422. (?=\n{2,}|\Z) # followed by a blank line or end of document
  423. )
  424. }x',
  425. array(&$this, '_hashHTMLBlocks_callback'),
  426. $text);
  427. return $text;
  428. }
  429. function _hashHTMLBlocks_callback($matches) {
  430. $text = $matches[1];
  431. $key = $this->hashBlock($text);
  432. return "\n\n$key\n\n";
  433. }
  434. function hashBlock($text) {
  435. #
  436. # Called whenever a tag must be hashed when a function insert a block-level
  437. # tag in $text, it pass through this function and is automaticaly escaped,
  438. # which remove the need to call _HashHTMLBlocks at every step.
  439. #
  440. # Swap back any tag hash found in $text so we do not have to `unhash`
  441. # multiple times at the end.
  442. $text = $this->unhash($text);
  443. # Then hash the block.
  444. $key = "B\x1A". md5($text);
  445. $this->html_hashes[$key] = $text;
  446. $this->html_blocks[$key] = $text;
  447. return $key; # String that will replace the tag.
  448. }
  449. function hashSpan($text, $word_separator = false) {
  450. #
  451. # Called whenever a tag must be hashed when a function insert a span-level
  452. # element in $text, it pass through this function and is automaticaly
  453. # escaped, blocking invalid nested overlap. If optional argument
  454. # $word_separator is true, surround the hash value by spaces.
  455. #
  456. # Swap back any tag hash found in $text so we do not have to `unhash`
  457. # multiple times at the end.
  458. $text = $this->unhash($text);
  459. # Then hash the span.
  460. $key = "S\x1A". md5($text);
  461. if ($word_separator) $key = ":$key:";
  462. $this->html_hashes[$key] = $text;
  463. return $key; # String that will replace the span tag.
  464. }
  465. var $block_gamut = array(
  466. #
  467. # These are all the transformations that form block-level
  468. # tags like paragraphs, headers, and list items.
  469. #
  470. "doHeaders" => 10,
  471. "doHorizontalRules" => 20,
  472. "doLists" => 40,
  473. "doCodeBlocks" => 50,
  474. "doBlockQuotes" => 60,
  475. );
  476. function runBlockGamut($text) {
  477. #
  478. # Run block gamut tranformations.
  479. #
  480. # We need to escape raw HTML in Markdown source before doing anything
  481. # else. This need to be done for each block, and not only at the
  482. # begining in the Markdown function since hashed blocks can be part of
  483. # list items and could have been indented. Indented blocks would have
  484. # been seen as a code block in a previous pass of hashHTMLBlocks.
  485. $text = $this->hashHTMLBlocks($text);
  486. return $this->runBasicBlockGamut($text);
  487. }
  488. function runBasicBlockGamut($text) {
  489. #
  490. # Run block gamut tranformations, without hashing HTML blocks. This is
  491. # useful when HTML blocks are known to be already hashed, like in the first
  492. # whole-document pass.
  493. #
  494. foreach ($this->block_gamut as $method => $priority) {
  495. $text = $this->$method($text);
  496. }
  497. # Finally form paragraph and restore hashed blocks.
  498. $text = $this->formParagraphs($text);
  499. return $text;
  500. }
  501. function doHorizontalRules($text) {
  502. # Do Horizontal Rules:
  503. return preg_replace(
  504. array('{^[ ]{0,2}([ ]?\*[ ]?){3,}[ ]*$}mx',
  505. '{^[ ]{0,2}([ ]? -[ ]?){3,}[ ]*$}mx',
  506. '{^[ ]{0,2}([ ]? _[ ]?){3,}[ ]*$}mx'),
  507. "\n".$this->hashBlock("<hr$this->empty_element_suffix")."\n",
  508. $text);
  509. }
  510. var $span_gamut = array(
  511. #
  512. # These are all the transformations that occur *within* block-level
  513. # tags like paragraphs, headers, and list items.
  514. #
  515. "escapeSpecialCharsWithinTagAttributes" => -20,
  516. "doCodeSpans" => -10,
  517. "encodeBackslashEscapes" => -5,
  518. # Process anchor and image tags. Images must come first,
  519. # because ![foo][f] looks like an anchor.
  520. "doImages" => 10,
  521. "doAnchors" => 20,
  522. # Make links out of things like `<http://example.com/>`
  523. # Must come after doAnchors, because you can use < and >
  524. # delimiters in inline links like [this](<url>).
  525. "doAutoLinks" => 30,
  526. "encodeAmpsAndAngles" => 40,
  527. "doItalicsAndBold" => 50,
  528. "doHardBreaks" => 60,
  529. );
  530. function runSpanGamut($text) {
  531. #
  532. # Run span gamut tranformations.
  533. #
  534. foreach ($this->span_gamut as $method => $priority) {
  535. $text = $this->$method($text);
  536. }
  537. return $text;
  538. }
  539. function doHardBreaks($text) {
  540. # Do hard breaks:
  541. $br_tag = $this->hashSpan("<br$this->empty_element_suffix\n");
  542. return preg_replace('/ {2,}\n/', $br_tag, $text);
  543. }
  544. function escapeSpecialCharsWithinTagAttributes($text) {
  545. #
  546. # Within tags -- meaning between < and > -- encode [\ ` * _] so they
  547. # don't conflict with their use in Markdown for code, italics and strong.
  548. # We're replacing each such character with its corresponding MD5 checksum
  549. # value; this is likely overkill, but it should prevent us from colliding
  550. # with the escape values by accident.
  551. #
  552. if ($this->no_markup) return $text;
  553. $tokens = $this->tokenizeHTML($text);
  554. $text = ''; # rebuild $text from the tokens
  555. foreach ($tokens as $cur_token) {
  556. if ($cur_token[0] == 'tag') {
  557. // $cur_token[1] = str_replace('\\', $this->escape_table['\\'], $cur_token[1]);
  558. // $cur_token[1] = str_replace('`', $this->escape_table['`'], $cur_token[1]);
  559. // $cur_token[1] = str_replace('*', $this->escape_table['*'], $cur_token[1]);
  560. // $cur_token[1] = str_replace('_', $this->escape_table['_'], $cur_token[1]);
  561. $cur_token[1] = $this->hashSpan($cur_token[1]);
  562. }
  563. $text .= $cur_token[1];
  564. }
  565. return $text;
  566. }
  567. function doAnchors($text) {
  568. #
  569. # Turn Markdown link shortcuts into XHTML <a> tags.
  570. #
  571. if ($this->in_anchor) return $text;
  572. $this->in_anchor = true;
  573. #
  574. # First, handle reference-style links: [link text] [id]
  575. #
  576. $text = preg_replace_callback('{
  577. ( # wrap whole match in $1
  578. \[
  579. ('.$this->nested_brackets.') # link text = $2
  580. \]
  581. [ ]? # one optional space
  582. (?:\n[ ]*)? # one optional newline followed by spaces
  583. \[
  584. (.*?) # id = $3
  585. \]
  586. )
  587. }xs',
  588. array(&$this, '_doAnchors_reference_callback'), $text);
  589. #
  590. # Next, inline-style links: [link text](url "optional title")
  591. #
  592. $text = preg_replace_callback('{
  593. ( # wrap whole match in $1
  594. \[
  595. ('.$this->nested_brackets.') # link text = $2
  596. \]
  597. \( # literal paren
  598. [ ]*
  599. (?:
  600. <(\S*)> # href = $3
  601. |
  602. ('.$this->nested_url_parenthesis.') # href = $4
  603. )
  604. [ ]*
  605. ( # $5
  606. ([\'"]) # quote char = $6
  607. (.*?) # Title = $7
  608. \6 # matching quote
  609. [ ]* # ignore any spaces/tabs between closing quote and )
  610. )? # title is optional
  611. \)
  612. )
  613. }xs',
  614. array(&$this, '_DoAnchors_inline_callback'), $text);
  615. #
  616. # Last, handle reference-style shortcuts: [link text]
  617. # These must come last in case you've also got [link test][1]
  618. # or [link test](/foo)
  619. #
  620. // $text = preg_replace_callback('{
  621. // ( # wrap whole match in $1
  622. // \[
  623. // ([^\[\]]+) # link text = $2; can\'t contain [ or ]
  624. // \]
  625. // )
  626. // }xs',
  627. // array(&$this, '_doAnchors_reference_callback'), $text);
  628. $this->in_anchor = false;
  629. return $text;
  630. }
  631. function _doAnchors_reference_callback($matches) {
  632. $whole_match = $matches[1];
  633. $link_text = $matches[2];
  634. $link_id =& $matches[3];
  635. if ($link_id == "") {
  636. # for shortcut links like [this][] or [this].
  637. $link_id = $link_text;
  638. }
  639. # lower-case and turn embedded newlines into spaces
  640. $link_id = strtolower($link_id);
  641. $link_id = preg_replace('{[ ]?\n}', ' ', $link_id);
  642. if (isset($this->urls[$link_id])) {
  643. $url = $this->urls[$link_id];
  644. $url = $this->encodeAmpsAndAngles($url);
  645. $result = "<a href=\"$url\"";
  646. if ( isset( $this->titles[$link_id] ) ) {
  647. $title = $this->titles[$link_id];
  648. $title = $this->encodeAmpsAndAngles($title);
  649. $result .= " title=\"$title\"";
  650. }
  651. $link_text = $this->runSpanGamut($link_text);
  652. $result .= ">$link_text</a>";
  653. $result = $this->hashSpan($result);
  654. }
  655. else {
  656. $result = $whole_match;
  657. }
  658. return $result;
  659. }
  660. function _doAnchors_inline_callback($matches) {
  661. $whole_match = $matches[1];
  662. $link_text = $this->runSpanGamut($matches[2]);
  663. $url = $matches[3] == '' ? $matches[4] : $matches[3];
  664. $title =& $matches[7];
  665. $url = $this->encodeAmpsAndAngles($url);
  666. $result = "<a href=\"$url\"";
  667. if (isset($title)) {
  668. $title = str_replace('"', '&quot;', $title);
  669. $title = $this->encodeAmpsAndAngles($title);
  670. $result .= " title=\"$title\"";
  671. }
  672. $link_text = $this->runSpanGamut($link_text);
  673. $result .= ">$link_text</a>";
  674. return $this->hashSpan($result);
  675. }
  676. function doImages($text) {
  677. #
  678. # Turn Markdown image shortcuts into <img> tags.
  679. #
  680. #
  681. # First, handle reference-style labeled images: ![alt text][id]
  682. #
  683. $text = preg_replace_callback('{
  684. ( # wrap whole match in $1
  685. !\[
  686. ('.$this->nested_brackets.') # alt text = $2
  687. \]
  688. [ ]? # one optional space
  689. (?:\n[ ]*)? # one optional newline followed by spaces
  690. \[
  691. (.*?) # id = $3
  692. \]
  693. )
  694. }xs',
  695. array(&$this, '_doImages_reference_callback'), $text);
  696. #
  697. # Next, handle inline images: ![alt text](url "optional title")
  698. # Don't forget: encode * and _
  699. #
  700. $text = preg_replace_callback('{
  701. ( # wrap whole match in $1
  702. !\[
  703. ('.$this->nested_brackets.') # alt text = $2
  704. \]
  705. \s? # One optional whitespace character
  706. \( # literal paren
  707. [ ]*
  708. (?:
  709. <(\S*)> # src url = $3
  710. |
  711. ('.$this->nested_url_parenthesis.') # src url = $4
  712. )
  713. [ ]*
  714. ( # $5
  715. ([\'"]) # quote char = $6
  716. (.*?) # title = $7
  717. \6 # matching quote
  718. [ ]*
  719. )? # title is optional
  720. \)
  721. )
  722. }xs',
  723. array(&$this, '_doImages_inline_callback'), $text);
  724. return $text;
  725. }
  726. function _doImages_reference_callback($matches) {
  727. $whole_match = $matches[1];
  728. $alt_text = $matches[2];
  729. $link_id = strtolower($matches[3]);
  730. if ($link_id == "") {
  731. $link_id = strtolower($alt_text); # for shortcut links like ![this][].
  732. }
  733. $alt_text = str_replace('"', '&quot;', $alt_text);
  734. if (isset($this->urls[$link_id])) {
  735. $url = $this->urls[$link_id];
  736. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  737. if (isset($this->titles[$link_id])) {
  738. $title = $this->titles[$link_id];
  739. $result .= " title=\"$title\"";
  740. }
  741. $result .= $this->empty_element_suffix;
  742. $result = $this->hashSpan($result);
  743. }
  744. else {
  745. # If there's no such link ID, leave intact:
  746. $result = $whole_match;
  747. }
  748. return $result;
  749. }
  750. function _doImages_inline_callback($matches) {
  751. $whole_match = $matches[1];
  752. $alt_text = $matches[2];
  753. $url = $matches[3] == '' ? $matches[4] : $matches[3];
  754. $title =& $matches[7];
  755. $alt_text = str_replace('"', '&quot;', $alt_text);
  756. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  757. if (isset($title)) {
  758. $title = str_replace('"', '&quot;', $title);
  759. $result .= " title=\"$title\""; # $title already quoted
  760. }
  761. $result .= $this->empty_element_suffix;
  762. return $this->hashSpan($result);
  763. }
  764. function doHeaders($text) {
  765. # Setext-style headers:
  766. # Header 1
  767. # ========
  768. #
  769. # Header 2
  770. # --------
  771. #
  772. $text = preg_replace_callback('{ ^(.+?)[ ]*\n=+[ ]*\n+ }mx',
  773. array(&$this, '_doHeaders_callback_setext_h1'), $text);
  774. $text = preg_replace_callback('{ ^(.+?)[ ]*\n-+[ ]*\n+ }mx',
  775. array(&$this, '_doHeaders_callback_setext_h2'), $text);
  776. # atx-style headers:
  777. # # Header 1
  778. # ## Header 2
  779. # ## Header 2 with closing hashes ##
  780. # ...
  781. # ###### Header 6
  782. #
  783. $text = preg_replace_callback('{
  784. ^(\#{1,6}) # $1 = string of #\'s
  785. [ ]*
  786. (.+?) # $2 = Header text
  787. [ ]*
  788. \#* # optional closing #\'s (not counted)
  789. \n+
  790. }xm',
  791. array(&$this, '_doHeaders_callback_atx'), $text);
  792. return $text;
  793. }
  794. function _doHeaders_callback_setext_h1($matches) {
  795. $block = "<h1>".$this->runSpanGamut($matches[1])."</h1>";
  796. return "\n" . $this->hashBlock($block) . "\n\n";
  797. }
  798. function _doHeaders_callback_setext_h2($matches) {
  799. $block = "<h2>".$this->runSpanGamut($matches[1])."</h2>";
  800. return "\n" . $this->hashBlock($block) . "\n\n";
  801. }
  802. function _doHeaders_callback_atx($matches) {
  803. $level = strlen($matches[1]);
  804. $block = "<h$level>".$this->runSpanGamut($matches[2])."</h$level>";
  805. return "\n" . $this->hashBlock($block) . "\n\n";
  806. }
  807. function doLists($text) {
  808. #
  809. # Form HTML ordered (numbered) and unordered (bulleted) lists.
  810. #
  811. $less_than_tab = $this->tab_width - 1;
  812. # Re-usable patterns to match list item bullets and number markers:
  813. $marker_ul = '[*+-]';
  814. $marker_ol = '\d+[.]';
  815. $marker_any = "(?:$marker_ul|$marker_ol)";
  816. $markers = array($marker_ul, $marker_ol);
  817. foreach ($markers as $marker) {
  818. # Re-usable pattern to match any entirel ul or ol list:
  819. $whole_list = '
  820. ( # $1 = whole list
  821. ( # $2
  822. [ ]{0,'.$less_than_tab.'}
  823. ('.$marker.') # $3 = first list item marker
  824. [ ]+
  825. )
  826. (?s:.+?)
  827. ( # $4
  828. \z
  829. |
  830. \n{2,}
  831. (?=\S)
  832. (?! # Negative lookahead for another list item marker
  833. [ ]*
  834. '.$marker.'[ ]+
  835. )
  836. )
  837. )
  838. '; // mx
  839. # We use a different prefix before nested lists than top-level lists.
  840. # See extended comment in _ProcessListItems().
  841. if ($this->list_level) {
  842. $text = preg_replace_callback('{
  843. ^
  844. '.$whole_list.'
  845. }mx',
  846. array(&$this, '_doLists_callback'), $text);
  847. }
  848. else {
  849. $text = preg_replace_callback('{
  850. (?:(?<=\n)\n|\A\n?) # Must eat the newline
  851. '.$whole_list.'
  852. }mx',
  853. array(&$this, '_doLists_callback'), $text);
  854. }
  855. }
  856. return $text;
  857. }
  858. function _doLists_callback($matches) {
  859. # Re-usable patterns to match list item bullets and number markers:
  860. $marker_ul = '[*+-]';
  861. $marker_ol = '\d+[.]';
  862. $marker_any = "(?:$marker_ul|$marker_ol)";
  863. $list = $matches[1];
  864. $list_type = preg_match("/$marker_ul/", $matches[3]) ? "ul" : "ol";
  865. $marker_any = ( $list_type == "ul" ? $marker_ul : $marker_ol );
  866. $list .= "\n";
  867. $result = $this->processListItems($list, $marker_any);
  868. $result = $this->hashBlock("<$list_type>\n" . $result . "</$list_type>");
  869. return "\n". $result ."\n\n";
  870. }
  871. var $list_level = 0;
  872. function processListItems($list_str, $marker_any) {
  873. #
  874. # Process the contents of a single ordered or unordered list, splitting it
  875. # into individual list items.
  876. #
  877. # The $this->list_level global keeps track of when we're inside a list.
  878. # Each time we enter a list, we increment it; when we leave a list,
  879. # we decrement. If it's zero, we're not in a list anymore.
  880. #
  881. # We do this because when we're not inside a list, we want to treat
  882. # something like this:
  883. #
  884. # I recommend upgrading to version
  885. # 8. Oops, now this line is treated
  886. # as a sub-list.
  887. #
  888. # As a single paragraph, despite the fact that the second line starts
  889. # with a digit-period-space sequence.
  890. #
  891. # Whereas when we're inside a list (or sub-list), that line will be
  892. # treated as the start of a sub-list. What a kludge, huh? This is
  893. # an aspect of Markdown's syntax that's hard to parse perfectly
  894. # without resorting to mind-reading. Perhaps the solution is to
  895. # change the syntax rules such that sub-lists must start with a
  896. # starting cardinal number; e.g. "1." or "a.".
  897. $this->list_level++;
  898. # trim trailing blank lines:
  899. $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str);
  900. $list_str = preg_replace_callback('{
  901. (\n)? # leading line = $1
  902. (^[ ]*) # leading whitespace = $2
  903. ('.$marker_any.') [ ]+ # list marker = $3
  904. ((?s:.+?)) # list item text = $4
  905. (?:(\n+(?=\n))|\n) # tailing blank line = $5
  906. (?= \n* (\z | \2 ('.$marker_any.') [ ]+))
  907. }xm',
  908. array(&$this, '_processListItems_callback'), $list_str);
  909. $this->list_level--;
  910. return $list_str;
  911. }
  912. function _processListItems_callback($matches) {
  913. $item = $matches[4];
  914. $leading_line =& $matches[1];
  915. $leading_space =& $matches[2];
  916. $tailing_blank_line =& $matches[5];
  917. if ($leading_line || $tailing_blank_line ||
  918. preg_match('/\n{2,}/', $item))
  919. {
  920. $item = $this->runBlockGamut($this->outdent($item)."\n");
  921. }
  922. else {
  923. # Recursion for sub-lists:
  924. $item = $this->doLists($this->outdent($item));
  925. $item = preg_replace('/\n+$/', '', $item);
  926. $item = $this->runSpanGamut($item);
  927. }
  928. return "<li>" . $item . "</li>\n";
  929. }
  930. function doCodeBlocks($text) {
  931. #
  932. # Process Markdown `<pre><code>` blocks.
  933. #
  934. $text = preg_replace_callback('{
  935. (?:\n\n|\A)
  936. ( # $1 = the code block -- one or more lines, starting with a space/tab
  937. (?:
  938. (?:[ ]{'.$this->tab_width.'} | \t) # Lines must start with a tab or a tab-width of spaces
  939. .*\n+
  940. )+
  941. )
  942. ((?=^[ ]{0,'.$this->tab_width.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
  943. }xm',
  944. array(&$this, '_doCodeBlocks_callback'), $text);
  945. return $text;
  946. }
  947. function _doCodeBlocks_callback($matches) {
  948. $codeblock = $matches[1];
  949. $codeblock = $this->encodeCode($this->outdent($codeblock));
  950. // $codeblock = $this->detab($codeblock);
  951. # trim leading newlines and trailing whitespace
  952. $codeblock = preg_replace(array('/\A\n+/', '/\n+\z/'), '', $codeblock);
  953. $result = "\n\n".$this->hashBlock("<pre><code>" . $codeblock . "\n</code></pre>")."\n\n";
  954. return $result;
  955. }
  956. function doCodeSpans($text) {
  957. #
  958. # * Backtick quotes are used for <code></code> spans.
  959. #
  960. # * You can use multiple backticks as the delimiters if you want to
  961. # include literal backticks in the code span. So, this input:
  962. #
  963. # Just type ``foo `bar` baz`` at the prompt.
  964. #
  965. # Will translate to:
  966. #
  967. # <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
  968. #
  969. # There's no arbitrary limit to the number of backticks you
  970. # can use as delimters. If you need three consecutive backticks
  971. # in your code, use four for delimiters, etc.
  972. #
  973. # * You can use spaces to get literal backticks at the edges:
  974. #
  975. # ... type `` `bar` `` ...
  976. #
  977. # Turns to:
  978. #
  979. # ... type <code>`bar`</code> ...
  980. #
  981. $text = preg_replace_callback('@
  982. (?<!\\\) # Character before opening ` can\'t be a backslash
  983. (`+) # $1 = Opening run of `
  984. (.+?) # $2 = The code block
  985. (?<!`)
  986. \1 # Matching closer
  987. (?!`)
  988. @xs',
  989. array(&$this, '_doCodeSpans_callback'), $text);
  990. return $text;
  991. }
  992. function _doCodeSpans_callback($matches) {
  993. $c = $matches[2];
  994. $c = preg_replace('/^[ ]*/', '', $c); # leading whitespace
  995. $c = preg_replace('/[ ]*$/', '', $c); # trailing whitespace
  996. $c = $this->encodeCode($c);
  997. return $this->hashSpan("<code>$c</code>");
  998. }
  999. function encodeCode($_) {
  1000. #
  1001. # Encode/escape certain characters inside Markdown code runs.
  1002. # The point is that in code, these characters are literals,
  1003. # and lose their special Markdown meanings.
  1004. #
  1005. # Encode all ampersands; HTML entities are not
  1006. # entities within a Markdown code span.
  1007. $_ = str_replace('&', '&amp;', $_);
  1008. # Do the angle bracket song and dance:
  1009. $_ = str_replace(array('<', '>'),
  1010. array('&lt;', '&gt;'), $_);
  1011. # Now, escape characters that are magic in Markdown:
  1012. // $_ = str_replace(array_keys($this->escape_table),
  1013. // array_values($this->escape_table), $_);
  1014. return $_;
  1015. }
  1016. function doItalicsAndBold($text) {
  1017. # <strong> must go first:
  1018. $text = preg_replace_callback('{
  1019. ( # $1: Marker
  1020. (?<!\*\*) \* | # (not preceded by two chars of
  1021. (?<!__) _ # the same marker)
  1022. )
  1023. \1
  1024. (?=\S) # Not followed by whitespace
  1025. (?!\1\1) # or two others marker chars.
  1026. ( # $2: Content
  1027. (?>
  1028. [^*_]+? # Anthing not em markers.
  1029. |
  1030. # Balence any regular emphasis inside.
  1031. \1 (?=\S) .+? (?<=\S) \1
  1032. |
  1033. . # Allow unbalenced * and _.
  1034. )+?
  1035. )
  1036. (?<=\S) \1\1 # End mark not preceded by whitespace.
  1037. }sx',
  1038. array(&$this, '_doItalicAndBold_strong_callback'), $text);
  1039. # Then <em>:
  1040. $text = preg_replace_callback(
  1041. '{ ( (?<!\*)\* | (?<!_)_ ) (?=\S) (?! \1) (.+?) (?<=\S)(?<!\s(?=\1).) \1 }sx',
  1042. array(&$this, '_doItalicAndBold_em_callback'), $text);
  1043. return $text;
  1044. }
  1045. function _doItalicAndBold_em_callback($matches) {
  1046. $text = $matches[2];
  1047. $text = $this->runSpanGamut($text);
  1048. return $this->hashSpan("<em>$text</em>");
  1049. }
  1050. function _doItalicAndBold_strong_callback($matches) {
  1051. $text = $matches[2];
  1052. $text = $this->runSpanGamut($text);
  1053. return $this->hashSpan("<strong>$text</strong>");
  1054. }
  1055. function doBlockQuotes($text) {
  1056. $text = preg_replace_callback('/
  1057. ( # Wrap whole match in $1
  1058. (
  1059. ^[ ]*>[ ]? # ">" at the start of a line
  1060. .+\n # rest of the first line
  1061. (.+\n)* # subsequent consecutive lines
  1062. \n* # blanks
  1063. )+
  1064. )
  1065. /xm',
  1066. array(&$this, '_doBlockQuotes_callback'), $text);
  1067. return $text;
  1068. }
  1069. function _doBlockQuotes_callback($matches) {
  1070. $bq = $matches[1];
  1071. # trim one level of quoting - trim whitespace-only lines
  1072. $bq = preg_replace(array('/^[ ]*>[ ]?/m', '/^[ ]+$/m'), '', $bq);
  1073. $bq = $this->runBlockGamut($bq); # recurse
  1074. $bq = preg_replace('/^/m', " ", $bq);
  1075. # These leading spaces cause problem with <pre> content,
  1076. # so we need to fix that:
  1077. $bq = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx',
  1078. array(&$this, '_DoBlockQuotes_callback2'), $bq);
  1079. return "\n". $this->hashBlock("<blockquote>\n$bq\n</blockquote>")."\n\n";
  1080. }
  1081. function _doBlockQuotes_callback2($matches) {
  1082. $pre = $matches[1];
  1083. $pre = preg_replace('/^ /m', '', $pre);
  1084. return $pre;
  1085. }
  1086. function formParagraphs($text) {
  1087. #
  1088. # Params:
  1089. # $text - string to process with html <p> tags
  1090. #
  1091. # Strip leading and trailing lines:
  1092. $text = preg_replace(array('/\A\n+/', '/\n+\z/'), '', $text);
  1093. $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY);
  1094. #
  1095. # Wrap <p> tags.
  1096. #
  1097. foreach ($grafs as $key => $value) {
  1098. if (!isset( $this->html_blocks[$value] )) {
  1099. $value = $this->runSpanGamut($value);
  1100. $value = preg_replace('/^([ ]*)/', "<p>", $value);
  1101. $value .= "</p>";
  1102. $grafs[$key] = $this->unhash($value);
  1103. }
  1104. }
  1105. #
  1106. # Unhashify HTML blocks
  1107. #
  1108. foreach ($grafs as $key => $graf) {
  1109. # Modify elements of @grafs in-place...
  1110. if (isset($this->html_blocks[$graf])) {
  1111. $block = $this->html_blocks[$graf];
  1112. $graf = $block;
  1113. // if (preg_match('{
  1114. // \A
  1115. // ( # $1 = <div> tag
  1116. // <div \s+
  1117. // [^>]*
  1118. // \b
  1119. // markdown\s*=\s* ([\'"]) # $2 = attr quote char
  1120. // 1
  1121. // \2
  1122. // [^>]*
  1123. // >
  1124. // )
  1125. // ( # $3 = contents
  1126. // .*
  1127. // )
  1128. // (</div>) # $4 = closing tag
  1129. // \z
  1130. // }xs', $block, $matches))
  1131. // {
  1132. // list(, $div_open, , $div_content, $div_close) = $matches;
  1133. //
  1134. // # We can't call Markdown(), because that resets the hash;
  1135. // # that initialization code should be pulled into its own sub, though.
  1136. // $div_content = $this->hashHTMLBlocks($div_content);
  1137. //
  1138. // # Run document gamut methods on the content.
  1139. // foreach ($this->document_gamut as $method => $priority) {
  1140. // $div_content = $this->$method($div_content);
  1141. // }
  1142. //
  1143. // $div_open = preg_replace(
  1144. // '{\smarkdown\s*=\s*([\'"]).+?\1}', '', $div_open);
  1145. //
  1146. // $graf = $div_open . "\n" . $div_content . "\n" . $div_close;
  1147. // }
  1148. $grafs[$key] = $graf;
  1149. }
  1150. }
  1151. return implode("\n\n", $grafs);
  1152. }
  1153. function encodeAmpsAndAngles($text) {
  1154. # Smart processing for ampersands and angle brackets that need to be encoded.
  1155. if ($this->no_entities) {
  1156. $text = str_replace('&', '&amp;', $text);
  1157. $text = str_replace('<', '&lt;', $text);
  1158. return $text;
  1159. }
  1160. # Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
  1161. # http://bumppo.net/projects/amputator/
  1162. $text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/',
  1163. '&amp;', $text);;
  1164. # Encode naked <'s
  1165. $text = preg_replace('{<(?![a-z/?\$!%])}i', '&lt;', $text);
  1166. return $text;
  1167. }
  1168. function encodeBackslashEscapes($text) {
  1169. #
  1170. # Parameter: String.
  1171. # Returns: The string, with after processing the following backslash
  1172. # escape sequences.
  1173. #
  1174. # Must process escaped backslashes first (should be first in list).
  1175. foreach ($this->backslash_escape_table as $search => $replacement) {
  1176. $text = str_replace($search, $this->hashSpan($replacement), $text);
  1177. }
  1178. return $text;
  1179. }
  1180. function doAutoLinks($text) {
  1181. $text = preg_replace_callback('{<((https?|ftp|dict):[^\'">\s]+)>}',
  1182. array(&$this, '_doAutoLinks_url_callback'), $text);
  1183. # Email addresses: <address@domain.foo>
  1184. $text = preg_replace_callback('{
  1185. <
  1186. (?:mailto:)?
  1187. (
  1188. [-.\w\x80-\xFF]+
  1189. \@
  1190. [-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+
  1191. )
  1192. >
  1193. }xi',
  1194. array(&$this, '_doAutoLinks_email_callback'), $text);
  1195. return $text;
  1196. }
  1197. function _doAutoLinks_url_callback($matches) {
  1198. $url = $this->encodeAmpsAndAngles($matches[1]);
  1199. $link = "<a href=\"$url\">$url</a>";
  1200. return $this->hashSpan($link);
  1201. }
  1202. function _doAutoLinks_email_callback($matches) {
  1203. $address = $matches[1];
  1204. $link = $this->encodeEmailAddress($address);
  1205. return $this->hashSpan($link);
  1206. }
  1207. function encodeEmailAddress($addr) {
  1208. #
  1209. # Input: an email address, e.g. "foo@example.com"
  1210. #
  1211. # Output: the email address as a mailto link, with each character
  1212. # of the address encoded as either a decimal or hex entity, in
  1213. # the hopes of foiling most address harvesting spam bots. E.g.:
  1214. #
  1215. # <p><a href="&#109;&#x61;&#105;&#x6c;&#116;&#x6f;&#58;&#x66;o&#111;
  1216. # &#x40;&#101;&#x78;&#97;&#x6d;&#112;&#x6c;&#101;&#46;&#x63;&#111;
  1217. # &#x6d;">&#x66;o&#111;&#x40;&#101;&#x78;&#97;&#x6d;&#112;&#x6c;
  1218. # &#101;&#46;&#x63;&#111;&#x6d;</a></p>
  1219. #
  1220. # Based by a filter by Matthew Wickline, posted to BBEdit-Talk.
  1221. # With some optimizations by Milian Wolff.
  1222. #
  1223. $addr = "mailto:" . $addr;
  1224. $chars = preg_split('/(?<!^)(?!$)/', $addr);
  1225. $seed = (int)abs(crc32($addr) / strlen($addr)); # Deterministic seed.
  1226. foreach ($chars as $key => $char) {
  1227. $ord = ord($char);
  1228. # Ignore non-ascii chars.
  1229. if ($ord < 128) {
  1230. $r = ($seed * (1 + $key)) % 100; # Pseudo-random function.
  1231. # roughly 10% raw, 45% hex, 45% dec
  1232. # '@' *must* be encoded. I insist.
  1233. if ($r > 90 && $char != '@') /* do nothing */;
  1234. else if ($r < 45) $chars[$key] = '&#x'.dechex($ord).';';
  1235. else $chars[$key] = '&#'.$ord.';';
  1236. }
  1237. }
  1238. $addr = implode('', $chars);
  1239. $text = implode('', array_slice($chars, 7)); # text without `mailto:`
  1240. $addr = "<a href=\"$addr\">$text</a>";
  1241. return $addr;
  1242. }
  1243. function tokenizeHTML($str) {
  1244. #
  1245. # Parameter: String containing HTML + Markdown markup.
  1246. # Returns: An array of the tokens comprising the input
  1247. # string. Each token is either a tag or a run of text
  1248. # between tags. Each element of the array is a
  1249. # two-element array; the first is either 'tag' or 'text';
  1250. # the second is the actual value.
  1251. # Note: Markdown code spans are taken into account: no tag token is
  1252. # generated within a code span.
  1253. #
  1254. $tokens = array();
  1255. while ($str != "") {
  1256. #
  1257. # Each loop iteration seach for either the next tag or the next
  1258. # openning code span marker. If a code span marker is found, the
  1259. # code span is extracted in entierty and will result in an extra
  1260. # text token.
  1261. #
  1262. $parts = preg_split('{
  1263. (
  1264. (?<![`\\\\])
  1265. `+ # code span marker
  1266. |
  1267. <!-- .*? --> # comment
  1268. |
  1269. <\?.*?\?> | <%.*?%> # processing instruction
  1270. |
  1271. <[/!$]?[-a-zA-Z0-9:]+ # regular tags
  1272. (?:
  1273. \s
  1274. (?>[^"\'>]+|"[^"]*"|\'[^\']*\')*
  1275. )?
  1276. >
  1277. )
  1278. }xs', $str, 2, PREG_SPLIT_DELIM_CAPTURE);
  1279. # Create token from text preceding tag.
  1280. if ($parts[0] != "") {
  1281. $tokens[] = array('text', $parts[0]);
  1282. }
  1283. # Check if we reach the end.
  1284. if (count($parts) < 3) {
  1285. break;
  1286. }
  1287. # Create token from tag or code span.
  1288. if ($parts[1]{0} == "`") {
  1289. $tokens[] = array('text', $parts[1]);
  1290. $str = $parts[2];
  1291. # Skip the whole code span, pass as text token.
  1292. if (preg_match('/^(.*(?<!`\\\\)'.$parts[1].'(?!`))(.*)$/sm',
  1293. $str, $matches))
  1294. {
  1295. $tokens[] = array('text', $matches[1]);
  1296. $str = $matches[2];
  1297. }
  1298. } else {
  1299. $tokens[] = array('tag', $parts[1]);
  1300. $str = $parts[2];
  1301. }
  1302. }
  1303. return $tokens;
  1304. }
  1305. function outdent($text) {
  1306. #
  1307. # Remove one level of line-leading tabs or spaces
  1308. #
  1309. return preg_replace("/^(\\t|[ ]{1,$this->tab_width})/m", "", $text);
  1310. }
  1311. # String length function for detab. `_initDetab` will create a function to
  1312. # hanlde UTF-8 if the default function does not exist.
  1313. var $utf8_strlen = 'mb_strlen';
  1314. function detab($text) {
  1315. #
  1316. # Replace tabs with the appropriate amount of space.
  1317. #
  1318. # For each line we separate the line in blocks delemited by
  1319. # tab characters. Then we reconstruct every line by adding the
  1320. # appropriate number of space between each blocks.
  1321. $strlen = $this->utf8_strlen; # strlen function for UTF-8.
  1322. $lines = explode("\n", $text);
  1323. $text = "";
  1324. foreach ($lines as $line) {
  1325. # Split in blocks.
  1326. $blocks = explode("\t", $line);
  1327. # Add each blocks to the line.
  1328. $line = $blocks[0];
  1329. unset($blocks[0]); # Do not add first block twice.
  1330. foreach ($blocks as $block) {
  1331. # Calculate amount of space, insert spaces, insert block.
  1332. $amount = $this->tab_width -
  1333. $strlen($line, 'UTF-8') % $this->tab_width;
  1334. $line .= str_repeat(" ", $amount) . $block;
  1335. }
  1336. $text .= "$line\n";
  1337. }
  1338. return $text;
  1339. }
  1340. function _initDetab() {
  1341. #
  1342. # Check for the availability of the function in the `utf8_strlen` property
  1343. # (initially `mb_strlen`). If the function is not available, create a
  1344. # function that will loosely count the number of UTF-8 characters with a
  1345. # regular expression.
  1346. #
  1347. if (function_exists($this->utf8_strlen)) return;
  1348. $this->utf8_strlen = create_function('$text', 'return preg_match_all(
  1349. "/[\\\\x00-\\\\xBF]|[\\\\xC0-\\\\xFF][\\\\x80-\\\\xBF]*/",
  1350. $text, $m);');
  1351. }
  1352. function unhash($text) {
  1353. #
  1354. # Swap back in all the tags hashed by _HashHTMLBlocks.
  1355. #
  1356. return str_replace(array_keys($this->html_hashes),
  1357. array_values($this->html_hashes), $text);
  1358. }
  1359. }
  1360. #
  1361. # Markdown Extra Parser Class
  1362. #
  1363. class MarkdownExtra_Parser extends Markdown_Parser {
  1364. # Prefix for footnote ids.
  1365. var $fn_id_prefix = "";
  1366. # Optional title attribute for footnote links and backlinks.
  1367. var $fn_link_title = MARKDOWN_FN_LINK_TITLE;
  1368. var $fn_backlink_title = MARKDOWN_FN_BACKLINK_TITLE;
  1369. # Optional class attribute for footnote links and backlinks.
  1370. var $fn_link_class = MARKDOWN_FN_LINK_CLASS;
  1371. var $fn_backlink_class = MARKDOWN_FN_BACKLINK_CLASS;
  1372. function MarkdownExtra_Parser() {
  1373. #
  1374. # Constructor function. Initialize the parser object.
  1375. #
  1376. # Add extra escapable characters before parent constructor
  1377. # initialize the table.
  1378. $this->escape_chars .= ':|';
  1379. # Insert extra document, block, and span transformations.
  1380. # Parent constructor will do the sorting.
  1381. $this->document_gamut += array(
  1382. "stripFootnotes" => 15,
  1383. "stripAbbreviations" => 25,
  1384. "appendFootnotes" => 50,
  1385. );
  1386. $this->block_gamut += array(
  1387. "doTables" => 15,
  1388. "doDefLists" => 45,
  1389. );
  1390. $this->span_gamut += array(
  1391. "doFootnotes" => 5,
  1392. "doAbbreviations" => 70,
  1393. );
  1394. parent::Markdown_Parser();
  1395. }
  1396. # Extra hashes used during extra transformations.
  1397. var $footnotes = array();
  1398. var $footnotes_ordered = array();
  1399. var $abbr_desciptions = array();
  1400. var $abbr_matches = array();
  1401. var $html_cleans = array();
  1402. # Status flag to avoid invalid nesting.
  1403. var $in_footnote = false;
  1404. function transform($text) {
  1405. #
  1406. # Added clear to the new $html_hashes, reordered `hashHTMLBlocks` before
  1407. # blank line stripping and added extra parameter to `runBlockGamut`.
  1408. #
  1409. # Clear the global hashes. If we don't clear these, you get conflicts
  1410. # from other articles when generating a page which contains more than
  1411. # one article (e.g. an index page that shows the N most recent
  1412. # articles):
  1413. $this->footnotes = array();
  1414. $this->footnotes_ordered = array();
  1415. $this->abbr_desciptions = array();
  1416. $this->abbr_matches = array();
  1417. $this->html_cleans = array();
  1418. return parent::transform($text);
  1419. }
  1420. ### HTML Block Parser ###
  1421. # Tags that are always treated as block tags:
  1422. var $block_tags = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend';
  1423. # Tags treated as block tags only if the opening tag is alone on it's line:
  1424. var $context_block_tags = 'script|noscript|math|ins|del';
  1425. # Tags where markdown="1" default to span mode:
  1426. var $contain_span_tags = 'p|h[1-6]|li|dd|dt|td|th|legend|address';
  1427. # Tags which must not have their contents modified, no matter where
  1428. # they appear:
  1429. var $clean_tags = 'script|math';
  1430. # Tags that do not need to be closed.
  1431. var $auto_close_tags = 'hr|img';
  1432. function hashHTMLBlocks($text) {
  1433. #
  1434. # Hashify HTML Blocks and "clean tags".
  1435. #
  1436. # We only want to do this for block-level HTML tags, such as headers,
  1437. # lists, and tables. That's because we still want to wrap <p>s around
  1438. # "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  1439. # phrase emphasis, and spans. The list of tags we're looking for is
  1440. # hard-coded.
  1441. #
  1442. # This works by calling _HashHTMLBlocks_InMarkdown, which then calls
  1443. # _HashHTMLBlocks_InHTML when it encounter block tags. When the markdown="1"
  1444. # attribute is found whitin a tag, _HashHTMLBlocks_InHTML calls back
  1445. # _HashHTMLBlocks_InMarkdown to handle the Markdown syntax within the tag.
  1446. # These two functions are calling each other. It's recursive!
  1447. #
  1448. #
  1449. # Call the HTML-in-Markdown hasher.
  1450. #
  1451. list($text, ) = $this->_hashHTMLBlocks_inMarkdown($text);
  1452. return $text;
  1453. }
  1454. function _hashHTMLBlocks_inMarkdown($text, $indent = 0,
  1455. $enclosing_tag = '', $span = false)
  1456. {
  1457. #
  1458. # Parse markdown text, calling _HashHTMLBlocks_InHTML for block tags.
  1459. #
  1460. # * $indent is the number of space to be ignored when checking for code
  1461. # blocks. This is important because if we don't take the indent into
  1462. # account, something like this (which looks right) won't work as expected:
  1463. #
  1464. # <div>
  1465. # <div markdown="1">
  1466. # Hello World. <-- Is this a Markdown code block or text?
  1467. # </div> <-- Is this a Markdown code block or a real tag?
  1468. # <div>
  1469. #
  1470. # If you don't like this, just don't indent the tag on which
  1471. # you apply the markdown="1" attribute.
  1472. #
  1473. # * If $enclosing_tag is not empty, stops at the first unmatched closing
  1474. # tag with that name. Nested tags supported.
  1475. #
  1476. # * If $span is true, text inside must treated as span. So any double
  1477. # newline will be replaced by a single newline so that it does not create
  1478. # paragraphs.
  1479. #
  1480. # Returns an array of that form: ( processed text , remaining text )
  1481. #
  1482. if ($text === '') return array('', '');
  1483. # Regex to check for the presense of newlines around a block tag.
  1484. $newline_match_before = '/(?:^\n?|\n\n)*$/';
  1485. $newline_match_after =
  1486. '{
  1487. ^ # Start of text following the tag.
  1488. (?:[ ]*<!--.*?-->)? # Optional comment.
  1489. [ ]*\n # Must be followed by newline.
  1490. }xs';
  1491. # Regex to match any tag.
  1492. $block_tag_match =
  1493. '{
  1494. ( # $2: Capture hole tag.
  1495. </? # Any opening or closing tag.
  1496. (?: # Tag name.
  1497. '.$this->block_tags.' |
  1498. '.$this->context_block_tags.' |
  1499. '.$this->clean_tags.' |
  1500. (?!\s)'.$enclosing_tag.'
  1501. )
  1502. \s* # Whitespace.
  1503. (?>
  1504. ".*?" | # Double quotes (can contain `>`)
  1505. \'.*?\' | # Single quotes (can contain `>`)
  1506. .+? # Anything but quotes and `>`.
  1507. )*?
  1508. > # End of tag.
  1509. |
  1510. <!-- .*? --> # HTML Comment
  1511. |
  1512. <\?.*?\?> | <%.*?%> # Processing instruction
  1513. |
  1514. <!\[CDATA\[.*?\]\]> # CData Block
  1515. )
  1516. }xs';
  1517. $depth = 0; # Current depth inside the tag tree.
  1518. $parsed = ""; # Parsed text that will be returned.
  1519. #
  1520. # Loop through every tag until we find the closing tag of the parent
  1521. # or loop until reaching the end of text if no parent tag specified.
  1522. #
  1523. do {
  1524. #
  1525. # Split the text using the first $tag_match pattern found.
  1526. # Text before pattern will be first in the array, text after
  1527. # pattern will be at the end, and between will be any catches made
  1528. # by the pattern.
  1529. #
  1530. $parts = preg_split($block_tag_match, $text, 2,
  1531. PREG_SPLIT_DELIM_CAPTURE);
  1532. # If in Markdown span mode, add a empty-string span-level hash
  1533. # after each newline to prevent triggering any block element.
  1534. if ($span) {
  1535. $void = $this->hashSpan("", true) ;
  1536. $newline = $this->hashSpan("", true) . "\n";
  1537. $parts[0] = $void . str_replace("\n", $newline, $parts[0]) . $void;
  1538. }
  1539. $parsed .= $parts[0]; # Text before current tag.
  1540. # If end of $text has been reached. Stop loop.
  1541. if (count($parts) < 3) {
  1542. $text = "";
  1543. break;
  1544. }
  1545. $tag = $parts[1]; # Tag to handle.
  1546. $text = $parts[2]; # Remaining text after current tag.
  1547. #
  1548. # Check for: Tag inside code block or span
  1549. #
  1550. if (# Find current paragraph
  1551. preg_match('/(?>^\n?|\n\n)((?>.\n?)+?)$/', $parsed, $matches) &&
  1552. (
  1553. # Then match in it either a code block...
  1554. preg_match('/^ {'.($indent+4).'}.*(?>\n {'.($indent+4).'}.*)*'.
  1555. '(?!\n)$/', $matches[1], $x) ||
  1556. # ...or unbalenced code span markers. (the regex matches balenced)
  1557. !preg_match('/^(?>[^`]+|(`+)(?>[^`]+|(?!\1[^`])`)*?\1(?!`))*$/s',
  1558. $matches[1])
  1559. ))
  1560. {
  1561. # Tag is in code block or span and may not be a tag at all. So we
  1562. # simply skip the first char (should be a `<`).
  1563. $parsed .= $tag{0};
  1564. $text = substr($tag, 1) . $text; # Put back $tag minus first char.
  1565. }
  1566. #
  1567. # Check for: Opening Block level tag or
  1568. # Opening Content Block tag (like ins and del)
  1569. # used as a block tag (tag is alone on it's line).
  1570. #
  1571. else if (preg_match("{^<(?:$this->block_tags)\b}", $tag) ||
  1572. ( preg_match("{^<(?:$this->context_block_tags)\b}", $tag) &&
  1573. preg_match($newline_match_before, $parsed) &&
  1574. preg_match($newline_match_after, $text) )
  1575. )
  1576. {
  1577. # Need to parse tag and following text using the HTML parser.
  1578. list($block_text, $text) =
  1579. $this->_hashHTMLBlocks_inHTML($tag . $text, "hashBlock", true);
  1580. # Make sure it stays outside of any paragraph by adding newlines.
  1581. $parsed .= "\n\n$block_text\n\n";
  1582. }
  1583. #
  1584. # Check for: Clean tag (like script, math)
  1585. # HTML Comments, processing instructions.
  1586. #
  1587. else if (preg_match("{^<(?:$this->clean_tags)\b}", $tag) ||
  1588. $tag{1} == '!' || $tag{1} == '?')
  1589. {
  1590. # Need to parse tag and following text using the HTML parser.
  1591. # (don't check for markdown attribute)
  1592. list($block_text, $text) =
  1593. $this->_hashHTMLBlocks_inHTML($tag . $text, "hashClean", false);
  1594. $parsed .= $block_text;
  1595. }
  1596. #
  1597. # Check for: Tag with same name as enclosing tag.
  1598. #
  1599. else if ($enclosing_tag !== '' &&
  1600. # Same name as enclosing tag.
  1601. preg_match("{^</?(?:$enclosing_tag)\b}", $tag))
  1602. {
  1603. #
  1604. # Increase/decrease nested tag count.
  1605. #
  1606. if ($tag{1} == '/') $depth--;
  1607. else if ($tag{strlen($tag)-2} != '/') $depth++;
  1608. if ($depth < 0) {
  1609. #
  1610. # Going out of parent element. Clean up and break so we
  1611. # return to the calling function.
  1612. #
  1613. $text = $tag . $text;
  1614. break;
  1615. }
  1616. $parsed .= $tag;
  1617. }
  1618. else {
  1619. $parsed .= $tag;
  1620. }
  1621. } while ($depth >= 0);
  1622. return array($parsed, $text);
  1623. }
  1624. function _hashHTMLBlocks_inHTML($text, $hash_method, $md_attr) {
  1625. #
  1626. # Parse HTML, calling _HashHTMLBlocks_InMarkdown for block tags.
  1627. #
  1628. # * Calls $hash_method to convert any blocks.
  1629. # * Stops when the first opening tag closes.
  1630. # * $md_attr indicate if the use of the `markdown="1"` attribute is allowed.
  1631. # (it is not inside clean tags)
  1632. #
  1633. # Returns an array of that form: ( processed text , remaining text )
  1634. #
  1635. if ($text === '') return array('', '');
  1636. # Regex to match `markdown` attribute inside of a tag.
  1637. $markdown_attr_match = '
  1638. {
  1639. \s* # Eat whitespace before the `markdown` attribute
  1640. markdown
  1641. \s*=\s*
  1642. (?:
  1643. (["\']) # $1: quote delimiter
  1644. (.*?) # $2: attribute value
  1645. \1 # matching delimiter
  1646. |
  1647. ([^\s>]*) # $3: unquoted attribute value
  1648. )
  1649. () # $4: make $3 always defined (avoid warnings)
  1650. }xs';
  1651. # Regex to match any tag.
  1652. $tag_match = '{
  1653. ( # $2: Capture hole tag.
  1654. </? # Any opening or closing tag.
  1655. [\w:$]+ # Tag name.
  1656. \s* # Whitespace.
  1657. (?>
  1658. ".*?" | # Double quotes (can contain `>`)
  1659. \'.*?\' | # Single quotes (can contain `>`)
  1660. .+? # Anything but quotes and `>`.
  1661. )*?
  1662. > # End of tag.
  1663. |
  1664. <!-- .*? --> # HTML Comment
  1665. |
  1666. <\?.*?\?> | <%.*?%> # Processing instruction
  1667. |
  1668. <!\[CDATA\[.*?\]\]> # CData Block
  1669. )
  1670. }xs';
  1671. $original_text = $text; # Save original text in case of faliure.
  1672. $depth = 0; # Current depth inside the tag tree.
  1673. $block_text = ""; # Temporary text holder for current text.
  1674. $parsed = ""; # Parsed text that will be returned.
  1675. #
  1676. # Get the name of the starting tag.
  1677. #
  1678. if (preg_match("/^<([\w:$]*)\b/", $text, $matches))
  1679. $base_tag_name = $matches[1];
  1680. #
  1681. # Loop through every tag until we find the corresponding closing tag.
  1682. #
  1683. do {
  1684. #
  1685. # Split the text using the first $tag_match pattern found.
  1686. # Text before pattern will be first in the array, text after
  1687. # pattern will be at the end, and between will be any catches made
  1688. # by the pattern.
  1689. #
  1690. $parts = preg_split($tag_match, $text, 2, PREG_SPLIT_DELIM_CAPTURE);
  1691. if (count($parts) < 3) {
  1692. #
  1693. # End of $text reached with unbalenced tag(s).
  1694. # In that case, we return original text unchanged and pass the
  1695. # first character as filtered to prevent an infinite loop in the
  1696. # parent function.
  1697. #
  1698. return array($original_text{0}, substr($original_text, 1));
  1699. }
  1700. $block_text .= $parts[0]; # Text before current tag.
  1701. $tag = $parts[1]; # Tag to handle.
  1702. $text = $parts[2]; # Remaining text after current tag.
  1703. #
  1704. # Check for: Auto-close tag (like <hr/>)
  1705. # Comments and Processing Instructions.
  1706. #
  1707. if (preg_match("{^</?(?:$this->auto_close_tags)\b}", $tag) ||
  1708. $tag{1} == '!' || $tag{1} == '?')
  1709. {
  1710. # Just add the tag to the block as if it was text.
  1711. $block_text .= $tag;
  1712. }
  1713. else {
  1714. #
  1715. # Increase/decrease nested tag count. Only do so if
  1716. # the tag's name match base tag's.
  1717. #
  1718. if (preg_match("{^</?$base_tag_name\b}", $tag)) {
  1719. if ($tag{1} == '/') $depth--;
  1720. else if ($tag{strlen($tag)-2} != '/') $depth++;
  1721. }
  1722. #
  1723. # Check for `markdown="1"` attribute and handle it.
  1724. #
  1725. if ($md_attr &&
  1726. preg_match($markdown_attr_match, $tag, $attr_m) &&
  1727. preg_match('/^1|block|span$/', $attr_m[2] . $attr_m[3]))
  1728. {
  1729. # Remove `markdown` attribute from opening tag.
  1730. $tag = preg_replace($markdown_attr_match, '', $tag);
  1731. # Check if text inside this tag must be parsed in span mode.
  1732. $this->mode = $attr_m[2] . $attr_m[3];
  1733. $span_mode = $this->mode == 'span' || $this->mode != 'block' &&
  1734. preg_match("{^<(?:$this->contain_span_tags)\b}", $tag);
  1735. # Calculate indent before tag.
  1736. preg_match('/(?:^|\n)( *?)(?! ).*?$/', $block_text, $matches);
  1737. $indent = strlen($matches[1]);
  1738. # End preceding block with this tag.
  1739. $block_text .= $tag;
  1740. $parsed .= $this->$hash_method($block_text);
  1741. # Get enclosing tag name for the ParseMarkdown function.
  1742. preg_match('/^<([\w:$]*)\b/', $tag, $matches);
  1743. $tag_name = $matches[1];
  1744. # Parse the content using the HTML-in-Markdown parser.
  1745. list ($block_text, $text)
  1746. = $this->_hashHTMLBlocks_inMarkdown($text, $indent,
  1747. $tag_name, $span_mode);
  1748. # Outdent markdown text.
  1749. if ($indent > 0) {
  1750. $block_text = preg_replace("/^[ ]{1,$indent}/m", "",
  1751. $block_text);
  1752. }
  1753. # Append tag content to parsed text.
  1754. if (!$span_mode) $parsed .= "\n\n$block_text\n\n";
  1755. else $parsed .= "$block_text";
  1756. # Start over a new block.
  1757. $block_text = "";
  1758. }
  1759. else $block_text .= $tag;
  1760. }
  1761. } while ($depth > 0);
  1762. #
  1763. # Hash last block text that wasn't processed inside the loop.
  1764. #
  1765. $parsed .= $this->$hash_method($block_text);
  1766. return array($parsed, $text);
  1767. }
  1768. function hashClean($text) {
  1769. #
  1770. # Called whenever a tag must be hashed when a function insert a "clean" tag
  1771. # in $text, it pass through this function and is automaticaly escaped,
  1772. # blocking invalid nested overlap.
  1773. #
  1774. # Swap back any tag hash found in $text so we do not have to `unhash`
  1775. # multiple times at the end.
  1776. $text = $this->unhash($text);
  1777. # Then hash the tag.
  1778. $key = "C\x1A". md5($text);
  1779. $this->html_cleans[$key] = $text;
  1780. $this->html_hashes[$key] = $text;
  1781. return $key; # String that will replace the clean tag.
  1782. }
  1783. function doHeaders($text) {
  1784. #
  1785. # Redefined to add id attribute support.
  1786. #
  1787. # Setext-style headers:
  1788. # Header 1 {#header1}
  1789. # ========
  1790. #
  1791. # Header 2 {#header2}
  1792. # --------
  1793. #
  1794. $text = preg_replace_callback(
  1795. '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ ]*\n=+[ ]*\n+ }mx',
  1796. array(&$this, '_doHeaders_callback_setext_h1'), $text);
  1797. $text = preg_replace_callback(
  1798. '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ ]*\n-+[ ]*\n+ }mx',
  1799. array(&$this, '_doHeaders_callback_setext_h2'), $text);
  1800. # atx-style headers:
  1801. # # Header 1 {#header1}
  1802. # ## Header 2 {#header2}
  1803. # ## Header 2 with closing hashes ## {#header3}
  1804. # ...
  1805. # ###### Header 6 {#header2}
  1806. #
  1807. $text = preg_replace_callback('{
  1808. ^(\#{1,6}) # $1 = string of #\'s
  1809. [ ]*
  1810. (.+?) # $2 = Header text
  1811. [ ]*
  1812. \#* # optional closing #\'s (not counted)
  1813. (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # id attribute
  1814. [ ]*
  1815. \n+
  1816. }xm',
  1817. array(&$this, '_doHeaders_callback_atx'), $text);
  1818. return $text;
  1819. }
  1820. function _doHeaders_attr($attr) {
  1821. if (empty($attr)) return "";
  1822. return " id=\"$attr\"";
  1823. }
  1824. function _doHeaders_callback_setext_h1($matches) {
  1825. $attr = $this->_doHeaders_attr($id =& $matches[2]);
  1826. $block = "<h1$attr>".$this->runSpanGamut($matches[1])."</h1>";
  1827. return "\n" . $this->hashBlock($block) . "\n\n";
  1828. }
  1829. function _doHeaders_callback_setext_h2($matches) {
  1830. $attr = $this->_doHeaders_attr($id =& $matches[2]);
  1831. $block = "<h2$attr>".$this->runSpanGamut($matches[1])."</h2>";
  1832. return "\n" . $this->hashBlock($block) . "\n\n";
  1833. }
  1834. function _doHeaders_callback_atx($matches) {
  1835. $level = strlen($matches[1]);
  1836. $attr = $this->_doHeaders_attr($id =& $matches[3]);
  1837. $block = "<h$level$attr>".$this->runSpanGamut($matches[2])."</h$level>";
  1838. return "\n" . $this->hashBlock($block) . "\n\n";
  1839. }
  1840. function doTables($text) {
  1841. #
  1842. # Form HTML tables.
  1843. #
  1844. $less_than_tab = $this->tab_width - 1;
  1845. #
  1846. # Find tables with leading pipe.
  1847. #
  1848. # | Header 1 | Header 2
  1849. # | -------- | --------
  1850. # | Cell 1 | Cell 2
  1851. # | Cell 3 | Cell 4
  1852. #
  1853. $text = preg_replace_callback('
  1854. {
  1855. ^ # Start of a line
  1856. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1857. [|] # Optional leading pipe (present)
  1858. (.+) \n # $1: Header row (at least one pipe)
  1859. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1860. [|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline
  1861. ( # $3: Cells
  1862. (?:
  1863. [ ]* # Allowed whitespace.
  1864. [|] .* \n # Row content.
  1865. )*
  1866. )
  1867. (?=\n|\Z) # Stop at final double newline.
  1868. }xm',
  1869. array(&$this, '_doTable_leadingPipe_callback'), $text);
  1870. #
  1871. # Find tables without leading pipe.
  1872. #
  1873. # Header 1 | Header 2
  1874. # -------- | --------
  1875. # Cell 1 | Cell 2
  1876. # Cell 3 | Cell 4
  1877. #
  1878. $text = preg_replace_callback('
  1879. {
  1880. ^ # Start of a line
  1881. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1882. (\S.*[|].*) \n # $1: Header row (at least one pipe)
  1883. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  1884. ([-:]+[ ]*[|][-| :]*) \n # $2: Header underline
  1885. ( # $3: Cells
  1886. (?:
  1887. .* [|] .* \n # Row content
  1888. )*
  1889. )
  1890. (?=\n|\Z) # Stop at final double newline.
  1891. }xm',
  1892. array(&$this, '_DoTable_callback'), $text);
  1893. return $text;
  1894. }
  1895. function _doTable_leadingPipe_callback($matches) {
  1896. $head = $matches[1];
  1897. $underline = $matches[2];
  1898. $content = $matches[3];
  1899. # Remove leading pipe for each row.
  1900. $content = preg_replace('/^ *[|]/m', '', $content);
  1901. return $this->_doTable_callback(array($matches[0], $head, $underline, $content));
  1902. }
  1903. function _doTable_callback($matches) {
  1904. $head = $matches[1];
  1905. $underline = $matches[2];
  1906. $content = $matches[3];
  1907. # Remove any tailing pipes for each line.
  1908. $head = preg_replace('/[|] *$/m', '', $head);
  1909. $underline = preg_replace('/[|] *$/m', '', $underline);
  1910. $content = preg_replace('/[|] *$/m', '', $content);
  1911. # Reading alignement from header underline.
  1912. $separators = preg_split('/ *[|] */', $underline);
  1913. foreach ($separators as $n => $s) {
  1914. if (preg_match('/^ *-+: *$/', $s)) $attr[$n] = ' align="right"';
  1915. else if (preg_match('/^ *:-+: *$/', $s))$attr[$n] = ' align="center"';
  1916. else if (preg_match('/^ *:-+ *$/', $s)) $attr[$n] = ' align="left"';
  1917. else $attr[$n] = '';
  1918. }
  1919. # Creating code spans before splitting the row is an easy way to
  1920. # handle a code span containg pipes.
  1921. $head = $this->doCodeSpans($head);
  1922. $headers = preg_split('/ *[|] */', $head);
  1923. $col_count = count($headers);
  1924. # Write column headers.
  1925. $text = "<table>\n";
  1926. $text .= "<thead>\n";
  1927. $text .= "<tr>\n";
  1928. foreach ($headers as $n => $header)
  1929. $text .= " <th$attr[$n]>".$this->runSpanGamut(trim($header))."</th>\n";
  1930. $text .= "</tr>\n";
  1931. $text .= "</thead>\n";
  1932. # Split content by row.
  1933. $rows = explode("\n", trim($content, "\n"));
  1934. $text .= "<tbody>\n";
  1935. foreach ($rows as $row) {
  1936. # Creating code spans before splitting the row is an easy way to
  1937. # handle a code span containg pipes.
  1938. $row = $this->doCodeSpans($row);
  1939. # Split row by cell.
  1940. $row_cells = preg_split('/ *[|] */', $row, $col_count);
  1941. $row_cells = array_pad($row_cells, $col_count, '');
  1942. $text .= "<tr>\n";
  1943. foreach ($row_cells as $n => $cell)
  1944. $text .= " <td$attr[$n]>".$this->runSpanGamut(trim($cell))."</td>\n";
  1945. $text .= "</tr>\n";
  1946. }
  1947. $text .= "</tbody>\n";
  1948. $text .= "</table>";
  1949. return $this->hashBlock($text) . "\n";
  1950. }
  1951. function doDefLists($text) {
  1952. #
  1953. # Form HTML definition lists.
  1954. #
  1955. $less_than_tab = $this->tab_width - 1;
  1956. # Re-usable pattern to match any entire dl list:
  1957. $whole_list = '
  1958. ( # $1 = whole list
  1959. ( # $2
  1960. [ ]{0,'.$less_than_tab.'}
  1961. ((?>.*\S.*\n)+) # $3 = defined term
  1962. \n?
  1963. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  1964. )
  1965. (?s:.+?)
  1966. ( # $4
  1967. \z
  1968. |
  1969. \n{2,}
  1970. (?=\S)
  1971. (?! # Negative lookahead for another term
  1972. [ ]{0,'.$less_than_tab.'}
  1973. (?: \S.*\n )+? # defined term
  1974. \n?
  1975. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  1976. )
  1977. (?! # Negative lookahead for another definition
  1978. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  1979. )
  1980. )
  1981. )
  1982. '; // mx
  1983. $text = preg_replace_callback('{
  1984. (?:(?<=\n\n)|\A\n?)
  1985. '.$whole_list.'
  1986. }mx',
  1987. array(&$this, '_doDefLists_callback'), $text);
  1988. return $text;
  1989. }
  1990. function _doDefLists_callback($matches) {
  1991. # Re-usable patterns to match list item bullets and number markers:
  1992. $list = $matches[1];
  1993. # Turn double returns into triple returns, so that we can make a
  1994. # paragraph for the last item in a list, if necessary:
  1995. $result = trim($this->processDefListItems($list));
  1996. $result = "<dl>\n" . $result . "\n</dl>";
  1997. return $this->hashBlock($result) . "\n\n";
  1998. }
  1999. function processDefListItems($list_str) {
  2000. #
  2001. # Process the contents of a single definition list, splitting it
  2002. # into individual term and definition list items.
  2003. #
  2004. $less_than_tab = $this->tab_width - 1;
  2005. # trim trailing blank lines:
  2006. $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str);
  2007. # Process definition terms.
  2008. $list_str = preg_replace_callback('{
  2009. (?:\n\n+|\A\n?) # leading line
  2010. ( # definition terms = $1
  2011. [ ]{0,'.$less_than_tab.'} # leading whitespace
  2012. (?![:][ ]|[ ]) # negative lookahead for a definition
  2013. # mark (colon) or more whitespace.
  2014. (?: \S.* \n)+? # actual term (not whitespace).
  2015. )
  2016. (?=\n?[ ]{0,3}:[ ]) # lookahead for following line feed
  2017. # with a definition mark.
  2018. }xm',
  2019. array(&$this, '_processDefListItems_callback_dt'), $list_str);
  2020. # Process actual definitions.
  2021. $list_str = preg_replace_callback('{
  2022. \n(\n+)? # leading line = $1
  2023. [ ]{0,'.$less_than_tab.'} # whitespace before colon
  2024. [:][ ]+ # definition mark (colon)
  2025. ((?s:.+?)) # definition text = $2
  2026. (?= \n+ # stop at next definition mark,
  2027. (?: # next term or end of text
  2028. [ ]{0,'.$less_than_tab.'} [:][ ] |
  2029. <dt> | \z
  2030. )
  2031. )
  2032. }xm',
  2033. array(&$this, '_processDefListItems_callback_dd'), $list_str);
  2034. return $list_str;
  2035. }
  2036. function _processDefListItems_callback_dt($matches) {
  2037. $terms = explode("\n", trim($matches[1]));
  2038. $text = '';
  2039. foreach ($terms as $term) {
  2040. $term = $this->runSpanGamut(trim($term));
  2041. $text .= "\n<dt>" . $term . "</dt>";
  2042. }
  2043. return $text . "\n";
  2044. }
  2045. function _processDefListItems_callback_dd($matches) {
  2046. $leading_line = $matches[1];
  2047. $def = $matches[2];
  2048. if ($leading_line || preg_match('/\n{2,}/', $def)) {
  2049. $def = $this->runBlockGamut($this->outdent($def . "\n\n"));
  2050. $def = "\n". $def ."\n";
  2051. }
  2052. else {
  2053. $def = rtrim($def);
  2054. $def = $this->runSpanGamut($this->outdent($def));
  2055. }
  2056. return "\n<dd>" . $def . "</dd>\n";
  2057. }
  2058. function doItalicsAndBold($text) {
  2059. #
  2060. # Redefined to change emphasis by underscore behaviour so that it does not
  2061. # work in the middle of a word.
  2062. #
  2063. # <strong> must go first:
  2064. $text = preg_replace_callback(array(
  2065. '{
  2066. ( # $1: Marker
  2067. (?<![a-zA-Z0-9]) # Not preceded by alphanum
  2068. (?<!__) # or by two marker chars.
  2069. __
  2070. )
  2071. (?=\S) # Not followed by whitespace
  2072. (?!__) # or two others marker chars.
  2073. ( # $2: Content
  2074. (?>
  2075. [^_]+? # Anthing not em markers.
  2076. |
  2077. # Balence any regular _ emphasis inside.
  2078. (?<![a-zA-Z0-9]) _ (?=\S) (.+?)
  2079. (?<=\S) _ (?![a-zA-Z0-9])
  2080. |
  2081. _+ # Allow unbalenced as last resort.
  2082. )+?
  2083. )
  2084. (?<=\S) __ # End mark not preceded by whitespace.
  2085. (?![a-zA-Z0-9]) # Not followed by alphanum
  2086. (?!__) # or two others marker chars.
  2087. }sx',
  2088. '{
  2089. ( (?<!\*\*) \*\* ) # $1: Marker (not preceded by two *)
  2090. (?=\S) # Not followed by whitespace
  2091. (?!\1) # or two others marker chars.
  2092. ( # $2: Content
  2093. (?>
  2094. [^*]+? # Anthing not em markers.
  2095. |
  2096. # Balence any regular * emphasis inside.
  2097. \* (?=\S) (.+?) (?<=\S) \*
  2098. |
  2099. \* # Allow unbalenced as last resort.
  2100. )+?
  2101. )
  2102. (?<=\S) \*\* # End mark not preceded by whitespace.
  2103. }sx',
  2104. ),
  2105. array(&$this, '_doItalicAndBold_strong_callback'), $text);
  2106. # Then <em>:
  2107. $text = preg_replace_callback(array(
  2108. '{ ( (?<![a-zA-Z0-9])(?<!_)_ ) (?=\S) (?! \1) (.+?) (?<=\S) \1(?![a-zA-Z0-9]) }sx',
  2109. '{ ( (?<!\*)\* ) (?=\S) (?! \1) (.+?) (?<=\S)(?<!\s\*) \1 }sx',
  2110. ),
  2111. array(&$this, '_doItalicAndBold_em_callback'), $text);
  2112. return $text;
  2113. }
  2114. function formParagraphs($text) {
  2115. #
  2116. # Params:
  2117. # $text - string to process with html <p> tags
  2118. #
  2119. # Strip leading and trailing lines:
  2120. $text = preg_replace(array('/\A\n+/', '/\n+\z/'), '', $text);
  2121. $grafs = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY);
  2122. #
  2123. # Wrap <p> tags and unhashify HTML blocks
  2124. #
  2125. foreach ($grafs as $key => $value) {
  2126. $value = trim($this->runSpanGamut($value));
  2127. # Check if this should be enclosed in a paragraph.
  2128. # Clean tag hashes & block tag hashes are left alone.
  2129. $clean_key = $value;
  2130. $block_key = substr($value, 0, 34);
  2131. $is_p = (!isset($this->html_blocks[$block_key]) &&
  2132. !isset($this->html_cleans[$clean_key]));
  2133. if ($is_p) {
  2134. $value = "<p>$value</p>";
  2135. }
  2136. $grafs[$key] = $value;
  2137. }
  2138. # Join grafs in one text, then unhash HTML tags.
  2139. $text = implode("\n\n", $grafs);
  2140. # Finish by removing any tag hashes still present in $text.
  2141. $text = $this->unhash($text);
  2142. return $text;
  2143. }
  2144. ### Footnotes
  2145. function stripFootnotes($text) {
  2146. #
  2147. # Strips link definitions from text, stores the URLs and titles in
  2148. # hash references.
  2149. #
  2150. $less_than_tab = $this->tab_width - 1;
  2151. # Link defs are in the form: [^id]: url "optional title"
  2152. $text = preg_replace_callback('{
  2153. ^[ ]{0,'.$less_than_tab.'}\[\^(.+?)\][ ]?: # note_id = $1
  2154. [ ]*
  2155. \n? # maybe *one* newline
  2156. ( # text = $2 (no blank lines allowed)
  2157. (?:
  2158. .+ # actual text
  2159. |
  2160. \n # newlines but
  2161. (?!\[\^.+?\]:\s)# negative lookahead for footnote marker.
  2162. (?!\n+[ ]{0,3}\S)# ensure line is not blank and followed
  2163. # by non-indented content
  2164. )*
  2165. )
  2166. }xm',
  2167. array(&$this, '_stripFootnotes_callback'),
  2168. $text);
  2169. return $text;
  2170. }
  2171. function _stripFootnotes_callback($matches) {
  2172. $note_id = $this->fn_id_prefix . $matches[1];
  2173. $this->footnotes[$note_id] = $this->outdent($matches[2]);
  2174. return ''; # String that will replace the block
  2175. }
  2176. function doFootnotes($text) {
  2177. #
  2178. # Replace footnote references in $text [^id] with a special text-token
  2179. # which will be can be
  2180. #
  2181. if (!$this->in_footnote && !$this->in_anchor) {
  2182. $text = preg_replace('{\[\^(.+?)\]}', "F\x1Afn:\\1\x1A:", $text);
  2183. }
  2184. return $text;
  2185. }
  2186. function appendFootnotes($text) {
  2187. #
  2188. # Append footnote list to text.
  2189. #
  2190. $text = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}',
  2191. array(&$this, '_appendFootnotes_callback'), $text);
  2192. if (!empty($this->footnotes_ordered)) {
  2193. $text .= "\n\n";
  2194. $text .= "<div class=\"footnotes\">\n";
  2195. $text .= "<hr". MARKDOWN_EMPTY_ELEMENT_SUFFIX ."\n";
  2196. $text .= "<ol>\n\n";
  2197. $attr = " rev=\"footnote\"";
  2198. if ($this->fn_backlink_class != "") {
  2199. $class = $this->fn_backlink_class;
  2200. $class = $this->encodeAmpsAndAngles($class);
  2201. $class = str_replace('"', '&quot;', $class);
  2202. $attr .= " class=\"$class\"";
  2203. }
  2204. if ($this->fn_backlink_title != "") {
  2205. $title = $this->fn_backlink_title;
  2206. $title = $this->encodeAmpsAndAngles($title);
  2207. $title = str_replace('"', '&quot;', $title);
  2208. $attr .= " title=\"$title\"";
  2209. }
  2210. $num = 0;
  2211. $this->in_footnote = true;
  2212. foreach ($this->footnotes_ordered as $note_id => $footnote) {
  2213. $footnote .= "\n"; # Need to append newline before parsing.
  2214. $footnote = $this->runBlockGamut("$footnote\n");
  2215. $attr2 = str_replace("%%", ++$num, $attr);
  2216. # Add backlink to last paragraph; create new paragraph if needed.
  2217. $backlink = "<a href=\"#fnref:$note_id\"$attr2>&#8617;</a>";
  2218. if (preg_match('{</p>$}', $footnote)) {
  2219. $footnote = substr($footnote, 0, -4) . "&#160;$backlink</p>";
  2220. } else {
  2221. $footnote .= "\n\n<p>$backlink</p>";
  2222. }
  2223. $text .= "<li id=\"fn:$note_id\">\n";
  2224. $text .= $footnote . "\n";
  2225. $text .= "</li>\n\n";
  2226. }
  2227. $this->in_footnote = false;
  2228. $text .= "</ol>\n";
  2229. $text .= "</div>";
  2230. }
  2231. return $text;
  2232. }
  2233. function _appendFootnotes_callback($matches) {
  2234. $node_id = $this->fn_id_prefix . $matches[1];
  2235. # Create footnote marker only if it has a corresponding footnote *and*
  2236. # the footnote hasn't been used by another marker.
  2237. if (isset($this->footnotes[$node_id])) {
  2238. # Transfert footnote content to the ordered list.
  2239. $this->footnotes_ordered[$node_id] = $this->footnotes[$node_id];
  2240. unset($this->footnotes[$node_id]);
  2241. $num = count($this->footnotes_ordered);
  2242. $attr = " rel=\"footnote\"";
  2243. if ($this->fn_link_class != "") {
  2244. $class = $this->fn_link_class;
  2245. $class = $this->encodeAmpsAndAngles($class);
  2246. $class = str_replace('"', '&quot;', $class);
  2247. $attr .= " class=\"$class\"";
  2248. }
  2249. if ($this->fn_link_title != "") {
  2250. $title = $this->fn_link_title;
  2251. $title = $this->encodeAmpsAndAngles($title);
  2252. $title = str_replace('"', '&quot;', $title);
  2253. $attr .= " title=\"$title\"";
  2254. }
  2255. $attr = str_replace("%%", $num, $attr);
  2256. return
  2257. "<sup id=\"fnref:$node_id\">".
  2258. "<a href=\"#fn:$node_id\"$attr>$num</a>".
  2259. "</sup>";
  2260. }
  2261. return "[^".$matches[1]."]";
  2262. }
  2263. ### Abbreviations ###
  2264. function stripAbbreviations($text) {
  2265. #
  2266. # Strips abbreviations from text, stores titles in hash references.
  2267. #
  2268. $less_than_tab = $this->tab_width - 1;
  2269. # Link defs are in the form: [id]*: url "optional title"
  2270. $text = preg_replace_callback('{
  2271. ^[ ]{0,'.$less_than_tab.'}\*\[(.+?)\][ ]?: # abbr_id = $1
  2272. (.*) # text = $2 (no blank lines allowed)
  2273. }xm',
  2274. array(&$this, '_stripAbbreviations_callback'),
  2275. $text);
  2276. return $text;
  2277. }
  2278. function _stripAbbreviations_callback($matches) {
  2279. $abbr_word = $matches[1];
  2280. $abbr_desc = $matches[2];
  2281. $this->abbr_matches[] = preg_quote($abbr_word);
  2282. $this->abbr_desciptions[$abbr_word] = trim($abbr_desc);
  2283. return ''; # String that will replace the block
  2284. }
  2285. function doAbbreviations($text) {
  2286. #
  2287. # Find defined abbreviations in text and wrap them in <abbr> elements.
  2288. #
  2289. if ($this->abbr_matches) {
  2290. // cannot use the /x modifier because abbr_matches may
  2291. // contain spaces:
  2292. $text = preg_replace_callback('{'.
  2293. '(?<![\w\x1A])'.
  2294. '(?:'. implode('|', $this->abbr_matches) .')'.
  2295. '(?![\w\x1A])'.
  2296. '}',
  2297. array(&$this, '_doAbbreviations_callback'), $text);
  2298. }
  2299. return $text;
  2300. }
  2301. function _doAbbreviations_callback($matches) {
  2302. $abbr = $matches[0];
  2303. if (isset($this->abbr_desciptions[$abbr])) {
  2304. $desc = $this->abbr_desciptions[$abbr];
  2305. if (empty($desc)) {
  2306. return $this->hashSpan("<abbr>$abbr</abbr>");
  2307. } else {
  2308. $desc = $this->escapeSpecialCharsWithinTagAttributes($desc);
  2309. return $this->hashSpan("<abbr title=\"$desc\">$abbr</abbr>");
  2310. }
  2311. } else {
  2312. return $matches[0];
  2313. }
  2314. }
  2315. }
  2316. /*
  2317. PHP Markdown Extra
  2318. ==================
  2319. Description
  2320. -----------
  2321. This is a PHP port of the original Markdown formatter written in Perl
  2322. by John Gruber. This special "Extra" version of PHP Markdown features
  2323. further enhancements to the syntax for making additional constructs
  2324. such as tables and definition list.
  2325. Markdown is a text-to-HTML filter; it translates an easy-to-read /
  2326. easy-to-write structured text format into HTML. Markdown's text format
  2327. is most similar to that of plain text email, and supports features such
  2328. as headers, *emphasis*, code blocks, blockquotes, and links.
  2329. Markdown's syntax is designed not as a generic markup language, but
  2330. specifically to serve as a front-end to (X)HTML. You can use span-level
  2331. HTML tags anywhere in a Markdown document, and you can use block level
  2332. HTML tags (like <div> and <table> as well).
  2333. For more information about Markdown's syntax, see:
  2334. <http://daringfireball.net/projects/markdown/>
  2335. Bugs
  2336. ----
  2337. To file bug reports please send email to:
  2338. <michel.fortin@michelf.com>
  2339. Please include with your report: (1) the example input; (2) the output you
  2340. expected; (3) the output Markdown actually produced.
  2341. Version History
  2342. ---------------
  2343. See Readme file for details.
  2344. Extra 1.1.4 (3 Aug 2007):
  2345. Extra 1.1.3 (3 Jul 2007):
  2346. Extra 1.1.2 (7 Feb 2007)
  2347. Extra 1.1.1 (28 Dec 2006)
  2348. Extra 1.1 (1 Dec 2006)
  2349. Extra 1.0.1 (9 Dec 2005)
  2350. Extra 1.0 (5 Sep 2005)
  2351. Copyright and License
  2352. ---------------------
  2353. PHP Markdown & Extra
  2354. Copyright (c) 2004-2007 Michel Fortin
  2355. <http://www.michelf.com/>
  2356. All rights reserved.
  2357. Based on Markdown
  2358. Copyright (c) 2003-2006 John Gruber
  2359. <http://daringfireball.net/>
  2360. All rights reserved.
  2361. Redistribution and use in source and binary forms, with or without
  2362. modification, are permitted provided that the following conditions are
  2363. met:
  2364. * Redistributions of source code must retain the above copyright notice,
  2365. this list of conditions and the following disclaimer.
  2366. * Redistributions in binary form must reproduce the above copyright
  2367. notice, this list of conditions and the following disclaimer in the
  2368. documentation and/or other materials provided with the distribution.
  2369. * Neither the name "Markdown" nor the names of its contributors may
  2370. be used to endorse or promote products derived from this software
  2371. without specific prior written permission.
  2372. This software is provided by the copyright holders and contributors "as
  2373. is" and any express or implied warranties, including, but not limited
  2374. to, the implied warranties of merchantability and fitness for a
  2375. particular purpose are disclaimed. In no event shall the copyright owner
  2376. or contributors be liable for any direct, indirect, incidental, special,
  2377. exemplary, or consequential damages (including, but not limited to,
  2378. procurement of substitute goods or services; loss of use, data, or
  2379. profits; or business interruption) however caused and on any theory of
  2380. liability, whether in contract, strict liability, or tort (including
  2381. negligence or otherwise) arising in any way out of the use of this
  2382. software, even if advised of the possibility of such damage.
  2383. */
  2384. ?>