git.iko.soy/typeable/pandoc
on fbb0482 help sign in
typeable/pandoc/web/html2x.pl
40 lines 1.2 kB history raw
1#!/usr/bin/env perl
2use strict;
3use CGI qw/:standard/;
4use CGI::Carp 'fatalsToBrowser';
6$CGI::POST_MAX=1024 * 100; # max 100K posts
7$CGI::DISABLE_UPLOADS = 1; # no uploads
8
9param('url') && param('format') or die "Missing url and/or format parameters.\n";
11my $options = '-r html --standalone --reference-links';
12my $url = param('url');
13my $format = param('format') || 'markdown';
14if ($format =~ /^markdown$/) {
15 $options .= ' --strict';
17if ($format =~ /^markdown\+$/) {
18 $format = 'markdown';
21# Validate URL and format
22unless ($url =~ /^(https?:\/\/)?[\w#_-]+(\.[\w#_-]+)+(:\d+)?[\w\/#=:?_.-]*$/) {
23 die "Illegal URL: $url\n" ;
25unless ($format =~ /^markdown\+?|rst|latex|context|rtf|man|docbook$/) {
26 die "Illegal format: $format\n";
29# Note - pass through head to truncate file to 100K if greater.
30# This should prevent certain kinds of DoS attacks.
31my $output = `wget -O- $url | head -c100000 | tidy -asxhtml -utf8 | pandoc -w $format $options`;
32if ($output =~ /^\s*$/) {
33 print start_html,
34 h1("No output"),
35 p("Either $url could not be retrieved, or its HTML was too malformed to parse."),
36 end_html;
37 exit 0;
39print header(-charset=>"utf8",-type=>"text/plain"),
40 $output;