branch: externals/tomelr
commit 1d65064ffa0c6e1d5e9cb14a31de8ada38dc3395
Author: Kaushal Modi <[email protected]>
Commit: Kaushal Modi <[email protected]>
feat: Recognize local date format YYYY-MM-DD
---
README.org | 49 ++++++++++++++++++--------------
test/tscalar.el | 10 +++++++
tomelr.el | 86 ++++++++++++++++++++++++++++++++++-----------------------
3 files changed, 90 insertions(+), 55 deletions(-)
diff --git a/README.org b/README.org
index 6583e6dcca..757e641f7b 100644
--- a/README.org
+++ b/README.org
@@ -24,8 +24,8 @@ specification defined below.
- [X] Integer
- [X] Float
- [X] String
+ - [X] Date
- [ ] Date + Time with Offset
- - [ ] Date
- [ ] Nil
- [ ] Arrays
- [ ] Array of Arrays
@@ -181,51 +181,58 @@ Violets are blue"""
: "str1": "Roses are red",
: "str2": "Roses are red\nViolets are blue"
: }
-*** Date + Time with Offset
-https://toml.io/en/v1.0.0#offset-date-time
+*** DONE Date
+CLOSED: [2022-04-28 Thu 22:40]
+https://toml.io/en/v1.0.0#local-date
**** S-expression
-#+begin_src emacs-lisp :eval no :noweb-ref scalar-odt
-'((odt1 . "1979-05-27T07:32:00Z")
- (odt2 . "1979-05-27T00:32:00-07:00")
- (odt3 . "1979-05-27T00:32:00.999999-07:00"))
+#+begin_src emacs-lisp :eval no :noweb-ref scalar-date
+'((ld1 . "1979-05-27"))
#+end_src
**** TOML
+#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
+(tomelr-encode
+ <<scalar-date>>)
+#+end_src
+
+#+RESULTS:
#+begin_src toml
-odt1 = 1979-05-27T07:32:00Z
-odt2 = 1979-05-27T00:32:00-07:00
-odt3 = 1979-05-27T00:32:00.999999-07:00
+ld1 = 1979-05-27
#+end_src
**** JSON Reference
#+begin_src emacs-lisp :noweb yes :exports results
(json-encode-pretty
- <<scalar-odt>>)
+ <<scalar-date>>)
#+end_src
#+RESULTS:
: {
-: "odt1": "1979-05-27T07:32:00Z",
-: "odt2": "1979-05-27T00:32:00-07:00",
-: "odt3": "1979-05-27T00:32:00.999999-07:00"
+: "ld1": "1979-05-27"
: }
-*** Date
-https://toml.io/en/v1.0.0#local-date
+*** Date + Time with Offset
+https://toml.io/en/v1.0.0#offset-date-time
**** S-expression
-#+begin_src emacs-lisp :eval no :noweb-ref scalar-date
-'((ld1 . "1979-05-27"))
+#+begin_src emacs-lisp :eval no :noweb-ref scalar-odt
+'((odt1 . "1979-05-27T07:32:00Z")
+ (odt2 . "1979-05-27T00:32:00-07:00")
+ (odt3 . "1979-05-27T00:32:00.999999-07:00"))
#+end_src
**** TOML
#+begin_src toml
-ld1 = 1979-05-27
+odt1 = 1979-05-27T07:32:00Z
+odt2 = 1979-05-27T00:32:00-07:00
+odt3 = 1979-05-27T00:32:00.999999-07:00
#+end_src
**** JSON Reference
#+begin_src emacs-lisp :noweb yes :exports results
(json-encode-pretty
- <<scalar-date>>)
+ <<scalar-odt>>)
#+end_src
#+RESULTS:
: {
-: "ld1": "1979-05-27"
+: "odt1": "1979-05-27T07:32:00Z",
+: "odt2": "1979-05-27T00:32:00-07:00",
+: "odt3": "1979-05-27T00:32:00.999999-07:00"
: }
** TOML Arrays: Lists
https://toml.io/en/v1.0.0#array
diff --git a/test/tscalar.el b/test/tscalar.el
index 27fd940d00..12ebb081fa 100644
--- a/test/tscalar.el
+++ b/test/tscalar.el
@@ -90,5 +90,15 @@ Violets are blue\"\"\""
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))
+;;;; Scalar - Local Date
+(ert-deftest test-scalar-date ()
+ (let ((inp '(((date . "1979-05-27"))))
+ (ref '("date = 1979-05-27"))
+ out)
+ (dolist (el inp)
+ (push (tomelr-encode el) out))
+ (should (equal ref (nreverse out)))))
+
+
(provide 'tscalar)
diff --git a/tomelr.el b/tomelr.el
index 342ba01a5d..28cf4b242d 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -60,6 +60,22 @@ Dictates repetitions of
`tomelr-encoding-default-indentation'.")
(defvar tomelr--print-keyval-separator " = "
"String used to separate key-value pairs during encoding.")
+(defvar tomelr--date-time-regexp
+ (concat "\\`[[:digit:]]\\{4\\}-[[:digit:]]\\{2\\}-[[:digit:]]\\{2\\}"
+ "\\(?:[T
][[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}\\(?:\\.[[:digit:]]+\\)*"
+ "\\(?:Z\\|[+-][[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}\\)*\\)*\\'")
+ "Regexp to match RFC 3339 formatted date-time with offset.
+
+- https://toml.io/en/v1.0.0#offset-date-time
+- https://tools.ietf.org/html/rfc3339#section-5.8
+
+Examples:
+ 1979-05-27
+ 1979-05-27T07:32:00Z
+ 1979-05-27 07:32:00Z
+ 1979-05-27T00:32:00-07:00
+ 1979-05-27T00:32:00.999999+04:00.")
+
;;; Error conditions
@@ -142,43 +158,45 @@ Return the same STRING passed as input."
(?\\ . ?\\)))
special-chars-re
begin-q end-q)
- ;; Use multi-line string quotation if the string contains a " char
- ;; or a newline.
- (if (string-match-p "\n\\|\"" string)
- (progn ;Triple quotation """STRING"""
- ;; From https://toml.io/en/v1.0.0#string, Any Unicode
- ;; character may be used except those that must be escaped:
- ;; backslash and the control characters other than tab, line
- ;; feed, and carriage return (U+0000 to U+0008, U+000B,
- ;; U+000C, U+000E to U+001F, U+007F).
- (setq special-chars-re (rx (in ?\\
- (?\u0000 . ?\u0008)
- ?\u000B ?\u000C
- (?\u000E . ?\u001F)
- ?\u007F)))
- (setq begin-q "\"\"\"\n")
- (setq end-q "\"\"\""))
- (progn ;Basic quotation "STRING"
- (setq special-chars-re (rx (in ?\" ?\\ cntrl ?\u007F))) ;cntrl is same
as (?\u0000 . ?\u001F)
- (push '(?\" . ?\") special-chars)
- (push '(?t . ?\t) special-chars) ;U+0009
- (push '(?n . ?\n) special-chars) ;U+000A
- (push '(?r . ?\r) special-chars) ;U+000D
- (setq begin-q "\"")
- (setq end-q begin-q)))
+ (cond
+ ((string-match-p tomelr--date-time-regexp string)) ;RFC 3339 formatted
date-time with offset
+ ;; Use multi-line string quotation if the string contains a " char
+ ;; or a newline - """STRING"""
+ ((string-match-p "\n\\|\"" string)
+ ;; From https://toml.io/en/v1.0.0#string, Any Unicode
+ ;; character may be used except those that must be escaped:
+ ;; backslash and the control characters other than tab, line
+ ;; feed, and carriage return (U+0000 to U+0008, U+000B,
+ ;; U+000C, U+000E to U+001F, U+007F).
+ (setq special-chars-re (rx (in ?\\
+ (?\u0000 . ?\u0008)
+ ?\u000B ?\u000C
+ (?\u000E . ?\u001F)
+ ?\u007F)))
+ (setq begin-q "\"\"\"\n")
+ (setq end-q "\"\"\""))
+ (t ;Basic quotation "STRING"
+ (setq special-chars-re (rx (in ?\" ?\\ cntrl ?\u007F))) ;cntrl is same
as (?\u0000 . ?\u001F)
+ (push '(?\" . ?\") special-chars)
+ (push '(?t . ?\t) special-chars) ;U+0009
+ (push '(?n . ?\n) special-chars) ;U+000A
+ (push '(?r . ?\r) special-chars) ;U+000D
+ (setq begin-q "\"")
+ (setq end-q begin-q)))
;; (message "[tomelr--print-string DBG] string = `%s'" string)
- (insert begin-q)
+ (and begin-q (insert begin-q))
(goto-char (prog1 (point) (princ string)))
(and trim-init-chars (delete-char trim-init-chars))
- (while (re-search-forward special-chars-re nil :noerror)
- (let ((char (preceding-char)))
- (delete-char -1)
- (insert ?\\ (or
- ;; Escape special characters
- (car (rassq char special-chars))
- ;; Fallback: UCS code point in \uNNNN form.
- (format "u%04x" char)))))
- (insert end-q)
+ (when special-chars-re
+ (while (re-search-forward special-chars-re nil :noerror)
+ (let ((char (preceding-char)))
+ (delete-char -1)
+ (insert ?\\ (or
+ ;; Escape special characters
+ (car (rassq char special-chars))
+ ;; Fallback: UCS code point in \uNNNN form.
+ (format "u%04x" char))))))
+ (and end-q (insert end-q))
string))
(defun tomelr-encode-string (string)