Markdown.pl 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. #!/usr/bin/perl
  2. #
  3. # Markdown -- A text-to-HTML conversion tool for web writers
  4. #
  5. # Copyright (c) 2004 John Gruber
  6. # <http://daringfireball.net/projects/markdown/>
  7. #
  8. package Markdown;
  9. require 5.006_000;
  10. use strict;
  11. use warnings;
  12. use Digest::MD5 qw(md5_hex);
  13. use vars qw($VERSION);
  14. $VERSION = '1.0.1';
  15. # Tue 14 Dec 2004
  16. ## Disabled; causes problems under Perl 5.6.1:
  17. # use utf8;
  18. # binmode( STDOUT, ":utf8" ); # c.f.: http://acis.openlib.org/dev/perl-unicode-struggle.html
  19. #
  20. # Global default settings:
  21. #
  22. my $g_empty_element_suffix = " />"; # Change to ">" for HTML output
  23. my $g_tab_width = 4;
  24. #
  25. # Globals:
  26. #
  27. # Regex to match balanced [brackets]. See Friedl's
  28. # "Mastering Regular Expressions", 2nd Ed., pp. 328-331.
  29. my $g_nested_brackets;
  30. $g_nested_brackets = qr{
  31. (?> # Atomic matching
  32. [^\[\]]+ # Anything other than brackets
  33. |
  34. \[
  35. (??{ $g_nested_brackets }) # Recursive set of nested brackets
  36. \]
  37. )*
  38. }x;
  39. # Table of hash values for escaped characters:
  40. my %g_escape_table;
  41. foreach my $char (split //, '\\`*_{}[]()>#+-.!') {
  42. $g_escape_table{$char} = md5_hex($char);
  43. }
  44. # Global hashes, used by various utility routines
  45. my %g_urls;
  46. my %g_titles;
  47. my %g_html_blocks;
  48. # Used to track when we're inside an ordered or unordered list
  49. # (see _ProcessListItems() for details):
  50. my $g_list_level = 0;
  51. #### Blosxom plug-in interface ##########################################
  52. # Set $g_blosxom_use_meta to 1 to use Blosxom's meta plug-in to determine
  53. # which posts Markdown should process, using a "meta-markup: markdown"
  54. # header. If it's set to 0 (the default), Markdown will process all
  55. # entries.
  56. my $g_blosxom_use_meta = 0;
  57. sub start { 1; }
  58. sub story {
  59. my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
  60. if ( (! $g_blosxom_use_meta) or
  61. (defined($meta::markup) and ($meta::markup =~ /^\s*markdown\s*$/i))
  62. ){
  63. $$body_ref = Markdown($$body_ref);
  64. }
  65. 1;
  66. }
  67. #### Movable Type plug-in interface #####################################
  68. eval {require MT}; # Test to see if we're running in MT.
  69. unless ($@) {
  70. require MT;
  71. import MT;
  72. require MT::Template::Context;
  73. import MT::Template::Context;
  74. eval {require MT::Plugin}; # Test to see if we're running >= MT 3.0.
  75. unless ($@) {
  76. require MT::Plugin;
  77. import MT::Plugin;
  78. my $plugin = new MT::Plugin({
  79. name => "Markdown",
  80. description => "A plain-text-to-HTML formatting plugin. (Version: $VERSION)",
  81. doc_link => 'http://daringfireball.net/projects/markdown/'
  82. });
  83. MT->add_plugin( $plugin );
  84. }
  85. MT::Template::Context->add_container_tag(MarkdownOptions => sub {
  86. my $ctx = shift;
  87. my $args = shift;
  88. my $builder = $ctx->stash('builder');
  89. my $tokens = $ctx->stash('tokens');
  90. if (defined ($args->{'output'}) ) {
  91. $ctx->stash('markdown_output', lc $args->{'output'});
  92. }
  93. defined (my $str = $builder->build($ctx, $tokens) )
  94. or return $ctx->error($builder->errstr);
  95. $str; # return value
  96. });
  97. MT->add_text_filter('markdown' => {
  98. label => 'Markdown',
  99. docs => 'http://daringfireball.net/projects/markdown/',
  100. on_format => sub {
  101. my $text = shift;
  102. my $ctx = shift;
  103. my $raw = 0;
  104. if (defined $ctx) {
  105. my $output = $ctx->stash('markdown_output');
  106. if (defined $output && $output =~ m/^html/i) {
  107. $g_empty_element_suffix = ">";
  108. $ctx->stash('markdown_output', '');
  109. }
  110. elsif (defined $output && $output eq 'raw') {
  111. $raw = 1;
  112. $ctx->stash('markdown_output', '');
  113. }
  114. else {
  115. $raw = 0;
  116. $g_empty_element_suffix = " />";
  117. }
  118. }
  119. $text = $raw ? $text : Markdown($text);
  120. $text;
  121. },
  122. });
  123. # If SmartyPants is loaded, add a combo Markdown/SmartyPants text filter:
  124. my $smartypants;
  125. {
  126. no warnings "once";
  127. $smartypants = $MT::Template::Context::Global_filters{'smarty_pants'};
  128. }
  129. if ($smartypants) {
  130. MT->add_text_filter('markdown_with_smartypants' => {
  131. label => 'Markdown With SmartyPants',
  132. docs => 'http://daringfireball.net/projects/markdown/',
  133. on_format => sub {
  134. my $text = shift;
  135. my $ctx = shift;
  136. if (defined $ctx) {
  137. my $output = $ctx->stash('markdown_output');
  138. if (defined $output && $output eq 'html') {
  139. $g_empty_element_suffix = ">";
  140. }
  141. else {
  142. $g_empty_element_suffix = " />";
  143. }
  144. }
  145. $text = Markdown($text);
  146. $text = $smartypants->($text, '1');
  147. },
  148. });
  149. }
  150. }
  151. else {
  152. #### BBEdit/command-line text filter interface ##########################
  153. # Needs to be hidden from MT (and Blosxom when running in static mode).
  154. # We're only using $blosxom::version once; tell Perl not to warn us:
  155. no warnings 'once';
  156. unless ( defined($blosxom::version) ) {
  157. use warnings;
  158. #### Check for command-line switches: #################
  159. my %cli_opts;
  160. use Getopt::Long;
  161. Getopt::Long::Configure('pass_through');
  162. GetOptions(\%cli_opts,
  163. 'version',
  164. 'shortversion',
  165. 'html4tags',
  166. );
  167. if ($cli_opts{'version'}) { # Version info
  168. print "\nThis is Markdown, version $VERSION.\n";
  169. print "Copyright 2004 John Gruber\n";
  170. print "http://daringfireball.net/projects/markdown/\n\n";
  171. exit 0;
  172. }
  173. if ($cli_opts{'shortversion'}) { # Just the version number string.
  174. print $VERSION;
  175. exit 0;
  176. }
  177. if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML
  178. $g_empty_element_suffix = ">";
  179. }
  180. #### Process incoming text: ###########################
  181. my $text;
  182. {
  183. local $/; # Slurp the whole file
  184. $text = <>;
  185. }
  186. print Markdown($text);
  187. }
  188. }
  189. sub Markdown {
  190. #
  191. # Main function. The order in which other subs are called here is
  192. # essential. Link and image substitutions need to happen before
  193. # _EscapeSpecialChars(), so that any *'s or _'s in the <a>
  194. # and <img> tags get encoded.
  195. #
  196. my $text = shift;
  197. # Clear the global hashes. If we don't clear these, you get conflicts
  198. # from other articles when generating a page which contains more than
  199. # one article (e.g. an index page that shows the N most recent
  200. # articles):
  201. %g_urls = ();
  202. %g_titles = ();
  203. %g_html_blocks = ();
  204. # Standardize line endings:
  205. $text =~ s{\r\n}{\n}g; # DOS to Unix
  206. $text =~ s{\r}{\n}g; # Mac to Unix
  207. # Make sure $text ends with a couple of newlines:
  208. $text .= "\n\n";
  209. # Convert all tabs to spaces.
  210. $text = _Detab($text);
  211. # Strip any lines consisting only of spaces and tabs.
  212. # This makes subsequent regexen easier to write, because we can
  213. # match consecutive blank lines with /\n+/ instead of something
  214. # contorted like /[ \t]*\n+/ .
  215. $text =~ s/^[ \t]+$//mg;
  216. # Turn block-level HTML blocks into hash entries
  217. $text = _HashHTMLBlocks($text);
  218. # Strip link definitions, store in hashes.
  219. $text = _StripLinkDefinitions($text);
  220. $text = _RunBlockGamut($text);
  221. $text = _UnescapeSpecialChars($text);
  222. return $text . "\n";
  223. }
  224. sub _StripLinkDefinitions {
  225. #
  226. # Strips link definitions from text, stores the URLs and titles in
  227. # hash references.
  228. #
  229. my $text = shift;
  230. my $less_than_tab = $g_tab_width - 1;
  231. # Link defs are in the form: ^[id]: url "optional title"
  232. while ($text =~ s{
  233. ^[ ]{0,$less_than_tab}\[(.+)\]: # id = $1
  234. [ \t]*
  235. \n? # maybe *one* newline
  236. [ \t]*
  237. <?(\S+?)>? # url = $2
  238. [ \t]*
  239. \n? # maybe one newline
  240. [ \t]*
  241. (?:
  242. (?<=\s) # lookbehind for whitespace
  243. ["(]
  244. (.+?) # title = $3
  245. [")]
  246. [ \t]*
  247. )? # title is optional
  248. (?:\n+|\Z)
  249. }
  250. {}mx) {
  251. $g_urls{lc $1} = _EncodeAmpsAndAngles( $2 ); # Link IDs are case-insensitive
  252. if ($3) {
  253. $g_titles{lc $1} = $3;
  254. $g_titles{lc $1} =~ s/"/&quot;/g;
  255. }
  256. }
  257. return $text;
  258. }
  259. sub _HashHTMLBlocks {
  260. my $text = shift;
  261. my $less_than_tab = $g_tab_width - 1;
  262. # Hashify HTML blocks:
  263. # We only want to do this for block-level HTML tags, such as headers,
  264. # lists, and tables. That's because we still want to wrap <p>s around
  265. # "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  266. # phrase emphasis, and spans. The list of tags we're looking for is
  267. # hard-coded:
  268. my $block_tags_a = qr/p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del/;
  269. my $block_tags_b = qr/p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math/;
  270. # First, look for nested blocks, e.g.:
  271. # <div>
  272. # <div>
  273. # tags for inner block must be indented.
  274. # </div>
  275. # </div>
  276. #
  277. # The outermost tags must start at the left margin for this to match, and
  278. # the inner nested divs must be indented.
  279. # We need to do this before the next, more liberal match, because the next
  280. # match will start at the first `<div>` and stop at the first `</div>`.
  281. $text =~ s{
  282. ( # save in $1
  283. ^ # start of line (with /m)
  284. <($block_tags_a) # start tag = $2
  285. \b # word break
  286. (.*\n)*? # any number of lines, minimally matching
  287. </\2> # the matching end tag
  288. [ \t]* # trailing spaces/tabs
  289. (?=\n+|\Z) # followed by a newline or end of document
  290. )
  291. }{
  292. my $key = md5_hex($1);
  293. $g_html_blocks{$key} = $1;
  294. "\n\n" . $key . "\n\n";
  295. }egmx;
  296. #
  297. # Now match more liberally, simply from `\n<tag>` to `</tag>\n`
  298. #
  299. $text =~ s{
  300. ( # save in $1
  301. ^ # start of line (with /m)
  302. <($block_tags_b) # start tag = $2
  303. \b # word break
  304. (.*\n)*? # any number of lines, minimally matching
  305. .*</\2> # the matching end tag
  306. [ \t]* # trailing spaces/tabs
  307. (?=\n+|\Z) # followed by a newline or end of document
  308. )
  309. }{
  310. my $key = md5_hex($1);
  311. $g_html_blocks{$key} = $1;
  312. "\n\n" . $key . "\n\n";
  313. }egmx;
  314. # Special case just for <hr />. It was easier to make a special case than
  315. # to make the other regex more complicated.
  316. $text =~ s{
  317. (?:
  318. (?<=\n\n) # Starting after a blank line
  319. | # or
  320. \A\n? # the beginning of the doc
  321. )
  322. ( # save in $1
  323. [ ]{0,$less_than_tab}
  324. <(hr) # start tag = $2
  325. \b # word break
  326. ([^<>])*? #
  327. /?> # the matching end tag
  328. [ \t]*
  329. (?=\n{2,}|\Z) # followed by a blank line or end of document
  330. )
  331. }{
  332. my $key = md5_hex($1);
  333. $g_html_blocks{$key} = $1;
  334. "\n\n" . $key . "\n\n";
  335. }egx;
  336. # Special case for standalone HTML comments:
  337. $text =~ s{
  338. (?:
  339. (?<=\n\n) # Starting after a blank line
  340. | # or
  341. \A\n? # the beginning of the doc
  342. )
  343. ( # save in $1
  344. [ ]{0,$less_than_tab}
  345. (?s:
  346. <!
  347. (--.*?--\s*)+
  348. >
  349. )
  350. [ \t]*
  351. (?=\n{2,}|\Z) # followed by a blank line or end of document
  352. )
  353. }{
  354. my $key = md5_hex($1);
  355. $g_html_blocks{$key} = $1;
  356. "\n\n" . $key . "\n\n";
  357. }egx;
  358. return $text;
  359. }
  360. sub _RunBlockGamut {
  361. #
  362. # These are all the transformations that form block-level
  363. # tags like paragraphs, headers, and list items.
  364. #
  365. my $text = shift;
  366. $text = _DoHeaders($text);
  367. # Do Horizontal Rules:
  368. $text =~ s{^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$}{\n<hr$g_empty_element_suffix\n}gmx;
  369. $text =~ s{^[ ]{0,2}([ ]? -[ ]?){3,}[ \t]*$}{\n<hr$g_empty_element_suffix\n}gmx;
  370. $text =~ s{^[ ]{0,2}([ ]? _[ ]?){3,}[ \t]*$}{\n<hr$g_empty_element_suffix\n}gmx;
  371. $text = _DoLists($text);
  372. $text = _DoCodeBlocks($text);
  373. $text = _DoBlockQuotes($text);
  374. # We already ran _HashHTMLBlocks() before, in Markdown(), but that
  375. # was to escape raw HTML in the original Markdown source. This time,
  376. # we're escaping the markup we've just created, so that we don't wrap
  377. # <p> tags around block-level tags.
  378. $text = _HashHTMLBlocks($text);
  379. $text = _FormParagraphs($text);
  380. return $text;
  381. }
  382. sub _RunSpanGamut {
  383. #
  384. # These are all the transformations that occur *within* block-level
  385. # tags like paragraphs, headers, and list items.
  386. #
  387. my $text = shift;
  388. $text = _DoCodeSpans($text);
  389. $text = _EscapeSpecialChars($text);
  390. # Process anchor and image tags. Images must come first,
  391. # because ![foo][f] looks like an anchor.
  392. $text = _DoImages($text);
  393. $text = _DoAnchors($text);
  394. # Make links out of things like `<http://example.com/>`
  395. # Must come after _DoAnchors(), because you can use < and >
  396. # delimiters in inline links like [this](<url>).
  397. $text = _DoAutoLinks($text);
  398. $text = _EncodeAmpsAndAngles($text);
  399. $text = _DoItalicsAndBold($text);
  400. # Do hard breaks:
  401. $text =~ s/ {2,}\n/ <br$g_empty_element_suffix\n/g;
  402. return $text;
  403. }
  404. sub _EscapeSpecialChars {
  405. my $text = shift;
  406. my $tokens ||= _TokenizeHTML($text);
  407. $text = ''; # rebuild $text from the tokens
  408. # my $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags.
  409. # my $tags_to_skip = qr!<(/?)(?:pre|code|kbd|script|math)[\s>]!;
  410. foreach my $cur_token (@$tokens) {
  411. if ($cur_token->[0] eq "tag") {
  412. # Within tags, encode * and _ so they don't conflict
  413. # with their use in Markdown for italics and strong.
  414. # We're replacing each such character with its
  415. # corresponding MD5 checksum value; this is likely
  416. # overkill, but it should prevent us from colliding
  417. # with the escape values by accident.
  418. $cur_token->[1] =~ s! \* !$g_escape_table{'*'}!gx;
  419. $cur_token->[1] =~ s! _ !$g_escape_table{'_'}!gx;
  420. $text .= $cur_token->[1];
  421. } else {
  422. my $t = $cur_token->[1];
  423. $t = _EncodeBackslashEscapes($t);
  424. $text .= $t;
  425. }
  426. }
  427. return $text;
  428. }
  429. sub _DoAnchors {
  430. #
  431. # Turn Markdown link shortcuts into XHTML <a> tags.
  432. #
  433. my $text = shift;
  434. #
  435. # First, handle reference-style links: [link text] [id]
  436. #
  437. $text =~ s{
  438. ( # wrap whole match in $1
  439. \[
  440. ($g_nested_brackets) # link text = $2
  441. \]
  442. [ ]? # one optional space
  443. (?:\n[ ]*)? # one optional newline followed by spaces
  444. \[
  445. (.*?) # id = $3
  446. \]
  447. )
  448. }{
  449. my $result;
  450. my $whole_match = $1;
  451. my $link_text = $2;
  452. my $link_id = lc $3;
  453. if ($link_id eq "") {
  454. $link_id = lc $link_text; # for shortcut links like [this][].
  455. }
  456. if (defined $g_urls{$link_id}) {
  457. my $url = $g_urls{$link_id};
  458. $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
  459. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold.
  460. $result = "<a href=\"$url\"";
  461. if ( defined $g_titles{$link_id} ) {
  462. my $title = $g_titles{$link_id};
  463. $title =~ s! \* !$g_escape_table{'*'}!gx;
  464. $title =~ s! _ !$g_escape_table{'_'}!gx;
  465. $result .= " title=\"$title\"";
  466. }
  467. $result .= ">$link_text</a>";
  468. }
  469. else {
  470. $result = $whole_match;
  471. }
  472. $result;
  473. }xsge;
  474. #
  475. # Next, inline-style links: [link text](url "optional title")
  476. #
  477. $text =~ s{
  478. ( # wrap whole match in $1
  479. \[
  480. ($g_nested_brackets) # link text = $2
  481. \]
  482. \( # literal paren
  483. [ \t]*
  484. <?(.*?)>? # href = $3
  485. [ \t]*
  486. ( # $4
  487. (['"]) # quote char = $5
  488. (.*?) # Title = $6
  489. \5 # matching quote
  490. )? # title is optional
  491. \)
  492. )
  493. }{
  494. my $result;
  495. my $whole_match = $1;
  496. my $link_text = $2;
  497. my $url = $3;
  498. my $title = $6;
  499. $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
  500. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold.
  501. $result = "<a href=\"$url\"";
  502. if (defined $title) {
  503. $title =~ s/"/&quot;/g;
  504. $title =~ s! \* !$g_escape_table{'*'}!gx;
  505. $title =~ s! _ !$g_escape_table{'_'}!gx;
  506. $result .= " title=\"$title\"";
  507. }
  508. $result .= ">$link_text</a>";
  509. $result;
  510. }xsge;
  511. return $text;
  512. }
  513. sub _DoImages {
  514. #
  515. # Turn Markdown image shortcuts into <img> tags.
  516. #
  517. my $text = shift;
  518. #
  519. # First, handle reference-style labeled images: ![alt text][id]
  520. #
  521. $text =~ s{
  522. ( # wrap whole match in $1
  523. !\[
  524. (.*?) # alt text = $2
  525. \]
  526. [ ]? # one optional space
  527. (?:\n[ ]*)? # one optional newline followed by spaces
  528. \[
  529. (.*?) # id = $3
  530. \]
  531. )
  532. }{
  533. my $result;
  534. my $whole_match = $1;
  535. my $alt_text = $2;
  536. my $link_id = lc $3;
  537. if ($link_id eq "") {
  538. $link_id = lc $alt_text; # for shortcut links like ![this][].
  539. }
  540. $alt_text =~ s/"/&quot;/g;
  541. if (defined $g_urls{$link_id}) {
  542. my $url = $g_urls{$link_id};
  543. $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
  544. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold.
  545. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  546. if (defined $g_titles{$link_id}) {
  547. my $title = $g_titles{$link_id};
  548. $title =~ s! \* !$g_escape_table{'*'}!gx;
  549. $title =~ s! _ !$g_escape_table{'_'}!gx;
  550. $result .= " title=\"$title\"";
  551. }
  552. $result .= $g_empty_element_suffix;
  553. }
  554. else {
  555. # If there's no such link ID, leave intact:
  556. $result = $whole_match;
  557. }
  558. $result;
  559. }xsge;
  560. #
  561. # Next, handle inline images: ![alt text](url "optional title")
  562. # Don't forget: encode * and _
  563. $text =~ s{
  564. ( # wrap whole match in $1
  565. !\[
  566. (.*?) # alt text = $2
  567. \]
  568. \( # literal paren
  569. [ \t]*
  570. <?(\S+?)>? # src url = $3
  571. [ \t]*
  572. ( # $4
  573. (['"]) # quote char = $5
  574. (.*?) # title = $6
  575. \5 # matching quote
  576. [ \t]*
  577. )? # title is optional
  578. \)
  579. )
  580. }{
  581. my $result;
  582. my $whole_match = $1;
  583. my $alt_text = $2;
  584. my $url = $3;
  585. my $title = '';
  586. if (defined($6)) {
  587. $title = $6;
  588. }
  589. $alt_text =~ s/"/&quot;/g;
  590. $title =~ s/"/&quot;/g;
  591. $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
  592. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold.
  593. $result = "<img src=\"$url\" alt=\"$alt_text\"";
  594. if (defined $title) {
  595. $title =~ s! \* !$g_escape_table{'*'}!gx;
  596. $title =~ s! _ !$g_escape_table{'_'}!gx;
  597. $result .= " title=\"$title\"";
  598. }
  599. $result .= $g_empty_element_suffix;
  600. $result;
  601. }xsge;
  602. return $text;
  603. }
  604. sub _DoHeaders {
  605. my $text = shift;
  606. # Setext-style headers:
  607. # Header 1
  608. # ========
  609. #
  610. # Header 2
  611. # --------
  612. #
  613. $text =~ s{ ^(.+)[ \t]*\n=+[ \t]*\n+ }{
  614. "<h1>" . _RunSpanGamut($1) . "</h1>\n\n";
  615. }egmx;
  616. $text =~ s{ ^(.+)[ \t]*\n-+[ \t]*\n+ }{
  617. "<h2>" . _RunSpanGamut($1) . "</h2>\n\n";
  618. }egmx;
  619. # atx-style headers:
  620. # # Header 1
  621. # ## Header 2
  622. # ## Header 2 with closing hashes ##
  623. # ...
  624. # ###### Header 6
  625. #
  626. $text =~ s{
  627. ^(\#{1,6}) # $1 = string of #'s
  628. [ \t]*
  629. (.+?) # $2 = Header text
  630. [ \t]*
  631. \#* # optional closing #'s (not counted)
  632. \n+
  633. }{
  634. my $h_level = length($1);
  635. "<h$h_level>" . _RunSpanGamut($2) . "</h$h_level>\n\n";
  636. }egmx;
  637. return $text;
  638. }
  639. sub _DoLists {
  640. #
  641. # Form HTML ordered (numbered) and unordered (bulleted) lists.
  642. #
  643. my $text = shift;
  644. my $less_than_tab = $g_tab_width - 1;
  645. # Re-usable patterns to match list item bullets and number markers:
  646. my $marker_ul = qr/[*+-]/;
  647. my $marker_ol = qr/\d+[.]/;
  648. my $marker_any = qr/(?:$marker_ul|$marker_ol)/;
  649. # Re-usable pattern to match any entirel ul or ol list:
  650. my $whole_list = qr{
  651. ( # $1 = whole list
  652. ( # $2
  653. [ ]{0,$less_than_tab}
  654. (${marker_any}) # $3 = first list item marker
  655. [ \t]+
  656. )
  657. (?s:.+?)
  658. ( # $4
  659. \z
  660. |
  661. \n{2,}
  662. (?=\S)
  663. (?! # Negative lookahead for another list item marker
  664. [ \t]*
  665. ${marker_any}[ \t]+
  666. )
  667. )
  668. )
  669. }mx;
  670. # We use a different prefix before nested lists than top-level lists.
  671. # See extended comment in _ProcessListItems().
  672. #
  673. # Note: There's a bit of duplication here. My original implementation
  674. # created a scalar regex pattern as the conditional result of the test on
  675. # $g_list_level, and then only ran the $text =~ s{...}{...}egmx
  676. # substitution once, using the scalar as the pattern. This worked,
  677. # everywhere except when running under MT on my hosting account at Pair
  678. # Networks. There, this caused all rebuilds to be killed by the reaper (or
  679. # perhaps they crashed, but that seems incredibly unlikely given that the
  680. # same script on the same server ran fine *except* under MT. I've spent
  681. # more time trying to figure out why this is happening than I'd like to
  682. # admit. My only guess, backed up by the fact that this workaround works,
  683. # is that Perl optimizes the substition when it can figure out that the
  684. # pattern will never change, and when this optimization isn't on, we run
  685. # afoul of the reaper. Thus, the slightly redundant code to that uses two
  686. # static s/// patterns rather than one conditional pattern.
  687. if ($g_list_level) {
  688. $text =~ s{
  689. ^
  690. $whole_list
  691. }{
  692. my $list = $1;
  693. my $list_type = ($3 =~ m/$marker_ul/) ? "ul" : "ol";
  694. # Turn double returns into triple returns, so that we can make a
  695. # paragraph for the last item in a list, if necessary:
  696. $list =~ s/\n{2,}/\n\n\n/g;
  697. my $result = _ProcessListItems($list, $marker_any);
  698. $result = "<$list_type>\n" . $result . "</$list_type>\n";
  699. $result;
  700. }egmx;
  701. }
  702. else {
  703. $text =~ s{
  704. (?:(?<=\n\n)|\A\n?)
  705. $whole_list
  706. }{
  707. my $list = $1;
  708. my $list_type = ($3 =~ m/$marker_ul/) ? "ul" : "ol";
  709. # Turn double returns into triple returns, so that we can make a
  710. # paragraph for the last item in a list, if necessary:
  711. $list =~ s/\n{2,}/\n\n\n/g;
  712. my $result = _ProcessListItems($list, $marker_any);
  713. $result = "<$list_type>\n" . $result . "</$list_type>\n";
  714. $result;
  715. }egmx;
  716. }
  717. return $text;
  718. }
  719. sub _ProcessListItems {
  720. #
  721. # Process the contents of a single ordered or unordered list, splitting it
  722. # into individual list items.
  723. #
  724. my $list_str = shift;
  725. my $marker_any = shift;
  726. # The $g_list_level global keeps track of when we're inside a list.
  727. # Each time we enter a list, we increment it; when we leave a list,
  728. # we decrement. If it's zero, we're not in a list anymore.
  729. #
  730. # We do this because when we're not inside a list, we want to treat
  731. # something like this:
  732. #
  733. # I recommend upgrading to version
  734. # 8. Oops, now this line is treated
  735. # as a sub-list.
  736. #
  737. # As a single paragraph, despite the fact that the second line starts
  738. # with a digit-period-space sequence.
  739. #
  740. # Whereas when we're inside a list (or sub-list), that line will be
  741. # treated as the start of a sub-list. What a kludge, huh? This is
  742. # an aspect of Markdown's syntax that's hard to parse perfectly
  743. # without resorting to mind-reading. Perhaps the solution is to
  744. # change the syntax rules such that sub-lists must start with a
  745. # starting cardinal number; e.g. "1." or "a.".
  746. $g_list_level++;
  747. # trim trailing blank lines:
  748. $list_str =~ s/\n{2,}\z/\n/;
  749. $list_str =~ s{
  750. (\n)? # leading line = $1
  751. (^[ \t]*) # leading whitespace = $2
  752. ($marker_any) [ \t]+ # list marker = $3
  753. ((?s:.+?) # list item text = $4
  754. (\n{1,2}))
  755. (?= \n* (\z | \2 ($marker_any) [ \t]+))
  756. }{
  757. my $item = $4;
  758. my $leading_line = $1;
  759. my $leading_space = $2;
  760. if ($leading_line or ($item =~ m/\n{2,}/)) {
  761. $item = _RunBlockGamut(_Outdent($item));
  762. }
  763. else {
  764. # Recursion for sub-lists:
  765. $item = _DoLists(_Outdent($item));
  766. chomp $item;
  767. $item = _RunSpanGamut($item);
  768. }
  769. "<li>" . $item . "</li>\n";
  770. }egmx;
  771. $g_list_level--;
  772. return $list_str;
  773. }
  774. sub _DoCodeBlocks {
  775. #
  776. # Process Markdown `<pre><code>` blocks.
  777. #
  778. my $text = shift;
  779. $text =~ s{
  780. (?:\n\n|\A)
  781. ( # $1 = the code block -- one or more lines, starting with a space/tab
  782. (?:
  783. (?:[ ]{$g_tab_width} | \t) # Lines must start with a tab or a tab-width of spaces
  784. .*\n+
  785. )+
  786. )
  787. ((?=^[ ]{0,$g_tab_width}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
  788. }{
  789. my $codeblock = $1;
  790. my $result; # return value
  791. $codeblock = _EncodeCode(_Outdent($codeblock));
  792. $codeblock = _Detab($codeblock);
  793. $codeblock =~ s/\A\n+//; # trim leading newlines
  794. $codeblock =~ s/\s+\z//; # trim trailing whitespace
  795. $result = "\n\n<pre><code>" . $codeblock . "\n</code></pre>\n\n";
  796. $result;
  797. }egmx;
  798. return $text;
  799. }
  800. sub _DoCodeSpans {
  801. #
  802. # * Backtick quotes are used for <code></code> spans.
  803. #
  804. # * You can use multiple backticks as the delimiters if you want to
  805. # include literal backticks in the code span. So, this input:
  806. #
  807. # Just type ``foo `bar` baz`` at the prompt.
  808. #
  809. # Will translate to:
  810. #
  811. # <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
  812. #
  813. # There's no arbitrary limit to the number of backticks you
  814. # can use as delimters. If you need three consecutive backticks
  815. # in your code, use four for delimiters, etc.
  816. #
  817. # * You can use spaces to get literal backticks at the edges:
  818. #
  819. # ... type `` `bar` `` ...
  820. #
  821. # Turns to:
  822. #
  823. # ... type <code>`bar`</code> ...
  824. #
  825. my $text = shift;
  826. $text =~ s@
  827. (`+) # $1 = Opening run of `
  828. (.+?) # $2 = The code block
  829. (?<!`)
  830. \1 # Matching closer
  831. (?!`)
  832. @
  833. my $c = "$2";
  834. $c =~ s/^[ \t]*//g; # leading whitespace
  835. $c =~ s/[ \t]*$//g; # trailing whitespace
  836. $c = _EncodeCode($c);
  837. "<code>$c</code>";
  838. @egsx;
  839. return $text;
  840. }
  841. sub _EncodeCode {
  842. #
  843. # Encode/escape certain characters inside Markdown code runs.
  844. # The point is that in code, these characters are literals,
  845. # and lose their special Markdown meanings.
  846. #
  847. local $_ = shift;
  848. # Encode all ampersands; HTML entities are not
  849. # entities within a Markdown code span.
  850. s/&/&amp;/g;
  851. # Encode $'s, but only if we're running under Blosxom.
  852. # (Blosxom interpolates Perl variables in article bodies.)
  853. {
  854. no warnings 'once';
  855. if (defined($blosxom::version)) {
  856. s/\$/&#036;/g;
  857. }
  858. }
  859. # Do the angle bracket song and dance:
  860. s! < !&lt;!gx;
  861. s! > !&gt;!gx;
  862. # Now, escape characters that are magic in Markdown:
  863. s! \* !$g_escape_table{'*'}!gx;
  864. s! _ !$g_escape_table{'_'}!gx;
  865. s! { !$g_escape_table{'{'}!gx;
  866. s! } !$g_escape_table{'}'}!gx;
  867. s! \[ !$g_escape_table{'['}!gx;
  868. s! \] !$g_escape_table{']'}!gx;
  869. s! \\ !$g_escape_table{'\\'}!gx;
  870. return $_;
  871. }
  872. sub _DoItalicsAndBold {
  873. my $text = shift;
  874. # <strong> must go first:
  875. $text =~ s{ (\*\*|__) (?=\S) (.+?[*_]*) (?<=\S) \1 }
  876. {<strong>$2</strong>}gsx;
  877. $text =~ s{ (\*|_) (?=\S) (.+?) (?<=\S) \1 }
  878. {<em>$2</em>}gsx;
  879. return $text;
  880. }
  881. sub _DoBlockQuotes {
  882. my $text = shift;
  883. $text =~ s{
  884. ( # Wrap whole match in $1
  885. (
  886. ^[ \t]*>[ \t]? # '>' at the start of a line
  887. .+\n # rest of the first line
  888. (.+\n)* # subsequent consecutive lines
  889. \n* # blanks
  890. )+
  891. )
  892. }{
  893. my $bq = $1;
  894. $bq =~ s/^[ \t]*>[ \t]?//gm; # trim one level of quoting
  895. $bq =~ s/^[ \t]+$//mg; # trim whitespace-only lines
  896. $bq = _RunBlockGamut($bq); # recurse
  897. $bq =~ s/^/ /g;
  898. # These leading spaces screw with <pre> content, so we need to fix that:
  899. $bq =~ s{
  900. (\s*<pre>.+?</pre>)
  901. }{
  902. my $pre = $1;
  903. $pre =~ s/^ //mg;
  904. $pre;
  905. }egsx;
  906. "<blockquote>\n$bq\n</blockquote>\n\n";
  907. }egmx;
  908. return $text;
  909. }
  910. sub _FormParagraphs {
  911. #
  912. # Params:
  913. # $text - string to process with html <p> tags
  914. #
  915. my $text = shift;
  916. # Strip leading and trailing lines:
  917. $text =~ s/\A\n+//;
  918. $text =~ s/\n+\z//;
  919. my @grafs = split(/\n{2,}/, $text);
  920. #
  921. # Wrap <p> tags.
  922. #
  923. foreach (@grafs) {
  924. unless (defined( $g_html_blocks{$_} )) {
  925. $_ = _RunSpanGamut($_);
  926. s/^([ \t]*)/<p>/;
  927. $_ .= "</p>";
  928. }
  929. }
  930. #
  931. # Unhashify HTML blocks
  932. #
  933. foreach (@grafs) {
  934. if (defined( $g_html_blocks{$_} )) {
  935. $_ = $g_html_blocks{$_};
  936. }
  937. }
  938. return join "\n\n", @grafs;
  939. }
  940. sub _EncodeAmpsAndAngles {
  941. # Smart processing for ampersands and angle brackets that need to be encoded.
  942. my $text = shift;
  943. # Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
  944. # http://bumppo.net/projects/amputator/
  945. $text =~ s/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/&amp;/g;
  946. # Encode naked <'s
  947. $text =~ s{<(?![a-z/?\$!])}{&lt;}gi;
  948. return $text;
  949. }
  950. sub _EncodeBackslashEscapes {
  951. #
  952. # Parameter: String.
  953. # Returns: The string, with after processing the following backslash
  954. # escape sequences.
  955. #
  956. local $_ = shift;
  957. s! \\\\ !$g_escape_table{'\\'}!gx; # Must process escaped backslashes first.
  958. s! \\` !$g_escape_table{'`'}!gx;
  959. s! \\\* !$g_escape_table{'*'}!gx;
  960. s! \\_ !$g_escape_table{'_'}!gx;
  961. s! \\\{ !$g_escape_table{'{'}!gx;
  962. s! \\\} !$g_escape_table{'}'}!gx;
  963. s! \\\[ !$g_escape_table{'['}!gx;
  964. s! \\\] !$g_escape_table{']'}!gx;
  965. s! \\\( !$g_escape_table{'('}!gx;
  966. s! \\\) !$g_escape_table{')'}!gx;
  967. s! \\> !$g_escape_table{'>'}!gx;
  968. s! \\\# !$g_escape_table{'#'}!gx;
  969. s! \\\+ !$g_escape_table{'+'}!gx;
  970. s! \\\- !$g_escape_table{'-'}!gx;
  971. s! \\\. !$g_escape_table{'.'}!gx;
  972. s{ \\! }{$g_escape_table{'!'}}gx;
  973. return $_;
  974. }
  975. sub _DoAutoLinks {
  976. my $text = shift;
  977. $text =~ s{<((https?|ftp):[^'">\s]+)>}{<a href="$1">$1</a>}gi;
  978. # Email addresses: <address@domain.foo>
  979. $text =~ s{
  980. <
  981. (?:mailto:)?
  982. (
  983. [-.\w]+
  984. \@
  985. [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
  986. )
  987. >
  988. }{
  989. _EncodeEmailAddress( _UnescapeSpecialChars($1) );
  990. }egix;
  991. return $text;
  992. }
  993. sub _EncodeEmailAddress {
  994. #
  995. # Input: an email address, e.g. "foo@example.com"
  996. #
  997. # Output: the email address as a mailto link, with each character
  998. # of the address encoded as either a decimal or hex entity, in
  999. # the hopes of foiling most address harvesting spam bots. E.g.:
  1000. #
  1001. # <a href="&#x6D;&#97;&#105;&#108;&#x74;&#111;:&#102;&#111;&#111;&#64;&#101;
  1002. # x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;">&#102;&#111;&#111;
  1003. # &#64;&#101;x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;</a>
  1004. #
  1005. # Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
  1006. # mailing list: <http://tinyurl.com/yu7ue>
  1007. #
  1008. my $addr = shift;
  1009. srand;
  1010. my @encode = (
  1011. sub { '&#' . ord(shift) . ';' },
  1012. sub { '&#x' . sprintf( "%X", ord(shift) ) . ';' },
  1013. sub { shift },
  1014. );
  1015. $addr = "mailto:" . $addr;
  1016. $addr =~ s{(.)}{
  1017. my $char = $1;
  1018. if ( $char eq '@' ) {
  1019. # this *must* be encoded. I insist.
  1020. $char = $encode[int rand 1]->($char);
  1021. } elsif ( $char ne ':' ) {
  1022. # leave ':' alone (to spot mailto: later)
  1023. my $r = rand;
  1024. # roughly 10% raw, 45% hex, 45% dec
  1025. $char = (
  1026. $r > .9 ? $encode[2]->($char) :
  1027. $r < .45 ? $encode[1]->($char) :
  1028. $encode[0]->($char)
  1029. );
  1030. }
  1031. $char;
  1032. }gex;
  1033. $addr = qq{<a href="$addr">$addr</a>};
  1034. $addr =~ s{">.+?:}{">}; # strip the mailto: from the visible part
  1035. return $addr;
  1036. }
  1037. sub _UnescapeSpecialChars {
  1038. #
  1039. # Swap back in all the special characters we've hidden.
  1040. #
  1041. my $text = shift;
  1042. while( my($char, $hash) = each(%g_escape_table) ) {
  1043. $text =~ s/$hash/$char/g;
  1044. }
  1045. return $text;
  1046. }
  1047. sub _TokenizeHTML {
  1048. #
  1049. # Parameter: String containing HTML markup.
  1050. # Returns: Reference to an array of the tokens comprising the input
  1051. # string. Each token is either a tag (possibly with nested,
  1052. # tags contained therein, such as <a href="<MTFoo>">, or a
  1053. # run of text between tags. Each element of the array is a
  1054. # two-element array; the first is either 'tag' or 'text';
  1055. # the second is the actual value.
  1056. #
  1057. #
  1058. # Derived from the _tokenize() subroutine from Brad Choate's MTRegex plugin.
  1059. # <http://www.bradchoate.com/past/mtregex.php>
  1060. #
  1061. my $str = shift;
  1062. my $pos = 0;
  1063. my $len = length $str;
  1064. my @tokens;
  1065. my $depth = 6;
  1066. my $nested_tags = join('|', ('(?:<[a-z/!$](?:[^<>]') x $depth) . (')*>)' x $depth);
  1067. my $match = qr/(?s: <! ( -- .*? -- \s* )+ > ) | # comment
  1068. (?s: <\? .*? \?> ) | # processing instruction
  1069. $nested_tags/ix; # nested tags
  1070. while ($str =~ m/($match)/g) {
  1071. my $whole_tag = $1;
  1072. my $sec_start = pos $str;
  1073. my $tag_start = $sec_start - length $whole_tag;
  1074. if ($pos < $tag_start) {
  1075. push @tokens, ['text', substr($str, $pos, $tag_start - $pos)];
  1076. }
  1077. push @tokens, ['tag', $whole_tag];
  1078. $pos = pos $str;
  1079. }
  1080. push @tokens, ['text', substr($str, $pos, $len - $pos)] if $pos < $len;
  1081. \@tokens;
  1082. }
  1083. sub _Outdent {
  1084. #
  1085. # Remove one level of line-leading tabs or spaces
  1086. #
  1087. my $text = shift;
  1088. $text =~ s/^(\t|[ ]{1,$g_tab_width})//gm;
  1089. return $text;
  1090. }
  1091. sub _Detab {
  1092. #
  1093. # Cribbed from a post by Bart Lateur:
  1094. # <http://www.nntp.perl.org/group/perl.macperl.anyperl/154>
  1095. #
  1096. my $text = shift;
  1097. $text =~ s{(.*?)\t}{$1.(' ' x ($g_tab_width - length($1) % $g_tab_width))}ge;
  1098. return $text;
  1099. }
  1100. 1;
  1101. __END__
  1102. =pod
  1103. =head1 NAME
  1104. B<Markdown>
  1105. =head1 SYNOPSIS
  1106. B<Markdown.pl> [ B<--html4tags> ] [ B<--version> ] [ B<-shortversion> ]
  1107. [ I<file> ... ]
  1108. =head1 DESCRIPTION
  1109. Markdown is a text-to-HTML filter; it translates an easy-to-read /
  1110. easy-to-write structured text format into HTML. Markdown's text format
  1111. is most similar to that of plain text email, and supports features such
  1112. as headers, *emphasis*, code blocks, blockquotes, and links.
  1113. Markdown's syntax is designed not as a generic markup language, but
  1114. specifically to serve as a front-end to (X)HTML. You can use span-level
  1115. HTML tags anywhere in a Markdown document, and you can use block level
  1116. HTML tags (like <div> and <table> as well).
  1117. For more information about Markdown's syntax, see:
  1118. http://daringfireball.net/projects/markdown/
  1119. =head1 OPTIONS
  1120. Use "--" to end switch parsing. For example, to open a file named "-z", use:
  1121. Markdown.pl -- -z
  1122. =over 4
  1123. =item B<--html4tags>
  1124. Use HTML 4 style for empty element tags, e.g.:
  1125. <br>
  1126. instead of Markdown's default XHTML style tags, e.g.:
  1127. <br />
  1128. =item B<-v>, B<--version>
  1129. Display Markdown's version number and copyright information.
  1130. =item B<-s>, B<--shortversion>
  1131. Display the short-form version number.
  1132. =back
  1133. =head1 BUGS
  1134. To file bug reports or feature requests (other than topics listed in the
  1135. Caveats section above) please send email to:
  1136. support@daringfireball.net
  1137. Please include with your report: (1) the example input; (2) the output
  1138. you expected; (3) the output Markdown actually produced.
  1139. =head1 VERSION HISTORY
  1140. See the readme file for detailed release notes for this version.
  1141. 1.0.1 - 14 Dec 2004
  1142. 1.0 - 28 Aug 2004
  1143. =head1 AUTHOR
  1144. John Gruber
  1145. http://daringfireball.net
  1146. PHP port and other contributions by Michel Fortin
  1147. http://michelf.com
  1148. =head1 COPYRIGHT AND LICENSE
  1149. Copyright (c) 2003-2004 John Gruber
  1150. <http://daringfireball.net/>
  1151. All rights reserved.
  1152. Redistribution and use in source and binary forms, with or without
  1153. modification, are permitted provided that the following conditions are
  1154. met:
  1155. * Redistributions of source code must retain the above copyright notice,
  1156. this list of conditions and the following disclaimer.
  1157. * Redistributions in binary form must reproduce the above copyright
  1158. notice, this list of conditions and the following disclaimer in the
  1159. documentation and/or other materials provided with the distribution.
  1160. * Neither the name "Markdown" nor the names of its contributors may
  1161. be used to endorse or promote products derived from this software
  1162. without specific prior written permission.
  1163. This software is provided by the copyright holders and contributors "as
  1164. is" and any express or implied warranties, including, but not limited
  1165. to, the implied warranties of merchantability and fitness for a
  1166. particular purpose are disclaimed. In no event shall the copyright owner
  1167. or contributors be liable for any direct, indirect, incidental, special,
  1168. exemplary, or consequential damages (including, but not limited to,
  1169. procurement of substitute goods or services; loss of use, data, or
  1170. profits; or business interruption) however caused and on any theory of
  1171. liability, whether in contract, strict liability, or tort (including
  1172. negligence or otherwise) arising in any way out of the use of this
  1173. software, even if advised of the possibility of such damage.
  1174. =cut