1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#!/usr/bin/perl
# Author: 2007 Patrick Winnertz <patrick.winnertz@skolelinux.org>
# 2009 Holger Levsen <holger@debian.org>
# License: GPLv2 or later
#
# This small scripts downloads and stores the images in the images/ subdir,
# then it modifies the xml file to use these images
use strict;
use warnings;
use IO::Dir;
our $base = "https://wiki.debian.org";
our $imagedir = "./images";
our $file = $ARGV[0];
our $path = $ARGV[1];
sub create ($) {
my $link = shift;
my $name = $link;
my $url;
# use regex to replace the long url with shorter one
$name =~ s#$base/?/$path/\w+\?action=AttachFile&do=get&target=([\w\d]+)#$1#g;
# more permissive regex for smiley and alike
$name =~ s#$base/.*/([\w\d]+)#$1#g;
$url = $link;
$url =~ s/&/\&/g;
print "File name: ".$name."\n";
tie my %h_images , 'IO::Dir', $imagedir;
if (!(defined($h_images{$name}))) {
print "Need to download: ".$url." to ".$imagedir."/".$name." \n";
print "Name: ".$name."\n";
system("wget \"$url\" -O \"$imagedir/$name\" 2> /dev/null");
}
return "<imagedata fileref='". $imagedir."/".$name."'/>";
}
sub replace () {
open(FILE, "< $file") or die "Can't open $file perhaps not in the correct dir? Error: $!";
undef $/;
my $local = <FILE>;
close(FILE);
# look for images...
if ( $local =~ "m#<imagedata( width='\\d+')\? fileref=\"/".$path."/\\w+\?action=AttachFile&do=get&target=([^']+)\"( width=\"\\d+\")\?/>#"
or $local =~ "m#<imagedata( depth=\"\\d+\")\? fileref=\"([^\"]+)\"( width=\"\\d+\")\?/>#") {
# ..and replace the paths
$local =~ s#<imagedata( depth="\d+")? fileref="([^'"<>]+)"( width="\d+")?/>#create($2)#eg;
open(FILE, "> $file") or die "Can't open $file for writing";
print FILE $local;
close(FILE);
} else {
print "File $file has already been modified.\n";
}
}
&replace();
|