Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
laziness, impatience, and hubris
 
PerlMonks  

Printer friendly pages on perlmonks.

by Dog and Pony (Priest)
on Jun 04, 2002 at 12:35 UTC ( #171459=monkdiscuss: print w/ replies, xml ) Need Help??

Yes, I know, I know - the subject has long since been beaten thouroughly to death, at least some of you will think so.

Indeed it has been mentioned before, in such places like printer friendly... a few google searches didn't turn up that much more, but that is as usual just my querys that not rocks. :)

On certain places on this site, there is also the possibility to say &displaytype=raw, as described among other places here and here. On the interesting places though, such as normal nodes, it usually bombs out with a "500 internal error".

Well, anyhow, I once again had reason to print something from this site, to study somewhere else. Then I started to wonder... exactly how hard would it be to simply strip out the parts of the page that were unwanted? As it turned out, it was a 10 minute job, since I cheated and used regexps instead of a real parser - but save for some HTML changes (which of course are bound to happen sooner or later), it seems to work just fine. I'm just waiting to hear it break badly. :)

To demonstrate this, I put together a little CGI that one can use to get what I consider the parts of the page that I want to print, and the rest is stripped out. You can try it here, and see the code below. As an extra thought, I added possibility to paste some CSS into the page too, if you want nice boxes around code blocks or something like that. Another option would have been to forward username/password combos to retain your own personal CSS if any, but that would fast be quite a question of trust... :)

Another option that are available is of course http://perlmonks.thepen.com, which already features a pretty stripped version of the site, although I do not like the huge part at the top of it (for printing, that is).

Well, so what was the point? I'm not sure. :) For one thing, I still want the feature where I could click a link to get a printerfriendly version of any page. If nothing else, with this I can demonstrate what parts I think should be taken out on that page, and which should be still there. If this would be implemented on perlmonks, it would most likely be more wise to not produce those parts, rather than remove after producing, so this code is probably worthless. On the other hand, I got to spend some time coding on something pointless, which is one of my top three hobbies (the other two is most probably beer), and I got something to post here. Fun is important. :)

I am, however gonna leave the code up (and you can get it from here as well), so as long as it doesn't break, one can solve the problems themselves with this. But of course it would be really cool if anyone actually could use it for something. :) For now, I will use it to get my printouts when needed...

#!/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 gadge +t 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 &lt;style&gt; tags yourself. (This field will simply be inserted last in the &lt;head&gt; 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{<input[^>]+>(?:\s*(\+\+|--|\+=0))?}{}gi; # Remove ads and the search bar $$html_ref =~ s{<body>.+<!-- Begin title bar -->}{}si; # Strip out the top links, logged in version $$html_ref =~ s{<a HREF="/index\.pl\?op=logout&node_id=131">.+Need + Help\?\?</a>}{}si; # ... and logged out, which is the one used in this example $$html_ref =~ s{<a HREF="/index\.pl\?node=login">.+Need Help\?\?</ +a>}{}si; # Remove nodelets $$html_ref =~ s{<!-- Begin nodelets -->.+<!-- End nodelets -->}{}s +i; # Back to.... link. $$html_ref =~ s{Back\s+to\s+<a href[^<]+</a>}{}si; # Remove comment on... $$html_ref =~ s{<tr><th.+</th></tr>}{}si; # Insert CSS $$html_ref =~ s{</head>}{$css</head>}i if $css; # Keep the bottom notice on the page :) }

You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Comment on Printer friendly pages on perlmonks.
Select or Download Code
(wil) Re: Printer friendly pages on perlmonks.
by wil (Priest) on Jun 04, 2002 at 12:44 UTC
    Are you are aware of the 'download code' option that outputs a nice, printer friendly version of any code that is within a node?

    Just look for the d/l code link at the bottom of your node, or just append a &displaytype=displaycode to the end of the URL.

    This works great for grabbing clean copies of code, but of course, this doesn't capture any normal text.

    Update: Yeah, I did have this feeling that you would know that that one :-). I'll leave my suggestion here though, for others who might not be aware of it.

    I don't personally see a need for a printer-friendly version, as the current layout prints neat on my printer. But it's a valid suggestion, and ++ for providing code, which I'm about to d/l using &displaytype=displaycode and test :-)

    Update 2: should this be posted in the snippets section?

    - wil
      Bookmarked the cgi (but fear not, I have also downloaded the code so I can put it up elsewhere). Although, I don't foresee using it often, I do see a need for it. I don't want to turn off my nodelets just to print out a page. This takes care of that.
      Thanks!

      Who says that programmers can't work in the Marketing Department?
      Or is that who says that Marketing people can't program?
Re: Printer friendly pages on perlmonks.
by Maclir (Curate) on Jun 05, 2002 at 16:10 UTC
    Since much of the appearance of Perlmonks is driven by style sheets, all that is needed to have "printer friendly" pages is to add a style sheet designed for printing, and use the @MEDIA PRINT capabiity of CSS 2.

    And if your ancient browser doesn't support CSS2 properly, time to upgrade it.

      Perlmonks is definetely getting there with the CSS, and sure, my browsers do support CSS2.

      How about an example of a CSS that does all this then? You can test the code above to get a good approximation on how I think it should look to be worth it. I'd really appreciate it, and I am sure, so would the staff here as it would be an easy task (I assume) to spit out a page with just a different CSS as the printer friendly one.


      You have moved into a dark place.
      It is pitch black. You are likely to be eaten by a grue.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://171459]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2014-04-19 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls