Linkextract.pl

From The Document Foundation Wiki
Jump to: navigation, search
#!/usr/bin/perl
use HTML::SimpleLinkExtor;
use File::Find qw(find);
use URI::Escape;

my $dir     = shift; # where static html
my $rsynclist = shift; # to create for rsync
my $medium = shift; # wich medium to be extract
my $source_dir = '/var/www/sites/libreofficebox.org/';

# not covered by link targets - separate by "\n"
# my $more_items = "assets/bin\nassets/installer/windows\nassets/installer/linux_deb/64/\nassets/installer/linux_deb/32/";

my $more_items;
$more_items = "assets/bin\nassets/installer/3.4/windows\nassets/installer/3.4/linux_deb/64/\nassets/installer/3.4/linux_deb/32/" if $medium eq "dvd.de";
$more_items = "assets/bin\nassets/installer/3.3/windows\nassets/installer/3.3/linux_deb/64/\nassets/installer/3.3/linux_deb/32/" if $medium eq "dvd-betrieb.de";
$more_items = "assets/bin\nassets/installer/3.4/windows" if $medium eq "cd_windows.de";
$more_items = "assets/installer/3.4/linux_deb/32/" if $medium eq "cd_linux_deb32.de";

die "no dir given" unless ($dir);
die "no filename for rsync given" unless ($rsynclist);

# dir is the html-directory as provided in the tarball from
# StaticExporter with basedir relative
# rsynclist is file that will contain all the used files
my $pattern = 'html$';

my %assets;
my %themes;

my $extor = HTML::SimpleLinkExtor->new();
sub collect_links {
    return unless /$pattern/;
    $extor->parse_file($_);
    foreach $link ($extor->links) {
        $link =~ s/^\/?(..\/)*//;
        $assets{uri_unescape($link)}=1 if ($link =~ /^assets/);
        $themes{$link}=1 if ($link =~ s/^themes\/([^\/]+)\/.*/$1/);
    }
    $extor->clear_links; # reset the link list
}

find (\&collect_links, $dir);
print "Number of referenced files from assets: ".scalar(keys %assets)."\n";
open INCLUDE, ">$rsynclist";
foreach $key (keys %assets) {
    #print "\t $key\n";
    print INCLUDE "$key\n";
}
print "Used theme(s):\n";
foreach $key (keys %themes) {
    print "\t $key\n";
    print INCLUDE "themes/$key/css\n";
    print INCLUDE "themes/$key/images\n";
}

print INCLUDE $more_items;

close INCLUDE;

print STDERR "WARNING - MORE THAN ONE THEME!!\n" if ((keys %themes) >1);

print "now run the following command:\n";
print 'rsync -arv --dry-run --files-from='.$rsynclist." $source_dir ".$dir."\n";
print "If everything looks OK, remove the --dry-run and run again to actually copy the files\n";
print "Extrahiert von: ".$medium."\n";