post: Add helper procedures.

* haunt/post.scm (post-slug, posts/reverse-chronological): New procedures.
This commit is contained in:
David Thompson 2015-04-11 22:28:07 -04:00
parent c2191a68f2
commit 2464d9d06b
1 changed files with 21 additions and 1 deletions

View File

@ -24,12 +24,15 @@
(define-module (haunt post) (define-module (haunt post)
#:use-module (srfi srfi-9) #:use-module (srfi srfi-9)
#:use-module (srfi srfi-19)
#:export (make-post #:export (make-post
post? post?
post-file-name post-file-name
post-sxml post-sxml
post-metadata post-metadata
post-ref)) post-ref
post-slug
posts/reverse-chronological))
(define-record-type <post> (define-record-type <post>
(make-post file-name metadata sxml) (make-post file-name metadata sxml)
@ -41,3 +44,20 @@
(define (post-ref post key) (define (post-ref post key)
"Return the metadata corresponding to KEY within POST." "Return the metadata corresponding to KEY within POST."
(assq-ref (post-metadata post) key)) (assq-ref (post-metadata post) key))
(define (post-slug post)
"Transform the title of POST into a URL slug."
(string-join (map (lambda (s)
(string-filter char-set:letter+digit s))
(string-split (string-downcase (post-ref post 'title))
char-set:whitespace))
"-"))
(define (post->time post)
(date->time-utc (post-ref post 'date)))
(define (posts/reverse-chronological posts)
"Returns POSTS sorted in reverse chronological order."
(sort posts
(lambda (a b)
(time>? (post->time a) (post->time b)))))