Java Site MenuProgramming SectionsMiscellaneous StuffConsultancy ServicesDownloadsFeedback Form


Perl - Web Site Maintenance [Home]


Download the script

Like many people on the Web, you may use an ISP that does not provide server-side includes or scripting support. If so, keeping all the common content in your Web pages updated and in step can be difficult and tiresome. For example, just changing the copyright message or contact details in the footer of the pages can mean having to edit a large number of files. However, by writing some fairly simple Perl to pre-process your Web pages, some of these difficulties can be smoothed out. Described here is such a script.

build_file.pl

To use this script you write template web pages that reference common content. These templates get expanded by the script into the full HTML pages to upload to the Web-site. The script can either expand a single file from a source location to an equivalent destination location, or an entire directory and all the files below. To do the expansion the script reads the template page from the source location, processes it to find include references to other pre-written HTML snippets, or pagelets as I term them, and substitutes the similarly processed snippet content in place of the reference. The final expanded HTML is written to the destination location. The references are just special markup inside normal HTML.

During the processing of source directories, any file that does not contain inclusion markup is copied unchanged to the destination. This way it is possible to use the script to point to the top of a web site file structure and have it expanded and copied to the destination without worrying about whether binary files having been corrupted. I have tried this with GIFs, JPGs, Java class files and Zips amongst others with no harmful effect.

The script allows for cascading of page inclusions which means that the referenced pages can themselves reference further pages. The pagelet files can be organised into directories of common content because the inclusion reference separates pagelet name and location. Using the inclusion cascading and the directory separation, a manageable hierarchy of simple pagelets can then be created and knitted together for final use in the templates. Also, in order that context sensitive information can be passed down to the included pagelets, the markup syntax allows for parameters to be given in the inclusion reference.

Inclusion Syntax

The simplest inclusion request looks like this:

%%pagelets::copyright%%

The '::' characters separate the directory location part from the pagelet filename. The entire reference is replaced with the processed content of the 'pagelets/copyright' file. In this instance the location of the pagelets directory is relative to the directory in which you are when the the script is run. You can put in a fully qualified path instead if you wish.

A more complex, parameterised request might look like this:

%%pagelets::titlebarhead(DIR='../',TITLE='A page about PERL')%%

The parameters are passed as comma-separated key-value pairs. The value must be delimited with single quotes as shown. The values are expanded into the referenced pagelet file by using a similar markup to the references. For example in the pagelet file 'titlebarhead' from the example above, one could use the DIR parameter like this:

<a href="%%DIR%%index.html">link</a>

in order to help make the pagelet more easy to include in pages in various different sub-directories.

Example pagelet

I have made extensive use of this script in writing this Web site which is constructed entirely from template and pagelet files. For example the "next page" image links are generated using the pagelet shown below.

<a HREF="%%URL%%"><IMG
	SRC="%%DIR%%images/next_page.gif" WIDTH=26 HEIGHT=26 ALT="[Next]"
	ALIGN=absbottom BORDER=0></a>

Script Usage

the script is invoked as shown below.

build_file.pl [ -o ] ( src_dir | src_file ) dest_dir
        -o        overwrite existing destination files
        src_dir   source directory to copy and expand
        src_file  source file to copy and expand in equivalent
                  destination location
        dest_dir  top-level of destination, created if needed

Download

You can download a Zip file containing the script and the custom Perl modules it uses from here.


[Fiendish Home]


Content of this page Copyright © Robert Quince 1996 - 2005.
Site Comments