summaryrefslogtreecommitdiff
path: root/ipl/progs/htprep.icn
blob: fbe7b321b00e7e7e9bc7cd03d7641042d51da694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
############################################################################
#
#	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}	<!--comment-->
#	{tag}		<tag>
#	{tag ... }	<tag> ... <\tag>
#	  att=val...	  att="val"...   
#	{@url ...	<a href="url" ...
#	{:lbl ...	<a name="lbl" ...
#
#  Any input character can be preceded by a backslash (\) to prevent 
#  special interpretation by htprep.
#
#  Output is normally to stdout, but the command
#	{divert fname}
#  redirects output to the named file.  This can be used to produce
#  multiple related output files from a single input file.
#
############################################################################

$define SIGNATURE "<!-- Created by HTPREP -->"
$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 ||:= "</" || t.label || ">"
               }
            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 "<!--"
      }

   if c := tab(any('@:')) then {
      label := "a"
      if c == "@" then
         s := "<a href="
      else
         s := "<a name="
      s ||:= attval()
      }
   else {
      label := tab(many(idset)) | (lwarn("unlabeled tag") & "noname")
      s := "<" || label
      }

   if \cmdtable[map(label)] then
      return s := docommand(label)

   while s ||:= attrib()
   tab(many(WSPACE))
   ="}" | push(tagstack, tag(label, lineno))
   return s || ">"
end



#  attrib() -- match and return attribute

procedure attrib()
   return tab(many(WSPACE)) || tab(many(idset)) || ="=" || attval()
end



#  attval() -- match and return attribute value

procedure attval()
   static valset
   initial valset := &cset[34+:94] -- '\'\\"{}'
   return (="\"" || tab(upto('"')) || move(1)) |
      (="'" || tab(upto('\'')) || move(1)) |
      aquote(tab(many(valset)))
end



#  aquote(s) -- quote attribute value, but only if needed

procedure aquote(s)
   if many(idset, s) = *s + 1 then
      return s
   else
      return '"' || s || '"'
end



#  docommand(label) -- process a tag recognized as a command

procedure docommand(label)
   local p, atts, words, id, s

   p := cmdtable[label]
   atts := table()
   words := []
   while s := attrib() do s ? {
      tab(many(WSPACE))
      id := tab(many(idset))
      move(2)
      atts[id] := tab(-1)
      }
   while tab(many(WSPACE)) & (s := tab(bal(' }', '{', '}'))) do
      put(words, s)
   tab(many(WSPACE))
   ="}" | lwarn(label, ": unterminated command")
   return p(atts, words) | ""
end



#  in() -- read next line, interpreting escapes
#
#  Reads the next line from infile, removing leading and trailing whitespace.
#
#  If an ASCII character is preceded by a backslash, the character's eighth
#  bit is set to prevent its recognition as a special character, and the
#  backslash is retained.  If it's not an ASCII character (that is, if the
#  eighth bit is already set) the backslash is simply discarded.

procedure in()
   local s

   trim(read(infile), WSPACE) ? {
      tab(many(WSPACE))
      s := ""
      while s ||:= tab(upto('\\')) do {
         move(1)
         if any(&ascii) then
            s ||:= "\\" || char(128 + ord(move(1)))
         else
            s ||:= move(1)
         }
      return s ||:= tab(0)
      }
   fail
end



# divert(attlist, wordlist) -- process "divert" command
#
# If an error is seen, a message is issued and subsequent output is
# simply discarded.

procedure divert(atts, words)
   local fname, f

   close(\outfile)			# always close current file
   outfile := stdout := &null		# no current file, and no fallback

   if *words ~= 1 then {
      lwarn("usage: {divert filename}")
      fail
      }

   fname := get(words)
   if f := open(fname) then {
      if read(f) ~== SIGNATURE then {
         lwarn("divert: won't overwrite non-htprep file ", fname)
         close(f)
         fail
         }
      close(f)
      }

   if outfile := open(fname, "w") then {
      out(SIGNATURE)
      return ""
      }
   else {
      lwarn("divert: can't open ", fname)
      fail
      }
end



#  out(s) -- write line, interpreting escapes
#
#  When a backslash is seen, the backslash is discarded and the eighth
#  bit of the following character is cleared.

procedure out(s)
   local c

   if /outfile := (\stdout | fail) then
      write(outfile, SIGNATURE)		# if first write to &output

   s ? {
      while writes(outfile, tab(upto('\\'))) do {
         move(1)
         writes(outfile, char(iand(127, ord(move(1)))))
         }
      write(outfile, tab(0))
      }
   return
end



#  lwarn(s, ...) -- issue warning with line number  

procedure lwarn(a[])
   push(a, "line " || lineno || ": ")
   warn ! a
   return
end



# warn(s,...) -- issue warning message

procedure warn(a[])
   push(a, "  ")
   push(a, &errout)
   write ! a
   errors +:= 1
   return
end