diff options
-rw-r--r-- | doc/haunt.texi | 10 | ||||
-rw-r--r-- | haunt/reader.scm | 21 |
2 files changed, 26 insertions, 5 deletions
diff --git a/doc/haunt.texi b/doc/haunt.texi index da05b9a..a612f35 100644 --- a/doc/haunt.texi +++ b/doc/haunt.texi @@ -639,6 +639,16 @@ Return @code{#t} if @var{file-name} is a file supported by @var{reader}. @end deffn +@deffn {Procedure} reader-find? readers file-name +Return the first reader in @var{readers} that can parse +@var{file-name}, or @code{#f} if there is no such reader. +@end deffn + +@deffn {Procedure} reader-read reader file-name +Parse @var{file-name} using @var{reader} and return two values: an +alist of metadata and an SXML tree. +@end deffn + @deffn {Procedure} read-post @var{reader} @var{file-name} [@var{default-metadata}] Read a post object from @var{file-name} using @var{reader}, merging its metadata with @var{default-metadata}, or the empty list if not diff --git a/haunt/reader.scm b/haunt/reader.scm index cc9ed67..5f575d8 100644 --- a/haunt/reader.scm +++ b/haunt/reader.scm @@ -50,6 +50,8 @@ reader-matcher reader-proc reader-match? + reader-find + reader-read read-post read-posts @@ -67,10 +69,20 @@ "Return #t if FILE-NAME is a file supported by READER." ((reader-matcher reader) file-name)) +(define (reader-find readers file-name) + "Return the first reader in READERS that can parse FILE-NAME, or #f if +there is no such reader." + (find (cut reader-match? <> file-name) readers)) + +(define (reader-read reader file-name) + "Parse FILE-NAME using READER and return two values: an alist of +metadata and an SXML tree." + ((reader-proc reader) file-name)) + (define* (read-post reader file-name #:optional (default-metadata '())) "Read a post object from FILE-NAME using READER, merging its metadata with DEFAULT-METADATA." - (let-values (((metadata sxml) ((reader-proc reader) file-name))) + (let-values (((metadata sxml) (reader-read reader file-name))) (make-post file-name (append metadata default-metadata) sxml))) @@ -83,10 +95,9 @@ post." (define (leaf file-name stat memo) (if (keep? file-name) - (let ((reader (find (cut reader-match? <> file-name) readers))) - (if reader - (cons (read-post reader file-name default-metadata) memo) - (error "no reader available for post: " file-name))) + (match (reader-find readers file-name) + (reader (cons (read-post reader file-name default-metadata) memo)) + (#f (error "no reader available for post: " file-name))) memo)) (define (noop file-name stat result) |