(.*?)!$1!gi;
+ next(LINKLIST);
+ }
+ # don't process #anchor links
+ if ( $attrs{'href'} =~ m!\#(.*?)$! ) {
+ next(LINKLIST);
+ }
+
+ # does it go within aegisub.cellosoft.com?
+ if ( $attrs{'href'} =~ m!^$quoted!i or (substr($attrs{'href'},0,1) eq '/') ) {
+ push(@links_to_modify, $attrs{'href'});
+ }
+ # is not relative and goes somewhere else than aegisub.cellosoft.com
+ # so we're not interested in it
+ else { next(LINKLIST); }
+
+ push(@links, URI->new_abs($attrs{'href'}, $base));
+ }
+ elsif ( ($tagname eq 'link') and exists($attrs{'href'}) ) {
+ if ( $attrs{'href'} =~ m!^$quoted!i or (substr($attrs{'href'},0,1) eq '/') ) {
+ push(@links_to_modify, $attrs{'href'});
+ }
+ else { next(LINKLIST); }
+
+ push(@links, URI->new_abs($attrs{'href'}, $base));
+ }
+ elsif ( ($tagname eq 'script') and exists($attrs{'src'}) ) {
+ my $src = quotemeta($attrs{'src'});
+
+ # bogus link, skip it
+ if ( $attrs{'src'} =~ m!index\.php\?title=-!i ) {
+ next(LINKLIST);
+ }
+
+ if ( $attrs{'src'} =~ m!^$quoted!i or (substr($attrs{'src'},0,1) eq '/') ) {
+ push(@links_to_modify, $attrs{'src'});
+ }
+ else { next(LINKLIST); }
+
+ push(@links, URI->new_abs($attrs{'src'}, $base));
+ }
+ elsif ( ($tagname eq 'img') and exists($attrs{'src'}) ) {
+ if ( $attrs{'src'} =~ m!^$quoted!i or (substr($attrs{'src'},0,1) eq '/') ) {
+ push(@links_to_modify, $attrs{'src'});
+ }
+ else { next(LINKLIST); }
+
+ push(@links, URI->new_abs($attrs{'src'}, $base));
+ }
+ # else do nothing
+ }
+
+
+ # kill the topbar
+ $content =~ s!\!!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);
+
+ 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 lua scripts either
+ if ( $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;
+
+ LINKLIST:
+ while ( $content =~ m!url\(\"(.+?)\"\)!mgi ) {
+ push(@links, URI->new_abs($1, $base));
+ }
+
+ my $filename = convert_link($url);
+
+ return($filename, @links);
+}
\ No newline at end of file