UserLand Software
Powerful, cross-platform web scripting.
 

Final Touches

Frontier Scripting Tutorial

About This Tutorial

What Does Frontier Do?

Keywords, Handlers, Verbs, and Calls

Loops, Variables, Parameters, and Conditionals

The Handler Rule

Returns, Addresses, and Dereferencing

Scope and With

Strings and Files

Outlines and Tables

Running, Debugging, and Getting Help

Datatypes

A Real-Life Problem

String Parsing and Substitution

Manipulating Files and Folders

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 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 "bodytext" HTML for the table-of-contents page as we go along. Let's keep 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

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 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. There are Frontier 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. And one final touch: let's bring up the About Window so we can see our progress.


local (folder)
if not file.getFolderDialog ("Where is the AmelioWeb folder?", @folder) {return}
window.about ()
local (templatetext = string (file.readWholeFile (folder + "template.html")))
templatetext = string.replaceAll (templatetext, cr + lf, cr)
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, sizeOf (s) + 1)
   s = string.popLeading (s, ' ')
   s = string.popTrailing (s, ' ')
   return (s)
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, cr + lf, cr)
      fname = string.replace (fname, ".txt", ".html")
      outfile = outfolder + fname
      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)
      local (adrTable = @websites.["#data"])
      html.data.adrPageTable = adrTable
      adrTable^.activeURLs = true
      adrTable^.clayCompatibility = false
      adrTable^.autoParagraphs = true
      s = html.processMacros (s)
      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)
local (adrTable = @websites.["#data"])
html.data.adrPageTable = adrTable
adrTable^.activeURLs = true
adrTable^.clayCompatibility = false
adrTable^.autoParagraphs = true
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 Frontier 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 how to 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 Frontier scripting -- 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.

PreviousNext

   

Site Scripted By Frontier © Copyright 1996-98 UserLand Software. This page was last built on 2/10/98; 1:25:52 AM. It was originally posted on 4/15/97; 8:53:50 PM. Webmaster: brent@scripting.com.

 
This tutorial was adapted for Frontier 5 by Brent Simmons, from the Frontier 4 scripting tutorial written by Matt Neuburg.