############################################################################ # # File: htprep.icn # # Subject: Program to prepare HTML files # # Author: Gregg M. Townsend # # Date: November 3, 2000 # ############################################################################ # # This file is in the public domain. # ############################################################################ # # usage: htprep [file] # # Htprep is a filter for preparing HTML files (used, e.g., by Mosaic) # from a simpler and less error-prone input language. # # The following transformations are applied: # # input output # ------------ ------------ # {} # {!comment} # {tag} # {tag ... } ... <\tag> # att=val... att="val"... # {@url ... " $define WSPACE ' \t' # whitespace cset record tag(label, line) # tag record global tagstack # currently open tags global cmdtable # table of known special commands global infile # input file global outfile # output file global stdout # standard output, if usable global lineno # current input line number global errors # error count global idset # identifier characters # main procedure procedure main(args) local line, t idset := &letters ++ &digits ++ '.-_:' lineno := 0 errors := 0 tagstack := [] stdout := &output cmdtable := table() cmdtable["divert"] := divert if *args = 0 then infile := &input else infile := open(args[1]) | stop("can't open ", args[1]) while line := in() do { lineno +:= 1 line := braces(line) out(line) } while t := pop(tagstack) do warn("unclosed tag {", t.label, "} from line ", t.line) if errors > 0 then stop else return end # braces(line) -- process items identified by braces ('{}') procedure braces(line) local c, s, t line ? { s := "" while s ||:= tab(upto('{}')) do { c := move(1) if c == "{" then s ||:= newtag() else { # "}" if t := pop(tagstack) then { if t.label == "!" then s ||:= "-->" else s ||:= "" } else lwarn("tag stack underflow") } } return s ||:= tab(0) } end # newtag() -- process text following left brace ('{') procedure newtag() local label, s, c if ="}" then return "" if ="!" then { push(tagstack, tag("!", lineno)) return "