emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/tomelr ad8366d904 36/84: feat: Support basic TOML Table


From: ELPA Syncer
Subject: [elpa] externals/tomelr ad8366d904 36/84: feat: Support basic TOML Table Arrays
Date: Tue, 3 May 2022 09:58:10 -0400 (EDT)

branch: externals/tomelr
commit ad8366d904dea6fc3f4af5bf57bcd92c6b37f57e
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>

    feat: Support basic TOML Table Arrays
---
 README.org           | 35 +++++++++++++++++++++++++---
 test/all-tests.el    |  1 +
 test/ttable-array.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tomelr.el            | 50 ++++++++++++++++++++++++++++++++-------
 4 files changed, 141 insertions(+), 11 deletions(-)

diff --git a/README.org b/README.org
index 6e45a61a8f..18d1b37754 100644
--- a/README.org
+++ b/README.org
@@ -32,8 +32,8 @@ specification defined below.
 - [X] Tables
   - [X] Basic Tables
   - [X] Nested Tables
-- [ ] Array of Tables
-  - [ ] Basic Array of Tables
+- [-] Array of Tables
+  - [X] Basic Array of Tables
   - [ ] Nested Array of Tables
 - [ ] Property Lists
 * Specification and Conversion Examples
@@ -483,7 +483,8 @@ CLOSED: [2022-04-29 Fri 14:30]
 }
 #+end_example
 ** TOML Array of Tables: Lists of Maps
-*** Basic Array of Tables
+*** DONE Basic Array of Tables
+CLOSED: [2022-04-29 Fri 18:14]
 **** S-expression
 #+begin_src emacs-lisp :eval no :noweb-ref table-arrays
 '((products . (((name . "Hammer")
@@ -500,6 +501,34 @@ CLOSED: [2022-04-29 Fri 14:30]
                    (note . "A note `mono`.")))))
 #+end_src
 **** TOML
+#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
+(tomelr-encode
+  <<table-arrays>>)
+#+end_src
+
+#+RESULTS:
+#+begin_src toml
+[[products]]
+  name = "Hammer"
+  sku = 738594937
+[[products]]
+[[products]]
+  name = "Nail"
+  sku = 284758393
+  color = "gray"
+[[org_logbook]]
+  timestamp = 2022-04-08T14:53:00-04:00
+  note = """
+This note addition prompt shows up on typing the `C-c C-z` binding.
+See 
[org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#Drawers)."""
+[[org_logbook]]
+  timestamp = 2018-09-06T11:45:00-04:00
+  note = "Another note **bold** _italics_."
+[[org_logbook]]
+  timestamp = 2018-09-06T11:37:00-04:00
+  note = "A note `mono`."
+#+end_src
+
 #+begin_src toml
 [[products]]
   name = "Hammer"
diff --git a/test/all-tests.el b/test/all-tests.el
index 8a19f4a81a..b031e739ec 100644
--- a/test/all-tests.el
+++ b/test/all-tests.el
@@ -25,4 +25,5 @@
 (require 'tnil)
 (require 'tarray)
 (require 'ttable)
+(require 'ttable-array)
 (require 'tplist)
diff --git a/test/ttable-array.el b/test/ttable-array.el
new file mode 100644
index 0000000000..37392ba28d
--- /dev/null
+++ b/test/ttable-array.el
@@ -0,0 +1,66 @@
+;; -*- lexical-binding: t; -*-
+
+;; Authors: Kaushal Modi <kaushal.modi@gmail.com>
+
+;; This file is not part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Test conversion to TOML tables.
+
+;;; Code:
+(require 'tomelr)
+
+;;;; Basic array of tables
+(ert-deftest test-array-of-tables ()
+  (let ((inp '(((products . (((name . "Hammer")
+                              (sku . 738594937))
+                             ()
+                             ((name . "Nail")
+                              (sku . 284758393)
+                              (color . "gray"))))
+                (org_logbook . (((timestamp . 2022-04-08T14:53:00-04:00)
+                                 (note . "This note addition prompt shows up 
on typing the `C-c C-z` binding.\nSee 
[org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#Drawers)."))
+                                ((timestamp . 2018-09-06T11:45:00-04:00)
+                                 (note . "Another note **bold** _italics_."))
+                                ((timestamp . 2018-09-06T11:37:00-04:00)
+                                 (note . "A note `mono`.")))))))
+        (ref '("[[products]]
+  name = \"Hammer\"
+  sku = 738594937
+[[products]]
+[[products]]
+  name = \"Nail\"
+  sku = 284758393
+  color = \"gray\"
+[[org_logbook]]
+  timestamp = 2022-04-08T14:53:00-04:00
+  note = \"\"\"
+This note addition prompt shows up on typing the `C-c C-z` binding.
+See 
[org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#Drawers).\"\"\"
+[[org_logbook]]
+  timestamp = 2018-09-06T11:45:00-04:00
+  note = \"Another note **bold** _italics_.\"
+[[org_logbook]]
+  timestamp = 2018-09-06T11:37:00-04:00
+  note = \"A note `mono`.\""))
+        out)
+    (dolist (el inp)
+      (push (tomelr-encode el) out))
+    (should (equal ref (nreverse out)))))
+
+
+(provide 'ttable-array)
diff --git a/tomelr.el b/tomelr.el
index fc504b5a29..5561d5604e 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -53,7 +53,10 @@ ordered alphabetically.")
 Dictates repetitions of `tomelr-encoding-default-indentation'.")
 
 (defvar tomelr--print-table-hierarchy ()
-  "Internal variable used to save the TOML table hierarchy.")
+  "Internal variable used to save the TOML Table hierarchy.")
+
+(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.")
@@ -236,6 +239,10 @@ Return nil if OBJECT cannot be encoded as a TOML string."
              ;; (message "[tomelr--print-stringlike DBG] table hier: %S"
              ;;          tomelr--print-table-hierarchy)
              (princ (format "[%s]" (string-join tomelr--print-table-hierarchy 
"."))))
+            ((equal type 'table-array)
+             (let ((tta-name (format "[[%s]]" sym-name)))
+               (setq tomelr--print-table-array-key tta-name)
+               (princ tta-name)))
             (t
              (princ sym-name)))))))
 
@@ -290,6 +297,7 @@ Definition of a TOML Table (TT):
   "Insert TOML representation of KEY - VAL pair at point."
   (let ((type (cond
                ((tomelr--toml-table-p val) 'table)
+               ((tomelr--toml-table-array-p val) 'table-array)
                (t nil))))
     ;; (message "[tomelr--print-pair DBG] key = %S, val = %S, type = %S"
     ;;          key val type)
@@ -356,21 +364,47 @@ See `tomelr-encode-alist' that returns the same as a 
string."
         ((signal 'tomelr-error (list list)))))
 
 ;;;; Arrays
+(defun tomelr--toml-table-array-p (object)
+  "Return non-nil if OBJECT can represent a TOML Table Array.
+
+Definition of a TOML Table Array (TTA):
+
+- OBJECT is TTA if it is of type ((TT1) (TT2) ..) where each element is a
+  TOML Table (TT)."
+  (when (listp object)
+    (seq-every-p
+     (lambda (elem)
+       (tomelr--toml-table-p elem))
+     object)))
+
 (defun tomelr--print-array (array)
   "Insert a TOML representation of ARRAY at point.
 See `tomelr-encode-array' that returns the same as a string."
-  (insert "[ ")
-  (unless (= 0 (length array))
-    (tomelr--with-indentation
+  ;; (message "[tomelr--print-array DBG] array = %S, TTA = %S"
+  ;;          array (tomelr--toml-table-array-p array))
+  (cond
+   ((tomelr--toml-table-array-p array)
+    (unless (= 0 (length array))
       (let ((first t))
         (mapc (lambda (elt)
                 (if first
                     (setq first nil)
-                  (insert ", "))
+                  (insert (format "\n%s" tomelr--print-table-array-key)))
                 (tomelr--print elt))
-              array)))
-    (insert " "))
-  (insert "]"))
+              array))))
+   (t
+    (insert "[ ")
+    (unless (= 0 (length array))
+      (tomelr--with-indentation
+        (let ((first t))
+          (mapc (lambda (elt)
+                  (if first
+                      (setq first nil)
+                    (insert ", "))
+                  (tomelr--print elt))
+                array)))
+      (insert " "))
+    (insert "]"))))
 
 (defun tomelr-encode-array (array)
   "Return a TOML representation of ARRAY.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]