OS/2 eZine - http://www.os2ezine.com
Spacer
October 16, 2004
 
AL du Pisani is a South African, working at a large financial institution, where he looks after their Lotus Notes environment. He has been using computers off and an for the past twenty years. His first exposure to OS/2 was with 2.11. It is still the preferred environment, and is what he runs at home. He is also a fan of Science Fiction.
If you have a comment about the content of this article, please feel free to vent in the OS/2 e-Zine discussion forums.

There is also a Printer Friendly version of this page.



Spacer
Previous Article
Home
Next Article


Injoy Firewall


Case Study: Redeveloping a web site with PPWizard (Part 4): Generating a template file with REXX

In the previous part of this case study, I dealt with blocking out page elements, page specific header files, conditional processing and importing data.

Today I will be looking at extending PPWizard by doing REXX programming. You may want to refer to MakeBook.CMD which contains the code.


Why did I have to extend PPWizard?

I have mentioned before that I had some difficulty with the #import function, when dealing with anything but straight import into a table. So when I looked at the book reviews page, I had some trouble.

The book reviews page have the following layout:

  • Standard top of page element
  • Table with link to reviews from previous years
  • Table with links to books reviewed this year
  • Individual book review, per book
  • Standard bottom of page element

What I wanted

I wanted a single file with semi-structured text, containing the book reviews. I then wanted to turn this semi-structured text into a HTML page consisting of a table with links to the individual reviews, and the individual reviews, seperated by a horizontal rule between each review.

I had two problems: For one, I could not figure out from the PPWizard documentation how to do this. For another, it seemed that the way I wanted the book reviews text file to be structured was different from the way PPWizard was expecting to import the text.

At this stage I decided to cheat: I would write a REXX program to generate the template file from my semi-structured text, and then generate the HTML page from there.

It worked surprisingly well.

Structure of book reviews text file

Each review starts with information regarding the book, with the body of the review following.
  • Book Title:
  • Author:
  • Publisher:
  • Book Format:
  • Pages:
  • Price:
  • Reviewer:
  • Date of Review:
  • Body:
I implemented this as follows (sample below):
BTITLE: Naked Empire
BAUTHOR: Terry Goodkind
BPUBLISHER: Voyager (HarperCollins)
BFORMAT: Hard cover
BPAGES: 660 pages
BPRICE: R230?
BREV: AL du Pisani
BDATE: 13 January 2004
BBODY: Book 8 of The Sword of Truth
I have not read the previous novel in this series, The Pillars of
Creation, which told the story of Richard Rahl half sister, Jennsen,
and her encounter with the Imperial Order, and later, her half brother.
So I am not that familiar with some of the events alluded to in this
book. And this book follows on from the previous one. In fact, while
there are some continuity problems within the series, every book have
followed on to, and build on, events in the previous book. Because this
in a world in which actions have consequences, and Richard and his wife
Kahlan are still dealing with some of the consequences of events that
took place three books ago.
...

The review carries on for a while. The next review, with no special seperator between reviews, starts directly below this review. This pattern continues until the end of the file.

The program

Please look at the attached REXX source code. (See MakeBook.CMD). I have tried to document it by comment.

I was trained in programming in procedural code, and always try and explicitly declare all variables and constants. This is the first two elements of the program.

The previous year table is a list of previous years' reviews, and I update the table manually once a year. Currently I have only a single year's worth of previous reviews.

The REXX Utility functions are used later in the program. I declare them early.

The program then read the input file into memory, and parse it into two arrays: The OF array, which comprises of lines of data, and the Books array, which comprises a set of data per book.

To handle the various book information tags at the begining of each line, the program test using a SELECT statement.

For the Title tag, the program need to close the previous review, if any. The program then start a new Books entry, and read the book title and a generated anchor. The anchor value is generated from the number of the book review and the first part of the book title. This is for later use in the page book links table.

The program also create two new lines for the OF array, namely a seperator, in this case a horizontal rule, and the anchored book title.

For the rest of the book information lines the program read the requisite information into the Books array, and create the required OF array line or lines.

When the program get to the body of the review, it stops reading information into the Books array, and just read the information line for line into the OF array.

The only special part the program look for is blank lines, which is treated as paragraph seperators. First closing the preceding paragraph, and then opening the new paragraph.

Once the entire file's information have been read into the two arrays, the program finalise the arrays, and close the book reviews file.

The program now open a new output file, and generate the standard header, top of page information and previous years' links table.

The program then create the top of page books links table from the Books array, enabling the user of the page to jump directly to the review he wants.

Then the program create the review body, from the OF array. Finaly, the program create the standard foot of page element, and close down the output file.

This process is surprizingly fast, and will generate a template file from a nearly thousand line book reviews file in under five seconds.

Difficulties

I was shocked when tried out the initial rough progam, and it worked nearly perfectly the first time.

There were some spelling mistakes and typos, and I changed the order and structure of some of my generated template file elements, later. But it worked and did what I wanted.

The generated template file also generated clean HTML, once I ran it through PPWizard.

It would appear that if you know how to code REXX, it is simple to extend PPWizard to do things your way.

Conclusion

I found it easy to extend PPWizard externally, by writing code which generated a PPWizard template file. I did this to because I wanted to do something my way.

Next time I will look at separating information from presentation, by using Cascading Style Sheets.

Previous Article
Home
Next Article

Copyright (C) 2004. All Rights Reserved.