diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index e5aac6d..dc4783f 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -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 diff --git a/html-printer-lib/main.rkt b/html-printer-lib/main.rkt index eb3258e..de2307f 100644 --- a/html-printer-lib/main.rkt +++ b/html-printer-lib/main.rkt @@ -19,236 +19,223 @@ (null? (cddr v)) (string? (cadr v)))) -(define (safe-rest lst) - (or (and (not (null? lst)) - (cdr lst)) - '())) - -;; Given a list of attributes, return a flat list of strings for each attribute name -;; and value. Each string ("chunk") in the list is considered a discrete “word” for -;; line-wrapping purposes -(define (attr-chunks attrs) - (for/fold ([chunks '()] - [remaining (safe-rest attrs)] - #:result (reverse chunks)) - ([attr (in-list attrs)]) +;; An opening tag with its attributes, as a list of printer tokens. The tag name and the first +;; attribute name are glued together (as in ``. +(define (opener-tokens tag attrs) + (define (attr-tokens attr first? last?) (match-define (list key val) attr) - (define-values (key-str val-str) - (cond - [(boolean-attr? key) - (values (format (if (null? remaining) "~a>" "~a ") key) - "")] - [else - (values (format "~a=" key) - (format (if (null? remaining) "\"~a\">" "\"~a\" ") - (escape (string-normalize-spaces val) attribute-table)))])) - (values (cons val-str (cons key-str chunks)) - (safe-rest remaining)))) + (define lead (if first? (format "<~a " tag) "")) + (define end (if last? ">" "")) + (define sep (if first? '() (list 'space))) + (cond + [(boolean-attr? key) + (append sep (list (text (format "~a~a~a" lead key end))))] + [else + (append sep (list (text (format "~a~a=" lead key)) + 'softbreak + (text (format "\"~a\"~a" + (escape (string-normalize-spaces val) attribute-table) + end))))])) + (cond + [(null? attrs) (list (text (format "<~a>" tag)))] + [else + (define last-i (- (length attrs) 1)) + (apply append + (for/list ([attr (in-list attrs)] [i (in-naturals)]) + (attr-tokens attr (= i 0) (= i last-i))))])) (module+ test (require rackunit) - (check-equal? (attr-chunks '([href "a"] [title "x"])) - '("href=" "\"a\" " "title=" "\"x\">") - "attribute keys/vals correctly formatted as strings") - (check-equal? (attr-chunks '([href "a"] [selected "x"])) - '("href=" "\"a\" " "selected>" "") + (check-equal? (opener-tokens 'p '()) + (list (text "

")) + "no attributes") + (check-equal? (opener-tokens 'a '([href "a"] [title "x"])) + (list (text "")) + "attribute keys/vals correctly formatted as tokens") + (check-equal? (opener-tokens 'a '([href "a"] [selected "x"])) + (list (text "")) "boolean attributes handled correctly (when last attribute)") - (check-equal? (attr-chunks '([selected "x"] [href "a"])) - '("selected " "" "href=" "\"a\">") + (check-equal? (opener-tokens 'a '([selected "x"] [href "a"])) + (list (text "")) "boolean attributes handled correctly (when not last attribute)") - (check-equal? (attr-chunks '([SELECTED "x"] [href "a"])) - '("SELECTED " "" "href=" "\"a\">") - "boolean attributes handled correctly regardless of case")) - -(define (opener tag attrs) - (format (if (not (null? attrs)) "<~a " "<~a>") tag)) + (check-equal? (opener-tokens 'a '([SELECTED "x"] [href "a"])) + (list (text "")) + "boolean attributes handled correctly regardless of case") + (check-equal? (opener-tokens 'p '([title "a \n b"])) + (list (text "

")) + "whitespace in attribute values is normalized")) (define (closer tag) (if (self-closing? tag) "" (format "" tag))) -;; 'normal = inserting a line break before next element is allowed -;; 'sticky = inserting a line break before next element is not allowed -;; 'flow-opened = a line break+bump was already added directly after previous value -;; 'flow-closed = a line break+unbump was already added directly after previous value -;; 'block-closed = a line break was already added directly after the previous value - -(define (normal? v) (eq? v 'normal)) -(define (sticky? v) (eq? v 'sticky)) -(define (flow-opened? v) (eq? v 'flow-opened)) -(define (flow-closed? v) (eq? v 'flow-closed)) -(define (flow-any? v) (memq v '(first flow-opened flow-closed block-closed))) -(define (block-closed? v) (eq? v 'block-closed)) - -(define break-after-block? (make-parameter #f)) -(define (add-break? tag prev-tok) - (and (break-after-block?) - (not (memq tag '(meta link title))) - (case prev-tok - [(flow-closed block-closed) #t] - [else #f]))) - -(define (display-val yeet! v [prev-token 'first] #:in-inline? [inside-inline? #f]) - (match v - [(? null?) prev-token] - [(list* (and (? symbol?) (? br?)) _) - (log-expr break-tag found v ) - (when (or (flow-closed? prev-token) (block-closed? prev-token)) (yeet! 'indent)) - (yeet! '(put/wrap "
") 'flush 'break/indent) - 'flow-opened] ; hacky - - ; flow tag - [(list (? flow? tag) (list (? attr? attrs) ...) elems ...) - (log-expr flow starting… tag prev-token) - (when (add-break? tag prev-token) (yeet! 'break)) - (define weird-flow? (or inside-inline? (not (flow-any? prev-token)))) - (when weird-flow? - (log-expr "Flow tag ~a inside block/inline tag (considered weird)" tag) - (when (not (memq prev-token '(block-closed flow-closed))) - (yeet! 'flush 'break/++indent))) - - (unless (or (flow-opened? prev-token) weird-flow?) - (yeet! 'indent)) - - (yeet! `(put ,(opener tag attrs))) - (for ([a (in-list (attr-chunks attrs))]) - (yeet! `(put/wrap ,a))) - (yeet! 'flush 'break/++indent) - (define last-tok - (for/fold ([last-token 'flow-opened]) - ([elem (in-list elems)]) - (display-val yeet! elem last-token))) - (cond - [(or (block-closed? last-tok) (flow-closed? last-tok)) (yeet! '--indent)] - [(flow-opened? last-tok) (yeet! 'break/--indent)] - [else (yeet! 'flush 'break/--indent)]) - (log-expr flow …closing tag last-tok) - (yeet! `(put/wrap ,(closer tag))) - (if weird-flow? - (yeet! 'flush 'break/--indent) - (yeet! 'break)) - 'flow-closed] - - ; block tag - [(list (? block? tag) (list (? attr? attrs) ...) elems ...) - (when (add-break? tag prev-token) (yeet! 'break)) - (log-expr block starting… tag prev-token) - (when inside-inline? - (log-expr "Block tag ~a inside inline tag ~a (considered weird)" tag inside-inline?)) - (cond - [(memq prev-token '(normal sticky)) (yeet! 'flush 'break/indent)] - [else (unless (flow-opened? prev-token) (yeet! 'indent))]) - (yeet! `(put ,(opener tag attrs))) - (for ([a (in-list (attr-chunks attrs))]) - (yeet! `(put/wrap ,a))) - (define last-tok - (for/fold ([last-token 'normal]) - ([elem (in-list elems)]) - (display-val yeet! elem last-token))) - (log-expr block …closing tag last-tok) - (yeet! 'check/flush `(put ,(closer tag)) 'break) - 'block-closed] - - ; script, style, pre - [(list (? preserve-contents? tag) (list (? attr? attrs) ...) elems ...) - (log-expr preserve start… tag prev-token) - (when (add-break? tag prev-token) (yeet! 'break)) - (unless (flow-opened? prev-token) (yeet! 'indent)) - (yeet! `(put ,(opener tag attrs))) - (for ([a (in-list (attr-chunks attrs))]) - (yeet! `(put/wrap ,a))) - (case tag - [(script style) - (for ([elem (in-list elems)]) (yeet! `(put ,elem)))] - [(pre) - (for ([elem (in-list elems)]) - (yeet! `(put ,(if (string? elem) (escape elem string-element-table) elem))))]) - (log-expr preserve …closing tag) - (yeet! 'indent-if-col1 - `(put ,(closer tag)) - 'break) - 'block-closed] - - ; inline tag - [(list (? symbol? tag) (list (? attr? attrs) ...) elems ...) - (log-expr inline start… tag prev-token) - (when (add-break? tag prev-token) (yeet! 'break)) - (when (or (flow-closed? prev-token) - (block-closed? prev-token)) - (yeet! 'indent)) - (yeet! `(,(if (sticky? prev-token) 'put 'put/wrap) ,(opener tag attrs))) - (for ([a (in-list (attr-chunks attrs))]) - (yeet! `(put/wrap ,a))) - (define last-token - (for/fold ([last 'sticky]) ; was prev-token - ([elem (in-list elems)]) - (display-val yeet! elem last #:in-inline? tag))) - (define popped (yeet! 'pop-whitespace)) - (log-expr inline after popped tag) - (log-expr inline …closing tag last-token) - (yeet! `(,(if (sticky? last-token) 'put 'put/wrap) ,(closer tag))) - (when popped (yeet! `(put/wrap ,popped))) - (if (member last-token '(normal sticky)) - last-token - 'normal)] - - ; no attributes = send it through again - [(list* (? symbol? tag) elems) - (display-val yeet! `(,tag () ,@elems) prev-token #:in-inline? inside-inline?)] - - ;; Strings are broken up and printed one word at a time. Much depends on the behavior of - ;; in-words, which yields separate words for consecutive whitespace and for each individually - ;; valid combination of CRLF characters (so "\r\r\n" becomes '("\r" "\r\n"), e.g.) - ;; This match is never reached while inside and are indented, but + ;; goes at column 1: any indent there would be part of the preformatted content + (emit! (raw (closer tag) (not pre?)) 'newline) + #t] + + ; inline tag + [(list (? symbol? tag) (list (? attr? attrs) ...) elems ...) + (log-expr inline starting… tag parent prev-block?) + (maybe-blank! tag prev-block?) + (apply emit! (opener-tokens tag attrs)) + (walk-children elems 'inline) + ;; Trailing whitespace inside an inline tag is moved after the closing tag, so that + ;; word becomes word (and the closing tag stays glued to the word) + (define popped? (pop-space!)) + (log-expr inline …closing tag popped?) + (emit! (text (closer tag))) + (when popped? (emit! 'space)) + #f] + + ; no attributes = send it through again + [(list* (? symbol? tag) elems) + (walk `(,tag () ,@elems) parent prev-block?)] + + ;; Strings are split into words and whitespace; a line may break at any whitespace + [(? string? str) + (log-expr string found parent prev-block? str) + (for ([word (in-list (words str))]) + (emit! (if (whitespace? word) 'space (text (escape word string-element-table))))) + (if (whitespace? str) prev-block? #f)] + + ;; Comments are wrapped like text. A comment that starts a line (first thing after a flow tag + ;; opens, or after a block-level sibling) gets its own line(s); anywhere else it is inline. + [(? comment? c) + (log-expr comment found parent prev-block?) + (define own-line? (and (pair? acc) (eq? (car acc) 'newline))) + ;; A line break directly after a comment adds no significant whitespace if there was + ;; already whitespace directly before it (the two collapse together) + (define breakable-after? (and (not own-line?) (after-space?))) + (when own-line? (emit! 'newline)) + (emit! (text "")) + (when own-line? (emit! 'newline)) + (when breakable-after? (emit! 'softbreak)) + prev-block?] + + [(or (? symbol? v) (? exact-positive-integer? v)) + (log-expr entity found v) + (emit! (text (->string v))) + #f] + + ;; Anything else (CDATA, processing instructions…) is printed as-is, as one unbreakable + ;; chunk. As with comments, a break directly after it is harmless if whitespace preceded it. + [(? xexpr? v) + (log-expr other found v) + (define breakable-after? (after-space?)) + (emit! (text (->string v))) + (when breakable-after? (emit! 'softbreak)) + prev-block?] + + [_ + (raise-arguments-error 'xexpr->html5 + "not a valid element (= txexpr, string, symbol, character integer, CDATA, or comment" + "value" v)])) + + (walk v 'top #f) + (reverse acc)) (define (xexpr->html5 v #:wrap [wrap 100] #:add-breaks? [add-breaks? #f]) (with-output-to-string (λ () - (when (and (list? v) (symbol? (car v)) (eq? 'html (symbol-downcase (car v)))) + (when (and (pair? v) (symbol? (car v)) (eq? 'html (symbol-downcase (car v)))) (displayln "")) - (define proc (make-wrapping-printer #:wrap-at wrap)) - (parameterize ([break-after-block? add-breaks?]) - (display-val proc v)) - (proc 'flush)))) \ No newline at end of file + (define print! (make-wrapping-printer #:wrap-at wrap)) + (print! (xexpr->tokens v add-breaks?) 'flush)))) diff --git a/html-printer-lib/private/log.rkt b/html-printer-lib/private/log.rkt index 30cb033..cf940df 100644 --- a/html-printer-lib/private/log.rkt +++ b/html-printer-lib/private/log.rkt @@ -2,8 +2,7 @@ (require (for-syntax racket/base) racket/list - racket/logging - racket/mutable-treelist) + racket/logging) (provide html-printer-logger logging-enabled? @@ -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 diff --git a/html-printer-lib/private/printer.rkt b/html-printer-lib/private/printer.rkt index a13ff76..dd8b0a6 100644 --- a/html-printer-lib/private/printer.rkt +++ b/html-printer-lib/private/printer.rkt @@ -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

,