better error for malformed meta

pull/58/head
Matthew Butterick 9 years ago
parent 5f9fd6bd19
commit d2c9a5d057

@ -9,7 +9,7 @@
(with-syntax ([new-module-begin (format-id stx "new-module-begin")]) (with-syntax ([new-module-begin (format-id stx "new-module-begin")])
#'(begin #'(begin
(provide (except-out (all-from-out racket/base) #%module-begin) (provide (except-out (all-from-out racket/base) #%module-begin)
(rename-out [new-module-begin #%module-begin])) (rename-out [new-module-begin #%module-begin]))
(define-syntax (new-module-begin stx-arg) (define-syntax (new-module-begin stx-arg)
(syntax-case stx-arg () (syntax-case stx-arg ()
[(_ body-exprs (... ...)) [(_ body-exprs (... ...))
@ -31,7 +31,7 @@
body-exprs (... ...)) body-exprs (... ...))
(require 'inner) (require 'inner racket/list)
;; in an inline module, reader-here-path and parser-mode are undefined ;; in an inline module, reader-here-path and parser-mode are undefined
@ -46,36 +46,44 @@
;; Split out the metas. ;; Split out the metas.
(define (split-metas-to-hash x) (define (split-metas-to-hash x)
(define (meta-key x) (car (caadr x))) (define (first-attribute x) (caadr x))
(define (meta-value x) (cadr (caadr x))) (define (meta-key x) (first (first-attribute x)))
(define is-meta-element? (λ(x) (and (list? x) ; possible txexpr (define (meta-value x) (second (first-attribute x)))
(>= (length x) 2) ; = tag + attribute + other elements (which are ignored) (define (properly-formed-meta-payload? x) (and (list? (second x))
(equal? 'meta (car x)) ; tag is 'meta (pair? (first-attribute x))
(symbol? (meta-key x)) ; attribute key is symbol (symbol? (meta-key x)) ; attribute key is symbol
(string? (meta-value x))))) ; attribute value is string (string? (meta-value x)))) ; attribute value is string
(define is-meta-element?
(λ(x)
(define possible-meta (and (list? x) ; possible txexpr
(>= (length x) 2) ; = tag + attribute + other elements (which are ignored)
(equal? 'meta (car x)))) ; tag is 'meta
(if possible-meta
(if (properly-formed-meta-payload? x)
#t
(error 'is-meta-element? "error: meta must be a symbol / string pair, instead got: ~v" x))
#f)))
(define (splitter x) (define (splitter x)
(define meta-acc null) (define meta-acc empty)
(define (split-metas x) (define (split-metas x)
(cond (cond
[(list? x) (define-values (new-metas rest) (values (filter is-meta-element? x) (filter (compose1 not is-meta-element?) x))) [(list? x) (define-values (new-metas rest) (partition is-meta-element? x))
(set! meta-acc (append new-metas meta-acc)) (set! meta-acc (append new-metas meta-acc))
(map split-metas rest)] (map split-metas rest)]
[else x])) [else x]))
(define result (split-metas x)) (define result (split-metas x))
(values result meta-acc)) (values result meta-acc))
(define-values (doc-without-metas meta-elements) (splitter x)) (define-values (doc-without-metas meta-elements) (splitter x))
(define meta-xexpr (apply metaroot meta-elements)) (define metas-xexpr (apply metaroot meta-elements))
(define meta-element->assoc (λ(x) (let ([key (meta-key x)][value (meta-value x)]) (cons key value)))) (define meta-element->assoc (λ(me) (cons (meta-key me) (meta-value me))))
(define metas (make-hash (map meta-element->assoc (cdr meta-xexpr)))) (define metas (make-hash (map meta-element->assoc (cdr metas-xexpr))))
(values doc-without-metas metas)) (values doc-without-metas metas))
(require racket/list)
(define doc-with-metas (define doc-with-metas
`(placeholder-root `(placeholder-root
,@(cons (meta 'here-path: here-path) ,@(cons (meta 'here-path: here-path)
(if (list? doc-raw) (if (list? doc-raw)
(dropf doc-raw (λ(i) (equal? i "\n"))) ; discard all newlines at front of file (dropf doc-raw (λ(i) (equal? i "\n"))) ; discard all newlines at front of file
doc-raw)))) doc-raw))))
@ -91,9 +99,9 @@
[(equal? parser-mode world:mode-markdown) (λ xs (apply root (apply (compose1 (dynamic-require 'markdown 'parse-markdown) string-append) (map to-string xs))))] [(equal? parser-mode world:mode-markdown) (λ xs (apply root (apply (compose1 (dynamic-require 'markdown 'parse-markdown) string-append) (map to-string xs))))]
;; for preprocessor output, just make a string. ;; for preprocessor output, just make a string.
[else (λ xs (apply string-append (map to-string xs)))]) ; default mode is preprocish [else (λ xs (apply string-append (map to-string xs)))]) ; default mode is preprocish
(cdr doc-without-metas))) ;; cdr strips placeholder-root tag (cdr doc-without-metas))) ;; cdr strips placeholder-root tag
(provide metas doc (provide metas doc
;; hide the exports that were only for internal use. ;; hide the exports that were only for internal use.
(except-out (all-from-out 'inner) doc-raw #%top))))]))))])) (except-out (all-from-out 'inner) doc-raw #%top))))]))))]))
Loading…
Cancel
Save