Skip to content

Commit

Permalink
Improve date-time parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Sep 12, 2017
1 parent 3e61b0b commit a486141
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.org
Expand Up @@ -278,6 +278,11 @@ have the exported Markdown files.
front-matter [[[https://github.com/kaushalmodi/ox-hugo/issues/68][68]]].
- Code optimization: Use of =org-entry-get= at places instead of
maintaining global variables.
** 0.1.2
- Make DateTime matching better; new internal variable
=org-hugo--date-time-regexp=. Earlier time zones ahead of UTC (with
=+= sign) were not detected as dates in =org-hugo--quote-string= and
thus were unnecessarily quoted.
* Debug
If the =ox-hugo= exports do not work as expected, or if you get an
error backtrace,
Expand Down
5 changes: 5 additions & 0 deletions doc/ox-hugo-manual.org
Expand Up @@ -29,6 +29,11 @@ Markdown]] flavor.
front-matter {{{issue(68)}}}.
- Code optimization: Use of =org-entry-get= at places instead of
maintaining global variables.
** 0.1.2
- Make DateTime matching better; new internal variable
=org-hugo--date-time-regexp=. Earlier time zones ahead of UTC (with
=+= sign) were not detected as dates in =org-hugo--quote-string= and
thus were unnecessarily quoted.
* Note to Future Contributors
:PROPERTIES:
:CUSTOM_ID: note-to-future-contributors
Expand Down
2 changes: 1 addition & 1 deletion doc/ox-hugo.wiki
26 changes: 26 additions & 0 deletions example-site/content-org/all-posts.org
Expand Up @@ -1384,6 +1384,32 @@ If such a property is non-nil, the value (time-stamp) of that is used
to set the =date= field in the exported front-matter.

- Reference :: [[http://orgmode.org/manual/Special-properties.html][(org) Special properties]] or =C-h i g (org) Special properties=
* Date Formats :date:
** Just date
:PROPERTIES:
:EXPORT_FILE_NAME: date-just-date
:EXPORT_DATE: 2017-09-12
:END:
** Date + Time
:PROPERTIES:
:EXPORT_FILE_NAME: date-plus-time
:EXPORT_DATE: 2017-09-12T16:10:00
:END:
** Date + Time (UTC)
:PROPERTIES:
:EXPORT_FILE_NAME: date-plus-time-utc
:EXPORT_DATE: 2017-09-12T16:10:00Z
:END:
** Date + Time (behind UTC)
:PROPERTIES:
:EXPORT_FILE_NAME: date-plus-time-minus-utc
:EXPORT_DATE: 2017-09-12T16:10:00-04:00
:END:
** Date + Time (after UTC)
:PROPERTIES:
:EXPORT_FILE_NAME: date-plus-time-plus-utc
:EXPORT_DATE: 2017-09-12T16:10:00+05:30
:END:
* TODO Pre-Draft State
:PROPERTIES:
:EXPORT_FILE_NAME: draft-state-todo
Expand Down
6 changes: 6 additions & 0 deletions example-site/content/posts/date-just-date.md
@@ -0,0 +1,6 @@
+++
title = "Just date"
date = 2017-09-12
tags = ["date"]
draft = false
+++
6 changes: 6 additions & 0 deletions example-site/content/posts/date-plus-time-minus-utc.md
@@ -0,0 +1,6 @@
+++
title = "Date + Time (behind UTC)"
date = 2017-09-12T16:10:00-04:00
tags = ["date"]
draft = false
+++
6 changes: 6 additions & 0 deletions example-site/content/posts/date-plus-time-plus-utc.md
@@ -0,0 +1,6 @@
+++
title = "Date + Time (after UTC)"
date = 2017-09-12T16:10:00+05:30
tags = ["date"]
draft = false
+++
6 changes: 6 additions & 0 deletions example-site/content/posts/date-plus-time-utc.md
@@ -0,0 +1,6 @@
+++
title = "Date + Time (UTC)"
date = 2017-09-12T16:10:00Z
tags = ["date"]
draft = false
+++
6 changes: 6 additions & 0 deletions example-site/content/posts/date-plus-time.md
@@ -0,0 +1,6 @@
+++
title = "Date + Time"
date = 2017-09-12T16:10:00
tags = ["date"]
draft = false
+++
24 changes: 20 additions & 4 deletions ox-hugo.el
Expand Up @@ -5,7 +5,7 @@
;; URL: https://github.com/kaushalmodi/ox-hugo
;; Package-Requires: ((emacs "24.5") (org "9.0"))
;; Keywords: Org, markdown, docs
;; Version: 0.1.1
;; Version: 0.1.2

;;; Commentary:

Expand Down Expand Up @@ -299,6 +299,20 @@ joinLines
This variable is for internal use only, and must not be
modified.")

(defvar org-hugo--date-time-regexp (concat "\\`[[:digit:]]\\{4\\}-[[:digit:]]\\{2\\}-[[:digit:]]\\{2\\}"
"\\(?:T[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}"
"\\(?:Z\\|[+-][[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}\\)*\\)*\\'")
"Regexp to match the Hugo time stamp strings.
Reference: https://tools.ietf.org/html/rfc3339#section-5.8
Examples:
2017-07-31
2017-07-31T17:05:38
2017-07-31T17:05:38Z
2017-07-31T17:05:38+04:00
2017-07-31T17:05:38-04:00.")


;;; User-Configurable Variables

Expand Down Expand Up @@ -884,7 +898,9 @@ string with just alphanumeric characters."
(string= "true" val)
(string= "false" val)
;; or if it is a date (date, publishDate, expiryDate, lastmod)
(string-match-p "\\`[-0-9T:]+\\'" val))
(string-match-p org-hugo--date-time-regexp val)
;; or if it is any number
(string-match-p "\\`[[:digit:]]+\\'" val))
val)
((and prefer-no-quotes
(string-match-p "\\`[a-zA-Z0-9]+\\'" val))
Expand Down Expand Up @@ -1116,7 +1132,7 @@ INFO is a plist used as a communication channel."
;; the Org file.
(org-string-nw-p (org-export-get-date info hugo-date-fmt))))
(date-nocolon (and (stringp date-raw)
(if (string-match-p "\\`[0-9T:-]+\\'" date-raw)
(if (string-match-p org-hugo--date-time-regexp date-raw)
;; If the set DATE is already in
;; Hugo-compatible date format, use it.
date-raw
Expand All @@ -1137,7 +1153,7 @@ INFO is a plist used as a communication channel."
;; If the set HUGO_LASTMOD is already in
;; Hugo-compatible lastmod format, use it.
((and (stringp lastmod-raw)
(string-match-p "\\`[0-9T:-]+\\'" lastmod-raw))
(string-match-p org-hugo--date-time-regexp lastmod-raw))
lastmod-raw)
;; Else if it's a string, try to parse the lastmod.
((stringp lastmod-raw)
Expand Down

0 comments on commit a486141

Please sign in to comment.