From 44fdd16202c0b6e82922d8b5e869f399922ea056 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 11 Apr 2015 23:34:23 -0400 Subject: post: Add posts/group-by-tag. * haunt/post.scm (group-by-tag): New procedure. --- haunt/post.scm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 ;;; -- cgit v1.2.3