@@ -1,34 +1,38 @@
11#!/usr/bin/env perl
2+use strict;
23use CGI qw/:standard/;
34use CGI::Carp 'fatalsToBrowser';
45
56$CGI::POST_MAX=1024 * 100; # max 100K posts
67$CGI::DISABLE_UPLOADS = 1; # no uploads
78
8−if (param('url') && param('format')) {
9− $options = '--standalone --reference-links';
10− $url = param('url');
11− $format = param('format') || 'markdown';
12− if ($format =~ '^markdown$') {
13− $options .= ' --strict';
14− }
15− if ($format =~ '^markdown\+$') {
16− $format = 'markdown';
17− }
18− $output = `wget -O- $url | tidy -asxhtml -utf8 | pandoc -r html -w $format $options`;
19− if ($format =~ "rtf") {
20− $type = "application/rtf"
21− } else {
22− $type = "text/plain"
23− };
24− print header(-charset=>"utf8",-type=>"$type"),
25− $output;
26−} else {
27− print start_html(-title=>"html2x"),
28− h1("Usage"),
29− p("You have tried to call html2x.pl without the proper parameters."),
30− p("Please use <a href=\"/pandoc/html2x.html\">this form</a>."),
31− end_html();
9+param('url') && param('format') or die "Missing url and/or format parameters.\n";
10+
11+my $options = '-r html --standalone --reference-links';
12+my $url = param('url');
13+my $format = param('format') || 'markdown';
14+if ($format =~ /^markdown$/) {
15+ $options .= ' --strict';
16+}
17+if ($format =~ /^markdown\+$/) {
18+ $format = 'markdown';
3219}
3320
21+# Validate URL and format
22+unless ($url =~ /^(https?:\/\/)?[\w#?_-]+(\.[\w#?_-]+)+[\w\/#?_.-]*$/) {
23+ die "Illegal URL: $url\n" ;
24+}
25+unless ($format =~ /^markdown\+?|rst|latex|context|rtf|man|docbook$/) {
26+ die "Illegal format: $format\n";
27+}
3428
29+my $output = `wget -O- $url | tidy -asxhtml -utf8 | pandoc -w $format $options`;
30+if ($output =~ /^\s*$/) {
31+ print start_html,
32+ h1("No output"),
33+ p("Either $url could not be retrieved, or its HTML was too malformed to parse."),
34+ end_html;
35+ exit 0;
36+}
37+print header(-charset=>"utf8",-type=>"text/plain"),
38+ $output;