From ebd2db97d0c529faaaa4d14550832528ab9182aa Mon Sep 17 00:00:00 2001 From: Takaaki ISHIKAWA Date: Sun, 2 Sep 2018 01:38:28 +0900 Subject: [PATCH] Add org-hugo-suppress-lastmod-period https://github.com/kaushalmodi/ox-hugo/pull/197 --- doc/ox-hugo-manual.org | 26 ++++++++ ox-hugo.el | 60 +++++++++++++++++++ .../single-posts/suppress-lastmod.org | 18 ++++++ .../single-posts/unsuppress-lastmod.org | 16 +++++ test/site/content-org/suppress-lastmod.org | 58 ++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 test/site/content-org/single-posts/suppress-lastmod.org create mode 100644 test/site/content-org/single-posts/unsuppress-lastmod.org create mode 100644 test/site/content-org/suppress-lastmod.org diff --git a/doc/ox-hugo-manual.org b/doc/ox-hugo-manual.org index 99176cf..1424e78 100644 --- a/doc/ox-hugo-manual.org +++ b/doc/ox-hugo-manual.org @@ -985,6 +985,32 @@ Examples of RFC3339-compatible values for this variable: Do =C-h v org-hugo-date-format= for more information (within Emacs, once the =ox-hugo= package is loaded). + +***** =org-hugo-suppress-lastmod-period= + - Default value :: =0.0= + +A suppressing period not to write the =lastmod= item in the front matter. + +This variable is used to control the duration of the suppressing +period. If the value is 86400.0, the =lastmod= item will not be added to +the front matter within 24 hours from the initial exporting. + +=lastmod= would be exported when you initially change the Org TODO +state to =DONE= by saving the file automatically with the following +conditions. Some users may not prefer this behavior. + +| Variable | Value | +|----------------------------------+-------| +| org-hugo-auto-set-lastmod | t | +| org-hugo-auto-export-on-save | t | +| org-log-done | time | + +In such case, you can suppress =lastmod= in the front matter if you set +=org-hugo-suppress-lastmod-period= to =60.0= or other appropriate value. + +Note that to enable this feature, set =org-hugo-auto-set-lastmod= or +=EXPORT_HUGO_AUTO_SET_LASTMOD= to non-nil. + **** File-based Exports :PROPERTIES: :CUSTOM_ID: dates-file-based-exports diff --git a/ox-hugo.el b/ox-hugo.el index cceb4f6..5ada982 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -533,6 +533,19 @@ it is if `org-hugo-prefer-hyphen-in-tags' is nil." :type 'boolean :safe #'booleanp) +(defcustom org-hugo-suppress-lastmod-period 0.0 + "A suppressing period not to write the lastmod item in the front matter. +The default value is 0.0, which means that the lastmod item will be +added to front matter even if the entry is modified within just 0.1[s] +after the initial creation of the entry. +This variable is used to control the duration of the suppressing period. +If the value is 86400.0, the lastmod item will not be added to the front +matter within 24 hours from the initial exporting. +Note that to enable this feature, set `org-hugo-auto-set-lastmod' or +`EXPORT_HUGO_AUTO_SET_LASTMOD' to non-nil." + :group 'org-export-hugo + :type 'float) + (defcustom org-hugo-export-with-toc nil "When non-nil, Markdown format TOC will be inserted. @@ -1436,6 +1449,52 @@ INFO is a plist used as a communication channel." (setf (car found-key-cell) key-repl)))))))) data)) +;;;; Helper function to use `time-subtract' +(defun org-hugo--date-to-time (date &optional ignoretz) + "Return a valid time that can be passed to `time-subtract'. +If IGNORETZ is non-nil, the timezone is removed from DATE." + (let ((ignoretz (or ignoretz nil)) + (key (car date)) + (value (cdr date)) + (tz (format-time-string "%:z" (current-time)))) + (cond ((eq (length value) 10) ;; "2000-01-01" + (setq value (concat value "T00:00:00" (unless ignoretz tz )))) + ((eq (length value) 19) ;; "2000-01-01T00:00:00" + (setq value (concat value (unless ignoretz tz)))) + ((and (eq (length value) 25) ;; "2000-01-01T00:00:00+09:00" + ignoretz) + (setq value (substring value 0 19)))) + ;; (message "[ox-hugo auto-lastmod] output %s:\t%s" key value) + (let ((time (safe-date-to-time value))) + (if (equal '(0 0) time) + (user-error "Invalid %s value: \"%s\"" key (cdr date)) + time)))) + +;;;; Amend lastmod +(defun org-hugo--amend-lastmod (data info) + "Return updated DATA. +The lastmode value in DATA could be nil on the basis of +`org-hugo-suppress-lastmod-period'. +INFO is a plist used as a communication channel." + (when (and (or (org-hugo--plist-get-true-p info :hugo-auto-set-lastmod) + org-hugo-auto-set-lastmod) + (not (plist-get info :lastmod))) + (let ((date (assoc 'date data)) + (lastmod (assoc 'lastmod data))) + ;; (message "[ox-hugo auto-lastmod] input date=%s, lastmod=%s" + ;; (cdr date) (cdr lastmod)) + ;; (message (concat "[ox-hugo auto-lastmod] val" + ;; (format " org-hugo-suppress-lastmod-period=%s" + ;; org-hugo-suppress-lastmod-period))) + (when (and (cdr date) + (cdr lastmod) + (>= org-hugo-suppress-lastmod-period + (float-time (time-subtract + (org-hugo--date-to-time lastmod) + (org-hugo--date-to-time date))))) + (setf (cdr lastmod) nil)))) + data) + ;;;; TODO keywords (defun org-hugo--todo (todo info) "Format TODO keywords into HTML. @@ -3044,6 +3103,7 @@ INFO is a plist used as a communication channel." ;; (message "[fm categories DBG] %S" categories) ;; (message "[fm keywords DBG] %S" keywords) (setq data (org-hugo--replace-keys-maybe data info)) + (setq data (org-hugo--amend-lastmod data info)) (setq ret (org-hugo--gen-front-matter data fm-format)) (if (and (string= "toml" fm-format) (org-hugo--pandoc-citations-enabled-p info)) diff --git a/test/site/content-org/single-posts/suppress-lastmod.org b/test/site/content-org/single-posts/suppress-lastmod.org new file mode 100644 index 0000000..1047121 --- /dev/null +++ b/test/site/content-org/single-posts/suppress-lastmod.org @@ -0,0 +1,18 @@ +#+title: Single Post with suppressed lastmod +#+hugo_base_dir: ../../ +#+hugo_section: singles +#+date: [2118-09-01 Sat 05:08] +#+macro: doc [[https://ox-hugo.scripter.co/doc/$1][$2]] +#+hugo_auto_set_lastmod: t + +This post will not export =lastmod= because +=org-hugo-suppress-lastmod-period= is greater than the time +difference between =[2118-09-01 Sat 05:08]= and the current +date. + +* Local Variables :ARCHIVE: +# Local Variables: +# org-hugo-auto-set-lastmod: nil +# org-hugo-suppress-lastmod-period: 0.0 +# End: + diff --git a/test/site/content-org/single-posts/unsuppress-lastmod.org b/test/site/content-org/single-posts/unsuppress-lastmod.org new file mode 100644 index 0000000..74e4938 --- /dev/null +++ b/test/site/content-org/single-posts/unsuppress-lastmod.org @@ -0,0 +1,16 @@ +#+title: Single Post with unsuppressed lastmod +#+hugo_base_dir: ../../ +#+hugo_section: singles +#+date: [1999-09-01 Sat 05:11] +#+hugo_auto_set_lastmod: t + +This post will export =lastmod= because +=org-hugo-suppress-lastmod-period= is less than the time difference +between =[1999-09-01 Sat 05:11]= and the current date. + +* Local Variables :ARCHIVE: +# Local Variables: +# org-hugo-auto-set-lastmod: nil +# org-hugo-suppress-lastmod-period: 0.0 +# End: + diff --git a/test/site/content-org/suppress-lastmod.org b/test/site/content-org/suppress-lastmod.org new file mode 100644 index 0000000..e894754 --- /dev/null +++ b/test/site/content-org/suppress-lastmod.org @@ -0,0 +1,58 @@ +#+hugo_base_dir: ../ + +* org-hugo-suppress-lastmod-period +** DONE suppress lastmod with auto-set-lastmod + CLOSED: [2118-09-01 Wed 08:35] +:PROPERTIES: +:EXPORT_FILE_NAME: suppress-lastmod-in-subtree-with-auto-lastmod +:EXPORT_HUGO_AUTO_SET_LASTMOD: t +:END: + +This post will never export =lastmod= when you initially change the +Org TODO state to =DONE= by saving the file because +=org-hugo-suppress-lastmod-period= is always greater than the time +difference between =date= and =lastmod= in the following condition. + +| Variable | Value | +|----------------------------------+-------| +| org-hugo-suppress-lastmod-period | 60.0 | +| org-hugo-auto-set-lastmod | t | +| org-hugo-auto-export-on-save | t | +| org-log-done | time | + +For instance, auto generated =date= would be =2018-09-01T08:00:00+00:00= +and =lastmod= could be =2018-09-01T08:00:59+00:00=. The time +difference is less than =org-hugo-suppress-lastmod-period= so +=lastmod= filed will not be exported. But if you change something in +this post after the initial exporting, the =lastmod= will be exported +because the time difference will exceed +=org-hugo-suppress-lastmod-period=. + +** DONE unsuppress lastmod + CLOSED: [1999-09-01 Sat 02:57] +:PROPERTIES: +:EXPORT_FILE_NAME: unsuppress-lastmod-in-subtree +:EXPORT_HUGO_AUTO_SET_LASTMOD: t +:END: + +This post will export =lastmod= because +=org-hugo-suppress-lastmod-period= is less than the time difference +between =[1999-09-01 Sat 02:57]= and the current date. + +** DONE ignore suppress lastmod + CLOSED: [2018-09-05 Wed 14:19] +:PROPERTIES: +:EXPORT_FILE_NAME: ignore-suppress-lastmod-in-subtree +:EXPORT_HUGO_LASTMOD: 1999-09-05 +:EXPORT_HUGO_AUTO_SET_LASTMOD: nil +:END: + +This post will export =lastmod= because =EXPORT_HUGO_LASTMOD= is +provided even if =org-hugo-suppress-lastmod-period= is less than the +time difference between =[2018-09-05 Wed 14:19]= and =1999-09-05=. + +* Local Variables :ARCHIVE: +# Local Variables: +# org-hugo-auto-set-lastmod: nil +# org-hugo-suppress-lastmod-period: 0.0 +# End: