summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-04-11 23:34:23 -0400
committerDavid Thompson <dthompson2@worcester.edu>2015-04-11 23:34:23 -0400
commit44fdd16202c0b6e82922d8b5e869f399922ea056 (patch)
tree100215e894b1650e9837ea34e0c12d216f102b24
parent390980cf5ce1d83c52d16098417267ee92f2f6bf (diff)
post: Add posts/group-by-tag.
* haunt/post.scm (group-by-tag): New procedure.
-rw-r--r--haunt/post.scm15
1 files changed, 15 insertions, 0 deletions
diff --git a/haunt/post.scm b/haunt/post.scm
index 7c5051c..964ae6f 100644
--- a/haunt/post.scm
+++ b/haunt/post.scm
@@ -23,6 +23,7 @@
;;; Code:
(define-module (haunt post)
+ #:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-19)
#:use-module (haunt utils)
@@ -34,6 +35,7 @@
post-ref
post-slug
posts/reverse-chronological
+ posts/group-by-tag
register-metdata-parser!
parse-metadata))
@@ -66,6 +68,19 @@
(lambda (a b)
(time>? (post->time a) (post->time b)))))
+(define (posts/group-by-tag posts)
+ "Return an alist of tags mapped to the posts that used them."
+ (let ((table (make-hash-table)))
+ (for-each (lambda (post)
+ (for-each (lambda (tag)
+ (let ((current (hash-ref table tag)))
+ (if current
+ (hash-set! table tag (cons post current))
+ (hash-set! table tag (list post)))))
+ (or (post-ref post 'tags) '())))
+ posts)
+ (hash-fold alist-cons '() table)))
+
;;;
;;; Metadata
;;;