http://qs321.pair.com?node_id=54281
Category: HTML Utility
Author/Contact Info Stephen Doolan (willdooUK) - willdooUK@hotmail.com
Description: Utility to expand Include statements in html files, allowing them to be viewed without running a web server.
#!/usr/bin/perl

###########################################################
# shtml publisher - v1.0 (25/01/01)
#
# Stephen Doolan (willdooUK @ hotmail.com)
#
# This is a utility that expands include statements
# in shtml files, allowing you to view them without
# running a web server.
#
# These include statments take the form:
# <!--#include file="example.html"-->
#
# To use it, run the script and give it the names of
# two directories - one containing the shtml files, and
# the other which will contain the resulting html files
# (these will have had any references to '.shtml' changed
# to '.html')
#
# Note that at present this script only runs over a
# flat directory structure.
###########################################################

use strict;

my $sIncBefore      = "<!--#include file=\"";
my $sIncAfter       = "\"-->";
my $sShtmlDir       = "";
my $sHtmlDir        = "";
my $iArgCount       = @ARGV;
my $iChanges        = 0;
my $iFiles          = 0;
my $iChangedFiles   = 0;
my @files;

###################
# Parse arguments
###################

if($iArgCount < 2)
{
    die("Usage: publish SHTMLDIR HTMLDIR");
}

$sShtmlDir = @ARGV[0];
$sHtmlDir = @ARGV[1];

#####################
# Get list of files
#####################

if(!opendir(directory, $sShtmlDir))
{
    die ("Could not open $sShtmlDir");
}

@files = grep(/\.shtml/i, readdir(directory));
closedir(directory);

$iFiles = @files;

print "\nFound $iFiles files.  Press any key to start.\n";
<STDIN>;

##########################
# Loop through file list
##########################

foreach(@files)
{
    ################################
    # Get filenames and open files
    ################################

    my $sMadeChange = "";

    my $sReadFilename   = $sShtmlDir."\\".$_;
    my $sWriteFilename  = $sHtmlDir."\\".$_;

    $sWriteFilename =~ s/shtml$/html/;

    print "\n$sReadFilename:            ";

    if(!open(READFILE, $sReadFilename))
    {
        die ("Could not open read file: $sReadFilename: !");
    }

    if(!open(WRITEFILE, ">".$sWriteFilename))
    {

        die ("Could not open write file sWriteFilename: !");
    }

    ######################################
    # Go through each line in shtml file
    ######################################

    while(<READFILE>)
    {
        my $sLine = $_;

        # convert any references to '.shtml'
        $sLine =~ s/\.shtml/.html/;

        if($sLine =~ m/^(.*)$sIncBefore(.*)$sIncAfter(.*)$/)
        {
            $iChanges++;
            $sMadeChange .= "<Expanded> ";

            my $sBefore     = $1;
            my $sInclude    = $2;
            my $sAfter      = $3;

            print WRITEFILE $sBefore;

            ############################
            # Expand include statement
            ############################

            my $sIncFile = $sShtmlDir."\\".$sInclude;

            if(!open(INCLUDEFILE, $sIncFile))
            {
                die "Could not include $sIncFile: $!";
            }

            while(<INCLUDEFILE>)
            {
                # convert any references to '.shtml'
                s/\.shtml/.html/;

                # write line to html file
                print WRITEFILE $_;
            }

            close(INCLUDEFILE);

            print WRITEFILE $sAfter."\n";
        }
        else
        {
            print WRITEFILE $sLine;
        }
    }

    close(READFILE);
    close(WRITEFILE);

    ####################
    # Display progress
    ####################

    print $sMadeChange;

    if (!$sMadeChange eq "")
    {
        $iChangedFiles++;
    }
}

print "\n\nAll Done: $iChanges changes made on ";
print "$iChangedFiles out of $iFiles files.\n";

exit;