|
Web Authoring Examples |
|
See Also: |
This example builds on the work that was done in Part 2: NavBars and Menus.
Example files for this topic are found in
Program
Files\APGen\Examples\Aut_Auth\Build
The example files were copied from the Aut_Auth\NavBar directory. A script named build.apg is added in this example.
So far we have automated several web authoring tasks, but we still need to manually run each page script in the site. In this example, we will automate the site build process. As you recall from the previous examples, the XML file contains a webpage tag plus attributes for each page. This provides all the information needed to automate the process of building all the pages in the site.
The build script, build.apg, walks the XML hierarchy in the same manner as RenderNavBar does. But, instead of writing out navbar links for each page, build.apg runs the APG script for each page, using programmatic execution.
Build.apg writes all output files to a subdirectory named "output". Build.apg also copies all accessory files to the output directory. Accessory files are images and other files that are necessary for the output files to be properly viewed.
BuildPage is the recursive procedure. The procedure is run once per XML webpage element. The procedure runs the APG script for the current webpage element, then recursively calls itself for each child webpage.
Sub Main is run once. Sub Main creates the msxml object, sets the APG script output directory and loads the site.xml document. Next, Sub Main calls BuildPage for each webpage element that is a child of the site object. Finally, Sub Main copies all the accessory files to the output directory.
<%#
Option Explicit
'--------------------------------------------------------------
' build.apg
'
' Script to build all the web pages in site.xml.
'
' Assumption: Given a webpage item with a fname attribute,
' there's an APG script with the same name.
' For example, fname="hello.htm" requires a hello.apg script.
'--------------------------------------------------------------
'
' BuildPage
'
' Given an XML element, builds its web page
'
Sub BuildPage (oXmlElt)
' Figure out the APG script filename for this oXmlElt
' Do this by replacing the filename extension with "apg"
Dim strFname, strAPGFname
strFname = oXmlElt.getAttribute("fname")
strAPGFname = Left(strFname, InStrRev(strFname, ".")) & "apg"
' Open the script
Dim oScript
Set oScript = APGen.OpenScript(strAPGFname)
' Enable creation of output dir
oScript.Output.CanCreateDirs = True
' Run the script
oScript.Run
' Free oScript object (optional, but not needed anymore)
Set oScript = Nothing
' Recursively build all children
Dim oChildElt
If Not oXmlElt.children Is Nothing Then
For Each oChildElt In oXmlElt.children
BuildPage oChildElt
Next
End If
End Sub
'
' Main
'
' Build all pages in site.xml
'
Sub Main
' Set path of output directory
Dim strOutputDir
strOutputDir = "output/"
' Set output directory for all pages
APGen.OutputDir = strOutputDir
' Create the XML object and parse the XML file
Dim oXml
Set oXml = Script.CreateObject("msxml")
oXml.URL = "file://" & Script.Dir & "site.xml"
' Note: previous line assumes that the site.xml file is in
' the same directory as build.apg
' Call BuildPage for each <webpage> that's a child of <site>
Dim oXmlElt
For Each oXmlElt In oXml.root.children
BuildPage oXmlElt
Next
' Free XML object
Set oXML = Nothing
' Copy accessory files to output dir
Dim oFSO, rgAccFiles, strAccFile
rgAccFiles = Array("arrow.gif", "site.css", "trfaq.gif")
Set oFSO = Script.CreateObject("Scripting.FileSystemObject")
For Each strAccFile In rgAccFiles
oFSO.CopyFile strAccFile, strOutputDir
Next
End Sub
#%>
Run build.apg by double-clicking the script file. Then, browse the contents of the created /output/ directory.
The output directory is set in build.apg by
strOutputDir = "output/"
...
' Set output directory for all pages
oAPGen.OutputDir = strOutputDir
This code sets the output directory to a subdirectory named "output". In BuildPage, we set the script's Output.CanCreateDirs property to ensure that the output directory is created (if it does not already exist):
' Enable creation of output dir
oScript.Output.CanCreateDirs = True
The accessory files' filenames are stored in an array. Each accessory file is copied to the output directory by
oFSO.CopyFile strAccFile, strOutputDir
In a production environment, you may want to store the list of accessory files in the site.xml file. Remember to copy the accessory files when writing a build script: Without the accessory files, the web pages can not be properly viewed in the browser.
The build.apg script makes building the web site a single-step task. Running build.apg generates all output files and places them in a single directory. When you add, move, rename, or delete a page, make the appropriate change in the site.xml file, and then run the build.apg script. Your web site has become much easier to maintain.
In the example above, there are no relative paths. Every file is in the same directory, including the images and CSS file. So every HREF and SRC value can use the form "filename.ext".
In a production web site, the output directory will have subdirectories. When you are building the site, you want to keep all links valid. Here are a few suggestions for maintaining relative links:
| Page Script | Creates Output File |
| /default.apg | /default.htm |
| /dir/default.apg | /dir/default.asp |
| /dir/press.apg | /dir/press.htm |
Continue with Part 4: Creating New Pages ...