File: struct.rkt

package info (click to toggle)
racket 7.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,432 kB
  • sloc: ansic: 258,980; pascal: 59,975; sh: 33,650; asm: 13,558; lisp: 7,124; makefile: 3,329; cpp: 2,889; exp: 499; python: 274; xml: 11
file content (429 lines) | stat: -rw-r--r-- 17,565 bytes parent folder | download | duplicates (6)
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
#lang scheme/base
(require (rename-in (except-in "core.rkt"
                               target-url struct:target-url target-url? target-url-addr)
                    [make-target-url core:make-target-url])
         "private/provide-structs.rkt"
         "html-properties.rkt"
         scheme/provide-syntax
         scheme/struct-info
         racket/contract/base
         (for-syntax scheme/base))

(define-provide-syntax (compat**-out stx)
  (syntax-case stx ()
    [(_ struct-out o) 
     (let ([id (syntax-case #'o ()
                 [(id (field-id ...)) #'id]
                 [id #'id])])
       (with-syntax ([make-id (datum->syntax id
                                             (string->symbol (format "make-~a" (syntax-e id)))
                                             id)]
                     [make-id/compat (datum->syntax id
                                                    (string->symbol (format "make-~a/compat" (syntax-e id)))
                                                    id)])
         #'(combine-out
            (except-out (struct-out o) make-id)
            (rename-out [make-id/compat make-id]))))]
    [(_ struct-out o ...) #'(combine-out (compat**-out struct-out o) ...)]))

(define-provide-syntax (compat-out stx)
  (syntax-case stx ()
    [(_ . outs) #'(compat**-out struct-out . outs)]))

(define-provide-syntax (compat*-out stx)
  (syntax-case stx ()
    [(_ . outs) #'(compat**-out struct*-out . outs)]))

(define-provide-syntax (struct*-out stx)
  (syntax-case stx ()
    [(_ [id (field-id ...)]) 
     (with-syntax ([id? (datum->syntax #'id
                                       (string->symbol (format "~a?" (syntax-e #'id)))
                                       #'id)]
                   [struct:id (datum->syntax #'id
                                             (string->symbol (format "struct:~a" (syntax-e #'id)))
                                             #'id)]
                   [make-id (datum->syntax #'id
                                           (string->symbol (format "make-~a" (syntax-e #'id)))
                                           #'id)]
                   [(sel-id ...)
                    (map (lambda (field-id)
                           (datum->syntax field-id
                                          (string->symbol (format "~a-~a" (syntax-e #'id) (syntax-e field-id)))
                                          field-id))
                         (syntax->list #'(field-id ...)))])
              #'(combine-out
                 id struct:id make-id id? sel-id ...))]
    [(_ [id (field-id ...)]...)
     #'(combine-out (struct*-out [id (field-id ...)]) ...)]))

(provide (struct-out collect-info)
         (struct-out resolve-info)
         tag? block?
         
         make-flow flow? flow-paragraphs

         (except-out (compat-out part) part-title-content)
         (rename-out [part-blocks part-flow]
                     [part-title-content/compat part-title-content])
         make-versioned-part versioned-part?
         make-unnumbered-part unnumbered-part?

         (except-out (compat-out paragraph) paragraph-content)
         (rename-out [paragraph-content/compat paragraph-content])
         make-styled-paragraph
         (rename-out [paragraph? styled-paragraph?]
                     [paragraph-style styled-paragraph-style])
         make-omitable-paragraph omitable-paragraph?

         (compat-out table) 
         table-flowss
         make-auxiliary-table auxiliary-table?

         (struct-out delayed-block)

         (compat-out itemization)
         (rename-out [itemization-blockss itemization-flows]
                     [itemization? styled-itemization?]
                     [itemization-style styled-itemization-style])
         make-styled-itemization

         make-blockquote

         (compat-out compound-paragraph)

         (except-out (compat-out element) element? element-style element-content)
         (rename-out [element?/compat element?]
                     [element-style/compat element-style]
                     [element-content/compat element-content])
         (except-out (compat*-out [toc-element (toc-content)])
                     toc-element-toc-content)
         (rename-out [toc-element-toc-content/compat toc-element-toc-content])
         (compat*-out [target-element (tag)]
                      [toc-target-element ()]
                      [toc-target2-element (toc-content)])
         (compat*-out [page-target-element ()]
                      [redirect-target-element (alt-path alt-anchor)]
                      [link-element (tag)]
                      [index-element (tag plain-seq entry-seq desc)])
         make-aux-element aux-element?
         make-hover-element hover-element? hover-element-text
         make-script-element script-element? script-element-type script-element-script

         (struct-out collected-info)

         (struct-out delayed-element)
         ; delayed-element-content delayed-block-blocks current-serialize-resolve-info
         
         (struct-out part-relative-element)
         ; part-relative-element-content collect-info-parents

         (struct-out delayed-index-desc)

         (struct*-out [collect-element (collect)])

         (struct*-out [render-element (render)])

         (struct-out generated-tag)
         ; generate-tag tag-key current-tag-prefixes add-current-tag-prefix

         content->string
         (rename-out [content->string element->string]
                     [content-width element-width])
         ; strip-aux

         block-width

         info-key? part-collected-info collect-put!
         resolve-get resolve-get/tentative resolve-get/ext? resolve-search resolve-get-keys)

(provide-structs
 [with-attributes ([style any/c]
                   [assoc (listof (cons/c symbol? string?))])]
 [image-file ([path (or/c path-string?
                          (cons/c (one-of/c 'collects)
                                  (listof bytes?)))]
              [scale real?])]
 [target-url ([addr path-string?] [style any/c])])

(define (make-flow l) l)
(define (flow? l) (and (list? l) (andmap block? l)))
(define (flow-paragraphs l) l)

(define (list->content l)
  (if (and (pair? l) (null? (cdr l)))
      (car l)
      l))

(define (content->list v)
  (if (list? v)
      v
      (list v)))

(define (make-part/compat tag-prefix tags title-content orig-style to-collect flow parts)
  (make-part tag-prefix 
             tags
             (list->content title-content)
             (convert-style orig-style)
             to-collect 
             (flow-paragraphs flow)
             parts))

(define (part-title-content/compat p)
  (list (part-title-content p)))

(define (make-versioned-part tag-prefix tags title-content orig-style to-collect flow parts version)
  (make-part tag-prefix 
             tags
             (list->content title-content)
             (let ([s (convert-style orig-style)])
               (make-style (style-name s)
                           (cons
                            (make-document-version version)
                            (style-properties s))))
             to-collect 
             (flow-paragraphs flow)
             parts))
(define (versioned-part? p)
  (and (part? p) (ormap document-version? (style-properties (part-style p)))))

(define (make-unnumbered-part tag-prefix tags title-content orig-style to-collect flow parts)
  (make-part tag-prefix 
             tags
             (list->content title-content)
             (let ([s (convert-style orig-style)])
               (make-style (style-name s)
                           (cons 'unnumbered (style-properties s))))
             to-collect 
             (flow-paragraphs flow)
             parts))
(define (unnumbered-part? p)
  (and (part? p) (memq 'unnumbered (style-properties (part-style p)))))

(define (make-paragraph/compat content)
  (make-paragraph plain (list->content content)))
(define (paragraph-content/compat p)
  (content->list (paragraph-content p)))
(define (make-styled-paragraph content style)
  (make-paragraph (convert-style style) (list->content content)))

(define (make-omitable-paragraph content)
  (make-paragraph (make-style #f '(omitable)) (list->content content)))
(define (omitable-paragraph? p)
  (and (paragraph? p) (memq 'omitable (style-properties (paragraph-style p)))))

(define (make-table/compat style cellss)
  (make-table (convert-style style)
              (map (lambda (cells)
                     (map (lambda (cell)
                            (cond
                             [(eq? cell 'cont) 'cont]
                             [(= 1 (length cell)) (car cell)]
                             [else (make-nested-flow plain cell)]))
                          cells))
                   cellss)))
(define (table-flowss t)
  (map (lambda (row) (map (lambda (c) (make-flow (list c))) row))
       (table-blockss t)))

(define (make-auxiliary-table style cells)
  (let ([t (make-table/compat style cells)])
    (make-table (make-style (style-name (table-style t))
                            (cons 'aux
                                  (style-properties (table-style t))))
                (table-blockss t))))

(define (auxiliary-table? t)
  (ormap (lambda (v) (eq? v 'aux) (style-properties (table-style t)))))

(define (make-itemization/compat flows)
  (make-itemization plain flows))
(define (make-styled-itemization style flows)
  (make-itemization (convert-style style) flows))

(define (make-blockquote style blocks)
  (make-nested-flow (convert-style (or style 'inset)) blocks))

(define (make-compound-paragraph/compat style blocks)
  (make-compound-paragraph (convert-style style) blocks))

(define (element-style-name s)
  (if (style? s)
      (style-name s)
      s))
(define (element-style-properties s)
  (if (style? s)
      (style-properties s)
      null))

(define (add-element-property v e)
  (make-element (make-style (element-style-name (element-style e))
                            (cons v
                                  (element-style-properties (element-style e))))
                (element-content e)))
(define (check-element-style e pred)
  (ormap pred (style-properties (element-style e))))

(define (handle-image-style ctr style . args)
  (if (image-file? style)
      (make-image-element #f (list (apply ctr #f args)) 
                          (image-file-path style)
                          null
                          (image-file-scale style))
      (apply ctr (convert-element-style style) args)))

(define (convert-element-style style)
  (cond
   [(not style) style]
   [(string? style) style]
   [(symbol? style) style]
   [else (convert-style style)]))

(define (element?/compat e)
  (or (element? e) (and (list? e) (content? e))))
(define (element-content/compat e)
  (cond
   [(element? e) (content->list (element-content e))]
   [else e]))
(define (element-style/compat e)
  (cond
   [(element? e) (element-style e)]
   [else #f]))

(define (make-element/compat style content)
  (handle-image-style make-element style (list->content content)))
(define (make-toc-element/compat style content toc-content)
  (handle-image-style make-toc-element style (list->content content) (list->content toc-content)))
(define (toc-element-toc-content/compat e)
  (content->list (toc-element-toc-content e)))
(define (make-target-element/compat style content tag)
  (handle-image-style make-target-element style (list->content content) tag))
(define (make-toc-target-element/compat style content tag)
  (handle-image-style make-toc-target-element style (list->content content) tag))
(define (make-toc-target2-element/compat style content tag toc-content)
  (handle-image-style make-toc-target2-element style (list->content content) tag toc-content))
(define (make-page-target-element/compat style content tag)
  (handle-image-style make-page-target-element style (list->content content) tag))
(define (make-redirect-target-element/compat style content tag alt-path alt-anchor)
  (handle-image-style make-redirect-target-element style (list->content content) tag alt-path alt-anchor))
(define (make-link-element/compat style content tag)
  (handle-image-style make-link-element style (list->content content) tag))
(define (make-index-element/compat style content tag plain-seq etry-seq desc)
  (handle-image-style make-index-element style (list->content content) tag plain-seq etry-seq desc))

(define (make-aux-element style content)
  (add-element-property 'aux (make-element/compat style content)))
(define (aux-element? e)
  (check-element-style e (lambda (v) (eq? v 'aux))))

(define (make-hover-element style content text)
  (add-element-property (make-hover-property text)
                        (make-element/compat style content)))
(define (hover-element? e)
  (check-element-style e hover-property?))
(define (hover-element-text e)
  (ormap (lambda (v)
           (and (hover-property? v) (hover-property-text e)))
         (style-properties (element-style e))))

(define (make-script-element style content type script)
  (add-element-property (make-script-property type script)
                        (make-element/compat style content)))
(define (script-element? e)
  (check-element-style e script-property?))
(define (script-element-type e)
  (ormap (lambda (v)
           (and (script-property? v) (script-property-type e)))
         (style-properties (element-style e))))
(define (script-element-script e)
  (ormap (lambda (v)
           (and (script-property? v) (script-property-script e)))
         (style-properties (element-style e))))

;; ----------------------------------------

(define (convert-style s)
  (cond
   [(not s) plain]
   [(style? s) s]
   [(string? s) (make-style s null)]
   [(symbol? s) (make-style s null)]
   [(and (list? s) (andmap symbol? s)) (make-style #f s)]
   [(with-attributes? s) (let* ([wa (flatten-style s)]
                                [s (convert-style (with-attributes-style wa))])
                           (make-style (style-name s)
                                       (cons
                                        (make-attributes (with-attributes-assoc wa))
                                        (style-properties s))))]
   [(target-url? s) (let ([s (convert-style (target-url-style s))])
                      (make-style (style-name s)
                                       (cons
                                        (core:make-target-url (target-url-addr s))
                                        (style-properties s))))]
   [(image-file? s) (make-style #f null)]
   [(and (list? s) (pair? s) (eq? (car s) 'color))
    (make-style #f (list (make-color-property
                          (if (string? (cadr s)) (cadr s) (cdr s)))))]
   [(and (list? s) (pair? s) (eq? (car s) 'bg-color))
    (make-style #f (list (make-background-color-property
                          (if (string? (cadr s)) (cadr s) (cdr s)))))]
   [(and (pair? s)
         (list? s)
         (andmap (lambda (v) (and (pair? v) 
                                  (memq (car v) '(alignment valignment row-styles style))))
                 s))
    (let ([gen-columns (lambda (sn a va)
                         (map (lambda (sn a va)
                                (make-style sn
                                            (append (if a (list a) null)
                                                    (if va (list va) null))))
                              (cdr (or sn (map (lambda (x) #f) (or va a))))
                              (cdr (or a (map (lambda (x) #f) (or va sn))))
                              (cdr (or va (map (lambda (x) #f) (or a sn))))))])
      (make-style (let ([s (assq 'style s)])
                    (and s (cadr s)))
                  (let ([a (assq 'alignment s)]
                        [va (assq 'valignment s)])
                    (if (or a va)
                        (list (make-table-columns (gen-columns #f a va)))
                        (let ([l (cdr (assq 'row-styles s))])
                          (list
                           (make-table-cells
                            (map (lambda (row)
                                   (let ([sn (assq 'style row)]
                                         [a (assq 'alignment row)]
                                         [va (assq 'valignment row)])
                                     (if (or sn a va)
                                         (gen-columns sn a va)
                                         (error 'convert-style "no row style found"))))
                                 l))))))))]
   [else (error 'convert-style "unrecognized style: ~e" s)]))

(define (flatten-style s)
  (cond
   [(with-attributes? s)
    (let ([rest (flatten-style (with-attributes-style s))])
      (if (with-attributes? rest)
          ;; collapse nested with-attributes
          (make-with-attributes 
           (with-attributes-style rest)
           (append (with-attributes-assoc s)
                   (with-attributes-assoc rest)))
          ;; rebuild with flattened inner:
          (make-with-attributes 
           rest
           (with-attributes-assoc s))))]
   [(target-url? s)
    (let ([rest (flatten-style (target-url-style s))])
      (if (with-attributes? rest)
          ;; lift nested attributes out:
          (make-with-attributes 
           (make-target-url
            (target-url-addr s)
            (with-attributes-style rest))
           (with-attributes-assoc rest))
          ;; rebuild with flattened inner:
          (make-target-url
           (target-url-addr s)
           rest)))]
   [else s]))