summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-12-27 17:41:37 -0500
committerDavid Thompson <dthompson2@worcester.edu>2023-12-28 08:18:16 -0500
commit9ab3a50ead137ec0e6a81565a5ae9b1b320ed22d (patch)
treeacf2f73698f323b49ae4199201880004525a745a
parent04a0bab716edc23f6f7fc4f7d7ef002e8381892b (diff)
reader: Factor out new reader-find and reader-read procedures.
-rw-r--r--doc/haunt.texi10
-rw-r--r--haunt/reader.scm21
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)