From cbce1a7a4a591c0aa1d17fc97fdd585981f42e47 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Tue, 18 Dec 2007 10:46:28 +0000 Subject: [PATCH] Add initial activate-blosxom-include and globalise-config-variables scripts. --- scripts/activate-blosxom-include | 60 ++++++++++++++++++++++++++ scripts/globalise-config-variables | 67 ++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100755 scripts/activate-blosxom-include create mode 100755 scripts/globalise-config-variables diff --git a/scripts/activate-blosxom-include b/scripts/activate-blosxom-include new file mode 100755 index 0000000..2cd8efc --- /dev/null +++ b/scripts/activate-blosxom-include @@ -0,0 +1,60 @@ +#!/usr/bin/perl +# +# Script to activate Blosxom::Include in blosxom plugins, if available +# +# Usage: perl -MBlosxom::Include -e '1' 2>/dev/null && activate-blosxom-include +# + +use strict; +use Getopt::Std; +use IO::File; +use File::Copy; +use Blosxom::Include 0.002; + +my %opts = (); +getopts('Dnq', \%opts); +my $deactivate = $opts{D}; +my $noop = $opts{n}; +my $quiet = $opts{q}; + +my $dir = shift @ARGV || '*'; +for my $pname ( glob $dir ) { + next unless -f $pname; + my $ph = IO::File->new( $pname, 'r' ) + or warn "Cannot open plugin '$pname' for read: $!" and next; + my $plugin = ''; + { + local $/ = undef; + $plugin = <$ph>; + close $ph; + } + next unless $plugin; + next unless $plugin =~ m/__END_CONFIG__/; + if ($deactivate) { + next unless $plugin =~ m/^use\s+Blosxom::Include/m; + } + else { + next if $plugin =~ m/use\s+Blosxom::Include/; + } + print "Updating $pname ...\n" unless $quiet; + unless ($noop) { + my $modified = $plugin; + if ($deactivate) { + $modified =~ s/^use Blosxom::Include[^\n]*\n\s*//mg; + } + else { + $modified =~ s/^([^#])/\nuse Blosxom::Include qw($pname);\n$1/m; + } + next if $plugin eq $modified; + $ph = IO::File->new( "$pname.tmp", 'w' ) + or die "Cannot open plugin '$pname.tmp' for write: $!"; + print $ph $modified + or die "Cannot write to plugin '$pname.tmp': $!"; + close $ph + or die "Cannot close plugin '$pname.tmp': $!"; + move "$pname.tmp", $pname + or die "Cannot move plugin '$pname.tmp' to '$pname': $!"; + } +} +print "Done.\n" unless $quiet; + diff --git a/scripts/globalise-config-variables b/scripts/globalise-config-variables new file mode 100755 index 0000000..5eed75f --- /dev/null +++ b/scripts/globalise-config-variables @@ -0,0 +1,67 @@ +#!/usr/bin/perl +# +# Script to convert blosxom plugin config variables to globals, +# for setting via 'blosxom.conf' or 'config' or 'prefs' +# + +use strict; +use Getopt::Std; +use IO::File; +use File::Copy; + +my %opts = (); +getopts('nq', \%opts); +my $noop = $opts{n}; +my $quiet = $opts{q}; + +my $dir = shift @ARGV || '*'; +for my $pname ( glob $dir ) { + next unless -f $pname; + my $ph = IO::File->new( $pname, 'r' ) + or warn "Cannot open plugin '$pname' for read: $!" and next; + my $plugin = ''; + { + local $/ = undef; + $plugin = <$ph>; + close $ph; + } + next unless $plugin; + next unless $plugin =~ m/__END_CONFIG__/; + print "Updating $pname ...\n" unless $quiet; + unless ($noop) { + my ($pre, $post) = split m/__END_CONFIG__/, $plugin, 2; + my $modified = $pre; + + # Globalise lexicals + my @lexicals = (); + while ($modified =~ m/^(\s*#\s*)?my\s+((\$\w+)\s*=)/m) { + push @lexicals, $3; + $modified =~ s/^(\s*#\s*)?my\s+((\$\w+)\s*=)/$1$2/m; + } + # Add a 'use vars' for lexicals if in strict mode + my %seen = (); + @lexicals = map { $seen{$_}++ ? () : $_ } @lexicals; + my $lexicals = join ' ', @lexicals; + $modified =~ s/^(use\s+strict\b[^\n]+\n)/$1use vars qw($lexicals);\n/m; + + # Add unless defined fragments to all config variables + $modified =~ s/\G(.*?^#?\s*([\$\@%]\w+)\s*=[^;]+);/$1 unless defined $2;/msg; + +# print "modified:\n\n$modified\n\n"; +# next; + next if $modified eq $pre; + + # Reconstitute and write out + $plugin = $modified . "__END_CONFIG__" . $post; + $ph = IO::File->new( "$pname.tmp", 'w' ) + or die "Cannot open plugin '$pname.tmp' for write: $!"; + print $ph $plugin + or die "Cannot write to plugin '$pname.tmp': $!"; + close $ph + or die "Cannot close plugin '$pname.tmp': $!"; + move "$pname.tmp", $pname + or die "Cannot move plugin '$pname.tmp' to '$pname': $!"; + } +} +print "Done.\n" unless $quiet; + -- 2.30.2