Text::Markdown::Discount
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.3 KiB

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use ExtUtils::testlib;
  5. use Text::Markdown::Discount qw(markdown);
  6. =head1 NAME
  7. Markdown.pl - Convert Markdown syntax to (X)HTML
  8. =head1 DESCRIPTION
  9. This program is distributed as part of Perl's Text::Markdown module,
  10. illustrating sample usage.
  11. Markdown can be invoked on any file containing Markdown-syntax, and
  12. will produce the corresponding (X)HTML on STDOUT:
  13. $ cat file.txt
  14. This is a *test*.
  15. Absolutely _nothing_ to see here. _Just a **test**_!
  16. * test
  17. * Yup, test.
  18. $ Markdown.pl file.txt
  19. <p>This is a <em>test</em>.</p>
  20. <p>Absolutely <em>nothing</em> to see here. <em>Just a <strong>test</strong></em>!</p>
  21. <ul>
  22. <li>test</li>
  23. <li>Yup, test.</li>
  24. </ul>
  25. If no file is specified, it will expect its input from STDIN:
  26. $ echo "A **simple** test" | markdown
  27. <p>A <strong>simple</strong> test</p>
  28. =head1 OPTIONS
  29. =over
  30. =item version
  31. Shows the full information for this version
  32. =item shortversion
  33. Shows only the version number
  34. =item html4tags
  35. Produce HTML 4-style tags instead of XHTML - XHTML requires elements
  36. that do not wrap a block (i.e. the C<hr> tag) to state they will not
  37. be closed, by closing with C</E<gt>>. HTML 4-style will plainly output
  38. the tag as it comes:
  39. $ echo '---' | markdown
  40. <hr />
  41. $ echo '---' | markdown --html4tags
  42. <hr>
  43. =item help
  44. Shows this documentation
  45. =back
  46. =head1 AUTHOR
  47. Copyright 2004 John Gruber
  48. Copyright 2008 Tomas Doran
  49. The manpage was written by Gunnar Wolf <gwolf@debian.org> for its use
  50. in Debian systems, but can be freely used elsewhere.
  51. For full licensing information, please refer to
  52. L<Text::Markdown.pm>'s full documentation.
  53. =head1 SEE ALSO
  54. L<Text::Markdown>, L<http://daringfireball.net/projects/markdown/>
  55. =cut
  56. #### Check for command-line switches: #################
  57. my %cli_opts;
  58. use Getopt::Long;
  59. Getopt::Long::Configure('pass_through');
  60. GetOptions(\%cli_opts,
  61. 'version',
  62. 'shortversion',
  63. 'html4tags',
  64. 'help',
  65. );
  66. if ($cli_opts{'version'}) { # Version info
  67. print "\nThis is Markdown, version $Text::Markdown::VERSION.\n";
  68. print "Copyright 2004 John Gruber\n";
  69. print "Copyright 2008 Tomas Doran\n";
  70. print "Parts contributed by several other people.";
  71. print "http://daringfireball.net/projects/markdown/\n\n";
  72. exit 0;
  73. }
  74. if ($cli_opts{'shortversion'}) { # Just the version number string.
  75. print $Text::Markdown::VERSION;
  76. exit 0;
  77. }
  78. if ($cli_opts{'help'}) {
  79. for my $dir (split m/:/, $ENV{PATH}) {
  80. my $cmd = "$dir/perldoc";
  81. exec($cmd, $0) if (-f $cmd and -x $cmd);
  82. }
  83. die "perldoc could not be found in your path - Cannot show help, sorry\n";
  84. }
  85. my $m;
  86. if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML
  87. $m = Text::Markdown::Discount->new(empty_element_suffix => '>');
  88. }
  89. else {
  90. $m = Text::Markdown::Discount->new;
  91. }
  92. sub main {
  93. my (@fns) = @_;
  94. my $f;
  95. if (scalar @fns) {
  96. foreach my $fn (@fns) {
  97. die("Cannot find file $fn") unless (-r $fn);
  98. my $fh;
  99. open($fh, '<', $fn) or die;
  100. $f = join('', <$fh>);
  101. close($fh) or die;
  102. }
  103. }
  104. else { # STDIN
  105. local $/; # Slurp the whole file
  106. $f = <>;
  107. }
  108. return $m->markdown($f);
  109. }
  110. print main(@ARGV) unless caller();