From 4e8104f11f81ca2a5e7f800f4601662508f6f885 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Tue, 25 Jul 2023 12:21:16 -0700 Subject: [PATCH] Only generate blog index when necessary --- publish.el | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/publish.el b/publish.el index 1b3bac2..e06b0f1 100644 --- a/publish.el +++ b/publish.el @@ -52,7 +52,9 @@ time (if (= time 1) "" "s"))) -(defun publish-get-summary-from-file (file props) +(defun publish-extract-summary-from-file (file props) + "Extract a summary from FILE. +PROPS is used as an aid in getting the right information from the file." (format "* %s\n:PROPERTIES:\n:CUSTOM_ID: %s\n:PUBDATE: %s\n:RSS_PERMALINK: %s\n:END:\n\n%s\n\n[[file:%s][Read More]]\n\n" (car (org-publish-find-property file :title props)) (file-name-nondirectory file) @@ -66,19 +68,43 @@ (list file))) (file-name-nondirectory file))) +(defun publish-get-latest-modified-time (files) + "Get the latest modification time of any file from the list FILES." + (car + (last + (sort (mapcar #'org-publish-cache-mtime-of-src files) #'time-less-p)))) + +(defun publish-time>= (a b) + "Check if time A is greater than or equal to time B." + (not (time-less-p a b))) + +(defun publish-empty-time () + "Get an empty time value." + (let ((current-time (current-time))) + (time-subtract current-time current-time))) + (defun publish-generate-index (props) "Generate an index from my posts. Argument PROPS ." - (let ((files (directory-files "posts/" t (rx bos (= 8 digit) "-" (= 4 digit) "-" (one-or-more nonl) (not "~") eos)))) - (with-temp-buffer - (insert "#+title: ryuslash's blog\n") - (insert "#+options: num:nil\n") - (insert "\n") - (apply 'insert - (mapcar (lambda (file) (publish-get-summary-from-file file props)) - (take 30 (reverse files)))) - (write-file "posts/index.org")))) + (let* ((index-file (expand-file-name "posts/index.org")) + (index-generated-time (or (and (file-exists-p index-file) + (org-publish-cache-mtime-of-src index-file)) + (publish-empty-time))) + (files (directory-files "posts/" t (rx bos (= 8 digit) "-" (= 4 digit) "-" (one-or-more nonl) (not "~") eos))) + (latest-modification-time (publish-get-latest-modified-time files))) + (if (publish-time>= index-generated-time latest-modification-time) + (message "Not generating index...") + (progn + (message "Generating index...") + (with-temp-buffer + (insert "#+title: ryuslash's blog\n") + (insert "#+options: num:nil\n") + (insert "\n") + (apply 'insert + (mapcar (lambda (file) (publish-extract-summary-from-file file props)) + (take 30 (reverse files)))) + (write-file "posts/index.org")))))) (setq org-export-exclude-tags '("noexport" "draft"))