Skip to content

Commit

Permalink
Support customizing date format in the date front-matter parameters
Browse files Browse the repository at this point in the history
New keyword HUGO_DATE_FORMAT and defcustom `org-hugo-date-format'.

Fixes #121
  • Loading branch information
kaushalmodi committed Jan 29, 2018
1 parent a88df9b commit d9348c1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
56 changes: 45 additions & 11 deletions ox-hugo.el
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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))))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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"))
Expand Down
18 changes: 18 additions & 0 deletions test/site/content-org/all-posts.org
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions 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
+++
6 changes: 6 additions & 0 deletions 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
+++
6 changes: 6 additions & 0 deletions 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
+++

0 comments on commit d9348c1

Please sign in to comment.