File Coverage

script/Markdown.pl
Criterion Covered Total %
statement 18 24 75.0
branch 1 8 12.5
condition n/a
subroutine 5 5 100.0
pod n/a
total 24 37 64.8


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2 1     1   1077 use strict;
  1         2  
  1         49  
3 1     1   7 use warnings;
  1         3  
  1         73  
4 1     1   879 use Text::Markdown qw(markdown);
  1         3  
  1         111  
5              
6             =head1 NAME
7              
8             Markdown.pl - Convert Markdown syntax to (X)HTML
9              
10             =head1 DESCRIPTION
11              
12             This program is distributed as part of Perl's Text::Markdown module,
13             illustrating sample usage.
14              
15             Markdown can be invoked on any file containing Markdown-syntax, and
16             will produce the corresponding (X)HTML on STDOUT:
17              
18             $ cat file.txt
19             This is a *test*.
20              
21             Absolutely _nothing_ to see here. _Just a **test**_!
22              
23             * test
24             * Yup, test.
25             $ Markdown.pl file.txt
26            

This is a test.

27              
28            

Absolutely nothing to see here. Just a test!

29              
30            
31            
  • test
  • 32            
  • Yup, test.
  • 33            
    34              
    35             If no file is specified, it will expect its input from STDIN:
    36              
    37             $ echo "A **simple** test" | markdown
    38            

    A simple test

    39              
    40             =head1 OPTIONS
    41              
    42             =over
    43              
    44             =item version
    45              
    46             Shows the full information for this version
    47              
    48             =item shortversion
    49              
    50             Shows only the version number
    51              
    52             =item html4tags
    53              
    54             Produce HTML 4-style tags instead of XHTML - XHTML requires elements
    55             that do not wrap a block (i.e. the C
    tag) to state they will not
    56             be closed, by closing with C>. HTML 4-style will plainly output
    57             the tag as it comes:
    58              
    59             $ echo '---' | markdown
    60            
    61             $ echo '---' | markdown --html4tags
    62            
    63              
    64             =item help
    65              
    66             Shows this documentation
    67              
    68             =back
    69              
    70             =head1 AUTHOR
    71              
    72             Copyright 2004 John Gruber
    73              
    74             Copyright 2008 Tomas Doran
    75              
    76             The manpage was written by Gunnar Wolf for its use
    77             in Debian systems, but can be freely used elsewhere.
    78              
    79             For full licensing information, please refer to
    80             L's full documentation.
    81              
    82             =head1 SEE ALSO
    83              
    84             L, L
    85              
    86             =cut
    87              
    88             #### Check for command-line switches: #################
    89             my %cli_opts;
    90 1     1   1439 use Getopt::Long;
      1         28984  
      1         8  
    91             Getopt::Long::Configure('pass_through');
    92             GetOptions(\%cli_opts,
    93             'version',
    94             'shortversion',
    95             'html4tags',
    96             'help',
    97             );
    98             if ($cli_opts{'version'}) { # Version info
    99             print "\nThis is Markdown, version $Text::Markdown::VERSION.\n";
    100             print "Copyright 2004 John Gruber\n";
    101             print "Copyright 2008 Tomas Doran\n";
    102             print "Parts contributed by several other people.";
    103             print "http://daringfireball.net/projects/markdown/\n\n";
    104             exit 0;
    105             }
    106             if ($cli_opts{'shortversion'}) { # Just the version number string.
    107             print $Text::Markdown::VERSION;
    108             exit 0;
    109             }
    110             if ($cli_opts{'help'}) {
    111             for my $dir (split m/:/, $ENV{PATH}) {
    112             my $cmd = "$dir/perldoc";
    113             exec($cmd, $0) if (-f $cmd and -x $cmd);
    114             }
    115             die "perldoc could not be found in your path - Cannot show help, sorry\n";
    116             }
    117             my $m;
    118             if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML
    119             $m = Text::Markdown->new(empty_element_suffix => '>');
    120             }
    121             else {
    122             $m = Text::Markdown->new;
    123             }
    124              
    125             sub main {
    126 1     1   964 my (@fns) = @_;
    127            
    128 1         2 my $f;
    129 1 50       4 if (scalar @fns) {
    130 0         0 foreach my $fn (@fns) {
    131 0 0       0 die("Cannot find file $fn") unless (-r $fn);
    132              
    133 0         0 my $fh;
    134 0 0       0 open($fh, '<', $fn) or die;
    135 0         0 $f = join('', <$fh>);
    136 0 0       0 close($fh) or die;
    137             }
    138             }
    139             else { # STDIN
    140 1         6 local $/; # Slurp the whole file
    141 1         206 $f = <>;
    142             }
    143            
    144 1         7 return $m->markdown($f);
    145             }
    146              
    147             print main(@ARGV) unless caller();