You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/quad/quad/atomize.rkt

106 lines
4.3 KiB
Racket

#lang debug racket/base
5 years ago
(require racket/string
racket/hash
racket/match
racket/list
5 years ago
racket/struct
5 years ago
txexpr
5 years ago
sugar/list
5 years ago
racket/function
"quad.rkt"
"param.rkt")
8 years ago
(provide (all-defined-out))
5 years ago
(module+ test
(require rackunit))
8 years ago
5 years ago
(define (update-with! h . update-hashes)
(apply hash-union! #:combine (λ (v1 v2) v2) h update-hashes))
6 years ago
(define (update-with base-hash . update-hashes)
;; starting with base-hash, add or update keys found in update-hashes
6 years ago
(define h (make-hasheq))
5 years ago
(apply update-with! h base-hash update-hashes)
6 years ago
h)
6 years ago
(module+ test
(check-equal?
((hasheq 'foo "bar" 'zim "zam") . update-with . (hasheq 'zim "BANG") (hasheq 'toe "jam") (hasheq 'foo "zay"))
6 years ago
(make-hasheq '((zim . "BANG") (foo . "zay") (toe . "jam")))))
6 years ago
6 years ago
(define whitespace-pat #px"\\s+")
5 years ago
(define (merge-and-isolate-white str)
(for/list ([(m idx) (in-indexed (regexp-match* whitespace-pat str #:gap-select? #t))]
#:when (non-empty-string? m))
(if (even? idx) m " ")))
6 years ago
6 years ago
(define (merge-adjacent-strings xs [isolate-white? #false])
(let loop ([xs xs][acc null])
(match xs
5 years ago
[(== empty) (reverse acc)]
6 years ago
[(list (? string? strs) ..1 others ...)
(loop others (append (reverse ((if isolate-white?
merge-and-isolate-white
6 years ago
list) (apply string-append strs))) acc))]
6 years ago
[(cons x others) (loop others (cons x acc))])))
6 years ago
(define run-key 'run)
(define (same-run? qa qb)
5 years ago
(eq? (quad-ref qa run-key) (quad-ref qb run-key)))
6 years ago
(define (atomize qx #:attrs-proc [attrs-proc values])
5 years ago
;; atomize a quad by reducing it to the smallest indivisible formatting units.
6 years ago
;; which are multi-character quads with the same formatting.
5 years ago
(define atomized-qs
(let loop ([x (make-quad qx)]
5 years ago
[attrs (hash-copy (current-default-attrs))]
[key (eq-hash-code (current-default-attrs))])
(match-define-values (next-key next-attrs)
;; make a new run when we encounter non-empty attrs
(match (quad-attrs x)
[(? hash-empty?) (values key attrs)]
[this-attrs (define next-key (eq-hash-code this-attrs))
(define next-attrs (attrs . update-with . this-attrs))
(hash-set! next-attrs run-key next-key)
(attrs-proc next-attrs)
5 years ago
(values next-key next-attrs)]))
(match (quad-elems x)
[(? pair? elems)
5 years ago
;; we don't use `struct-copy` here because it needs to have the structure id at compile time.
;; whereas with this technique, we can extract a constructor for any structure type.
;; notice that the technique depends on
;; 1) we only need to update attrs and elems
;; 2) we make them the first two fields, so we know to drop the first two fields of x-tail
(define x-maker (let-values ([(x-structure-type _) (struct-info x)])
(struct-type-make-constructor x-structure-type)))
(define x-tail (drop (struct->list x) 2))
5 years ago
(append*
(for/list ([elem (in-list (merge-adjacent-strings elems 'isolate-white))])
5 years ago
(if (string? elem)
5 years ago
(if (zero? (string-length elem))
null
(list (apply x-maker next-attrs (list elem) x-tail)))
5 years ago
(loop elem next-attrs next-key))))]
5 years ago
[_ ((quad-attrs x) . update-with! . next-attrs) (list x)])))
5 years ago
#;(trimf atomized-qs (λ (q) (equal? (quad-elems q) '(" "))))
atomized-qs)
6 years ago
5 years ago
(module+ test
(define (filter-private-keys qs)
(for-each (λ (q) (when (hash-has-key? (quad-attrs q) 'run)
(hash-remove! (quad-attrs q) 'run))) qs)
qs)
(struct $br quad ())
5 years ago
(define br (q #:type $br (make-hasheq '((br . "time")))))
5 years ago
(check-equal? (filter-private-keys (atomize (q (q "a b") br (q "x y"))))
(list (q "a") (q " ") (q "b") br (q "x") (q " ") (q "y")))
(check-equal?
(filter-private-keys (atomize (q (hasheq 'foo 42) (q "Hi" " idiot" (q (hasheq 'bar 84) "There") "Eve" "ry" "one"))))
(list (q (hasheq 'foo 42) "Hi")
(q (hasheq 'foo 42) " ")
(q (hasheq 'foo 42) "idiot")
(q (hasheq 'foo 42 'bar 84) "There")
(q (hasheq 'foo 42) "Everyone"))))