Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date format needs to be in RFC3339 format if setting front-matter to YAML #7

Closed
kaushalmodi opened this issue Jun 10, 2022 · 5 comments

Comments

@kaushalmodi
Copy link
Contributor

kaushalmodi commented Jun 10, 2022

If I create a note after setting:

  1. denote-front-matter-date-format to org-timestamp
  2. denote-file-type to markdown-yaml

, I get this:

---
title:      zyx
date:       [2022-06-10 Fri 11:25]
tags:       tag1
identifier: 20220610T112538
---

The date format is illegal in YAML. It will need to be converted to RFC3339 format.

Corrected date format:

date = 2022-06-10T11:25:38-04:00

I use this to convert Org date/time to RFC3339:

(defun org-hugo--org-date-time-to-rfc3339 (date-time info)
  "Convert DATE-TIME to RFC 3339 format.

DATE-TIME can be either Emacs format time list (example: return
value of `current-time'), or an Org date/time string.

INFO is a plist used as a communication channel."
  (let* ((date-time (if (stringp date-time)
                        (apply #'encode-time (org-parse-time-string date-time))
                      date-time))
         (date-nocolon (format-time-string
                        (plist-get info :hugo-date-format)
                        date-time)))
    ;; Hugo expects the date stamp in this format (RFC3339 -- See
    ;; `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 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.
    (and (stringp date-nocolon)
         (replace-regexp-in-string
          "\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)\\'" "\\1:\\2"
          date-nocolon))))
  • Above, (plist-get info :hugo-date-format) will retrieve this by default: "%Y-%m-%dT%T%z".
  • We don't have a date/time format available in Emacs-Lisp that provides the exact RFC3339 format (with colon between hour:min in the zone offset), so I have a workaround in the last part of the code above to fix that.

(reference)

References

RFC3339 date/time examples from the standard

More

The same RFC3339 format date/time will work for TOML too: https://toml.io/en/v1.0.0#offset-date-time

@protesilaos
Copy link
Owner

Oh, I see. Maybe we should turn denote-front-matter-date-format into
an alist or just do the right thing behind the scenes?

@kaushalmodi
Copy link
Contributor Author

just do the right thing behind the scenes?

+1

This, because if using YAML or TOML, we need to use the correct date/time type. Otherwise, if the user processes that Markdown file by a static site generator, the YAML/TOML parser in that tool would complain.

@protesilaos
Copy link
Owner

I implemented this but have not pushed the change yet. Diff below:

Notice in particular:

When denote-file-type specifies one of the Markdown flavors, we ignore this user option in order to enforce the RFC3339 specification (markdown is typically employed in static site generators as source code for web pages). However, when denote-front-matter-date-format has a string value, this rule is suspended: we use whatever the user wants.

 denote.el | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/denote.el b/denote.el
index fef2910..ee3999a 100644
--- a/denote.el
+++ b/denote.el
@@ -143,7 +143,14 @@ (defcustom denote-front-matter-date-format nil
 
 If a string, use it as the argument of `format-time-string'.
 Read the documentation of that function for valid format
-specifiers."
+specifiers.
+
+When `denote-file-type' specifies one of the Markdown flavors, we
+ignore this user option in order to enforce the RFC3339
+specification (markdown is typically employed in static site
+generators as source code for web pages).  However, when
+`denote-front-matter-date-format' has a string value, this rule
+is suspended: we use whatever the user wants."
   :type '(choice
           (const :tag "Just the date like 2022-06-08" nil)
           (const :tag "An inactive Org timestamp like [2022-06-08 Wed 06:19]" org-timestamp)
@@ -419,14 +426,25 @@ (defun denote--path (title keywords)
          (denote--sluggify title)
          (denote--file-extension))))
 
+;; Adapted from `org-hugo--org-date-time-to-rfc3339' in Kashual Modi's
+;; `ox-hugo' package: <https://github.com/kaushalmodi/ox-hugo>.
+(defun denote--date-rfc3339 ()
+  "Format date using the RFC3339 specification."
+  (replace-regexp-in-string
+   "\\([0-9]\\{2\\}\\)\\([0-9]\\{2\\}\\)\\'" "\\1:\\2"
+   (format-time-string "%FT%T%z")))
+
 (defun denote--date ()
   "Expand the date for a new note's front matter."
   (let ((format denote-front-matter-date-format))
     (cond
-     ((eq format 'org-timestamp)
-      (format-time-string "[%F %a %R]"))
      ((stringp format)
       (format-time-string format))
+     ((or (eq denote-file-type 'markdown-toml)
+          (eq denote-file-type 'markdown-yaml))
+      (denote--date-rfc3339))
+     ((eq format 'org-timestamp)
+      (format-time-string "[%F %a %R]"))
      (t (format-time-string "%F")))))
 
 (defun denote--prepare-note (title keywords &optional path)

protesilaos added a commit that referenced this issue Jun 11, 2022
Thanks ot Kaushal Modi for the feedback in issue 7 over at the GitHub
mirror: <#7>.
@kaushalmodi
Copy link
Contributor Author

I'll haven't got a chance to get to a computer to try it out, but I looked at the 254a6cd and it looks great! Thank you for all your efforts.

minor: My name got misspelled in a comment in that commit. But you don't need to mention my name in the code. Just the link to ox-hugo repo is OK. Thanks for the credit 😀.

protesilaos added a commit that referenced this issue Jun 11, 2022
@protesilaos
Copy link
Owner

My name got misspelled in a comment in that commit. But you don't need to mention my name in the code. Just the link to ox-hugo repo is OK.

Sorry about that! Updated it to only mention ox-hugo.

protesilaos added a commit to protesilaos/ef-themes that referenced this issue Aug 21, 2022
Thanks to Anthony Chavez for the feedback in issue 7 over at the GitHub
mirror: <protesilaos/denote#7>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants