page: Ensure output directory exists before writing.
* haunt/utils.scm (mkdir-p): New procedure. * haunt/page.scm (write-page): Create missing directories before writing.
This commit is contained in:
		@@ -26,7 +26,7 @@
 | 
				
			|||||||
  #:use-module (ice-9 match)
 | 
					  #:use-module (ice-9 match)
 | 
				
			||||||
  #:use-module (srfi srfi-9)
 | 
					  #:use-module (srfi srfi-9)
 | 
				
			||||||
  #:use-module (srfi srfi-26)
 | 
					  #:use-module (srfi srfi-26)
 | 
				
			||||||
  #:use-module (haunt build html)
 | 
					  #:use-module (haunt utils)
 | 
				
			||||||
  #:export (make-page
 | 
					  #:export (make-page
 | 
				
			||||||
            page?
 | 
					            page?
 | 
				
			||||||
            page-file-name
 | 
					            page-file-name
 | 
				
			||||||
@@ -46,4 +46,5 @@
 | 
				
			|||||||
  (match page
 | 
					  (match page
 | 
				
			||||||
    (($ <page> file-name contents writer)
 | 
					    (($ <page> file-name contents writer)
 | 
				
			||||||
     (let ((output (string-append output-directory "/" file-name)))
 | 
					     (let ((output (string-append output-directory "/" file-name)))
 | 
				
			||||||
 | 
					       (mkdir-p (dirname output))
 | 
				
			||||||
       (call-with-output-file output (cut writer contents <>))))))
 | 
					       (call-with-output-file output (cut writer contents <>))))))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,6 @@
 | 
				
			|||||||
;;; Haunt --- Static site generator for GNU Guile
 | 
					;;; Haunt --- Static site generator for GNU Guile
 | 
				
			||||||
;;; Copyright © 2015 David Thompson <davet@gnu.org>
 | 
					;;; Copyright © 2015 David Thompson <davet@gnu.org>
 | 
				
			||||||
 | 
					;;; Copyright © 2012 Ludovic Courtès <ludo@gnu.org>
 | 
				
			||||||
;;;
 | 
					;;;
 | 
				
			||||||
;;; This file is part of Haunt.
 | 
					;;; This file is part of Haunt.
 | 
				
			||||||
;;;
 | 
					;;;
 | 
				
			||||||
@@ -31,7 +32,8 @@
 | 
				
			|||||||
            flat-map
 | 
					            flat-map
 | 
				
			||||||
            string-split-at
 | 
					            string-split-at
 | 
				
			||||||
            absolute-file-name
 | 
					            absolute-file-name
 | 
				
			||||||
            clean-directory))
 | 
					            clean-directory
 | 
				
			||||||
 | 
					            mkdir-p))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(define* (flatten lst #:optional depth)
 | 
					(define* (flatten lst #:optional depth)
 | 
				
			||||||
  "Return a list that recursively concatenates the sub-lists of LST,
 | 
					  "Return a list that recursively concatenates the sub-lists of LST,
 | 
				
			||||||
@@ -70,3 +72,29 @@ flattened."
 | 
				
			|||||||
    #t)
 | 
					    #t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  (ftw dir delete-other-files))
 | 
					  (ftw dir delete-other-files))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;; Written by Ludovic Courtès for GNU Guix.
 | 
				
			||||||
 | 
					(define (mkdir-p dir)
 | 
				
			||||||
 | 
					  "Create directory DIR and all its ancestors."
 | 
				
			||||||
 | 
					  (define absolute?
 | 
				
			||||||
 | 
					    (string-prefix? "/" dir))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  (define not-slash
 | 
				
			||||||
 | 
					    (char-set-complement (char-set #\/)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  (let loop ((components (string-tokenize dir not-slash))
 | 
				
			||||||
 | 
					             (root       (if absolute?
 | 
				
			||||||
 | 
					                             ""
 | 
				
			||||||
 | 
					                             ".")))
 | 
				
			||||||
 | 
					    (match components
 | 
				
			||||||
 | 
					      ((head tail ...)
 | 
				
			||||||
 | 
					       (let ((path (string-append root "/" head)))
 | 
				
			||||||
 | 
					         (catch 'system-error
 | 
				
			||||||
 | 
					           (lambda ()
 | 
				
			||||||
 | 
					             (mkdir path)
 | 
				
			||||||
 | 
					             (loop tail path))
 | 
				
			||||||
 | 
					           (lambda args
 | 
				
			||||||
 | 
					             (if (= EEXIST (system-error-errno args))
 | 
				
			||||||
 | 
					                 (loop tail path)
 | 
				
			||||||
 | 
					                 (apply throw args))))))
 | 
				
			||||||
 | 
					      (() #t))))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user