tune up markdown (in progress)

pull/9/head
Matthew Butterick 11 years ago
parent eee3abb0a9
commit 51cfc26ef0

@ -9,7 +9,7 @@
(define (to-string x) (define (to-string x)
(if (string? x) (if (string? x)
x ; fast exit for strings x ; fast exit for strings
(with-handlers ([exn:fail? (λ(exn) (error (format "Pollen parser: can't convert ~a to ~a" x 'string)))]) (with-handlers ([exn:fail? (λ(exn) (error (format "Pollen parser: can't convert ~v to ~a" x 'string)))])
(cond (cond
[(equal? '() x) ""] [(equal? '() x) ""]
[(symbol? x) (symbol->string x)] [(symbol? x) (symbol->string x)]

@ -46,11 +46,15 @@
(define metas (make-hash (map meta-element->assoc meta-elements))) (define metas (make-hash (map meta-element->assoc meta-elements)))
(values doc-without-metas metas)) (values doc-without-metas metas))
(define doc-txexpr `(placeholder-root (define doc-txexpr
,@(cons (meta 'here: inner-here) (let ([doc-raw (if (equal? reader-mode world:reader-mode-markdown)
(cons (meta 'here-path: inner-here-path) (apply (compose1 (dynamic-require 'markdown 'parse-markdown) string-append) doc-raw)
;; cdr strips initial linebreak, but make sure doc-raw isn't blank doc-raw)])
(if (and (list? doc-raw) (> 0 (length doc-raw))) (cdr doc-raw) doc-raw))))) `(placeholder-root
,@(cons (meta 'here: inner-here)
(cons (meta 'here-path: inner-here-path)
;; cdr strips initial linebreak, but make sure doc-raw isn't blank
(if (and (list? doc-raw) (> 0 (length doc-raw))) (cdr doc-raw) doc-raw))))))
(define-values (doc-without-metas metas) (split-metas-to-hash doc-txexpr)) (define-values (doc-without-metas metas) (split-metas-to-hash doc-txexpr))
@ -66,21 +70,21 @@
(cond (cond
[(equal? (string->symbol here-ext) world:ptree-source-ext) world:reader-mode-ptree] [(equal? (string->symbol here-ext) world:ptree-source-ext) world:reader-mode-ptree]
[(equal? (string->symbol here-ext) world:markup-source-ext) world:reader-mode-markup] [(equal? (string->symbol here-ext) world:markup-source-ext) world:reader-mode-markup]
[(equal? (string->symbol here-ext) world:markdown-source-ext) world:reader-mode-markdown] [(equal? (string->symbol here-ext) world:markdown-source-ext) 'apply-root]
[else world:reader-mode-preproc])) [else world:reader-mode-preproc]))
reader-mode)) reader-mode))
(define doc (apply (cond (define doc (apply (cond
[(equal? parser-mode world:reader-mode-ptree) [(equal? parser-mode world:reader-mode-ptree)
(λ xs (decode (cons world:ptree-root-node xs) (λ xs (decode (cons world:ptree-root-node xs)
#:xexpr-elements-proc (λ(xs) (filter (compose1 not (def/c whitespace?)) xs))))] #:xexpr-elements-proc (λ(xs) (filter (compose1 not (def/c whitespace?)) xs))))]
;; 'root is the hook for the decoder function. ;; 'root is the hook for the decoder function.
;; If it's not a defined identifier, it just hits #%top and becomes `(root ,@body ...) ;; If it's not a defined identifier, it just hits #%top and becomes `(root ,@body ...)
[(equal? parser-mode world:reader-mode-markup) root] [(equal? parser-mode world:reader-mode-markup) root]
[(equal? parser-mode world:reader-mode-markdown) (compose1 (λ(x) `(root ,@x)) (dynamic-require 'markdown 'parse-markdown) string-append)] [(equal? parser-mode world:reader-mode-markdown) (λ xs `(root ,@xs))] ; do nothing, it's already decoded
;; for preprocessor output, just make a string. ;; for preprocessor output, just make a string.
[else (λ xs (apply string-append (map to-string xs)))]) [else (λ xs (apply string-append (map to-string xs)))])
(cdr doc-without-metas))) ;; cdr strips placeholder-root tag (cdr doc-without-metas))) ;; cdr strips placeholder-root tag
(provide metas doc (provide metas doc

Loading…
Cancel
Save