#!/usr/bin/perl use strict; use warnings; use File::Slurp qw/read_file/; if (scalar(@ARGV) < 1) { die "Usage: $0 include_dir file_to_replace..."; } my $include_dir = $ARGV[0]; if (! -d $include_dir) { die "'$include_dir' is not a directory"; } my $header = read_file("${include_dir}/head.html"); my $nav = read_file("${include_dir}/nav.html"); my $footer = read_file("${include_dir}/footer.html"); $header .= "\n"; $nav = <
$nav
EOT $footer = <
$footer
EOT my $oldargv; while(<>) { if (!$oldargv || $ARGV ne $oldargv) { # new file: open output file unlink($ARGV); open(ARGVOUT, ">", $ARGV); select(ARGVOUT); $oldargv = $ARGV; } s//$&$header/; if (//) { my $match = $&; my $classes = "blog et_fixed_nav et_cover_background et_right_sidebar"; if ($match =~ / class=/) { $match =~ s/ class="([^"]*)"/ class="$1 $classes"/; } else { $match =~ s/>/ class=\"$classes\">/; } s//$match$nav/; } s##$footer$&#; print; }