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

154 lines
7.2 KiB
Racket

6 years ago
#lang debug racket/base
6 years ago
(require (for-syntax racket/base) racket/runtime-path racket/string racket/promise racket/match racket/list
6 years ago
pitfall quad sugar/debug markdown pollen/tag (prefix-in pt: pollen/top))
(provide (except-out (all-from-out racket/base) #%module-begin #%top)
(rename-out [mb #%module-begin][pt:#%top #%top])
(all-defined-out))
(define-syntax-rule (p attrs . exprs)
(list 'q 'attrs . exprs))
6 years ago
(define-syntax-rule (code attrs . exprs)
(list 'q (list* '(font "fira-mono") '(bg "gray") 'attrs) . exprs))
6 years ago
(define-syntax-rule (strong attrs . exprs)
(list 'q (cons '(font "charter-bold") 'attrs) . exprs))
(define-syntax-rule (em attrs . exprs)
(list 'q (cons '(font "charter-italic") 'attrs) . exprs))
6 years ago
(define q:string (q #:in 'bi #:out 'bo ;; align to baseline
;; 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)))
6 years ago
(match-define (list str) (quad-elems q))
(match-define (list x y) (quad-origin q))
(text doc str x y))))
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.ttf")
6 years ago
(define-runtime-path fira-mono "fonts/fira-mono.ttf")
6 years ago
(define (->string-quad doc q)
(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
6 years ago
(match (string-downcase (string-replace val " " "-"))
6 years ago
["charter" charter]
6 years ago
["charter-bold" charter-bold]
["charter-italic" charter-italic]
6 years ago
["fira" fira]
["fira-mono" fira-mono]))))
6 years ago
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)))]))
(define line-height 20)
(define q:line (q #:size (pt +inf.0 line-height)
#:out 'sw
#:printable #true))
6 years ago
(struct line-spacer quad () #:transparent)
(define q:line-spacer (q #:type line-spacer
#:size (pt +inf.0 (* line-height 0.7))
#:out 'sw
#:printable (λ (q sig)
(not (memq sig '(start end))))))
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
6 years ago
(define (line-wrap xs size)
(break xs size
#:hard-break (λ (q) (equal? "" (car (quad-elems q))))
#:soft-break soft-break-for-line?
#:finish-wrap (λ (pcs q idx)
(append
(if (= idx 1) (list q:line-spacer) null)
(list (struct-copy quad q:line
6 years ago
[elems (consolidate-runs pcs)]))))))
6 years ago
(define q:page (q #:offset '(36 36)
#:pre-draw (λ (q doc) (add-page doc))))
(define q:doc (q #:pre-draw (λ (q doc) (start-doc doc))
6 years ago
#:post-draw (λ (q doc) (end-doc doc))))
6 years ago
(define (page-wrap xs vertical-height)
6 years ago
(break xs vertical-height
#:soft-break line-spacer?
#:finish-wrap (λ (pcs q idx) (list (struct-copy quad q:page [elems pcs])))))
6 years ago
(define (run xs path)
(define pdf (time-name make-pdf (make-pdf #:compress #t
#:auto-first-page #f
#:output-path path)))
(define line-width 200)
(define vertical-height 400)
6 years ago
(let* ([x (time-name runify (runify (qexpr->quad xs)))]
[x (time-name ->string-quad (map (λ (x) (->string-quad pdf x)) x))]
[x (time-name line-wrap (line-wrap x line-width))]
[x (time-name page-wrap (page-wrap x vertical-height))]
[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 ()
[(_ . STRS)
(with-syntax ([PS (syntax-property #'STRS 'ps)])
#'(#%module-begin
(define qx `(q ((font "Charter") (fontsize "12")) ,@(list . STRS)))
(run qx PS)))]))
(module reader syntax/module-reader
qtest/markdown
#:read quad-read
#:read-syntax quad-read-syntax
#:whole-body-readers? #t ;; need this to make at-reader work
6 years ago
(require scribble/reader markdown pollen/private/splice racket/list quad)
6 years ago
(define (quad-read p) (syntax->datum (quad-read-syntax (object-name p) p)))
(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 (add-between (parse-markdown (apply string-append (syntax->datum stx))) '(quad ""))))
6 years ago
(syntax-property parsed-stx 'ps (path-replace-extension path-string #".pdf"))))