From dac164892d549b8e4715518c77503efe0b65b5ba Mon Sep 17 00:00:00 2001 From: Filip Lajszczak Date: Sun, 5 Nov 2023 16:44:11 +0000 Subject: post: Add convenience procedures for post author, tags, and title. Author, tags and title metadata are commonly used, * haunt/post.scm (post-author, post-tags, post-title): New procedures. * tests/post.scm ("post-author, author metadata", "post-tags, tags metadata", "post-title, title metadata", "post-author, no metadata", "post-tags, no metadata", "post-title, no metadata"): New tests. * doc/haunt.texi (Posts): Docs for new procedures. --- doc/haunt.texi | 15 +++++++++++++++ haunt/post.scm | 20 ++++++++++++++++++-- tests/post.scm | 26 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/doc/haunt.texi b/doc/haunt.texi index 23f45d8..3deba4c 100644 --- a/doc/haunt.texi +++ b/doc/haunt.texi @@ -530,6 +530,21 @@ specified. Sort @var{posts}, a list of posts, in reverse chronological order. @end deffn +@deffn {Scheme Procedure} post-author @var{post} +Return the author of @var{post}, or @code{#f} if no author is +specified. +@end deffn + +@deffn {Scheme Procedure} post-tags @var{post} +Return list of tags for @var{post}, or the empty list if no tags are +specified. +@end deffn + +@deffn {Scheme Procedure} post-title @var{post} +Return the title of @var{post}, or @code{#f} if no title is +specified. +@end deffn + @deffn {Scheme Procedure} posts/group-by-tag @var{posts} Create an association list of tags mapped to the posts in the list @var{posts} that used them. diff --git a/haunt/post.scm b/haunt/post.scm index ebe2689..d15efd2 100644 --- a/haunt/post.scm +++ b/haunt/post.scm @@ -40,6 +40,9 @@ post-slug %default-date post-date + post-author + post-tags + post-title posts/reverse-chronological posts/group-by-tag @@ -73,7 +76,7 @@ (or (post-ref post 'slug) (string-join (map (lambda (s) (string-filter char-set:slug s)) - (string-split (string-downcase (post-ref post 'title)) + (string-split (string-downcase (post-title post)) char-set:whitespace)) "-"))) @@ -94,6 +97,19 @@ specified." (lambda (a b) (time>? (post-time a) (post-time b))))) +(define (post-author post) + "Return the author of POST, or #f if no author is specified." + (post-ref post 'author)) + +(define (post-tags post) + "Return list of tags for POST, or the empty list if no tags are +specified." + (or (post-ref post 'tags) '())) + +(define (post-title post) + "Return the title of POST, or #f if no title is specified." + (post-ref post 'title)) + (define (posts/group-by-tag posts) "Return an alist of tags mapped to the posts that used them." (let ((table (make-hash-table))) @@ -103,7 +119,7 @@ specified." (if current (hash-set! table tag (cons post current)) (hash-set! table tag (list post))))) - (or (post-ref post 'tags) '()))) + (post-tags post))) posts) (hash-fold alist-cons '() table))) diff --git a/tests/post.scm b/tests/post.scm index db1a82c..d01ed84 100644 --- a/tests/post.scm +++ b/tests/post.scm @@ -53,6 +53,32 @@ (list newest middle oldest) (posts/reverse-chronological (list oldest newest middle)))) + (let ((example-post (make-post "foo.skr" + '((author . "Eva Luator") + (tags "foo" "bar" "baz") + (title . "Hello, world!")) + '()))) + (test-equal "post-author, author metadata" + "Eva Luator" + (post-author example-post)) + (test-equal "post-tags, tags metadata" + '("foo" "bar" "baz") + (post-tags example-post)) + (test-equal "post-title, title metadata" + "Hello, world!" + (post-title example-post))) + + (let ((no-metadata-post (make-post "foo.skr" '() '()))) + (test-equal "post-author, no metadata" + #f + (post-author no-metadata-post)) + (test-equal "post-tags, no metadata" + '() + (post-tags no-metadata-post)) + (test-equal "post-title, no metadata" + #f + (post-title no-metadata-post))) + (let ((foo-post (make-post "foo.skr" '((tags "foo")) '())) (another-foo-post (make-post "another-foo.skr" '((tags "foo")) '())) (bar-post (make-post "bar.skr" '((tags "bar")) '()))) -- cgit v1.2.3