post: Add metadata parsers.

* haunt/post.scm (%metadata-parsers): New variable.  Register parser for
  'tags and 'date.
  (metadata-parser, register-metadata-parser!, parse-metadata): New
  procedures.
This commit is contained in:
David Thompson 2015-04-11 22:31:15 -04:00
parent 2464d9d06b
commit 84c65348ea
1 changed files with 28 additions and 1 deletions

View File

@ -25,6 +25,7 @@
(define-module (haunt post) (define-module (haunt post)
#:use-module (srfi srfi-9) #:use-module (srfi srfi-9)
#:use-module (srfi srfi-19) #:use-module (srfi srfi-19)
#:use-module (haunt utils)
#:export (make-post #:export (make-post
post? post?
post-file-name post-file-name
@ -32,7 +33,10 @@
post-metadata post-metadata
post-ref post-ref
post-slug post-slug
posts/reverse-chronological)) posts/reverse-chronological
register-metdata-parser!
parse-metadata))
(define-record-type <post> (define-record-type <post>
(make-post file-name metadata sxml) (make-post file-name metadata sxml)
@ -61,3 +65,26 @@
(sort posts (sort posts
(lambda (a b) (lambda (a b)
(time>? (post->time a) (post->time b))))) (time>? (post->time a) (post->time b)))))
;;;
;;; Metadata
;;;
(define %metadata-parsers
(make-hash-table))
(define (metadata-parser key)
(or (hash-ref %metadata-parsers key) identity))
(define (register-metadata-parser! name parser)
(hash-set! %metadata-parsers name parser))
(define (parse-metadata key value)
((metadata-parser key) value))
(register-metadata-parser!
'tags
(lambda (str)
(map string-trim-both (string-split str #\,))))
(register-metadata-parser! 'date string->date*)