site: Add a way to ignore post files that match a pattern.

This allows one to ignore files that would otherwise match a reader's
file extension, such as an Emacs temporary file like ".#foo.html".

* haunt/site.scm (<site>)[post-filter]: New field.
  (site-posts-filter): New accessor.
  (site): Add #:post-filter argument.
  (build-site): Pass post filter procedure to 'read-posts'.
  (make-file-name-filter, default-file-name-filter): New procedures.
* haunt/reader.scm (read-posts): Add 'ignore?' argument.
This commit is contained in:
David Thompson 2015-08-03 23:09:31 -04:00
parent ed271eb079
commit 1edd8aa07a
2 changed files with 36 additions and 10 deletions

View File

@ -63,16 +63,19 @@ metadata with DEFAULT-METADATA."
(append metadata default-metadata) (append metadata default-metadata)
sxml))) sxml)))
(define* (read-posts directory readers #:optional (default-metadata '())) (define* (read-posts directory ignore? readers #:optional (default-metadata '()))
"Read all of the files in DIRECTORY as post objects. The READERS "Read all of the files in DIRECTORY that do not match IGNORE? as
list must contain a matching reader for every post." post objects. The READERS list must contain a matching reader for
every post."
(define enter? (const #t)) (define enter? (const #t))
(define (leaf file-name stat memo) (define (leaf file-name stat memo)
(let ((reader (find (cut reader-match? <> file-name) readers))) (if (ignore? file-name)
(if reader memo
(cons (read-post reader file-name default-metadata) memo) (let ((reader (find (cut reader-match? <> file-name) readers)))
(error "no reader available for post: " file-name)))) (if reader
(cons (read-post reader file-name default-metadata) memo)
(error "no reader available for post: " file-name)))))
(define (noop file-name stat result) (define (noop file-name stat result)
result) result)

View File

@ -23,9 +23,11 @@
;;; Code: ;;; Code:
(define-module (haunt site) (define-module (haunt site)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9) #:use-module (srfi srfi-9)
#:use-module (srfi srfi-26) #:use-module (srfi srfi-26)
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (ice-9 regex)
#:use-module (haunt utils) #:use-module (haunt utils)
#:use-module (haunt reader) #:use-module (haunt reader)
#:use-module (haunt page) #:use-module (haunt page)
@ -36,21 +38,26 @@
site-title site-title
site-domain site-domain
site-posts-directory site-posts-directory
site-post-filter
site-build-directory site-build-directory
site-default-metadata site-default-metadata
site-make-slug site-make-slug
site-readers site-readers
site-builders site-builders
site-post-slug site-post-slug
build-site)) build-site
make-file-name-filter
default-file-name-filter))
(define-record-type <site> (define-record-type <site>
(make-site title domain posts-directory build-directory (make-site title domain posts-directory post-filter build-directory
default-metadata make-slug readers builders) default-metadata make-slug readers builders)
site? site?
(title site-title) (title site-title)
(domain site-domain) (domain site-domain)
(posts-directory site-posts-directory) (posts-directory site-posts-directory)
(post-filter site-post-filter)
(build-directory site-build-directory) (build-directory site-build-directory)
(default-metadata site-default-metadata) (default-metadata site-default-metadata)
(make-slug site-make-slug) (make-slug site-make-slug)
@ -61,6 +68,7 @@
(title "This Place is Haunted") (title "This Place is Haunted")
(domain "example.com") (domain "example.com")
(posts-directory "posts") (posts-directory "posts")
(post-filter default-file-name-filter)
(build-directory "site") (build-directory "site")
(default-metadata '()) (default-metadata '())
(make-slug post-slug) (make-slug post-slug)
@ -70,13 +78,15 @@
TITLE: The name of the site TITLE: The name of the site
POSTS-DIRECTORY: The directory where posts are found POSTS-DIRECTORY: The directory where posts are found
POST-FILTER: A predicate procedure that returns #t when a post file
should be ignored (ignores Emacs temp files by default)
BUILD-DIRECTORY: The directory that generated pages are stored in BUILD-DIRECTORY: The directory that generated pages are stored in
DEFAULT-METADATA: An alist of arbitrary default metadata for posts DEFAULT-METADATA: An alist of arbitrary default metadata for posts
whose keys are symbols whose keys are symbols
MAKE-SLUG: A procedure generating a file name slug from a post MAKE-SLUG: A procedure generating a file name slug from a post
READERS: A list of reader objects for processing posts READERS: A list of reader objects for processing posts
BUILDERS: A list of procedures for building pages from posts" BUILDERS: A list of procedures for building pages from posts"
(make-site title domain posts-directory build-directory (make-site title domain posts-directory post-filter build-directory
default-metadata make-slug readers builders)) default-metadata make-slug readers builders))
(define (site-post-slug site post) (define (site-post-slug site post)
@ -86,6 +96,7 @@ BUILDERS: A list of procedures for building pages from posts"
(define (build-site site) (define (build-site site)
"Build SITE in the appropriate build directory." "Build SITE in the appropriate build directory."
(let ((posts (read-posts (site-posts-directory site) (let ((posts (read-posts (site-posts-directory site)
(site-post-filter site)
(site-readers site) (site-readers site)
(site-default-metadata site))) (site-default-metadata site)))
(build-dir (absolute-file-name (site-build-directory site)))) (build-dir (absolute-file-name (site-build-directory site))))
@ -104,3 +115,15 @@ BUILDERS: A list of procedures for building pages from posts"
(obj (obj
(error "unrecognized site object: " obj))) (error "unrecognized site object: " obj)))
(flat-map (cut <> site posts) (site-builders site))))) (flat-map (cut <> site posts) (site-builders site)))))
(define (make-file-name-filter patterns)
(let ((patterns (map make-regexp patterns)))
(lambda (file-name)
(any (lambda (regexp)
(regexp-match?
(regexp-exec regexp (basename file-name))))
patterns))))
;; Filter out Emacs temporary files by default.
(define default-file-name-filter
(make-file-name-filter '("^\\.#")))