Serious First Steps In UserTalk Scripting
Prev |
Table Of Contents |
Next
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. Studying ALittleHelp, 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:
local (folder) if not file.getFolderDialog ("Where is the AmelioWeb folder?", @folder) {return} local (templatetext = string(toys.readWholeFile (folder + "template.html"))) local (infolder = folder + "Source Files:") local (outfolder = folder + "Website:") file.sureFolder (outfolder) toys.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 (toys.readWholeFile (infile)) 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) s = html.processMacros (s) toys.writeWholeFile (outfile, s, 'TEXT', Netscape.id, 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) toys.writeWholeFile (outfolder + "default.html", s, 'TEXT', Netscape.id, clock.now ()) webbrowser.openDocument (outfolder + "default.html")
Run this. The results are excellent!
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. His solution, by the way, is implemented as a double-clickable file, which accounts for some of the differences; in his version, you double-click the file ("buildSite") in the Finder and it runs. To examine it, if that's what you'd like to do, you need to load it into the database rather than running it; to do so, double-click it in the Finder but hold the command-key down.
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.
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 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 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 |
Table Of Contents |
Next
Text © Matt Neuburg 1997 ALL RIGHTS RESERVED
You can download a copy of this tutorial.
This Web document scripted with Frontier. Last build at 4/18/97; 10:50:31 PM.