diff --git a/ox-hugo.el b/ox-hugo.el index 82316dce..541ef66b 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -578,6 +578,37 @@ This option can also be set on with the CREATOR keyword." :type '(string :tag "Creator string") :safe #'stringp) +(defcustom org-hugo-date-format "%Y-%m-%dT%T%z" + "Date format used for exporting date in front-matter. + +Front-matter date parameters: `date', `publishDate', +`expiryDate', `lastmod'. + +Note that the date format must match the date specification from +RFC3339. See `org-hugo--date-time-regexp' for reference and +examples of compatible date strings. + +Examples of RFC3339-compatible values for this variable: + + - %Y-%m-%dT%T%z (default) -> 2017-07-31T17:05:38-04:00 + - %Y-%m-%dT%T -> 2017-07-31T17:05:38 + - %Y-%m-%d -> 2017-07-31 + +Note that \"%Y-%m-%dT%T%z\" actually produces a date string like +\"2017-07-31T17:05:38-0400\"; notice the missing colon in the +time-zone portion. + +A colon is needed to separate the hours and minutes in the +time-zone as per RFC3339. This gets fixed in the +`org-hugo--format-date' function, so that \"%Y-%m-%dT%T%z\" now +results in a date string like \"2017-07-31T17:05:38-04:00\". + +See `format-time-string' to learn about the date format string +expression." + :group 'org-export-hugo + :type 'string + :safe #'stringp) + (defcustom org-hugo-langs-no-descr-in-code-fences '() "List of languages whose descriptors should not be exported to Markdown. @@ -670,6 +701,7 @@ newer." (:hugo-custom-front-matter "HUGO_CUSTOM_FRONT_MATTER" nil nil space) (:hugo-blackfriday "HUGO_BLACKFRIDAY" nil nil space) (:hugo-front-matter-key-replace "HUGO_FRONT_MATTER_KEY_REPLACE" nil nil space) + (:hugo-date-format "HUGO_DATE_FORMAT" nil org-hugo-date-format) ;; Front matter variables ;; https://gohugo.io/content-management/front-matter/#front-matter-variables @@ -1084,7 +1116,7 @@ Possible values of DATE-KEY are `:date', `:hugo-lastmod', Return nil if the retrieved date from INFO is nil or if the date cannot be formatted in Hugo-compatible format." - (let* ((hugo-date-fmt "%Y-%m-%dT%T%z") + (let* ((date-fmt (plist-get info :hugo-date-format)) (date-raw (cond ((equal date-key :date) (or @@ -1099,7 +1131,7 @@ cannot be formatted in Hugo-compatible format." ;; Else try to get it from the #+DATE keyword in ;; the Org file. (org-string-nw-p - (org-export-get-date info hugo-date-fmt)))) + (org-export-get-date info date-fmt)))) ((and (equal date-key :hugo-publishdate) (org-entry-get (point) "SCHEDULED")) ;; Get the date from the "SCHEDULED" property. @@ -1124,7 +1156,7 @@ cannot be formatted in Hugo-compatible format." ((stringp date-raw) (condition-case err (format-time-string - hugo-date-fmt + date-fmt (apply #'encode-time (org-parse-time-string date-raw))) (error ;; Set date-nocolon to nil if error @@ -1142,19 +1174,19 @@ cannot be formatted in Hugo-compatible format." ;; lastmod field. ((and (equal date-key :hugo-lastmod) (org-hugo--plist-get-true-p info :hugo-auto-set-lastmod)) - (format-time-string hugo-date-fmt (org-current-time))) + (format-time-string date-fmt (org-current-time))) ;; Else.. do nothing. (t nil))) ;; Hugo expects the date stamp in this format (RFC3339 -- See - ;; `org-hugo--date-time-regexp'.) i.e. requires a colon to - ;; separate the hours and minutes in the time-zone section of - ;; the date. + ;; `org-hugo--date-time-regexp'.) i.e. if the date contains + ;; the time-zone, a colon is required to separate the hours + ;; and minutes in the time-zone section. ;; 2017-07-06T14:59:45-04:00 - ;; But the "%Y-%m-%dT%T%z" format (`hugo-date-fmt') produces the date - ;; in this format: - ;; 2017-07-06T14:59:45-0400 (Note the missing colon) - ;; Below simply adds that colon. + ;; But by default the "%z" placeholder for time-zone (see + ;; `format-time-string') produces the zone time-string as + ;; "-0400" (Note the missing colon). Below simply adds a + ;; colon between "04" and "00" in that example. (date-str (and (stringp date-nocolon) (replace-regexp-in-string "\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)\\'" "\\1:\\2" date-nocolon)))) @@ -2682,6 +2714,7 @@ are \"toml\" and \"yaml\"." "HUGO_WEIGHT" "HUGO_RESOURCES" "HUGO_FRONT_MATTER_KEY_REPLACE" + "HUGO_DATE_FORMAT" "HUGO_AUTO_SET_LASTMOD"))) (mapcar (lambda (str) (concat "EXPORT_" str)) @@ -3120,6 +3153,7 @@ buffer and returned as a string in Org format." ,(format "|org-hugo-front-matter-format |%S|" org-hugo-front-matter-format) ,(format "|org-hugo-default-static-subdirectory-for-externals |%S|" org-hugo-default-static-subdirectory-for-externals) ,(format "|org-hugo-external-file-extensions-allowed-for-copying |%S|" org-hugo-external-file-extensions-allowed-for-copying) + ,(format "|org-hugo-date-format |%S|" org-hugo-date-format) ,(format "|org-hugo-langs-no-descr-in-code-fences |%S|" org-hugo-langs-no-descr-in-code-fences) ,(format "|org-hugo-front-matter-format |%S|" org-hugo-front-matter-format)) "\n")) diff --git a/test/site/content-org/all-posts.org b/test/site/content-org/all-posts.org index 6eab1fbf..a68d7f34 100644 --- a/test/site/content-org/all-posts.org +++ b/test/site/content-org/all-posts.org @@ -3391,6 +3391,24 @@ If a subtree has the =DEADLINE= [[https://orgmode.org/manual/Special-properties. By default, =C-c C-d= in Org adds the =DEADLINE= property to the current subtree. +** Customizing date format :date_format: +*** Date with time and time-zone (default) :time_zone:time: +:PROPERTIES: +:EXPORT_FILE_NAME: date-with-time-and-time-zone +:EXPORT_DATE: <2018-01-29 Mon> +:END: +*** Date with time :time: +:PROPERTIES: +:EXPORT_FILE_NAME: date-with-time +:EXPORT_DATE: <2018-01-29 Mon> +:EXPORT_HUGO_DATE_FORMAT: %Y-%m-%dT%T +:END: +*** Date with only date :only_date: +:PROPERTIES: +:EXPORT_FILE_NAME: date-with-only-date +:EXPORT_DATE: <2018-01-29 Mon> +:EXPORT_HUGO_DATE_FORMAT: %Y-%m-%d +:END: * Preserve filling option :filling: ** Filling is preserved :PROPERTIES: diff --git a/test/site/content/posts/date-with-only-date.md b/test/site/content/posts/date-with-only-date.md new file mode 100644 index 00000000..facbf10d --- /dev/null +++ b/test/site/content/posts/date-with-only-date.md @@ -0,0 +1,6 @@ ++++ +title = "Date with only date" +date = 2018-01-29 +tags = ["dates", "date-format", "only-date"] +draft = false ++++ diff --git a/test/site/content/posts/date-with-time-and-time-zone.md b/test/site/content/posts/date-with-time-and-time-zone.md new file mode 100644 index 00000000..273685a3 --- /dev/null +++ b/test/site/content/posts/date-with-time-and-time-zone.md @@ -0,0 +1,6 @@ ++++ +title = "Date with time and time-zone (default)" +date = 2018-01-29T00:00:00+00:00 +tags = ["dates", "date-format", "time-zone", "time"] +draft = false ++++ diff --git a/test/site/content/posts/date-with-time.md b/test/site/content/posts/date-with-time.md new file mode 100644 index 00000000..70b86681 --- /dev/null +++ b/test/site/content/posts/date-with-time.md @@ -0,0 +1,6 @@ ++++ +title = "Date with time" +date = 2018-01-29T00:00:00 +tags = ["dates", "date-format", "time"] +draft = false ++++