svn: r8430

original commit: ec75cb5a9f57f27d138aa12a3d61db9711c12200
This commit is contained in:
Eli Barzilay 2008-01-27 07:00:07 +00:00
parent 81179a7eea
commit 40116e1d00
3 changed files with 394 additions and 443 deletions

View File

@ -1,5 +1,6 @@
(module base-render scheme/base
#lang scheme/base
(require "struct.ss"
mzlib/class
mzlib/serialize
@ -14,8 +15,7 @@
(init-field dest-dir)
(define/public (get-dest-directory)
dest-dir)
(define/public (get-dest-directory) dest-dir)
(define/public (get-substitutions) null)
@ -25,13 +25,9 @@
(if (or (null? number)
(andmap not number))
null
(cons
(let ([s (apply
(cons (let ([s (apply
string-append
(map (lambda (n)
(if n
(format "~s." n)
""))
(map (lambda (n) (if n (format "~s." n) ""))
(reverse number)))])
(substring s 0 (sub1 (string-length s))))
sep)))
@ -53,8 +49,8 @@
(define/public (deserialize-info v ci)
(let ([ht (deserialize v)]
[in-ht (collect-info-ext-ht ci)])
(hash-table-for-each ht (lambda (k v)
(hash-table-put! in-ht k v)))))
(for ([(k v) ht])
(hash-table-put! in-ht k v))))
(define/public (get-defined ci)
(hash-table-map (collect-info-ht ci) (lambda (k v) k)))
@ -63,9 +59,8 @@
(define/public (transfer-info ci src-ci)
(let ([in-ht (collect-info-ext-ht ci)])
(hash-table-for-each (collect-info-ext-ht src-ci)
(lambda (k v)
(hash-table-put! in-ht k v)))))
(for ([(k v) (collect-info-ext-ht src-ci)])
(hash-table-put! in-ht k v))))
;; ----------------------------------------
;; global-info collection
@ -82,12 +77,12 @@
ci))
(define/public (start-collect ds fns ci)
(map (lambda (d)
(collect-part d #f ci null))
(map (lambda (d) (collect-part d #f ci null))
ds))
(define/public (collect-part d parent ci number)
(let ([p-ci (make-collect-info (make-hash-table 'equal)
(let ([p-ci (make-collect-info
(make-hash-table 'equal)
(collect-info-ext-ht ci)
(collect-info-parts ci)
(collect-info-tags ci)
@ -108,27 +103,19 @@
(unless (null? parts)
(let ([s (car parts)])
(collect-part s d p-ci
(cons (if (unnumbered-part? s)
#f
pos)
(cons (if (unnumbered-part? s) #f pos)
number))
(loop (cdr parts)
(if (unnumbered-part? s) pos (add1 pos))))))
(hash-table-put! (collect-info-parts ci)
d
(make-collected-info
number
(make-collected-info number
parent
(collect-info-ht p-ci)))
(let ([prefix (part-tag-prefix d)])
(hash-table-for-each (collect-info-ht p-ci)
(lambda (k v)
(for ([(k v) (collect-info-ht p-ci)])
(when (cadr k)
(collect-put! ci
(if prefix
(convert-key prefix k)
k)
v)))))))
(collect-put! ci (if prefix (convert-key prefix k) k) v))))))
(define/private (convert-key prefix k)
(case (car k)
@ -138,31 +125,24 @@
k)]
[(index-entry)
(let ([v (convert-key prefix (cadr k))])
(if (eq? v (cadr k))
k
(list 'index-entry v)))]
(if (eq? v (cadr k)) k (list 'index-entry v)))]
[else k]))
(define/public (collect-part-tags d ci number)
(for-each (lambda (t)
(for ([t (part-tags d)])
(hash-table-put! (collect-info-ht ci)
(generate-tag t ci)
(list (or (part-title-content d) '("???"))
number)))
(part-tags d)))
(list (or (part-title-content d) '("???")) number))))
(define/public (collect-content c ci)
(for-each (lambda (i)
(collect-element i ci))
c))
(for ([i c]) (collect-element i ci)))
(define/public (collect-paragraph p ci)
(collect-content (paragraph-content p) ci))
(define/public (collect-flow p ci)
(for-each (lambda (p)
(collect-flow-element p ci))
(flow-paragraphs p)))
(for ([p (flow-paragraphs p)])
(collect-flow-element p ci)))
(define/public (collect-flow-element p ci)
(cond
@ -173,41 +153,32 @@
[else (collect-paragraph p ci)]))
(define/public (collect-table i ci)
(for-each (lambda (d) (when (flow? d)
(collect-flow d ci)))
(apply append (table-flowss i))))
(for ([d (apply append (table-flowss i))])
(when (flow? d) (collect-flow d ci))))
(define/public (collect-itemization i ci)
(for-each (lambda (d) (collect-flow d ci))
(itemization-flows i)))
(for ([d (itemization-flows i)])
(collect-flow d ci)))
(define/public (collect-blockquote i ci)
(for-each (lambda (d) (collect-flow-element d ci))
(blockquote-paragraphs i)))
(for ([d (blockquote-paragraphs i)])
(collect-flow-element d ci)))
(define/public (collect-element i ci)
(if (part-relative-element? i)
(let ([content
(or (hash-table-get (collect-info-relatives ci)
i
#f)
(or (hash-table-get (collect-info-relatives ci) i #f)
(let ([v ((part-relative-element-collect i) ci)])
(hash-table-put! (collect-info-relatives ci)
i
v)
(hash-table-put! (collect-info-relatives ci) i v)
v))])
(collect-content content ci))
(begin
(when (target-element? i)
(collect-target-element i ci))
(when (index-element? i)
(collect-index-element i ci))
(when (collect-element? i)
((collect-element-collect i) ci))
(when (target-element? i) (collect-target-element i ci))
(when (index-element? i) (collect-index-element i ci))
(when (collect-element? i) ((collect-element-collect i) ci))
(when (element? i)
(for-each (lambda (e)
(collect-element e ci))
(element-content i))))))
(for ([e (element-content i)])
(collect-element e ci))))))
(define/public (collect-target-element i ci)
(collect-put! ci
@ -233,30 +204,25 @@
ri))
(define/public (start-resolve ds fns ri)
(map (lambda (d)
(resolve-part d ri))
ds))
(map (lambda (d) (resolve-part d ri)) ds))
(define/public (resolve-part d ri)
(when (part-title-content d)
(resolve-content (part-title-content d) d ri))
(resolve-flow (part-flow d) d ri)
(for-each (lambda (p)
(resolve-part p ri))
(part-parts d)))
(for ([p (part-parts d)])
(resolve-part p ri)))
(define/public (resolve-content c d ri)
(for-each (lambda (i)
(resolve-element i d ri))
c))
(for ([i c])
(resolve-element i d ri)))
(define/public (resolve-paragraph p d ri)
(resolve-content (paragraph-content p) d ri))
(define/public (resolve-flow p d ri)
(for-each (lambda (p)
(resolve-flow-element p d ri))
(flow-paragraphs p)))
(for ([p (flow-paragraphs p)])
(resolve-flow-element p d ri)))
(define/public (resolve-flow-element p d ri)
(cond
@ -270,30 +236,25 @@
[else (resolve-paragraph p d ri)]))
(define/public (resolve-table i d ri)
(for-each (lambda (f) (when (flow? f)
(resolve-flow f d ri)))
(apply append (table-flowss i))))
(for ([f (apply append (table-flowss i))])
(when (flow? f) (resolve-flow f d ri))))
(define/public (resolve-itemization i d ri)
(for-each (lambda (f) (resolve-flow f d ri))
(itemization-flows i)))
(for ([f (itemization-flows i)])
(resolve-flow f d ri)))
(define/public (resolve-blockquote i d ri)
(for-each (lambda (f) (resolve-flow-element f d ri))
(blockquote-paragraphs i)))
(for ([f (blockquote-paragraphs i)])
(resolve-flow-element f d ri)))
(define/public (resolve-element i d ri)
(cond
[(part-relative-element? i)
(resolve-content (part-relative-element-content i ri) d ri)]
[(delayed-element? i)
(resolve-content (or (hash-table-get (resolve-info-delays ri)
i
#f)
(resolve-content (or (hash-table-get (resolve-info-delays ri) i #f)
(let ([v ((delayed-element-resolve i) this d ri)])
(hash-table-put! (resolve-info-delays ri)
i
v)
(hash-table-put! (resolve-info-delays ri) i v)
v))
d ri)]
[(element? i)
@ -305,21 +266,17 @@
(hash-table-put! (resolve-info-delays ri) e v))))]
[(link-element? i)
(resolve-get d ri (link-element-tag i))])
(for-each (lambda (e)
(resolve-element e d ri))
(element-content i))]))
(for ([e (element-content i)])
(resolve-element e d ri))]))
;; ----------------------------------------
;; render methods
(define/public (render ds fns ri)
(map (lambda (d fn)
(when report-output?
(printf " [Output to ~a]\n" fn))
(with-output-to-file fn
#:exists 'truncate/replace
(lambda ()
(render-one d ri fn))))
(when report-output? (printf " [Output to ~a]\n" fn))
(with-output-to-file fn #:exists 'truncate/replace
(lambda () (render-one d ri fn))))
ds
fns))
@ -335,10 +292,7 @@
(part-parts d))))
(define/public (render-content c part ri)
(apply append
(map (lambda (i)
(render-element i part ri))
c)))
(apply append (map (lambda (i) (render-element i part ri)) c)))
(define/public (render-paragraph p part ri)
(render-content (paragraph-content p) part ri))
@ -364,9 +318,7 @@
null)
(define/public (render-table i part ri)
(map (lambda (d) (if (flow? i)
(render-flow d part ri)
null))
(map (lambda (d) (if (flow? i) (render-flow d part ri) null))
(apply append (table-flowss i))))
(define/public (render-itemization i part ri)
@ -391,8 +343,7 @@
(render-content (delayed-element-content i ri) part ri)]
[(part-relative-element? i)
(render-content (part-relative-element-content i ri) part ri)]
[else
(render-other i part ri)]))
[else (render-other i part ri)]))
(define/public (render-other i part ri)
(list i))
@ -403,10 +354,8 @@
(let ([src-dir (path-only fn)]
[dest-dir (get-dest-directory)]
[fn (file-name-from-path fn)])
(let ([src-file (build-path (or src-dir (current-directory))
fn)]
[dest-file (build-path (or dest-dir (current-directory))
fn)])
(let ([src-file (build-path (or src-dir (current-directory)) fn)]
[dest-file (build-path (or dest-dir (current-directory)) fn)])
(unless (and (file-exists? dest-file)
(call-with-input-file*
src-file
@ -420,9 +369,7 @@
(let ([s (read-bytes 4096 src)]
[d (read-bytes 4096 dest)])
(and (equal? s d)
(if (eof-object? s)
#t
(loop)))))))))))
(or (eof-object? s) (loop)))))))))))
(when (file-exists? dest-file) (delete-file dest-file))
(make-directory* (path-only dest-file))
(copy-file src-file dest-file))
@ -449,13 +396,13 @@
(do-table-of-contents part ri 1 (lambda (x) #t)))
(define/private (generate-toc part ri base-len skip? quiet)
(let ([number (collected-info-number (part-collected-info part ri))])
(let ([subs
(let* ([number (collected-info-number (part-collected-info part ri))]
[subs
(if (quiet (and (part-style? part 'quiet)
(not (= base-len (sub1 (length number))))))
(apply
append
(map (lambda (p) (generate-toc p ri base-len #f quiet)) (part-parts part)))
(apply append (map (lambda (p)
(generate-toc p ri base-len #f quiet))
(part-parts part)))
null)])
(if skip?
subs
@ -464,25 +411,28 @@
(list
(make-paragraph
(list
(make-element 'hspace (list (make-string (* 2 (- (length number) base-len)) #\space)))
(make-link-element (if (= 1 (length number))
"toptoclink"
"toclink")
(make-element
'hspace
(list (make-string (* 2 (- (length number)
base-len))
#\space)))
(make-link-element
(if (= 1 (length number)) "toptoclink" "toclink")
(append
(format-number number
(list
(make-element 'hspace '(" "))))
(format-number
number
(list (make-element 'hspace '(" "))))
(or (part-title-content part) '("???")))
(car (part-tags part))))))))
subs)])
(if (and (= 1 (length number))
(or (not (car number))
((car number) . > . 1)))
(cons (list (make-flow (list (make-paragraph (list
(make-element 'hspace (list "")))))))
(or (not (car number)) ((car number) . > . 1)))
(cons (list (make-flow
(list (make-paragraph
(list (make-element 'hspace (list "")))))))
l)
l))))))
l)))))
;; ----------------------------------------
(super-new))))
(super-new)))

View File

@ -507,6 +507,7 @@
[(centered) '((align "center"))]
[(at-right) '((align "right"))]
[(at-left) '((align "left"))]
[(index) '((align "right"))]
[else null])
,@(let ([a (and (list? (table-style t))
(assoc 'style (table-style t)))])