From 0546f0917df26ef8400d181c887bcf835e7982c1 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 3 May 2016 23:36:19 +0100 Subject: [PATCH] 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. --- scripts/add-matrix-org-stylings.pl | 71 ++++++++++++++++++++++++++++++ scripts/add-matrix-org-stylings.sh | 45 ------------------- 2 files changed, 71 insertions(+), 45 deletions(-) create mode 100755 scripts/add-matrix-org-stylings.pl delete mode 100755 scripts/add-matrix-org-stylings.sh diff --git a/scripts/add-matrix-org-stylings.pl b/scripts/add-matrix-org-stylings.pl new file mode 100755 index 00000000..af6ac8de --- /dev/null +++ b/scripts/add-matrix-org-stylings.pl @@ -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 .= "\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; +} diff --git a/scripts/add-matrix-org-stylings.sh b/scripts/add-matrix-org-stylings.sh deleted file mode 100755 index bc46637a..00000000 --- a/scripts/add-matrix-org-stylings.sh +++ /dev/null @@ -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##$header - -#' "$@" - -perl -MFile::Slurp -pi -e 'BEGIN { $nav = read_file("'$NAV_BAR'") } s## -
-
- $nav -
-
-
-#' "$@" - -perl -MFile::Slurp -pi -e 'BEGIN { $footer = read_file("'$FOOTER'") } s## -
-
-
-
-
-
- $footer -
- - #' "$@"