From c86aabf53bdbce9819360ebb1caae2d80fc0f685 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Fri, 31 Jul 2020 02:52:57 +0530 Subject: Introduce ennum-directory-files. ennum-directory-files is a more convenient around directory-files and directory-files-recursively. * ennum.el (ennum-directory-files): New function. (ennum-posts, ennum-video-poster, ennum-publish): Use ennum-directory-files. (ennum-publish-generic): Accept files that are not prefixed by other-files-directory. --- ennum.el | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) (limited to 'ennum.el') diff --git a/ennum.el b/ennum.el index 2ca8dc2..aadd688 100644 --- a/ennum.el +++ b/ennum.el @@ -25,11 +25,11 @@ inputs outputs publish) (defun ennum-posts (posts-directory) - (sort (seq-map 'ennum-read-post - (file-expand-wildcards - (concat (file-name-as-directory - (ennum-setting :posts-directory)) - "*.org"))) + (sort (ennum--filter-map (lambda (file) + (when (string= (file-name-extension file) "org") + (ennum-read-post + (ennum--expand-relative file (ennum-setting :posts-directory))))) + (ennum-directory-files (ennum-setting :posts-directory))) 'ennum-later-post-p)) (defun ennum-later-post-p (post1 post2) @@ -105,6 +105,23 @@ last form in BODY." (insert-file-contents ,file) ,@body)) +(defun ennum-directory-files (directory &optional full include-directories) + "Return recursively the list of all files under DIRECTORY. Files are +returned in depth first order. + +If FULL is non-nil, absolute file names are returned. Else, the +file names are relative to DIRECTORY. If INCLUDE-DIRECTORIES is +non-nil, include directories in the output." + (let ((files (directory-files-recursively + directory (rx anything) include-directories))) + (if full + files + (seq-map (apply-partially 'string-remove-prefix + ;; Expand directory in case it is a + ;; relative path. + (file-name-as-directory (expand-file-name directory))) + files)))) + (defun ennum--org-output-filename (filename) (concat (file-name-sans-extension filename) ".html")) @@ -155,25 +172,24 @@ last form in BODY." (defun ennum-publish-generic (other-files-directory file) (ennum-make-operation - :inputs (list file) + :inputs (list (ennum--expand-relative file other-files-directory)) :outputs - (list (string-remove-prefix - (file-name-as-directory other-files-directory) - (pcase (file-name-extension file) - ("org" (ennum--org-output-filename file)) - (_ file)))) + (list (pcase (file-name-extension file) + ("org" (ennum--org-output-filename file)) + (_ file))) :publish (lambda (output-file) (pcase (file-name-extension file) ("org" (ennum-with-file-contents file (org-export-to-file 'html output-file))) - (_ (ennum-copy file output-file)))))) + (_ (ennum-copy (ennum--expand-relative file other-files-directory) + output-file)))))) (defun ennum-video-poster (video) - (pcase (directory-files (ennum-setting :images-directory) nil - (concat (file-name-sans-extension video) - "\\.\\(jpg\\|png\\)$")) - (`(,poster . ,_) poster) - (`() (user-error "Poster for %s not found" video)))) + (or (seq-find (lambda (file) + (string= (file-name-base file) + (file-name-base video))) + (ennum-directory-files (ennum-setting :images-directory))) + (user-error "Poster for %s not found" video))) (defun ennum-add-tongue-suffix (filename tongue) (pcase tongue @@ -500,9 +516,7 @@ as keys. Keys are compared using `equal'." ;; Publish other files (when-let ((other-files-directory (ennum-setting :other-files-directory))) (seq-map (apply-partially 'ennum-publish-generic other-files-directory) - (seq-map (apply-partially 'string-remove-prefix - (file-name-as-directory (expand-file-name default-directory))) - (directory-files-recursively other-files-directory ".")))))) + (ennum-directory-files other-files-directory))))) ;; Replace old output directory (let ((output (ennum-setting :output-directory))) (delete-directory output t) -- cgit v1.2.3