PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/system/vendor/Markdown.php

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