File: preparse.scm

package info (click to toggle)
sdc 1.0.8beta-8
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 1,400 kB
  • ctags: 874
  • sloc: lisp: 8,120; ansic: 967; makefile: 671; perl: 136; sh: 50
file content (697 lines) | stat: -rw-r--r-- 18,161 bytes parent folder | download
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
(set! sgml-opts (cons "-i HTML " sgml-opts))

;{{{ old scheme stuff -- to be removed
(set! process-cdata html-tr-string)
(set! process-text html-process-text)
;;;-----

(define current-label "")
(define bib-label "")
(define current-o-file "")
(define bib-file "")
(define index-file "")

;===========================================================================

;}}}

(message 1 "Loading html")
(set! *load-path* (append *typeset-lib* *load-path*))
(set! sgml-opts (cons "-i HTML " sgml-opts))
(set! sgml-subdir "sgml")

(load "include/normal.scm")
(load "include/file-split.scm")
(load "include/manpage.scm")
(load "include/layout.scm")

(set! normalize-make-index-section #t)
(set! normalize-make-bib-section #t)

(define html-p-data
  `(#(DATA) ,(rdp-hmap (lambda (t) (html-tr-string (data-token-data t))))))

;{{{ conv-for-repll

(define (conv-for-repll xxx)
  (letrec
      ((c1 (lambda (token x)
	     (if (symbol? x)
		 (xatv token x)
		 x)))
       (ce (lambda (token x)
	     (cond
	      ((symbol? x)
	       (let* ((attrib (xat token x))
		      (t (if attrib
			     (arg-type attrib)
			     (begin
			       (message 0 "Argument " x
					" not defined in " token)
			       #f))))
		 (cond
		  ((eq? t 'CDATA) (html-tr-string (arg-val attrib)))
		  ((not t) #f)
		  (else (arg-val attrib)))))
	      ((pair? x) (map (lambda (x) (ce token x)) x))
	      (else x)))))
    (cond
     ((and (pair? xxx) (not (procedure? (car xxx))))
      (lambda (token)
	 (ce token xxx)))
     (else xxx))))

(define (def-repl content lists trailer)
  (if (null? lists)
      trailer
      `(,@(map (lambda (i)
		 `(#(STARTTAG ,(car i))
		   ,(rdp-repll (conv-for-repll (cdr i)) content)))
	       (car lists))
	. ,(def-repl content (cdr lists) trailer))))

;}}}
;{{{ section numbering

(define (sect-no-from-id id)
  (let ((no (reverse id)))
    ((case (car no)
       ((S F C B F) ordinary-seq-format)
       ((A) appdx-seq-format)
       (else (message 0 'sect-no-from-id " "(car no)
		      " is not a valid format definition.")
	     ordinary-seq-format))
     (cdr no))))

(define (typed-sect-no-from-id id)
  (if
   (and (pair? id))
   (let ((no (reverse id)))
     (if
      (symbol? (car no))
      (string-append
       (symbol->string (car no))
       ((case (car no)
	  ((S F C B F) ordinary-seq-format)
	  ((A) appdx-seq-format)
	  (else (message 0 'typed-sect-no-from-id (car no)
			 " is not a valid format definition.")
		ordinary-seq-format))
	(cdr no)))
      (begin
	(message 0 'typed-sect-no-from-id " can't work with id " id)
	"ERROR")))
   (message 0 'typed-sect-no-from-id " can't work with id " id)))

(define (f-sect-no token) (sect-no-from-id (xatv token 'NO)))

(define (f-t-sect-no token) (typed-sect-no-from-id (xatv token 'NO)))

;}}}
;{{{ referencing functions

(define (ref-mark token)
  (let* ((mark (xat token 'MARK))
	 (val (if (eq? (arg-type mark) 'PROMISE)
		  (force (arg-val mark))
		  (arg-val mark))))
    (if val
	(sect-no-from-id (cdr val))
	"??")))

(define (href-mark token)
  (let* ((mark (xat token 'MARK))
	 (val (if (eq? (arg-type mark) 'PROMISE)
		  (force (arg-val mark))
		  (arg-val mark))))
    (if val
	`(("<A HREF=\"" ,(car val) #\#
	   ,(typed-sect-no-from-id (cdr val)) "\">")
	  (,(sect-no-from-id (cdr val)) "</A>"))
	'())))

;}}}
;{{{ (html-tbl l-contents)

(define html-tbl-stuff (delay (begin
			       (load-silent "include/tbl.scm")
			       (load-silent "target/man/tbl.scm")
			       (load-silent "target/html/tbl.scm"))))

; somewhat hacky to resemble the old behavior

(define (old-html-tbl l-contents)
  (define (empty-cells n)
    (if (<= n 0) '() (cons "" (empty-cells (- n 1)))))
  (let*
      ((tr man-tr-string)
       (patterns '())
       (rows '())
       (cp #f)
       (highpat (let ((x (ints 0))) (lambda () (set! x (tail x)) (head x))))
       (atvn (lambda (t a) (string->number (car (xatv t a)))))
       (rdpl (lambda (f . fx)  (apply rdp-accu-list f fx)))
       (pattern-length (lambda (pat) (length (cdr pat))))
       (definition-tag
	 (lambda (token)
	   (case (token-gi token)
	     ((LEFT)    `#(CELL ,(atvn token 'SPAWN) LEFT AUTO))
	     ((RIGHT)   `#(CELL ,(atvn token 'SPAWN) RIGHT AUTO))
	     ((CENTER)  `#(CELL ,(atvn token 'SPAWN) CENTER AUTO))
	     ((BLOCK)   `#(CELL ,(atvn token 'SPAWN)
				,(STRING->symbol (car (xatv token 'ALIGN)))
				,(atvn token 'WIDTH)))
	     ((DECIMAL) `#(DECIMAL ,(atvn token 'SPAWN)
				   ,(xatv token 'ALIGN) AUTO))
	     ((SEP)     `#(SEP 0 ,(car (xatv token 'SEP)) 0))))))
    (letrec
	((call-contents
	  (rdp-call
	   (rdp-cond*
	    (if #f			; don't use markup for nroff
		l-contents
		`((#(STARTTAG)
		   ,(rdp-repll '() call-contents))
		  (#(DATA) ,pass-token-action)
		  (#(ENDTAG))
		  (#((PI OUTPUT)) ,pass-token-action))
		))))
	 (pattern
	  (rdp-map1
	   (lambda (pat)
	     (let ((x (stream->list pat))
		   (id (xat (head pat) 'ID)))
	       (set! cp (cons (if (eq? (arg-type id) 'IMPLIED)
				  (highpat)
				  (arg-val id))
			      (reverse! (cdr x))))
	       (set! patterns (cons cp patterns))
	       empty-stream))
	   (rdp-repll
	    `(,identity)		; keep the <pattern> tag
	    (rdp-cond*
	     `((#(STARTTAG (LEFT RIGHT CENTER BLOCK DECIMAL SEP))
		,(rdp-repll (list definition-tag #f) rdp-leave)
		,(rdp-cond
		  `((#(STARTTAG SEP)
		     ,(rdp-repll (list definition-tag #f) rdp-leave))
		    (#(ENDTAG PATTERN) ,rdp-leave)
		    (#() ,(rdp-insert '#(SEP 0 "NO" 0))))))
	       )))))
	 (row
	  (rdp-map1
	   (lambda (r)
	     (let* ((x (stream->list r))
		    (pat (xat (car x) 'PATTERN))
		    (pn (if (not (eq? (arg-type pat) 'IMPLIED))
			    (assoc (arg-val pat) patterns)
			    #f))
		    (new-cp (if pn pn cp))
		    (pl (+ 1 (quotient (pattern-length new-cp) 2))))
	       (set! cp new-cp)
	       (stream
		(vector cp
			`(,@(reverse! (map conv (cdr x)))
			  . ,(empty-cells (- pl (length (cdr x)))))))))
	   (rdp-repll
	    `(,identity)
	    (rdp-cond* `((#(STARTTAG C)
			  ,(rdpl (rdp-repll '() call-contents))))))))
	 (row-sep (lambda (token) (vector 'SEP (car (xatv token 'SEP)))))
	 (r/p (rdp-cond*
	       `((#(STARTTAG PATTERN) ,(rdp-call pattern))
		 (#(STARTTAG SEP) ,(rdp-repll `(,row-sep) rdp-leave))
		 (#(STARTTAG R) ,(rdp-call row)))))
	 (conv-cell (lambda (i)
		      (if (data-token? i)
			  (tr (data-token-data i))
			  i)))
	 (conv (lambda (cell) (apply string-append (map conv-cell cell))))
	 )
      `((#(STARTTAG TBL)
	 ,(rdp-map1
	   (lambda (tbl)
	    (force html-tbl-stuff)
	    (let ((x (stream->list tbl)))
	      (stream
	       `#(OUTPUT
		  (#"\n<PRE>\n"
		   ,(tbl-nroff-tbl
		     (vector
		      patterns
		      (reverse! (cdr x))))
		   #"\n</PRE>")))))
	   (rdp-repll `(,identity) r/p)))
	. ,l-contents))))

;}}}
;{{{ Footnotes

(define html-footnote-list
  `(,(lambda (t fnl language)
       (if (not (null? (cdr fnl)))
	   (let ((footnote/s ((phrase language)
		   (if (null? (cddr fnl)) 'footnote 'footnotes))))
	     `(#"\n<HR>\n" ,footnote/s #"<DL>\n"
	       .
	       ,(let loop
		    ((fns (reverse!
			   (map (lambda (i)
				  (cons (car i)
					(accumulate cons '() (cdr i))))
				(cdr fnl))))
		     (n 1))
		  `(#"\n<DT><A NAME=\"FN"
		    ,(caar fns) "\">"
		    ,n ")</A></DT><DD>" ,(cdar fns) "</DD>"
		    . ,(if (null? (cdr fns))
			   (begin
			     (set-cdr! fnl '()) ; side effect
			     '(#"\n</DL><HR>\n"))
			   (loop (cdr fns) (+ n 1)))))))
	     #f))
       FOOTNOTES LANGUAGE))

;}}}
;{{{ text

(define html-g-text
  `((#(ENDTAG))
    ,html-p-data
    (#((PI OUTPUT)) ,pass-token-action)
    (#(STARTTAG FOOTNOTE)
     ;{{{ snarf and put it into the env.

     ,(rdp-repll
       '()
       (rdp-let `((FOOTNOTEID ,identity ,tail))
		(rdp-env-map
		 (lambda (c h)
		   (let* ((get (h 'get))
			  (fnl (get 'FOOTNOTES))
			  (id (head (get 'FOOTNOTEID))))
		     (set-cdr! fnl `((,id . ,c) . ,(cdr fnl)))
		     (stream "<A HREF=\"#FN" id
			     "\">(" (length (cdr fnl)) ")</A>")))
		 (rdp-call html-p-body)))))

    ;}}}
    (#(STARTTAG SQ)
     ;{{{ depends on the current language

     ,(rdp-repll (lambda (token)
		   (if (member (xatv token 'LANG) english-quotation-style)
		       '("``" "''")
		       '(",," "''")))
		 (rdp-call html-p-text)))

    ;}}}
    (#(STARTTAG UREF)
     ,(rdp-env-map
       (lambda (c env)
	 (stream-append
	  (stream "<A HREF=\"" (xatv (head c) 'ID) "\">")
	  (if (stream-empty? (tail c))
	      (stream ((phrase ((env 'get) 'LANGUAGE)) 'here))
	      (tail c))
	  (stream "</A>")))
       (rdp-repll `(,identity) (rdp-call html-p-text))))
    ; a somewhat crasy way because we have a different appearence
    ; if we have something in the xref or not
    (#(STARTTAG XREF)
     ,(rdp-map
       (lambda (c)
	 (let ((repl (href-mark (head c))))
	   (if (stream-empty? c)
	       (stream-append		; spit it out as is
		(apply stream (car repl))
		(apply stream (cdr repl)))
	       (stream-append
		(apply stream (car repl))
		c			; have the contents dumped
		(stream " (")		; and splice the number in ()'s
		(apply stream  `(,(caadr repl) ")" . ,(cdadr repl)))))))
       (rdp-repll `(,identity) (rdp-call html-p-text))))
    .
    ,(def-repl
       (rdp-call html-p-text)
       `((
	  (INDEX)
	  ;	  (XREF      . ,href-mark)
	  (BREF      . ,(lambda (t)
			  `(#f
			    ("[" ,(ref-mark t) "]"))))
	  (MREF      ( " " ID " ") )
	  ;    (LABEL . #("<label>" "<label>"))
	  
	  (STRONG  "<B><U>" "</U></B>")
;	  (SQ      ,@(if (member lang english-quotation-style) ; change style
;		   '("``" "''")
;		   '(",," "''")))
	  (BF      "<B>" "</B>")
	  (IT      "<I>" "</I>")
	  (TT      "<TT>" "</TT>")
	  (EM      . ,(lambda(t) (if (odd? (xatv t 'LEVEL))
				     '("<I>" "</I>")
				     '("<B>" "</B>"))))
	  (CODE    "<TT>" "</TT>")
	  (VAR     "<I>" "</I>")
	  (META    )
	  (MATH    )
	  (SET     "<U>" "</U>")
	  (SUB     "_")
	  (SUP     "^")
	  (LANG    )
	  (NEWPAGE "<HR>")
	  (NL      "<BR>")
	  (LABEL)
	  ))
       `()
       )))

(define html-p-text
  (rdp-cond* `(,@html-g-text
	       (#() ,(lambda (c h s)
		       (message 0 #"\nHtmlText unhandled: " (head s))
		       (rdp-reduce c h (tail s)))))))

;}}}
;{{{ INDEX

(define (html-make-index idx where)
  (if
   (> (length idx) 0)
   `(#"\n<UL>"
     ,@(map
	(lambda (i)
	  `(#"\n<LI>"
	    ,(html-tr-string (car i))	; the indexed text
	    ,@(list-join
	       "," (reverse! (map (lambda (i)
				    (let ((pos (cdr (where 'lookup i))))
				      `(" <A HREF=\""
					,(car pos)
					,@(let ((no (cdr pos)))
					    (if
					     (equal? no "")
					     '()
					     `("#"
					       ,(typed-sect-no-from-id
						 (cdr pos)))))
					"\">(o)</A>")))
				  (vector-ref (cdr i) 0))))
	    . ,(html-make-index (vector-ref (cdr i) 1) where)))
	idx)
     #"\n</UL>")
   ""))

(define html-index
  `(#(PLACE INDEX)
    ,(rdp-map1
      (lambda (s)
	(if (null? (car (token-args (head s))))
	    empty-stream
	    (apply
	     stream #"\n<P>"
	     (apply html-make-index (token-args (head s))))))
      pass-token-action)))

;}}}
;{{{ html-bib

(define html-bib
  (letrec
      ((tr html-tr-string)
       (fmt1
	(lambda (data)
	  (let* ((no (vector-ref data 0))
		 (tag (vector-ref data 1))
		 (entry (vector-ref data 2)))
	    (if
	     (not (start-tag? entry))
	     `(#"\n<DT><B>" ,no #"</B></DT>\n<DD>" ,tag #"\n</DD>\n")
	     (let ((auth (tr (xatv entry 'AUTHOR)))
		   (title (tr (xatv entry 'TITLE)))
		   (publ (tr (xatv entry 'PUBL)))
		   (year (xatv entry 'YEAR)))
	       `(#"\n<DT>" ,no #"<DT>\n<DD>\n<I>" ,auth "</I>"
		 ,title "; " ,year " <I>" ,publ "</I>\n</DD>"))))))
       (fmt-seq (lambda (dl)
		  `(,@(fmt1 (head dl))
		    . ,(if (null? (cdr dl))
			   '()
			   (fmt-seq (cdr dl)))))))
    `(#(PLACE BIB)
      ,(rdp-map1
	(lambda (s)
	  (let ((token (head s)))
	    (if (not (null? (token-args token)))
		(stream
		 `#(OUTPUT (#"\n<DL>"
			    ,@(fmt-seq (token-args token)) #"\n</DL>")))
		empty-stream)))
	pass-token-action))))

;}}}
;{{{ body

(define html-p-body
  (rdp-repll `() (rdp-call (rdp-cond* html-g-body))))

(define html-g-body
  (old-html-tbl
   `(,html-p-data
     (#((PI OUTPUT)) ,pass-token-action)
     (#(STARTTAG BODY) ,html-p-body)
     (#(ENDTAG))
     ,@html-g-text
     . ,
     (def-repl
       (rdp-call (rdp-cond* html-g-body))
       `(((SPLIT #"\n<P>\n")
	  (PAR)
	  ((LISTITEM ENUMITEM)#"\n<LI>" #"\n</LI>")
	  (DT #"\n<DT>" "</DT>")
	  (DD #"\n<DD>" #"\n</DD>")
	  (TABLE)
	  ((SLIDE INLINE))
	  (QUOTE    #"\n<BLOCKQUOTE>\n" #"\n</BLOCKQUOTE>\n")
	  (LIST #"<UL>" #"</UL>\n")
	  (ENUM #"<OL>" #"</OL>\n")
	  (DESC #"<DL>" #"</DL>\n")
	  (NOTE #"<H5>\n" #"\n</H5>\n")
	  (VERB #"<PRE>" #"\n</PRE>")
	  (FIGURE   (#"\n<A NAME=\"" ,f-t-sect-no "\">") "</A>")
	  (CAPTION  #f "<P>")
	  
	  ))
       `(,html-bib
	 ,html-index
	 (#() ,(rdp-map1
		(lambda (s)
		 (message 0 #"\nHtmlBody Unhandled: " (head s))
		 empty-stream)
		pass-token-action)))))))

;}}}
;{{{ divs

;;; the following is the simplified definition
;;; might get overridden for some document types (book)

(define html-divs
  `(((SECT2 APPDX2 SECTN SECT1 APPDX1 SECT APPDX APPDXN)
     (#"\n<A NAME=\"" ,f-t-sect-no "\">"))
    (CHAPT (#"\n<HR>\n<A NAME=\"" ,f-t-sect-no "\">"))
    ((H4 AH4) ("<H5>" ,f-sect-no " ") #"</H5></A>\n")
    ((H3 AH3) ("<H4>" ,f-sect-no " ") #"</H4></A>\n")
    ((H2 AH2) ("<H3>" ,f-sect-no " ") #"</H3></A>\n")
    ((H1 AH1) ("<H2>" ,f-sect-no " ") #"</H2></A>\n")
    (H0       ("<H1>" ,f-sect-no " ") #"</H1></A>\n")
    ((SECTS SECT1S SECT2S SECTNS APPDX1S APPDX2S APPDXS APPDXNS))
    (ABSTRACT)
    (INTRO)
    (PREFACE)
    ))

(define html-p-sections (rdp-call (rdp-cond* html-g-sections)))

(define html-g-sections
  `(,@html-g-text
    (#(STARTTAG BODY) ,html-p-body)
    ; some effort for the footnote shift to the end of:
    (#(STARTTAG (SECT APPDX))
     ,(rdp-repll
       (lambda (t)
	 `((#"\n<A NAME=\"" ,f-t-sect-no "\">")
	   ,html-footnote-list))
       html-p-sections))
    (#(STARTTAG SECTS)
     ,(rdp-repll
       `(,html-footnote-list)
       html-p-sections))
    ; H0 is what chapters follows, though we need a file header
    (#(STARTTAG H0)
     ,(rdp-map (lambda (s)
		 (stream-append
		  (stream #"<html>\n<head>\n<title>")
		  (tail s)
		  (stream #"</title>\n</head>\n<body>\n<a name=\""
			  (f-t-sect-no (head s))
			  "\"><h1>" (f-sect-no (head s)) " ")
		  (tail s)
		  (stream #"</h1></a>\n")))
	       (rdp-repll `(,identity) (rdp-call html-p-text))))
    . ,(def-repl (rdp-call html-p-sections)
	 `(,html-divs)
	 `((#((INDEXDB BIBDB)) ,(rdp-skip 1))
	   (#() ,(rdp-map1
		  (lambda (s)
		    (message 0 #"\nHtmlSect unhandled: " (head s))
		    empty-stream)
		  pass-token-action))))))

(define html-p-chapters
  (rdp-cond*
   `((#(STARTTAG CHAPT)
      ,(rdp-repll `(,(lambda (t) `#(DIVERT ,(xatv t 'FILE)))
		    #(DIVERT POP))
		  html-p-sections))
     (#(STARTTAG CHAPTS) ,(rdp-repll '() (rdp-call html-p-chapters)))
     (#(STARTTAG APPDXS)
      ,(rdp-repll `(,(lambda (t)
		       `(#(DIVERT ,(xatv t 'FILE))
			 #"<html>\n<head>\n<title>Appendix</title>"))
		    #(DIVERT POP))
		  html-p-sections))
     . ,html-g-sections)))

;}}}
;{{{ contents

(define html-skip-element
  (rdp-repll
   `()
   (rdp-cond* `((#(STARTTAG) ,(rdp-call html-skip-element))
		(#(ENDTAG))
		(#() ,(rdp-skip 1))))))

(define html-contents-division
  (rdp-repll `(,(lambda (t)
		  `((#"\n<LI><A HREF=\""
		     ,(xatv t 'FILE) "#" ,(f-t-sect-no t))
		    )))
	     (rdp-call html-contents)))

(define html-contents
  (rdp-cond*
   `((#(STARTTAG BODY) ,html-skip-element)
     ,@html-g-text
     (#(STARTTAG (SECT1 SECT APPDX CHAPT)) ,html-contents-division)
     . ,(def-repl
	  (rdp-call html-contents)
	  `((
	     ((SECT1S SECTS CHAPTS APPDXS)
	      #"\n<UL>" #"\n</UL>")
	     ((SECT1 SECT APPDX CHAPT)
	      (#"\n<LI><A HREF=\"#" ,f-t-sect-no))
	     ((H4 H3 H2 H1 H0 AH1 AH2 AH3)
	      ("\">" ,f-sect-no " ") "</A>")
	     ))
	  `((#(STARTTAG) ,html-skip-element)
	    (#() ,(rdp-skip 1)))))))

;}}}
;{{{ global

(define html-general-header
"<!--
Warning: don't edit this file. It has been generated by typeset
The next compilation will silently overwrite all changes.
-->
")

(define html-xttl
  (rdp-map
   (lambda (title)
     (stream-append
      (stream #"<HTML>\n<HEAD>\n<TITLE>\n") 
      title
     (stream #"\n</TITLE>\n</HEAD>\n<BODY>\n<A NAME=\"\"><H1>")
     title
     (stream #"</H1></A>\n")))
   (rdp-repll '() html-p-text)))

(define (html-report wrap)
  (rdp-repll
   wrap
   (rdp-cond `((#(STARTTAG RTTL) ,html-xttl)))
   (rdp-cond `((#(STARTTAG (ABSTRACT PREFACE))
		,(rdp-repll `(#"\n<P>" #"\n<HR>\n") html-p-body))))
   (lambda (c h s)
     (stream-append
      (html-contents `(,(lambda (c h s) empty-stream)) h s)
      (rdp-reduce c h s)))
   html-p-sections))

(define (html-switch s)
  (letrec
      ((wrap
	(lambda (t)
	  `(,html-general-header
	    (#"\n<HR>\n<ADDRESS>" ,(html-tr-string (xatv t 'AUTHOR))
	     #"</ADDRESS>\n</BODY>\n</HTML>\n")))))
    (rdp-parse
     (rdp-cond
      `((#(STARTTAG DOCUMENT)
	 ,(rdp-repll
	   wrap
	   (rdp-cond `((#(STARTTAG DTTL) ,html-xttl)))
	   html-p-sections))
	(#(STARTTAG REPORT) ,(html-report wrap))
	(#(STARTTAG BOOK)
	 ,(rdp-repll
	   wrap
	   (rdp-cond `((#(STARTTAG BTTL) ,html-xttl)))
	   (rdp-cond `((#(STARTTAG PREFACE)
			,(rdp-repll `(#"\n<P>" #"\n<HR>\n")
				    html-p-body))))
	   (lambda (c h s)
	     (stream-append
	      (html-contents `(,(lambda (c h s) empty-stream)) h s)
	      (rdp-reduce c h s)))
	   html-p-chapters))
	))
     s
     `(LANGUAGE ,(let* ((lang (xat (head s) 'LANG)))
		   (if lang
		       (car (arg-val lang))
		       "EN")))
     '(FOOTNOTES (FNL))
     `(FOOTNOTEID ,(ints 1)))))

;}}}
;{{{ glue code

(define (html-prepare o d e)
  (doc-preprocess-hook 'run)
  ((stream-display-diverted)
   (stream-through
    (token-stream o)
    html-switch
    stream->list
    (file-split-info-c/as (tail (subfiles-of (string-append d "-c") ".html")))
    ;(watch #"\nxvz: ")
    all-doctypes)))

(define (html-compile-function o d e)
  (if (equal? d "-")
      (html-prepare o d e)
      (with-output-to-file
	  (string-append d e)
	(lambda ()
	  (html-prepare o d e)))))

(set! compile-function html-compile-function)

;}}}