-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcounter.rkt
81 lines (75 loc) · 2.52 KB
/
counter.rkt
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
#lang scheme
(require scribble/struct
scribble/decode)
(provide new-counter
counter-target
counter-ref
silent-counter-ref)
(define-struct counter ([n #:mutable] name))
(define (new-counter name)
(make-counter 0 name))
(define (counter-target counter tag label . content)
(let ([content (decode-content content)])
(make-target-element
#f
(list
(make-collect-element
#f
(if label
(list
(make-delayed-element
(lambda (renderer part ri)
(let ([n (resolve-get part ri `(counter (,(counter-name counter) ,tag "value")))])
(list* label 'nbsp (format "~a" n)
content)))
(lambda () (if label
(list* label 'nbsp "N" content)
content))
(lambda () (if label
(list* label 'nbsp "N" content)
content))))
content)
(lambda (ci)
(let ([n (add1 (counter-n counter))])
(set-counter-n! counter n)
(collect-put! ci `(counter (,(counter-name counter) ,tag "value")) n)))))
`(counter (,(counter-name counter) ,tag)))))
(define (counter-ref counter tag label)
(let ([n (make-delayed-element
(lambda (renderer part ri)
(let ([n (resolve-get part ri `(counter (,(counter-name counter) ,tag "value")))])
(list (format "~a" n))))
(lambda () (if label
(list label 'nbsp "N")
(list "N")))
(lambda () (if label
(list label 'nbsp "N")
(list "N"))))])
(if label
(make-link-element
#f
(list
label
'nbsp
n)
`(counter (,(counter-name counter) ,tag)))
n)))
(define (silent-counter-ref counter tag label)
(let ([n (make-delayed-element
(lambda (renderer part ri)
(let ([n (resolve-get part ri `(counter ("" ,tag "value")))])
(list (format "~a" n))))
(lambda () (if label
(list label 'nbsp "N")
(list "N")))
(lambda () (if label
(list label 'nbsp "N")
(list "N"))))])
(if label
(make-link-element
#f
(list
label
)
`(counter (,(counter-name counter) ,tag)))
n)))