| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Text::Markdown qw(markdown); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | <p>This is a <em>test</em>.</p> | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | <p>Absolutely <em>nothing</em> to see here. <em>Just a <strong>test</strong></em>!</p> | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | <ul> | ||||||||||||||||||||||||||||
| 31 | <li>test</li> | ||||||||||||||||||||||||||||
| 32 | <li>Yup, test.</li> | ||||||||||||||||||||||||||||
| 33 | </ul> | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | If no file is specified, it will expect its input from STDIN: | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $ echo "A **simple** test" | markdown | ||||||||||||||||||||||||||||
| 38 | <p>A <strong>simple</strong> test</p> | ||||||||||||||||||||||||||||
| 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<hr> tag) to state they will not | ||||||||||||||||||||||||||||
| 56 | be closed, by closing with C</E<gt>>. HTML 4-style will plainly output | ||||||||||||||||||||||||||||
| 57 | the tag as it comes: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | $ echo '---' | markdown | ||||||||||||||||||||||||||||
| 60 | <hr /> | ||||||||||||||||||||||||||||
| 61 | $ echo '---' | markdown --html4tags | ||||||||||||||||||||||||||||
| 62 | <hr> | ||||||||||||||||||||||||||||
| 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 <gwolf@debian.org> for its use | ||||||||||||||||||||||||||||
| 77 | in Debian systems, but can be freely used elsewhere. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | For full licensing information, please refer to | ||||||||||||||||||||||||||||
| 80 | L<Text::Markdown.pm>'s full documentation. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | L<Text::Markdown>, L<http://daringfireball.net/projects/markdown/> | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | #### Check for command-line switches: ################# | ||||||||||||||||||||||||||||
| 89 | 1 | my %cli_opts; | |||||||||||||||||||||||||||
| 90 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 1 | Getopt::Long::Configure('pass_through'); | |||||||||||||||||||||||||||
| 92 | 1 | GetOptions(\%cli_opts, | |||||||||||||||||||||||||||
| 93 | 'version', | ||||||||||||||||||||||||||||
| 94 | 'shortversion', | ||||||||||||||||||||||||||||
| 95 | 'html4tags', | ||||||||||||||||||||||||||||
| 96 | 'help', | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | 1 | ▶ | if ($cli_opts{'version'}) { # Version info | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | print "\nThis is Markdown, version $Text::Markdown::VERSION.\n"; | |||||||||||||||||||||||||||
| 100 | 0 | print "Copyright 2004 John Gruber\n"; | |||||||||||||||||||||||||||
| 101 | 0 | print "Copyright 2008 Tomas Doran\n"; | |||||||||||||||||||||||||||
| 102 | 0 | print "Parts contributed by several other people."; | |||||||||||||||||||||||||||
| 103 | 0 | print "http://daringfireball.net/projects/markdown/\n\n"; | |||||||||||||||||||||||||||
| 104 | 0 | exit 0; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 1 | ▶ | if ($cli_opts{'shortversion'}) { # Just the version number string. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | print $Text::Markdown::VERSION; | |||||||||||||||||||||||||||
| 108 | 0 | exit 0; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 1 | ▶ | if ($cli_opts{'help'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | for my $dir (split m/:/, $ENV{PATH}) { | |||||||||||||||||||||||||||
| 112 | 0 | my $cmd = "$dir/perldoc"; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | exec($cmd, $0) if (-f $cmd and -x $cmd); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $cmd and -x $cmd
MC/DC0%0/2
-f $cmd and -x $cmd
-f $cmd
-x $cmd
Truth table0%0/3
A and B
A= -f $cmdB= -x $cmd
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | die "perldoc could not be found in your path - Cannot show help, sorry\n"; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 1 | my $m; | |||||||||||||||||||||||||||
| 118 | 1 | ▶ | if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $m = Text::Markdown->new(empty_element_suffix => '>'); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||||||||||
| 122 | 1 | $m = Text::Markdown->new; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | ▶ | sub main { | ||||||||||||||||||||||||||
|
Subroutine
main: called CC=6 SCAR=27.7
| |||||||||||||||||||||||||||||
| 126 | 1 | my (@fns) = @_; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | my $f; | |||||||||||||||||||||||||||
| 129 | 1 | ▶ | if (scalar @fns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | foreach my $fn (@fns) { | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | die("Cannot find file $fn") unless (-r $fn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $fh; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | open($fh, '<', $fn) or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | $f = join('', <$fh>); | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | close($fh) or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | else { # STDIN | ||||||||||||||||||||||||||||
| 140 | 1 | local $/; # Slurp the whole file | |||||||||||||||||||||||||||
| 141 | 1 | $f = <>; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | return $m->markdown($f); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | ▶ | print main(@ARGV) unless caller(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||