#!/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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|