#!/usr/bin/perl -wT use strict; use CGI; #use CGI::Carp 'fatalsToBrowser'; use LWP::Simple qw(get); use vars qw($error $html); # Good ole monks. Nothing beats monks. my $pm = 'http://www.perlmonks.org/index.pl'; my $q = CGI->new(); print $q->header; if($q->param) { my $node_id = $q->param('node_id') || ''; my $css = $q->param('css') || ''; if($node_id !~ m{^\d+$}) { $error = "Not a valid node id: '$node_id'"; } else { $html = get "$pm?node_id=$node_id" or $error = "Failed to fetch node with id: '$node_id'"; &strip_for_print(\$html, $css) if $html; } } # If first time, or something went wrong: if($error || !$html) { print $q->start_html(-title=>'Printerfriendly PM'); print $q->h2('Printerfriendly perlmonks'); # Error reporting, if any: print $q->h4($error) if $error; print $q->start_form(-method => 'post', -action => $q->self_url); print $q->table( $q->Tr($q->td('Input node id:')), $q->Tr($q->td($q->textfield(-name => 'node_id'))), $q->Tr($q->td('Input CSS (optional):')), $q->Tr($q->td($q->textarea(-name => 'css', -value => ''))), $q->Tr($q->td($q->submit(-name => 'go', -value => 'Go go gadget copter'))) ); print $q->end_form; # Some blah blah here print $q->h3('Instructions:'); print $q->p(<<'INSTRUCTIONS'); Enter the ID of a node on perlmonks in the textfield. INSTRUCTIONS print $q->p(<<'INSTRUCTIONS'); You can also supply your own CSS to get a better printout, if you like. It can be any external CSS, or just some normal CSS. You will have to supply the <style> tags yourself. (This field will simply be inserted last in the <head> section). INSTRUCTIONS print $q->end_html; } # Display the printable page else { print $html; } # Lots of assumptions here, will probably break like # dry twigs as soon as any PM developer sneezes. :) # # Anyhow, here is the important part, that strips all # the stuff we don't want in our printable page. sub strip_for_print { my $html_ref = shift; my $css = shift; # Remove radio boxes, buttons and ++/-- etc $$html_ref =~ s{]+>(?:\s*(\+\+|--|\+=0))?}{}gi; # Remove ads and the search bar $$html_ref =~ s{.+}{}si; # Strip out the top links, logged in version $$html_ref =~ s{.+Need Help\?\?}{}si; # ... and logged out, which is the one used in this example $$html_ref =~ s{.+Need Help\?\?}{}si; # Remove nodelets $$html_ref =~ s{.+}{}si; # Back to.... link. $$html_ref =~ s{Back\s+to\s+}{}si; # Remove comment on... $$html_ref =~ s{}{}si; # Insert CSS $$html_ref =~ s{}{$css}i if $css; # Keep the bottom notice on the page :) }