branch: externals/tomelr commit 044b5e1a042aa1058792af607b1d7cd4cc70d144 Author: Kaushal Modi <kaushal.m...@gmail.com> Commit: Kaushal Modi <kaushal.m...@gmail.com>
fix: TT with key with array value are detected correctly Use json-alist-p and json-plist-p for TOML Table detection. This uncomplicated the TOML Table logic quite a bit. Caveat: Lists of plist need to now use the vector notation (this might get fixed in a future commit). --- README.org | 68 ++++++++++++++++++++++++++++++----------------------- test/tinternal.el | 37 ++++++++++++++++++++++------- test/tjson-utils.el | 1 + test/ttable.el | 18 ++++++++++++++ tomelr.el | 55 +++---------------------------------------- 5 files changed, 88 insertions(+), 91 deletions(-) diff --git a/README.org b/README.org index d741e99b22..6cc67ef18e 100644 --- a/README.org +++ b/README.org @@ -681,7 +681,8 @@ CLOSED: [2022-04-30 Sat 01:32] } #+end_example -** Combinations of all of the above +** DONE Combinations of all of the above +CLOSED: [2022-05-02 Mon 10:29] *** S-expression #+begin_src emacs-lisp :eval no :noweb-ref medley '((title . "Keyword Collection") @@ -738,17 +739,23 @@ CLOSED: [2022-04-30 Sat 01:32] (title . "JPEG Image #:counter"))))) #+end_src *** TOML +#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml +(tomelr-encode + <<medley>>) +#+end_src + +#+RESULTS: #+begin_src toml title = "Keyword Collection" -author = ["firstname1 lastname1", "firstname2 lastname2", "firstname3 lastname3"] -aliases = ["/posts/keyword-concatenation", "/posts/keyword-merging"] -images = ["image 1", "image 2"] -keywords = ["keyword1", "keyword2", "three word keywords3"] -outputs = ["html", "json"] -series = ["series 1", "series 2"] -tags = ["mega front-matter", "keys", "collection", "concatenation", "merging"] -categories = ["cat1", "cat2"] -videos = ["video 1", "video 2"] +author = [ "firstname1 lastname1", "firstname2 lastname2", "firstname3 lastname3" ] +aliases = [ "/posts/keyword-concatenation", "/posts/keyword-merging" ] +images = [ "image 1", "image 2" ] +keywords = [ "keyword1", "keyword2", "three word keywords3" ] +outputs = [ "html", "json" ] +series = [ "series 1", "series 2" ] +tags = [ "mega front-matter", "keys", "collection", "concatenation", "merging" ] +categories = [ "cat1", "cat2" ] +videos = [ "video 1", "video 2" ] draft = false categories_weight = 999 tags_weight = 88 @@ -758,40 +765,41 @@ mybaz = "zoo" alpha = 1 beta = "two words" gamma = 10 -animals = ["dog", "cat", "penguin", "mountain gorilla"] -strings-symbols = ["abc", "def", "two words"] -integers = [123, -5, 17, 1_234] -floats = [12.3, -5.0, -1.7e-05] -booleans = [true, false] +animals = [ "dog", "cat", "penguin", "mountain gorilla" ] +strings-symbols = [ "abc", "def", "two words" ] +integers = [ 123, -5, 17, 1234 ] +floats = [ 12.3, -5.0, -1.7e-05 ] +booleans = [ true, false ] [dog] legs = 4 eyes = 2 - friends = ["poo", "boo"] + friends = [ "poo", "boo" ] [header] image = "projects/Readingabook.jpg" - caption = "stay hungry, stay foolish" + caption = "stay hungry stay foolish" [collection] nothing = false nonnil = true - animals = ["dog", "cat", "penguin", "mountain gorilla"] - strings-symbols = ["abc", "def", "two words"] - integers = [123, -5, 17, 1_234] - floats = [12.3, -5.0, -1.7e-05] - booleans = [true, false] -[menu.foo] - identifier = "keyword-collection" - weight = 10 + animals = [ "dog", "cat", "penguin", "mountain gorilla" ] + strings-symbols = [ "abc", "def", "two words" ] + integers = [ 123, -5, 17, 1234 ] + floats = [ 12.3, -5.0, -1.7e-05 ] + booleans = [ true, false ] +[menu] + [menu.foo] + identifier = "keyword-collection" + weight = 10 [[resources]] src = "*.png" name = "my-cool-image-:counter" title = "The Image #:counter" [resources.params] foo = "bar" - floats = [12.3, -5.0, -1.7e-05] - strings-symbols = ["abc", "def", "two words"] - animals = ["dog", "cat", "penguin", "mountain gorilla"] - integers = [123, -5, 17, 1_234] - booleans = [true, false] + floats = [ 12.3, -5.0, -1.7e-05 ] + strings-symbols = [ "abc", "def", "two words" ] + animals = [ "dog", "cat", "penguin", "mountain gorilla" ] + integers = [ 123, -5, 17, 1234 ] + booleans = [ true, false ] byline = "bep" [[resources]] src = "image-4.png" diff --git a/test/tinternal.el b/test/tinternal.el index 187b5dbe3e..554305b966 100644 --- a/test/tinternal.el +++ b/test/tinternal.el @@ -43,37 +43,56 @@ (dolist (el inp) (should (equal t (tomelr--toml-table-p el)))))) -(ert-deftest test-internal-invalid-toml-tables () +(ert-deftest test-internal-toml-table-false () (let ((inp '( (a 1) - ((:a 1)) ;This is an array of TOML table + ;; FIXME: TTA with plist and list notation doesn't get recognized as one + ;; ((:a 1)) ;This is an array of TOML table + ;; + [(:a 1)] ;This is an array of TOML table (((a . 1))) ;This is an array of TOML table ))) (dolist (el inp) (should (equal nil (tomelr--toml-table-p el)))))) ;;;; tomelr--toml-table-array-p -(ert-deftest test-internal-valid-tta () +(ert-deftest test-internal-tta-alist-true () (let ((inp '( ;; TTA with 1 table of 1 key-val pair (((a . 1))) - ((:a 1)) ;; TTA with 2 tables of 2 key-val pairs (((a . 1) (b . 2)) ((a . 100) (b . 200))) - ((:a 1 :b 2) - (:a 100 :b 200)) ;; TTA with 1 table nesting another TTA (((a . (((b . 2)))))) - ((:a ((:b 2)))) - ;; TTA with vector notation + ))) + (dolist (el inp) + (should (equal t (tomelr--toml-table-array-p el)))))) + +(ert-deftest test-internal-tta-plist-vector-notation-true () + (let ((inp '( + ;; TTA with 1 table of 1 key-val pair + [(:a 1)] + ;; TTA with 1 table nesting another TTA [(:a 100 :b "foo") (:a 200 :b "bar")] ))) (dolist (el inp) (should (equal t (tomelr--toml-table-array-p el)))))) -(ert-deftest test-internal-invalid-tta () +;; FIXME: TTA with list notation + plist doesn't work +;; (ert-deftest test-internal-tta-plist-list-notation-true () +;; (let ((inp '( +;; ;; TTA with 1 table of 1 key-val pair +;; ((:a 1)) +;; ;; TTA with 1 table nesting another TTA +;; ((:a 1 :b 2) +;; (:a 100 :b 200)) +;; ))) +;; (dolist (el inp) +;; (should (equal t (tomelr--toml-table-array-p el)))))) + +(ert-deftest test-internal-tta-false () (let ((inp '( ((a . 1)) ;This is a TOML table ))) diff --git a/test/tjson-utils.el b/test/tjson-utils.el index 4cf6e9d2e5..0c00877367 100644 --- a/test/tjson-utils.el +++ b/test/tjson-utils.el @@ -46,6 +46,7 @@ (a 1) ;; FIXME: `json-alist-p' returns non-nil for below TTA as well. ;; ((:a 1)) ;This is an array of TOML table + [(:a 1)] ;This is an array of TOML table (((a . 1))) ;This is an array of TOML table ))) (dolist (el inp) diff --git a/test/ttable.el b/test/ttable.el index 477324dc3f..e048c46839 100644 --- a/test/ttable.el +++ b/test/ttable.el @@ -45,6 +45,24 @@ (push (tomelr-encode el) out)) (should (equal ref (nreverse out))))) +;;;; TT with a key with an array value +(ert-deftest test-table-array-val () + (let ((inp '( + ((dog . ((legs . 4) + (eyes . 2) + (friends . ("poo" "boo"))))) + )) + (ref '( + "[dog] + legs = 4 + eyes = 2 + friends = [ \"poo\", \"boo\" ]" + )) + out) + (dolist (el inp) + (push (tomelr-encode el) out)) + (should (equal ref (nreverse out))))) + ;;;; Nested tables (ert-deftest test-nested-table () (let ((inp '(((table-1 . ((table-1a . ((key1 . "some string") diff --git a/tomelr.el b/tomelr.el index b7112771f0..3f944395e1 100644 --- a/tomelr.el +++ b/tomelr.el @@ -241,58 +241,9 @@ Signal `tomelr-key-format' if it cannot be encoded as a string." ;;;; Objects (defun tomelr--toml-table-p (object) - "Return non-nil if OBJECT can represent a TOML Table. - -Definition of a TOML Table (TT): - -- OBJECT is TT if it is of type ((KEY1 . VAL1) (KEY2 . VAL2) ..) -- If OBJECT if of type ((SYMBOL . (WHATEVER))), it's possible that - OBJECT is a nested TT. In that case, pass (WHATEVER) to - `tomelr--toml-table-p'." - (let (tablep) - ;; (message "[tomelr--toml-table-p DBG] object = %S, type = %S, len = %d" - ;; object (type-of object) (safe-length object)) - (when (listp object) - ;; (message "[tomelr--toml-table-p DBG] first elem = %S, type = %S, len = %d" - ;; (car object) (type-of (car object)) (safe-length (car object))) - (setq tablep - (cond - ((json-plist-p object) - t) - ((seq-every-p - ;; Ensure that every element in the `object' is a (KEY - ;; . VAL) kind of cons. - (lambda (elem) - ;; (message " [tomelr--toml-table-p DBG] elem = %S, type = %S, len = %d" - ;; elem (type-of elem) (safe-length elem)) - ;; (when (listp elem) - ;; (message " [tomelr--toml-table-p DBG] sub-elem 0 = %S, type = %S, len = %d" - ;; (car elem) (type-of (car elem)) (safe-length (car elem)))) - (or - ;; Basic TT case - ;; ((a . 1) - ;; (b . 2)) - (and (consp elem) - (= 1 (safe-length elem)) - (not (consp (car elem)))) - (and (listp elem) - (symbolp (car elem)) - (or - ;; Nested TT case - ;; ((b . ((c . 3) - ;; (d . 4)))) - (tomelr--toml-table-p (cdr elem)) - ;; Nested TTA case - ;; ((b . (((c . 3)) - ;; ((c . 300))))) - (tomelr--toml-table-array-p (cdr elem)))))) - object) - t) - (t - nil)))) - ;; (message "[tomelr--toml-table-p DBG] tablep = %S" tablep) - ;; (message "=====") - tablep)) + "Return non-nil if OBJECT can represent a TOML Table." + (or (json-alist-p object) + (json-plist-p object))) (defun tomelr--print-pair (key val) "Insert TOML representation of KEY - VAL pair at point."