Perl Script To Merge Javadoc Into Single HTML For Printing
Posted by John Manko | Posted in Computers & Technology, Java | Posted on 17-08-2010
I forgot Perl (been about 10 years), but this script I wrote does the job. Released under GPL v2. Modify and share as you see fit.
#!/usr/bin/perl
use Fcntl qw(:flock :seek);
use WWW::Mechanize;
# Would like to accept the javadoc root path as a parameter. Right now, need to hard code.
$allClassesFile = "./allclasses-noframe.html";
my $mech = WWW::Mechanize->new();
$mech->get( "file:$allClassesFile" );
my @links = $mech->links();
# This is the new single file
$sitedata="single.html";
open(DAT,">$sitedata") || die("Cannot Open File $sitedata");
flock(DAT, LOCK_EX);
seek(DAT, 0, SEEK_SET);
print DAT "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
print DAT "<HTML><HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">";
print DAT "<TITLE>Single Page</TITLE><LINK REL =\"stylesheet\" TYPE=\"text/css\" HREF=\"stylesheet.css\" TITLE=\"Style\"></HEAD><BODY BGCOLOR=\"white\">";
# Would like to include a table of contents of all classes docs included, with an href="#someclass" linking to the proper section in the page
# TOC
print DAT "<UL>";
for my $link ( @links ) {
$linkText = $link->text;
chomp($linkText);
$linkUrl = $link->url;
chomp($linkUrl);
if (($linkText ne '') && ($linkUrl ne '')){
$linkUrl =~ s/\//\./g;
$linkUrl =~ s/\.html//;
print DAT "<LI><a href='#$linkUrl'>$linkUrl</a>";
}
}
print DAT "</UL>";
print DAT "<hr /><br/>\n";
print DAT "<div style=\"page-break-before: always;\"></div>\n";
# maybe fix to better handle errors
for my $link ( @links ) {
$linkText = $link->text;
chomp($linkText);
$linkUrl = $link->url;
chomp($linkUrl);
if (($linkText ne '') && ($linkUrl ne '')){
#printf "%s, %s\n", $linkText, $linkUrl;
open ACF, "<./$linkUrl" || die("Cannot Open File $linkUrl");
$infile_contents = do { local $/; <ACF> };
close ACF;
# I'm sure there is a better way to split this
@content = split(/<!-- ======== START OF CLASS DATA ======== -->/, $infile_contents);
@content = split(/<!-- ========= END OF CLASS DATA ========= -->/, $content[1]);
print DAT "<a name='$linkUrl'\n";
print DAT "$content[0]\n";
print DAT "<div style=\"page-break-before: always;\"></div>\n";
}
}
print DAT "</BODY></HTML>";
close(DAT);




