summaryrefslogtreecommitdiffstats
path: root/scripts/Markdown.pl
blob: 169b15fed7c0dc4e79bff492bbde7bd10b0ec301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env perl
use strict;
use warnings;
use Text::Markdown qw(markdown);

=head1 NAME

Markdown.pl - Convert Markdown syntax to (X)HTML

=head1 DESCRIPTION

This program is distributed as part of Perl's Text::Markdown module,
illustrating sample usage.

Markdown can be invoked on any file containing Markdown-syntax, and
will produce the corresponding (X)HTML on STDOUT:

    $ cat file.txt
    This is a *test*.

    Absolutely _nothing_ to see here. _Just a **test**_!

    * test
    * Yup, test.
    $ Markdown.pl file.txt
    <p>This is a <em>test</em>.</p>

    <p>Absolutely <em>nothing</em> to see here. <em>Just a <strong>test</strong></em>!</p>

    <ul>
    <li>test</li>
    <li>Yup, test.</li>
    </ul>

If no file is specified, it will expect its input from STDIN:

    $ echo "A **simple** test" | markdown
    <p>A <strong>simple</strong> test</p>

=head1 OPTIONS

=over

=item version

Shows the full information for this version

=item shortversion

Shows only the version number

=item html4tags

Produce HTML 4-style tags instead of XHTML - XHTML requires elements
that do not wrap a block (i.e. the C<hr> tag) to state they will not
be closed, by closing with C</E<gt>>. HTML 4-style will plainly output
the tag as it comes:

    $ echo '---' | markdown
    <hr />
    $ echo '---' | markdown --html4tags
    <hr>

=item help

Shows this documentation

=back

=head1 AUTHOR

Copyright 2004 John Gruber

Copyright 2008 Tomas Doran

The manpage was written by Gunnar Wolf <gwolf@debian.org> for its use
in Debian systems, but can be freely used elsewhere.

For full licensing information, please refer to
L<Text::Markdown.pm>'s full documentation.

=head1 SEE ALSO

L<Text::Markdown>, L<http://daringfireball.net/projects/markdown/>

=cut

#### Check for command-line switches: #################
my %cli_opts;
use Getopt::Long;
Getopt::Long::Configure('pass_through');
GetOptions(\%cli_opts,
    'version',
    'shortversion',
    'html4tags',
    'help',
);
if ($cli_opts{'version'}) {     # Version info
    print "\nThis is Markdown, version $Text::Markdown::VERSION.\n";
    print "Copyright 2004 John Gruber\n";
    print "Copyright 2008 Tomas Doran\n";
    print "Parts contributed by several other people.";
    print "http://daringfireball.net/projects/markdown/\n\n";
    exit 0;
}
if ($cli_opts{'shortversion'}) {        # Just the version number string.
    print $Text::Markdown::VERSION;
    exit 0;
}
if ($cli_opts{'help'}) {
    for my $dir (split m/:/, $ENV{PATH}) {
	my $cmd = "$dir/perldoc";
	exec($cmd, $0) if (-f $cmd and -x $cmd);
    }
    die "perldoc could not be found in your path - Cannot show help, sorry\n";
}
my $m;
if ($cli_opts{'html4tags'}) {           # Use HTML tag style instead of XHTML
    $m = Text::Markdown->new(empty_element_suffix => '>');
}
else {
    $m = Text::Markdown->new;
}

sub main {
    my (@fns) = @_;
    
    my $f;
    if (scalar @fns) {
        foreach my $fn (@fns) {
            die("Cannot find file $fn") unless (-r $fn);

            my $fh;
            open($fh, '<', $fn) or die;
            $f = join('', <$fh>);
            close($fh) or die;
        }
    }
    else { # STDIN
        local $/;               # Slurp the whole file
        $f = <>;
    }
    
    return $m->markdown($f);
}

print main(@ARGV) unless caller();