!!s;
# kill the "toolbox" at the bottom left
$content =~ s!
.*?!!;
# handle the @import links to get the css right
while ( $content =~ m!\@import \"(.+?)\";!mg ) {
my $importlink = $1;
if ( convert_css_link($importlink) ) {
push(@links, URI->new_abs($importlink, $base));
push(@links_to_modify, '@' . $importlink);
}
}
# rewrite all the links
foreach my $link (@links_to_modify) {
my $converted = convert_link($link);
if ( substr($link,0,1) eq '@' ) {
substr($link,0,1) = '';
}
$link = quotemeta($link);
$content =~ s!\"$link\"!\"$converted\"!g;
}
my $filename = convert_link($url);
return($filename, $content, @links);
}
sub write_to_disk {
my ($path, $type, $thing) = @_;
# return() if ( -e $path ); # this was a dumb idea
$path =~ m!(.*)/(.*?)\.\w{2,4}$!;
my ($tree, $filename) = ($1, $2);
# is it an image link?
if ( $tree =~ m!\./images/! ) {
# hax it
$path =~ s!/images/.+/!/images/!i;
$tree =~ s!/images.+!/images!i;
}
# I don't think this is necessary really
mkpath($tree) unless ( -e $tree and -d $tree );
if ( $type =~ m!^text! ) {
write_text($path, $thing);
}
else {
write_bin($path, $thing);
}
print("Writing $filename to ${path}...\n");
$writes++;
}
sub write_text {
my ($outfile, $thing) = @_;
open(OUT, ">:utf8", $outfile) or die("Couldn't open $outfile for writing: $!");
print OUT $thing;
close(OUT) or die("Couldn't close ${outfile}: $!");
return();
}
sub write_bin {
my ($outfile, $thing) = @_;
open(OUT, ">", $outfile) or die("Couldn't open $outfile for writing: $!");
binmode(OUT);
print OUT $thing;
close(OUT) or die("Couldn't close ${outfile}: $!");
return();
}
# converts links to relative starting with $base_dir
sub convert_link {
my $link = shift(@_);
# dereference if necessary
if ( ref($link) ) {
$link = $$link;
}
# SPECIAL CASE: it's one of those fukken @import links, do something else with it
if ( substr($link,0,1) eq '@' ) {
substr($link,0,1) = '';
return(convert_css_link($link));
}
# is it relative?
if ( substr($link,0,1) eq '/' ) {
$link =~ s!^/docs/!$base_dir/!i;
}
else {
my $quoted = quotemeta($host);
$link =~ s!${quoted}/docs/!$base_dir/!i;
}
# if it doesn't have a filename extension it's probably a page,
# and then we need to tack on .html to the end (fuck internet explorer)
# oh and jfs's .lua pages aren't really lua scripts either
my $bdirquoted = quotemeta($base_dir);
if ( $link =~ m/^(${bdirquoted}.+?)\#.*$/ ) {
my $pagename = $1;
if ( $pagename !~ m!\.html$! or (substr($pagename,-4) eq '.lua') ) {
$link =~ s!^${pagename}!${pagename}.html!;
}
} elsif ( $link !~ m!/.*?\.\w{2,4}$! or (substr($link,-4) eq '.lua') ) {
$link = $link . '.html';
}
$link =~ s!\:!_!g; # replace : with _
$link =~ s!\?!_!g; # replace ? with _
return($link);
}
# HAX
sub convert_css_link {
my $link = shift(@_);
# does it seem like css?
if ( $link =~ m!MediaWiki:(.+?)\.css!i ) {
return(convert_link('/docs/' . $1 . '.css'));
}
# has a sensible name already, don't fuck with it
elsif ( $link =~ m!/(.+?)\.css$!i ) {
return(convert_link($link));
}
# doesn't seem like anything useful
else { return(undef); }
}
# argh
sub parse_css {
my ($url, $base, $content) = @_;
my @links;
# my $quoted = quotemeta($docs_base_url); # <--- not used
# find url("stuff") blocks
LINKLIST:
while ( $content =~ m!url\((\")?(.+?)(\")?\)!mgi ) {
my $text = $2;
# skip it if it's nonrelative and goes somewhere else than aegisub.cellosoft.com
if ( $text =~ m!^http!i ) {
# actually fuck this there shouldn't be any nonrelative links in there anyway
next(LINKLIST)
#unless ( $text =~ m!^${quoted}!i );
}
push(@links, URI->new_abs($text, $base));
}
my $filename = convert_link($url);
return($filename, @links);
}