Serious First Steps In UserTalk Scripting
by Matt Neuburg
Author of the book Frontier: The Definitive Guide

Prev | TOC | Next


Final Touches

Table of contents page

All that's left is to create the table-of-contents page, consisting of a list of links to the other HTML pages.

Let's plan a strategy for this. How do we want the page to look? We can use the same template we are already using; we'll supply our own "title" and "subtext".

The question is then what the "bodytext" should consist of. It should be a series of paragraphs, one for each HTML file we created. Each paragraph should consist of the "title" and "subtitle" for the corresponding HTML file, nicely formatted, and with the "title" being a live link to the HTML file.

This means we're going to have to retain the "title", "subtitle", and "fname" of each HTML file as we process it. So we may as well just accumulate the HTML for the "bodytext" part of the table-of-contents page as we go along. Let's build it in a string variable, calling it "tocbodytext".

Here is how we might want the finished HTML in "tocbodytext" to look:

<p><b><a href="face_time.html">Face time with 
the mini-poser hoo-haws</a></b>. 
A very close colleague of mine has a way with 
descriptive verbiage.</p>
<p><b><a href="geek_gaugin.html">Where geek meets Gaugin</a></b>. 
Content, creativity and computer code</p>

And so on. What's the formula for this? It's:

tocbodytext = tocbodytext + "<p><b><a href = \"" + fname + "\">" + \
title + "</a></b>. " + cr + subtitle + "</p>" + cr

(Notice that we express doublequotes inside a string by "escaping" them, that is, by saying backslash-doublequote. The backslash won't be present in the actual string; it's just a way of letting Frontier know that we aren't terminating the string yet.) If we just add this to the "fileloop" material somewhere, we'll be building the correct HTML as we go along.

Then, at the end of the whole routine, after the last pass through the "fileloop", we need to process "templatetext", "title", "subtitle", and "tocbodytext" in just the same way as we've been doing for each file, and write the result out as a textfile, which, since it's the table-of-contents page, we may as well call "default.html".

We just need to decide on an appropriate "title" and "subtitle" for this last page; let's have the "title" be "Gil Amelio's Opinions", and the subtitle be "Apple CEO Gil Amelio writes about issues of the day."

There is just one further point. Studying the files in "Source Files", we find that some of them have a space at the end of paragraph that we're using as our "title". This will generate an extra space before the period that we're inserting after our use of "title" in "tocbodytext".

Let's handle this in a completely general way; way back in our popline() handler, we'll remove leading and trailing spaces from the result before we hand it out. We find that once again there are UserTalk verbs to help us with this, string.popLeading() and string.popTrailing().

Here, then, is our whole routine as modified to generate a table-of-contents page at the end:

html.data.adrpagetable = @websites.["#data"]
local (folder)
if not file.getFolderDialog \
    ("Where is the AmelioWeb folder?", @folder) {return}
local (templatetext = string (file.readWholeFile (folder + "template.html")))
local (infolder = folder + "Source Files" + file.getPathChar())
local (outfolder = folder + "Website" + file.getPathChar())
file.sureFolder (outfolder)
file.emptyFolder (outfolder)
local (infile, filetext, title, subtitle, tocbodytext="")
on popline()
    local (s)
    s = string.NthField (filetext, cr, 1)
    filetext = string.delete (filetext, 1, string.length(s) + 1)
    s = string.popLeading (s, ' ')
    s = string.popTrailing (s, ' ')
    return s
webbrowser.bringToFront ()
fileloop (infile in infolder)
    local (fname = file.filefrompath (infile))
    msg(fname)
    if file.isFolder (infile)
        file.copy (infile, outfolder + fname)
    else
        filetext = string (file.readWholeFile (infile))
        filetext = string.replaceAll (filetext, lf, "")
        title = popline (); popline (); popline (); popline ()
        subtitle = popline (); popline ()
        local (s = templatetext)
        s = string.replaceAll (s, "<<title>>", title)
        s = string.replaceAll (s, "<<subtitle>>", subtitle)
        s = string.replaceAll (s, "<<bodytext>>", filetext)
        s = html.processMacros (s)
        fname = string.replace (fname, ".txt", ".html")
        outfile = outfolder + fname
        file.writeWholeFile (outfile, s, 'TEXT', 'MSIE', clock.now ())
        webbrowser.openDocument (outfile)
        tocbodytext = tocbodytext + "<p><b><a href = \"" + fname + "\">" + \
        title + "</a></b>. " + cr + subtitle + "</p>" + cr
title = "Gil Amelio's Opinions"
subtitle ="Apple CEO Gil Amelio writes about issues of the day."
local (s = templatetext)
s = string.replaceAll (s, "<<title>>", title)
s = string.replaceAll (s, "<<subtitle>>", subtitle)
s = string.replaceAll (s, "<<bodytext>>", tocbodytext)
s = html.processMacros (s)
file.writeWholeFile (outfolder + "default.html", s, 'TEXT', 'MSIE', clock.now ())
webbrowser.openDocument (outfolder + "default.html")

Run this. The results are excellent!

It's alive

This completes our program. Our solution is not absolutely identical to Dave Winer's, but it's extremely close; the differences are minor matters of form and implementation.

One difference between Dave's version and ours is that he has proceeded to take some extra steps to neaten up the code. In particular, it is quite silly that the next-to-last line of our script, and the five lines that precede it, are basically identical to six lines within the "fileloop".

We can eliminate this repetition by abstracting both sets of six lines into a local handler, and then just calling that handler with appropriate parameters at each point. This, as they say, is left as an exercise for the reader.

Frontier, at your service

We've taken some serious first steps in UserTalk scripting. They are only first steps, but we have shown that they take us far enough to let us get some serious work done, and we know that we can use the documentation to go further still.

We are now so far up the learning curve that your further studies should come relatively easily. The most important hurdle that I hope this tutorial has enabled you to leap, is fear. Don't shy away from programming Frontier -- your impulse should be to reach for it.

When there's work to be done, think of marshalling Frontier's power to do it. It's waiting, at your command.


Prev | TOC | Next

All text is by Matt Neuburg, phd, matt@tidbits.com.
For information about the book Frontier: The Definitive Guide, see my home page:
http://www.tidbits.com/matt
All text copyright Matt Neuburg, 1997 and 1998. ALL RIGHTS RESERVED.
No one else has any right to copy or reproduce in any form, including electronic. You may download this material but you may not post it for others to see or distribute it to others without explicit permission from the author.
Downloadable versions at http://www.ojai.net/matt/downloads/scriptingTutorial.hqx and http://www.ojai.net/matt/downloads/scriptingTutorial.zip.
Please do not confuse this tutorial with a certain other Frontier 5 tutorial based upon my earlier work.
This page created with Frontier, 2/11/2000; 6:58:59 PM.