<?xml version="1.0" encoding="windows-1252"?>
<node id="742905" title="Converting FLOSS manuals to Plucker format" created="2009-02-10 17:04:52" updated="2009-02-10 17:04:52">
<type id="115">
perlquestion</type>
<author id="110201">
hacker</author>
<data>
<field name="doctext">
I stumbled across a site called &amp;#8220;&lt;a href="http://en.flossmanuals.net/"&gt;FLOSS Manuals&lt;/a&gt;&amp;#8221; recently, and thought that it would be a great place to create some new &lt;a href="www.plkr.org"&gt;Plucker&lt;/a&gt; documents for our users, and distribute them. I&amp;#8217;ve create &lt;a href="http://projects.plkr.org/"&gt;hundreds of other Plucker documents&lt;/a&gt; for users in years past, so this was a natural progression of that.&lt;/p&gt;

&lt;p&gt;When I quickly Googled around, I found &lt;a href="http://www.robmyers.org/weblog/2009/02/08/floss-manuals-to-plucker/"&gt;someone was already doing exactly that&lt;/a&gt;, albeit in a one-off shell script.&lt;/p&gt;

&lt;p&gt;I decided to take his work and build upon it, making it self-healing, and created what I call the &amp;#8220;Plucker FLOSS Manuals Autobuilder v1.0&amp;#8243; :)&lt;/p&gt;

&lt;p&gt;Optimizations, comments and suggestions welcome and appreciated... 

&lt;code&gt;
#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;
use LWP::UserAgent;
use HTML::SimpleLinkExtor;

my $flossurl    = 'http://en.flossmanuals.net';
my $ua          = 'Plucker FLOSS Manuals Autobuilder v1.0 [desrod@gnu-designs.com]';
my $top_extor   = HTML::SimpleLinkExtor-&gt;new();

# fetch the top-level page and extract the child pages
$top_extor-&gt;parse_url($flossurl, $ua);
my @links       = grep(m:^/:, $top_extor-&gt;a);
pop @links;     # get rid of '/news' item from @links; fragile

# Get the print-only page of each child page
get_printpages($flossurl . $_) for @links;

############################################################################# 
#
# Get the pages themselves, and return their content to the caller
#
#############################################################################
sub get_content {
        my $url = shift;

        my $ua          = 'Mozilla/5.0 (en-US; rv:1.4b) Gecko/20030514';
        my $browser     = LWP::UserAgent-&gt;new();
        $browser-&gt;agent($ua);
        my $response    = $browser-&gt;get($url);
        my $decoded     = $response-&gt;decoded_content;
 
        # This was necessary, because of a bug in ::SimpleLinkExtor,
        # otherwise this code would be 10 lines shorter. Sigh.
        if ($response-&gt;is_success) {
                return $decoded;
        }
}


############################################################################# 
#
# Fetch the print links from the child pages snarfed from the top-level page
#
#############################################################################
sub get_printpages {
        my $page = shift;

        my $sub_extor   = HTML::SimpleLinkExtor-&gt;new();
        $sub_extor-&gt;parse(get_content($page));

        # Single out only the /print links on each sub-page
        my @printlinks  = grep(m:^/.*/print$:, $sub_extor-&gt;a);

        my $url         = $flossurl . $printlinks[0];
        (my $title      = $printlinks[0]) =~ s,\/(\w+)\/print,$1,;

        # Build it with Plucker
        print "Building $title from $url\n";
        plucker_build($url, $title);
}


############################################################################# 
#
# Build the content with Plucker, using a "safe" system() call in list-mode
#
#############################################################################
sub plucker_build {
        my ($url, $title) = @_;

        my $workpath            = "/tmp/";
        my $pl_url              = $url;
        my $pl_bpp              = "8";   
        my $pl_compression      = "zlib";
        my $pl_title            = $title;
        my $pl_copyprevention   = "0";
        my $pl_no_url_info      = "0";
        my $pdb                 = $title;

        my $systemcmd   = "/usr/bin/plucker-build";

        my @systemargs  = (
                        '-p', $workpath, 
                        '-P', $workpath,
                        '-H', $pl_url,
                        $pl_bpp ? "--bpp=$pl_bpp" : (),
                        ($pl_compression ? "--${pl_compression}-compression" : ''),
                        '-N', $pl_title,
                        $pl_copyprevention ? $pl_copyprevention : (),
                        $pl_no_url_info ? $pl_no_url_info : (),
                        '-V1',
                        "--staybelow=$flossurl/floss/pub/$title/",
                        '--stayonhost',
                           '-f', "$pdb");

        system($systemcmd, @systemargs);
}
&lt;/code&gt;
</field>
</data>
</node>
