summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-04-11 10:53:17 -0400
committerDavid Thompson <dthompson2@worcester.edu>2015-04-11 15:10:45 -0400
commita45269f52365ce15f74c7998343f6d03fc7f8871 (patch)
tree36df7d3084fa37bd57538d1af06a39f04ec8f207
parent0b5bdd9f01705c4de45d375a3573670a88826aff (diff)
reader: Add html-reader.
* haunt/reader.scm (read-html-post): New procedure. (html-reader): New variable.
-rw-r--r--haunt/reader.scm23
1 files changed, 23 insertions, 0 deletions
diff --git a/haunt/reader.scm b/haunt/reader.scm
index 0f853c1..fe13456 100644
--- a/haunt/reader.scm
+++ b/haunt/reader.scm
@@ -26,8 +26,12 @@
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-11)
+ #:use-module (srfi srfi-26)
+ #:use-module (ice-9 match)
#:use-module (ice-9 regex)
+ #:use-module (ice-9 rdelim)
#:use-module (haunt post)
+ #:use-module (haunt utils)
#:export (make-reader
reader?
reader-matcher
@@ -73,3 +77,22 @@ metadata with DEFAULT-METADATA."
(let ((contents (load file-name)))
(values (alist-delete 'content contents eq?)
(assq-ref contents 'content))))))
+
+(define (read-html-post port)
+ (let loop ((metadata '()))
+ (let ((line (read-line port)))
+ (cond
+ ((eof-object? line)
+ (error "end of file while reading metadata: " (port-filename port)))
+ ((string=? line "---")
+ (values metadata `(raw ,(read-string port))))
+ (else
+ (match (map string-trim-both (string-split-at line #\:))
+ ((key value)
+ (loop (cons (cons (string->symbol key) value)
+ metadata)))
+ (_ (error "invalid metadata format: " line))))))))
+
+(define html-reader
+ (make-reader (make-file-extension-matcher "html")
+ (cut call-with-input-file <> read-html-post)))