branch: externals/tomelr commit 10a1994aedcbd95c35096b257cf1e9e6fd4554cb Author: Kaushal Modi <kaushal.m...@gmail.com> Commit: Kaushal Modi <kaushal.m...@gmail.com>
feat: Support (lightly tested) nested TOML Table Arrays --- README.org | 14 ++++++++------ test/ttable-array.el | 40 ++++++++++++++++++++-------------------- tomelr.el | 23 +++++++++++++++-------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/README.org b/README.org index e44d71aea8..7a03d4bd7d 100644 --- a/README.org +++ b/README.org @@ -18,7 +18,7 @@ the Emacs core library [[https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/j It will then be gradually refactored so that it meets the specification defined below. -* Library Completion Status [5/7] +* Library Completion Status [6/7] - [X] Scalar - [X] Boolean - [X] Integer @@ -32,9 +32,9 @@ specification defined below. - [X] Tables - [X] Basic Tables - [X] Nested Tables -- [-] Array of Tables +- [X] Array of Tables - [X] Basic Array of Tables - - [ ] Nested Array of Tables + - [X] Nested Array of Tables - [ ] Property Lists * Specification and Conversion Examples [[https://scripter.co/defining-tomelr/][Companion blog post]] @@ -567,7 +567,8 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D ] } #+end_example -*** Nested Array of Tables +*** DONE Nested Array of Tables +CLOSED: [2022-04-30 Sat 01:32] **** S-expression #+begin_src emacs-lisp :eval no :noweb-ref nested-table-arrays '((fruits . (((name . "apple") @@ -584,13 +585,14 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D <<nested-table-arrays>>) #+end_src +#+RESULTS: #+begin_src toml [[fruits]] name = "apple" - [fruits.physical] # subtable + [fruits.physical] color = "red" shape = "round" - [[fruits.varieties]] # nested array of tables + [[fruits.varieties]] name = "red delicious" [[fruits.varieties]] name = "granny smith" diff --git a/test/ttable-array.el b/test/ttable-array.el index 7247f9d132..2b3deb3851 100644 --- a/test/ttable-array.el +++ b/test/ttable-array.el @@ -86,13 +86,13 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D ((fruits . (((name . "apple") (varieties . (((name . "red delicious")) ((name . "granny smith")))))))) - ;; ((fruits . (((name . "apple") - ;; (physical . ((color . "red") - ;; (shape . "round"))) - ;; (varieties . (((name . "red delicious")) - ;; ((name . "granny smith"))))) - ;; ((name . "banana") - ;; (varieties . (((name . "plantain")))))))) + ((fruits . (((name . "apple") + (physical . ((color . "red") + (shape . "round"))) + (varieties . (((name . "red delicious")) + ((name . "granny smith"))))) + ((name . "banana") + (varieties . (((name . "plantain")))))))) )) (ref '( "[[fruits]] @@ -106,19 +106,19 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D name = \"red delicious\" [[fruits.varieties]] name = \"granny smith\"" - ;; "[[fruits]] - ;; name = \"apple\" - ;; [fruits.physical] - ;; color = \"red\" - ;; shape = \"round\" - ;; [[fruits.varieties]] - ;; name = \"red delicious\" - ;; [[fruits.varieties]] - ;; name = \"granny smith\" - ;; [[fruits]] - ;; name = \"banana\" - ;; [[fruits.varieties]] - ;; name = \"plantain\"" + "[[fruits]] + name = \"apple\" + [fruits.physical] + color = \"red\" + shape = \"round\" + [[fruits.varieties]] + name = \"red delicious\" + [[fruits.varieties]] + name = \"granny smith\" +[[fruits]] + name = \"banana\" + [[fruits.varieties]] + name = \"plantain\"" )) out) (dolist (el inp) diff --git a/tomelr.el b/tomelr.el index 0c97552b62..4611a92b31 100644 --- a/tomelr.el +++ b/tomelr.el @@ -58,9 +58,6 @@ Dictates repetitions of `tomelr-encoding-default-indentation'.") This variable is used for both TOML Tables and Arrays of TOML Tables.") -(defvar tomelr--print-table-array-key "" - "Internal variable used to save the TOML Table Array name.") - (defvar tomelr--print-keyval-separator " = " "String used to separate key-value pairs during encoding.") @@ -242,9 +239,7 @@ Return nil if OBJECT cannot be encoded as a TOML string." ((equal type 'table) (princ (format "[%s]" (string-join tomelr--print-table-hierarchy ".")))) ((equal type 'table-array) - (let ((tta-name (format "[[%s]]" (string-join tomelr--print-table-hierarchy ".")))) - (setq tomelr--print-table-array-key tta-name) - (princ tta-name))) + (princ (format "[[%s]]" (string-join tomelr--print-table-hierarchy ".")))) ((stringp object) ;; (message "[tomelr--print-stringlike DBG] %S is string" object) (tomelr--print-string sym-name)) @@ -423,6 +418,19 @@ Definition of a TOML Table Array (TTA): nil)))) ttap)) +(defun tomelr--print-tta-key () + "Print TOML Table Array key." + ;; (message "[tomelr--print-array DBG] depth = %d" tomelr--print-indentation-depth) + ;; Throw away table keys collected at higher depths, if + ;; any, from earlier runs of this function. + (setq tomelr--print-table-hierarchy + (seq-take tomelr--print-table-hierarchy + (1+ tomelr--print-indentation-depth))) + + (tomelr--print-indentation) + (insert + (format "[[%s]]" (string-join tomelr--print-table-hierarchy ".")))) + (defun tomelr--print-array (array) "Insert a TOML representation of ARRAY at point. See `tomelr-encode-array' that returns the same as a string." @@ -435,8 +443,7 @@ See `tomelr-encode-array' that returns the same as a string." (mapc (lambda (elt) (if first (setq first nil) - (tomelr--print-indentation) - (insert tomelr--print-table-array-key)) + (tomelr--print-tta-key)) (tomelr--print elt)) array)))) (t