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/qtest/markdown.rkt

405 lines
19 KiB
Racket

6 years ago
#lang debug racket/base
6 years ago
(require (for-syntax racket/base) txexpr racket/runtime-path racket/string racket/promise racket/match racket/list
pitfall quad sugar/debug pollen/tag)
(provide (except-out (all-from-out racket/base) #%module-begin)
6 years ago
(rename-out [mb #%module-begin])
6 years ago
p id strong em attr-list h1 h2 h3 h4 h5 h6
ol li ul rsquo lsquo rquo lquo hellip
hr
code pre a blockquote)
(define rsquo "")
(define rquo "")
(define lsquo "")
(define lquo "")
(define hellip "")
6 years ago
6 years ago
(define-tag-function (p attrs exprs)
6 years ago
(qexpr attrs exprs))
6 years ago
6 years ago
(define-tag-function (hr attrs exprs)
(qexpr (cons '(hrule "yes") attrs) '(" ")))
(define-tag-function (blockquote attrs exprs)
6 years ago
(qexpr (list* '(container "bq") '(left-inset "5") attrs) exprs))
6 years ago
(define id (default-tag-function 'id))
6 years ago
(define class (default-tag-function 'class))
6 years ago
6 years ago
(define-tag-function (strong attrs exprs)
6 years ago
(qexpr (cons '(font "charter-bold") attrs) exprs))
6 years ago
6 years ago
(define-tag-function (a attrs exprs)
6 years ago
(qexpr `((link ,(cadr (assoc 'href attrs)))(color "MediumVioletRed")) exprs))
6 years ago
6 years ago
(define-tag-function (em attrs exprs)
6 years ago
(qexpr (cons '(font "charter-italic") attrs) exprs))
6 years ago
6 years ago
(define-syntax-rule (attr-list . attrs) 'attrs)
6 years ago
6 years ago
(define (heading-base font-size attrs exprs)
(qexpr (append '((font "fira")(fontsize (number->string font-size))(line-height (number->string (+ 12 font-size)))) attrs) exprs))
(define-tag-function (h1 attrs exprs) (heading-base 30 attrs exprs))
(define-tag-function (h2 attrs exprs) (heading-base 24 attrs exprs))
(define-tag-function (h3 attrs exprs) (heading-base 18 attrs exprs))
6 years ago
6 years ago
(define h4 h3)
(define h5 h3)
(define h6 h3)
6 years ago
(define-tag-function (code attrs exprs)
6 years ago
(qexpr (append '((font "fira-mono")(fontsize "11")(bg "aliceblue")) attrs) exprs))
6 years ago
(define-tag-function (pre attrs exprs)
6 years ago
;; pre needs to convert white space to equivalent layout elements
(define new-exprs (add-between
(for*/list ([expr (in-list exprs)]
[str (in-list (string-split (car (get-elements expr)) "\n"))])
6 years ago
`(,(get-tag expr) ,(get-attrs expr) ,str))
6 years ago
lbr))
6 years ago
(qexpr (list* '(container "codeblock") '(left-inset "12") '(right-inset "12") attrs) new-exprs))
(define (list-base attrs exprs [bullet-val #f])
(qexpr (list* '(left-inset "20") attrs)
(add-between
(for/list ([(expr idx) (in-indexed exprs)])
(list* (get-tag expr) (cons (list 'list-index (or bullet-val (format "~a." (add1 idx)))) (get-attrs expr)) (get-elements expr)))
pbr)))
(define-tag-function (ol attrs exprs) (list-base attrs exprs))
(define-tag-function (ul attrs exprs) (list-base attrs exprs ""))
(define-tag-function (li attrs exprs) (qexpr attrs exprs))
6 years ago
6 years ago
(define q:string (q #:in 'bi
#:out 'bo ;; align to baseline
6 years ago
;; printable unless single space, which is not printable at start or end
#:printable (λ (q [sig #f])
(case (car (quad-elems q))
[(" " #\space) (not (memq sig '(start end)))]
[else #true]))
;; draw with pdf text routine
#:draw (λ (q doc)
6 years ago
(font doc (path->string (hash-ref (quad-attrs q) 'font)))
(font-size doc (string->number (hash-ref (quad-attrs q) 'fontsize "12")))
6 years ago
(fill-color doc (hash-ref (quad-attrs q) 'color "black"))
6 years ago
(match-define (list str) (quad-elems q))
(match-define (list x y) (quad-origin q))
6 years ago
(text doc str x y #:bg (hash-ref (quad-attrs q) 'bg #f)
#:link (hash-ref (quad-attrs q) 'link #f)))
6 years ago
#:draw-end (λ (q doc) (draw-debug q doc "#99f" "#ccf"))))
6 years ago
(define-runtime-path charter "fonts/charter.ttf")
6 years ago
(define-runtime-path charter-bold "fonts/charter-bold.ttf")
(define-runtime-path charter-italic "fonts/charter-italic.ttf")
6 years ago
(define-runtime-path fira "fonts/fira-light.ttf")
6 years ago
(define-runtime-path fira-mono "fonts/fira-mono.ttf")
6 years ago
(define (->string-quad doc q)
6 years ago
(cond
[(line-break? q) q]
[else
(struct-copy
quad q:string
[attrs (let ([attrs (quad-attrs q)])
;; attrs hashes are shared between many quads.
;; so the first update will change every reference to the shared hash
;; hence why we ignore if val is already a path
;; but this op should ideally happen earlier
(hash-update! attrs 'font
(λ (val) (if (path? val)
val
(match (string-downcase (string-replace val " " "-"))
["charter" charter]
["charter-bold" charter-bold]
["charter-italic" charter-italic]
["fira" fira]
["fira-mono" fira-mono]))))
attrs)]
[elems (quad-elems q)]
[size (delay
(define fontsize (string->number (hash-ref (quad-attrs q) 'fontsize)))
(font-size doc fontsize)
(font doc (path->string (hash-ref (quad-attrs q) 'font)))
(define str (car (quad-elems q)))
(pt (string-width doc str) (current-line-height doc)))])]))
6 years ago
6 years ago
(define draw-debug? #t)
(define (draw-debug q doc [fill-color "#f99"] [stroke-color "#fcc"])
6 years ago
(when draw-debug?
(save doc)
(line-width doc 0.5)
(apply rect doc (append (quad-origin q) (size q)))
(stroke doc stroke-color)
(apply rect doc (append (quad-origin q) (size q)))
(clip doc)
(circle doc (pt-x (in-point q)) (pt-y (in-point q)) 3)
(circle doc (pt-x (out-point q)) (pt-y (out-point q)) 3)
(fill doc fill-color)
(restore doc)))
6 years ago
(define line-height 20)
6 years ago
(define q:line (q #:size (pt 380 line-height)
#:in 'nw
#:inner 'sw ; puts baseline at lower right corner of line box
6 years ago
#:out 'sw
6 years ago
#:printable #true
#:draw-start draw-debug))
6 years ago
(struct line-spacer quad () #:transparent)
(define q:line-spacer (q #:type line-spacer
6 years ago
#:size (pt 380 (* line-height 0.6))
6 years ago
#:out 'sw
6 years ago
#:printable (λ (q sig) (not (memq sig '(start end))))
#:draw-start draw-debug))
6 years ago
(define softies (map string '(#\space #\- #\u00AD)))
(define (soft-break-for-line? q)
(member (car (quad-elems q)) softies))
(define (consolidate-runs pcs)
(for/fold ([runs empty]
[pcs pcs]
#:result (reverse runs))
([i (in-naturals)]
#:break (empty? pcs))
(define-values (run-pcs rest) (splitf-at pcs (λ (p) (same-run? (car pcs) p))))
(define new-run (struct-copy quad q:string
[attrs (quad-attrs (car pcs))]
[elems (merge-adjacent-strings (apply append (for/list ([pc (in-list run-pcs)])
6 years ago
(quad-elems pc))))]
6 years ago
[size (delay (pt (for/sum ([pc (in-list run-pcs)])
6 years ago
(pt-x (size pc)))
6 years ago
(pt-y (size (car pcs)))))]))
(values (cons new-run runs) rest)))
6 years ago
(define-quad line-break quad ())
(define lbr (make-line-break #:printable #f))
6 years ago
;; treat paragraph break as special kind of line break
6 years ago
(define-quad para-break line-break ())
(define pbr (make-para-break #:printable #f))
6 years ago
(define-quad hr-break para-break ())
(define hrbr (make-hr-break #:printable #t))
6 years ago
(module+ test
(check-true (line-break? (second (quad-elems (q "foo" pbr "bar")))))
(check-true (line-break? (second (atomize (q "foo" pbr "bar"))))))
6 years ago
6 years ago
(define (line-wrap xs wrap-size)
(wrap xs (λ (q idx) (- wrap-size (string->number (quad-ref q 'left-inset "0"))
(string->number (quad-ref q 'right-inset "0"))))
6 years ago
#:hard-break line-break?
#:soft-break soft-break-for-line?
6 years ago
#:finish-wrap
(λ (pcs q idx)
(define new-elems (consolidate-runs pcs))
(append
(list (struct-copy quad q:line
[attrs (let ([attrs (hash-copy (quad-attrs q:line))])
(define container-val (quad-ref (car new-elems) 'container))
(when (and container-val
(for/and ([elem (in-list (cdr new-elems))])
(equal? (quad-ref elem 'container)
container-val)))
(hash-set! attrs 'container container-val))
attrs)]
[size (let ()
(define line-heights
(filter-map
(λ (q) (string->number (quad-ref q 'line-height "NaN")))
pcs))
(match-define (list w h) (quad-size q:line))
;; when `line-heights` is empty, this is just h
(pt w (apply max (cons h line-heights))))]
[elems new-elems]
[offset (pt
(string->number (quad-ref (car new-elems) 'left-inset "0"))
(second (quad-offset q:line)))]
[draw-start (if (quad-ref (car new-elems) 'hrule)
(λ (dq doc)
(save doc)
(match-define (list left top) (quad-origin dq))
(match-define (list right bottom) (size dq))
(translate doc left (+ top (/ bottom 2)))
(move-to doc 0 0)
(line-to doc wrap-size 0)
(line-width doc 3)
(stroke doc "#999")
(restore doc))
void)]
[draw-end (match (and (or (para-break? q) (not q))
(quad-ref (car new-elems) 'list-index))
[#false void]
[val (λ (q doc)
(save doc)
(translate doc (- (string->number (quad-ref (car new-elems) 'left-inset "0"))) 0)
(text doc val)
(restore doc))])]))
(if (para-break? q)
(list q:line-spacer)
null)))))
6 years ago
(define top-margin 60)
(define bottom-margin 120)
6 years ago
(define side-margin 120)
(define page-offset (pt side-margin top-margin))
(require racket/date)
6 years ago
(define q:page (q #:offset page-offset
#:draw-start (λ (q doc) (add-page doc))
#:draw-end (λ (q doc)
(font-size doc 10)
6 years ago
(font doc charter)
(fill-color doc "black")
(text doc (format "~a · ~a at ~a" (hash-ref (quad-attrs q) 'page-number)
(hash-ref (quad-attrs q) 'doc-title)
(date->string (current-date) #t))
side-margin
(+ (- (pdf-height doc) bottom-margin) 20)))))
6 years ago
(define q:doc (q #:draw-start (λ (q doc) (start-doc doc))
#:draw-end (λ (q doc) (end-doc doc))))
6 years ago
(define (make-blockquote pcs)
(q #:attrs (hasheq 'type "bq")
#:in 'nw
#:out 'sw
#:elems pcs
#:size (delay (pt (pt-x (size (car pcs)))
(for/sum ([pc (in-list pcs)])
6 years ago
(pt-y (size pc)))))
#:draw-start (λ (q doc)
(save doc)
(match-define (list left top) (quad-origin q))
(match-define (list right bottom) (size q))
(rect doc (- left 4) (+ top 6) right (+ bottom 2))
(line-width doc 1)
(fill-and-stroke doc "#eee" "#999")
(restore doc))))
6 years ago
(define (make-codeblock pcs)
(q #:attrs (hasheq 'type "codeblock")
#:in 'nw
#:out 'sw
#:elems pcs
#:size (delay (pt (pt-x (size (car pcs)))
(for/sum ([pc (in-list pcs)])
(pt-y (size pc)))))
#:draw-start (λ (q doc)
(save doc)
(match-define (list left top) (quad-origin q))
(match-define (list right bottom) (size q))
(translate doc (- left 4) (+ top 6)) ; reset origin to top left of quad
(rect doc 0 0 right (+ bottom 2))
(fill doc "aliceblue")
(define vert-line-width 6)
(move-to doc (/ vert-line-width 2) 0) ; affirmatively move cursor
(line-to doc (/ vert-line-width 2) bottom) ; strictly vert line
(line-width doc vert-line-width)
(stroke doc "#669")
#;(move-to doc 0 0) ; affirmatively move cursor
#;(line-to doc 0 bottom) ; strictly horiz line
#;(stroke doc "gray")
#;(translate doc 0 (+ bottom 2)) ; reset origin
#;(move-to doc 0 0) ; move again
#;(line-to doc right 0) ; same line, translated
(stroke doc "gray")
(restore doc))))
(define (contiguous-group-by pred xs)
;; like `group-by`, but only groups together contiguous xs with the same pred value.
(let loop ([xs xs][groups null])
(match xs
[(== empty) (reverse groups)]
[(cons first-x other-xs)
(define equivalence-val (pred first-x))
(define-values (group-members rest) (splitf-at other-xs (λ (x) (equal? (pred x) equivalence-val))))
(define new-group (cons first-x group-members)) ; group-members might be empty
(loop rest (cons new-group groups))])))
(module+ test
(require rackunit)
(check-equal?
(contiguous-group-by values '(1 1 2 2 2 3 4 5 5 6 6 7 8 9))
'((1 1) (2 2 2) (3) (4) (5 5) (6 6) (7) (8) (9))))
(define (page-wrap xs vertical-height path)
(wrap xs vertical-height
#:soft-break line-spacer?
#:wrap-anywhere? #t
#:finish-wrap (λ (lns q idx)
(list (struct-copy quad q:page
[attrs (let ([page-number idx]
[h (hash-copy (quad-attrs q:page))])
(hash-set! h 'page-number page-number)
(define-values (dir name _)
(split-path (path-replace-extension path #"")))
(hash-set! h 'doc-title (string-titlecase (path->string name)))
h)]
[elems lns])))))
(define (insert-containers pages)
;; container recomposition happens after page composition because page breaks can happen between lines.
;; iow, the lines within a container may be split over multiple pages, each of which should be drawn
;; as a separate container
(for/list ([page (in-list pages)])
6 years ago
(define lns (quad-elems page))
(define groups (contiguous-group-by (λ (x) (hash-ref (quad-attrs x) 'container #f)) lns))
(define lns-and-containers (append* (for/list ([grp (in-list groups)])
(match (hash-ref (quad-attrs (car grp)) 'container #f)
["bq" (list (make-blockquote grp))]
6 years ago
["codeblock" (list (make-codeblock grp))]
6 years ago
[_ grp]))))
(struct-copy quad page [elems lns-and-containers])))
6 years ago
(define (run xs path)
(define pdf (time-name make-pdf (make-pdf #:compress #t
#:auto-first-page #f
6 years ago
#:output-path path
#:size "letter")))
6 years ago
(define line-width (- (pdf-width pdf) (* 2 side-margin)))
(define vertical-height (- (pdf-height pdf) top-margin bottom-margin))
6 years ago
(let* ([x (time-name atomize (atomize (qexpr->quad xs)))]
6 years ago
[x (time-name ->string-quad (map (λ (x) (->string-quad pdf x)) x))]
6 years ago
[x (time-name line-wrap #R (line-wrap x line-width))]
[x (time-name page-wrap (page-wrap x vertical-height path))]
[x (time-name insert-containers (insert-containers x))]
6 years ago
[x (time-name position (position (struct-copy quad q:doc [elems x])))])
6 years ago
(time-name draw (draw x pdf))))
6 years ago
(define-syntax (mb stx)
(syntax-case stx ()
6 years ago
[(_ PDF-PATH . STRS)
#'(#%module-begin
6 years ago
;; stick an nbsp in the strings so we have one printing char
(define qx (list* 'q '((font "Charter") (fontsize "12")) (add-between (cons " " (list . STRS)) pbr)))
6 years ago
(run qx PDF-PATH))]))
6 years ago
6 years ago
(module+ reader
(require scribble/reader syntax/strip-context (only-in markdown parse-markdown)
racket/match txexpr)
(provide (rename-out [quad-read-syntax read-syntax]))
6 years ago
6 years ago
6 years ago
(define (xexpr->parse-tree x)
;; an ordinary txexpr can't serve as a parse tree because of the attrs list fails when passed to #%app.
;; so stick an `attr-list` identifier on it which can hook into the expander.
;; sort of SXML-ish.
(let loop ([x x])
(match x
[(txexpr tag attrs elems) (list* tag (cons 'attr-list attrs) (map loop elems))]
[(? list? xs) (map loop xs)]
[_ x])))
6 years ago
(define (quad-read-syntax path-string p)
(define quad-at-reader (make-at-reader
#:syntax? #t
#:inside? #t
#:command-char #\◊))
(define stx (quad-at-reader path-string p))
6 years ago
(define parsed-stx (datum->syntax stx #R (xexpr->parse-tree (parse-markdown (apply string-append (syntax->datum stx))))))
6 years ago
(strip-context
(with-syntax ([PT parsed-stx]
[PDF-PATH (path-replace-extension path-string #".pdf")])
#'(module _ qtest/markdown
PDF-PATH
. PT)))))