From 10cbb47950c5b295c6e23190bd27b7362a731806 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 18 Jul 2019 18:30:53 -0700 Subject: [PATCH] allow keyword arguments to have quotes (closes #39) --- quad/qtest/fark.rkt | 8 ++++---- quad/quad/scribblings/quad.scrbl | 8 ++++---- quad/quadwriter/lang-helper.rkt | 23 ++++++++++++----------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/quad/qtest/fark.rkt b/quad/qtest/fark.rkt index e05ce3c1..5d3f8d75 100644 --- a/quad/qtest/fark.rkt +++ b/quad/qtest/fark.rkt @@ -1,11 +1,11 @@ #lang quadwriter/markdown -#:page-size A3 -#:page-orientation wide +#:page-size "A3" +#:page-orientation "wide" #:column-count 3 #:column-gap 24 -#:line-align justify -#:line-wrap best +#:line-align "justify" +#:line-wrap "best" #:page-margin-left 200 #:page-margin-right 100 diff --git a/quad/quad/scribblings/quad.scrbl b/quad/quad/scribblings/quad.scrbl index e1acd9cc..8936b402 100644 --- a/quad/quad/scribblings/quad.scrbl +++ b/quad/quad/scribblings/quad.scrbl @@ -292,16 +292,16 @@ Even if you're using a @racketmodname[quadwriter] dialect, you can still set top @codeblock|{ #lang quadwriter/markdown -#:page-size tabloid -#:page-orientation wide +#:page-size "tabloid" +#:page-orientation "wide" #:font-size 18 -#:font-color red +#:font-color "red" Brennan and Dale like fancy sauce. }| ] -Any of the @secref{Markup} attributes documented below can be used as keyword arguments. The syntax follows the pattern above: one attribute + value pair per line, with the attribute prefixed with @litchar{#:} to make it a keyword, and the value unquoted. +Any of the @secref{Markup} attributes documented below can be used as keyword arguments. The syntax follows the pattern above: one attribute + value pair per line, with the attribute prefixed with @litchar{#:} to make it a keyword, followed by the value. This keyword syntax works in the @racketmodname[quadwriter], @racketmodname[quadwriter/markdown], and @racketmodname[quadwriter/markup] languages. The idea is to make it easy to adjust the default layout behavior without going outside the source file. diff --git a/quad/quadwriter/lang-helper.rkt b/quad/quadwriter/lang-helper.rkt index c90fa876..73bd29b3 100644 --- a/quad/quadwriter/lang-helper.rkt +++ b/quad/quadwriter/lang-helper.rkt @@ -1,6 +1,7 @@ #lang debug racket/base (require (for-syntax racket/base) racket/match + racket/dict pollen/tag racket/system racket/class @@ -19,20 +20,20 @@ (define ((make-read-syntax expander-mod pt-proc) path-string p) ;; peel off any lines of format #:keyword val (bounded by newline) ;; and turn them into qexpr attrs - (define kw-val-pat #px"^(#:\\S+)\\s+([\\w ]*)\n") (define kw-attrs - (let loop ([acc null]) + (let loop ([kw-val-pairs null]) (cond - [(regexp-try-match #px"^\\s+" p) (loop acc)] - [(regexp-try-match kw-val-pat p) - => - (λ (m) - (match m - [(list _ kw val) (loop (cons (list kw val) acc))]))] + [(regexp-try-match #px"^\\s+" p) (loop kw-val-pairs)] + [(equal? (peek-bytes 2 0 p) #"#:") ; discovered a keyword + (parameterize ([current-input-port (open-input-string (read-line p))]) + (match (list (read) (read)) + [(list (? keyword? kw) val) #:when (not (eof-object? val)) + (loop (cons (cons kw val) kw-val-pairs))] + [_ (loop kw-val-pairs)]))] ;; reverse in case of multiple values with same keyword, latest takes precedence (by becoming first) - [else (reverse (for/list ([item (in-list acc)]) - (match-define (list kw val) (map bytes->string/utf-8 item)) - (list (string->symbol (string-trim kw "#:")) val)))]))) + [else (reverse (for/list ([(kw val) (in-dict kw-val-pairs)]) + (list (string->symbol (string-trim (keyword->string kw) "#:")) + (format "~a" val))))]))) (strip-context (with-syntax ([PATH-STRING path-string] [((ATTR-NAME ATTR-VAL) ...) kw-attrs]