use strict;
use File::Find;
#以前のbazookaが全てh1がbazookaだったのを
#h2以降のhxを全て繰り上げてついでに以前のh2の内容をtitleにするスクリプト
my @filelist;
find( sub { push( @filelist, $File::Find::name ) if $File::Find::name =~ /.*\.(html|htm)$/ }, "./" );
foreach my $pass ( @filelist ) {
print "$pass ";
my ( @meta, @header, @contents, @footer, $title );
open FILE, "$pass";
&push_lines( '', \@meta, '' );
&push_lines( '', \@header, '' );
&push_lines( '', \@contents, '' );
&push_lines( '', \@footer, '' );
close FILE;
foreach ( @contents ) {
if ( /
(.*?)<\/h2>/ ) {
$title = $1;
last;
}
}
print ".";
foreach ( @meta ) {
if ( /.*?<\/title>/ ) {
s/.*?<\/title>/$title<\/title>/;
last;
}
}
print ".";
foreach ( 0 .. @header - 1 ) {
if ( $header[$_] =~ /.*?<\/h1>/ ) {
splice( @header, $_, 1 );
last;
}
}
print ".";
foreach ( @contents ) {
s//&h_top($1, $2)/eg;
s/<\/h([23456])>/&h_end($1)/eg;
}
print ".";
open FILE, ">$pass";
print FILE join( '', @meta );
print FILE join( '', @header );
print FILE join( '', @contents );
print FILE join( '', @footer );
close FILE;
print " done\n";
}
sub h_top {
my $x = shift;
my $add = shift;
$x--;
return "";
}
sub h_end {
my $x = shift;
$x--;
return "";
}
sub push_lines {
my $key = shift;
my $r = shift;
my $l = shift;
push( @$r, $l . "\n" ) if $l;
LOOP: while ( ) {
if ( /^$key/ and $key ) {
last LOOP;
} else {
push( @$r, $_ );
}
}
}