Replace hacky shell to do matrix styling with hacky perl

Since the shell just wrapped three invocations of perl, we might as well do
it all in perl.
This commit is contained in:
Richard van der Hoff 2016-05-03 23:36:19 +01:00
parent 23c4ffbc1b
commit 0546f0917d
2 changed files with 71 additions and 45 deletions

View file

@ -0,0 +1,71 @@
#!/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 .= "<link rel=\"stylesheet\" href=\"/docs/guides/css/docs_overrides.css\">\n";
$nav = <<EOT;
<div id="page-wrapper">
<div class="page-content" id="page-container">
$nav
<div id="main-content">
<div class="wrapper" id="wrapper">
<div class="document_foo" id="document">
EOT
$footer = <<EOT;
</div>
</div>
<div class="push">
</div>
</div>
</div>
$footer
</div>
</div>
EOT
my $oldargv;
while(<>) {
if (!$oldargv || $ARGV ne $oldargv) {
# new file: open output file
unlink($ARGV);
open(ARGVOUT, ">", $ARGV);
select(ARGVOUT);
$oldargv = $ARGV;
}
s/<head>/$&$header/;
if (/<body.*?>/) {
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/<body.*?>/$match$nav/;
}
s#</body>#$footer$&#;
print;
}

View file

@ -1,45 +0,0 @@
#!/bin/bash -eu
if [[ $# == 0 || ! -d $1 ]]; then
echo >&2 "Usage: $0 include_dir file_to_replace..."
exit 1
fi
include_dir="$1"
shift
HEADER="${include_dir}/head.html"
NAV_BAR="${include_dir}/nav.html"
FOOTER="${include_dir}/footer.html"
for f in "${include_dir}"/{head,nav,footer}.html; do
if [[ ! -e "${f}" ]]; then
echo >&2 "Need ${f} to exist"
exit 1
fi
done
perl -MFile::Slurp -pi -e 'BEGIN { $header = read_file("'$HEADER'") } s#<head>#<head>$header
<link rel="stylesheet" href="/docs/guides/css/docs_overrides.css">
#' "$@"
perl -MFile::Slurp -pi -e 'BEGIN { $nav = read_file("'$NAV_BAR'") } s#<body># <body class="blog et_fixed_nav et_cover_background et_right_sidebar">
<div id="page-wrapper">
<div class="page-content" id="page-container">
$nav
<div id="main-content">
<div class="wrapper" id="wrapper">
<div class="document_foo" id="document">
#' "$@"
perl -MFile::Slurp -pi -e 'BEGIN { $footer = read_file("'$FOOTER'") } s#</body>#
</div>
</div>
<div class="push">
</div>
</div>
</div>
$footer
</div>
</div>
</body>#' "$@"