Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

shtml publisher

by willdooUK (Beadle)
on Jan 25, 2001 at 20:17 UTC ( #54281=sourcecode: print w/ replies, xml ) Need Help??

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;

Comment on shtml publisher
Download Code
Re: shtml publisher
by dws (Chancellor) on Jan 26, 2001 at 01:23 UTC
    To make this correctly handle multiple SSI directives on a line, you're going to have to do a bit of work on one of your regexps.

    Try to make it work. The debugging will be insightful.

    Additionally, blindly doing s/\.shtml/.html/ may not be what you really want to do. But if it is, don't stop at the first one on the line.

    You might want to take a look at my SSI conversion script. It'll give you some ideas on how to handle other directives.

      Yup, this script has its limitations.

      In my case I didn't need it to handle more than one SSI include on one line (but I should have put that in the header)

      Since that shtml regexp is only meant to refer to links, it could maybe be changed to
      s/href=(.+)\.shtml/href=$1.html/gi
      although this would miss links in image maps and it wouldn't work if the href was split over two lines. Opening a can of worms....

      It should definately have had the global modifier :)

      Thanks for the tips.

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://54281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2015-07-02 01:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls