Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
version: ${{ matrix.racket-version }}
- name: Install package with dependencies
run: raco pkg install --auto --batch --link html-printer-lib/
- name: Install HTML Tidy (the tests compare output against it when a new enough version is found)
run: sudo apt-get update && sudo apt-get install -y tidy && tidy --version
- name: Run tests
run: raco test -c html-printer
- name: Build docs
Expand Down
423 changes: 205 additions & 218 deletions html-printer-lib/main.rkt

Large diffs are not rendered by default.

16 changes: 2 additions & 14 deletions html-printer-lib/private/log.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

(require (for-syntax racket/base)
racket/list
racket/logging
racket/mutable-treelist)
racket/logging)

(provide html-printer-logger
logging-enabled?
Expand Down Expand Up @@ -56,18 +55,7 @@
(format "~a└─ ~a" (make-string (* 3 nestlevel) #\space) who)]))

(define ($var name val)
(define output
(cond ;[(number? val) (~r val #:min-width 3)]
[(mutable-treelist? val) ($mtl val)]
[else val]))
(format "~a: ~v" name output))

(define ($mtl m)
(apply string-append
(add-between (map (lambda (v) (format "~v" v)) (mutable-treelist->list m)) '(",")
#:before-first '("{")
#:after-last '("}")
#:splice? #t)))
(format "~a: ~v" name val))

(define ($vals lst)
(apply string-append
Expand Down
302 changes: 101 additions & 201 deletions html-printer-lib/private/printer.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,219 +3,119 @@
(require "strings.rkt"
"log.rkt"
racket/match
(only-in racket/syntax format-symbol)
(rename-in racket/mutable-treelist
(mutable-treelist-first ~first)
(mutable-treelist-last ~last)
(mutable-treelist-add! ~add!)
(mutable-treelist-take! ~take!)
(mutable-treelist-drop! ~drop!)
(mutable-treelist-drop-right! ~drop-right!)
(mutable-treelist-empty? ~empty?)))
(only-in racket/syntax format-symbol))

(provide make-wrapping-printer)
(provide make-wrapping-printer
(struct-out text)
(struct-out raw))

(module+ test)

;; The printer consumes a flat stream of tokens and lays them out greedily, one line at a time:
;;
;; (text str) An unbreakable run of characters. Adjacent text tokens are glued into one
;; "cluster" which is always printed on a single line.
;; 'space A break opportunity that prints as a single space if the line continues.
;; Consecutive spaces collapse; a space at the start or end of a line is dropped.
;; 'softbreak A zero-width break opportunity: prints nothing unless the line breaks there.
;; 'newline A hard line break. Does nothing if nothing has been printed on the current line.
;; 'blank A hard line break that always produces an empty line.
;; 'indent+ Raise/lower the indent level. Takes effect the next time a line is started.
;; 'indent-
;; (raw str indent?)
;; Verbatim output (contents and closing tags of <pre>, <script>, <style>). Never
;; wrapped; the printer only keeps track of the column it ends in. If it arrives at
;; the start of a line, the indent is written first when indent? is true (closing
;; tags of <script> and <style> after content ending in a newline) and omitted when
;; it is false (content, and the closing tag of <pre>, where indent would be content).
;; 'flush Print whatever is still pending (used at the end of the document).
;;
;; A list of tokens is accepted anywhere a token is.

(struct text (str) #:transparent)
(struct raw (str indent?) #:transparent)

(define (make-wrapping-printer [outp (current-output-port)]
#:wrap-at [wrap-col 100]
#:indent-spaces [indent 2])
(let ([col 1] ; column of next char, counting actually-printed chars
[indent-level 0] ; indent level in columns
[logical-line-start #t] ; at "start"? (ignoring whitespace) counting actually-printed chars
[accum-width 0] ; length of strings in accumulator
[accumulator (mutable-treelist)]) ; accumulator (list of strings not yet actually printed)
(define col 1) ; 1-based column at which the next character will land
(define indent-level 0) ; current indent, in columns
(define line-start? #t) ; nothing written on the current line yet (indent is written lazily)
(define pending 'none) ; separator owed before the next cluster: 'none, 'softbreak or 'space
(define cluster '()) ; text glued together since the last break opportunity (reversed)
(define cluster-width 0) ; width of cluster, in graphemes

(define (accum-empty?)
(or (~empty? accumulator)
(equal? accumulator (mutable-treelist breakpoint))))
(define (write! s) (display s outp))

(define (end-is-whsp? mtl which-end)
(and (not (~empty? mtl))
(whitespace? ((if (eq? which-end 'right)
~last
~first)
mtl))))
(define (newline!)
(write! (sys-newline))
(set! col 1)
(set! line-start? #t)
(set! pending 'none))

(define (lop-whsp-end! mtl which-end)
(cond
[(end-is-whsp? mtl which-end)
(define-values (tl-get tl-drop)
(case which-end
[(left) (values ~first ~drop!)]
[(right) (values ~last ~drop-right!)]))
(define lopped-width 0)
(do () [(not (end-is-whsp? mtl which-end))]
(set! lopped-width (+ lopped-width (string-grapheme-count (tl-get mtl))))
(tl-drop mtl 1))
lopped-width]
[else #f]))

(define (lop-accum-whsp-end! which-end [lev 1])
(match (lop-whsp-end! accumulator which-end)
[(? number? lopped-len)
(log-printer lev lop-accum-end _ accum-width lopped-len which-end)
(set! accum-width (- accum-width lopped-len))]
[_ #f]))
;; Write the indent for a fresh line. Only called when something is about to be printed.
(define (start-line!)
(write! (make-string indent-level #\space))
(set! col (+ 1 indent-level))
(set! line-start? #f))

(define (flush! [lev 1])
(unless (accum-empty?)
(log-printer lev flush start… col accum-width logical-line-start indent-level accumulator)
(define lopped? (lop-accum-whsp-end! 'right (+ lev 1)))
(define buffer (mutable-treelist))
(define buf-width
(for/fold ([held-whsp? #f]
[buffer-width 0]
#:result buffer-width)
([v (in-mutable-treelist accumulator)])
(cond
[(breakpoint? v)
(log-printer (+ lev 1) flush at_bp col buffer-width held-whsp? buffer)
(when (> (+ col buffer-width) wrap-col) (break+indent! (+ lev 1)))
(cond
[(~empty? buffer) (values held-whsp? 0)]
[else
(log-printer (+ lev 2) flush printbuf… held-whsp? logical-line-start indent-level)
(for ([bv (in-mutable-treelist buffer)])
(unless (and logical-line-start (whitespace? bv))
(put! bv (+ lev 3))))
(when (and held-whsp? (not logical-line-start)) (put! " "))
(~take! buffer 0)
(values #f 0)])]
[(whitespace? v)
(log-printer (+ lev 1) flush whitespace v)
(values 1 (+ 1 buffer-width))]
[else
(when held-whsp? (~add! buffer " "))
(~add! buffer v)
(log-printer (+ lev 1) flush non-whsp held-whsp? buffer)
(values #f (+ buffer-width (string-grapheme-count v)))])))

(log-printer lev flush done-breaking col accum-width logical-line-start accumulator)
(when (not (~empty? buffer))
(when (> (+ col buf-width) wrap-col) (break+indent! (+ lev 1)))
(for ([bv (in-mutable-treelist buffer)])
(unless (and logical-line-start (whitespace? bv)) (put! bv (+ lev 1)))))
;; Print the pending cluster, breaking the line first if it would not fit. The cluster (with its
;; separating space, if any) will occupy columns col … col+needed-1, so it fits exactly when
;; col+needed-1 <= wrap-col. Nothing is ever broken at the start of a line: a cluster wider than
;; the whole line simply overflows.
(define (commit!)
(unless (null? cluster)
(define sep? (and (eq? pending 'space) (not line-start?)))
(define needed (+ cluster-width (if sep? 1 0)))
(define fits? (or line-start? (<= (+ col needed -1) wrap-col)))
(log-printer 1 commit _ col cluster-width sep? fits? line-start? indent-level cluster)
(unless fits? (newline!))
(cond
[line-start? (start-line!)]
[sep? (write! " ") (set! col (+ col 1))])
(for ([s (in-list (reverse cluster))]) (write! s))
(set! col (+ col cluster-width))
(set! cluster '())
(set! cluster-width 0)
(set! pending 'none)))

(set! accum-width 0)
(~take! accumulator 0)
(when (and lopped? (not logical-line-start))
(accumulate! " ")
(log-printer lev flush done col accum-width logical-line-start accumulator)
)))

(define (break! [lev 1])
(log-printer lev break! start… col accum-width logical-line-start indent-level accumulator)
(flush! (+ lev 1))
(display (sys-newline) outp)
(set! logical-line-start #t)
(set! col 1)
(log-printer lev break! …end col accum-width logical-line-start indent-level accumulator))

(define (break+indent! [lev 1])
(log-printer lev break+indent! start… col accum-width logical-line-start indent-level accumulator)
(display (sys-newline) outp)
(display (make-string indent-level #\space) outp)
(set! logical-line-start #t)
(set! col (+ 1 indent-level))
(lop-accum-whsp-end! 'left (+ lev 1))
(log-printer lev break+indent! …end col accum-width logical-line-start indent-level accumulator))
(define (text! s)
(unless (equal? s "")
(set! cluster (cons s cluster))
(set! cluster-width (+ cluster-width (string-grapheme-count s)))))

(define (accumulate! v [lev 1] #:breakpoint-before? [breakpoint-before? #f])
(log-printer lev accum! start… col accum-width logical-line-start indent-level breakpoint-before? accumulator)
(define str (->string v))
(define whsp? (whitespace? str))
(define len (string-grapheme-count str))
(unless (or (equal? "" str)
(and logical-line-start (accum-empty?) whsp?))
(when breakpoint-before? (~add! accumulator breakpoint))
(~add! accumulator str)
(set! accum-width (+ accum-width len)))
(log-printer lev accum! …end col accum-width logical-line-start indent-level accumulator))
(define (raw! s indent?)
(commit!)
(set! pending 'none)
(unless (equal? s "")
(when line-start?
(if indent? (start-line!) (set! line-start? #f)))
(write! s)
(define tail (car (regexp-match #rx"[^\r\n]*$" s)))
(cond
[(= (string-length tail) (string-length s)) ; no line breaks inside s
(set! col (+ col (string-grapheme-count s)))]
[(equal? tail "")
(set! col 1)
(set! line-start? #t)]
[else
(set! col (+ 1 (string-grapheme-count tail)))])))

(define (accumulate/wrap! v [lev 1])
(log-printer lev accum/wrap! start… col accum-width logical-line-start indent-level accumulator)
(define str (->string v))
(when (> (+ col accum-width (string-grapheme-count str)) wrap-col)
(flush! (+ 1 lev)))
(accumulate! str (+ lev 1) #:breakpoint-before? #t)
(log-printer lev accum/wrap! …end col accum-width logical-line-start indent-level accumulator))
(define (handle! tok)
(unless (list? tok)
(log-printer 1 token _ tok col line-start? pending cluster-width indent-level))
(match tok
[(text s) (text! s)]
[(raw s indent?) (raw! s indent?)]
['space (commit!) (set! pending 'space)]
['softbreak (commit!) (when (eq? pending 'none) (set! pending 'softbreak))]
['newline (commit!) (if line-start? (set! pending 'none) (newline!))]
['blank (commit!) (unless line-start? (newline!)) (newline!)]
['indent+ (commit!) (set! indent-level (+ indent-level indent))]
['indent- (commit!) (set! indent-level (max 0 (- indent-level indent)))]
['flush (commit!)]
[(? list? toks) (for-each handle! toks)]
[_ (raise-argument-error 'wrapping-printer "printer token" tok)]))

(define (put! v [lev 1])
(log-printer lev put! start… v col accum-width logical-line-start indent-level)
(define str (->string v))
(unless (whitespace? str) (set! logical-line-start #f))
(set! col
(for/fold ([c col])
([w (in-list (words str))])
(display w outp)
(cond [(linebreak? w)
(set! logical-line-start #t)
1]
[else (+ c (string-grapheme-count w))])))
(log-printer lev put! …end col accum-width logical-line-start indent-level))

(define proc
(lambda args
(for/fold ([result (void)])
([arg (in-list args)])
(case arg
[(get-line-length) wrap-col]
[(break)
(break!)]
[(check/flush)
(log-printer 1 check/flush col accum-width wrap-col indent-level)
(when (> (+ col accum-width) wrap-col) (flush! 2))]
[(flush)
(flush!)]
[(indent)
(log-printer 1 indent start col indent-level)
(display (make-string indent-level #\space) outp)
(set! col (+ col indent-level))
(log-printer 1 indent end col indent-level)]
[(indent-if-col1)
(log-printer 1 indent-if-col1 start col indent-level)
(when (= col 1)
(display (make-string indent-level #\space) outp)
(set! col (+ col indent-level)))
(log-printer 1 indent-if-col1 end col indent-level)]
[(--indent)
(log-printer 1 --indent start col indent-level)
(set! indent-level (max 0 (- indent-level indent)))
(display (make-string indent-level #\space) outp)
(set! col (+ col indent-level))
(log-printer 1 --indent end col indent-level)]
[(break/indent)
(break+indent!)]
[(break/++indent)
(log-printer 1 break/++indent start col indent-level)
(set! indent-level (+ indent-level indent))
(break+indent! 2)
(log-printer 1 break/++indent end col indent-level)]
[(break/--indent)
(log-printer 1 break/--indent _ col)
(set! indent-level (max 0 (- indent-level indent)))
(break+indent! 2)]
[(pop-whitespace)
(log-printer 1 pop-whitespace _ accumulator)
(cond
[(accum-empty?) #f]
[else
(match (~last accumulator)
[(? whitespace? popped)
(~drop-right! accumulator 1)
(set! accum-width (- accum-width (string-grapheme-count popped)))
(log-printer 1 pop-whitespace _ popped accumulator)
popped]
[_ #f])])]
[else
(when (and (list? arg) (not (null? arg)) (not (null? (cdr arg))))
(define-values (sym str) (values (car arg) (cadr arg)))
(case sym
[(put)
(cond
[(accum-empty?) (put! str)]
[else (accumulate! str)])]
[(put/wrap)
(accumulate/wrap! str)]))]))))
(procedure-rename proc (format-symbol "wrapping-printer[cols:~a,sp:~a]" wrap-col indent))))
(procedure-rename (lambda toks (for-each handle! toks))
(format-symbol "wrapping-printer[cols:~a,sp:~a]" wrap-col indent)))
Loading