[lug-nuts] another replacement script for managing large html site

From: Brian Lavender (brian@brie.com)
Date: Tue Dec 14 1999 - 22:49:15 PST


I have been playing more, so I thought I ought to share.
This script will go through a directory, and replace between
a couple comment files. Bottom line is, you can change a header and
footer without depending upon SSI's or PHP. Not to say those are
useful tools, but as PERL hackers say:

"There's more than one way to do it!"

It's real fast too. I don't know if my machine was caching I/O, but
after doing it once, the script ran in only a couple seconds on
my machine.

I also put it on a cvsweb interface, so you can see the mods. Check

http://www.brie.com/cgi-bin/cvsweb.cgi

You get to see my glorious picture on all the pages changed if you check
http://www.brie.com/~brian/bbd/ . Such a wonderful thing. :)

In addition, xemacs is working beautifully. For you vi lovers, I am
editing this email with vim, and I definitely like using it for editing
email, but if you have not thoroughly investigated emacs for hacking
PERL, C, or C++, you ought to investigate it. Emacs definitely
requires commitment, and may exasperate you, but I find it
worthwhile.

Enough babbling. Here's the script.

#!/usr/bin/perl

# Configuration Variables
# Change these variables to match your web server's configuration

$base_dir = "/home/brian/public_html/bbd";

use File::Find;

finddepth(\&wanted, $base_dir);

sub wanted {

  if (/\.html$/) {

    $file = "$File::Find::name";
    $old = "$file";
    $new = "$file.tmp.$$";
    $bak = "$file.bak";

    open(OLD, "< $old") or die "can't open $old: $!";
    open(NEW, "> $new") or die "can't open $new: $!";

    # Correct typos, preserving case
    while (<OLD>) {
      $count = 0;
      if (/<!-- replace navigable bar begin -->/) {
        <OLD>;
        while (<OLD>) {
          if ((/<!-- replace navigable bar end -->/) or ($count > 5)) {
            last;
          } else {
            $count++;
          }
        }
        print NEW '<!-- replace navigable bar begin -->',"\n";
        print NEW '<IMG SRC="/brian/pictures/brian.jpg" ALT="Picture of Brian Lavender" WIDTH="157" HEIGHT="180" BORDER="3" ALIGN="RIGHT" NATURALSIZEFLAG="0">', "\n";
        print NEW '<!-- replace navigable bar end -->',"\n";
      } else {
        (print NEW $_) or die "can't write to $new: $!";
      }
    }

    close(OLD) or die "can't close $old: $!";
    close(NEW) or die "can't close $new: $!";

    rename($old, $bak) or die "can't rename $old to $bak: $!";
    rename($new, $old) or die "can't rename $new to $old: $!";

  }

}

-- 
Brian Lavender
http://www.brie.com/brian/
****************************************************************************
* To UNSUBSCRIBE from the list, send a message with "unsubscribe lug-nuts"
* in the message body to majordomo@saclug.org. Please direct other
* questions, comments, or problems to lug-nuts-owner@saclug.org.



This archive was generated by hypermail 2b29 : Fri Feb 25 2000 - 14:29:09 PST