[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tomelr cedb75df72 29/84: feat: Support basic TOML table
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tomelr cedb75df72 29/84: feat: Support basic TOML tables |
Date: |
Tue, 3 May 2022 09:58:10 -0400 (EDT) |
branch: externals/tomelr
commit cedb75df72f9aed0ad990b631f32d71f6ba1b79d
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>
feat: Support basic TOML tables
---
README.org | 14 +++++++---
test/all-tests.el | 1 +
test/{all-tests.el => ttable.el} | 31 +++++++++++++++++-----
tomelr.el | 57 ++++++++++++++++++++++++++++------------
4 files changed, 76 insertions(+), 27 deletions(-)
diff --git a/README.org b/README.org
index ba51f2e6b2..2acd5d41be 100644
--- a/README.org
+++ b/README.org
@@ -29,8 +29,8 @@ specification defined below.
- [X] Nil
- [X] Arrays
- [X] Array of Arrays
-- [ ] Tables
- - [ ] Basic Tables
+- [-] Tables
+ - [X] Basic Tables
- [ ] Nested Tables
- [ ] Array of Tables
- [ ] Basic Array of Tables
@@ -393,7 +393,8 @@ nested_mixed_array = [ [ 1, 2 ], [ "a", "b", "c" ] ]
}
#+end_example
** TOML Tables: Maps or Dictionaries or Hash Tables
-*** Basic TOML Tables
+*** DONE Basic TOML Tables
+CLOSED: [2022-04-29 Fri 13:41]
**** S-expression
#+begin_src emacs-lisp :eval no :noweb-ref tables
'((table-1 . ((key1 . "some string")
@@ -402,11 +403,16 @@ nested_mixed_array = [ [ 1, 2 ], [ "a", "b", "c" ] ]
(key2 . 456))))
#+end_src
**** TOML
+#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
+(tomelr-encode
+ <<tables>>)
+#+end_src
+
+#+RESULTS:
#+begin_src toml
[table-1]
key1 = "some string"
key2 = 123
-
[table-2]
key1 = "another string"
key2 = 456
diff --git a/test/all-tests.el b/test/all-tests.el
index f89407594b..a28528c2c0 100644
--- a/test/all-tests.el
+++ b/test/all-tests.el
@@ -24,3 +24,4 @@
(require 'tscalar)
(require 'tnil)
(require 'tarray)
+(require 'ttable)
diff --git a/test/all-tests.el b/test/ttable.el
similarity index 55%
copy from test/all-tests.el
copy to test/ttable.el
index f89407594b..fdcafa87f4 100644
--- a/test/all-tests.el
+++ b/test/ttable.el
@@ -1,4 +1,4 @@
-;;; all-tests.el --- Tests for tomelr.el -*-
lexical-binding: t; -*-
+;; -*- lexical-binding: t; -*-
;; Authors: Kaushal Modi <kaushal.modi@gmail.com>
@@ -17,10 +17,29 @@
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
-;;; Code:
+;;; Commentary:
-(setq load-prefer-newer t)
+;; Test conversion to TOML tables.
-(require 'tscalar)
-(require 'tnil)
-(require 'tarray)
+;;; Code:
+(require 'tomelr)
+
+;;;; Simple tables
+(ert-deftest test-table ()
+ (let ((inp '(((table-1 . ((key1 . "some string")
+ (key2 . 123))))
+ ((table-2 . ((key1 . "another string")
+ (key2 . 456))))))
+ (ref '("[table-1]
+ key1 = \"some string\"
+ key2 = 123"
+ "[table-2]
+ key1 = \"another string\"
+ key2 = 456"))
+ out)
+ (dolist (el inp)
+ (push (tomelr-encode el) out))
+ (should (equal ref (nreverse out)))))
+
+
+(provide 'ttable)
diff --git a/tomelr.el b/tomelr.el
index f4ed83e757..ce1ebd4e9d 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -196,40 +196,63 @@ Return the same STRING passed as input."
"Return a TOML representation of STRING."
(tomelr--with-output-to-string (tomelr--print-string string)))
-(defun tomelr--print-stringlike (object)
+(defun tomelr--print-stringlike (object &optional type)
"Insert OBJECT encoded as a TOML string at point.
+
+TYPE is set to `table' if OBJECT is a TOML Table key.
+
Return nil if OBJECT cannot be encoded as a TOML string."
(cond ((stringp object)
- ;; (message "[tomelr--print-stringlike DBG] string")
+ ;; (message "[tomelr--print-stringlike DBG] %S is string" object)
(tomelr--print-string object))
((keywordp object)
- ;; (message "[tomelr--print-stringlike DBG] keyword")
+ ;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
(tomelr--print-string (symbol-name object) 1))
((symbolp object)
- ;; (message "[tomelr--print-stringlike DBG] symbol")
- (princ (symbol-name object))
- ;; (tomelr--print-string (symbol-name object))
- )))
+ ;; (message "[tomelr--print-stringlike DBG] %S is symbol" object)
+ (cond
+ ((equal type 'table)
+ (princ (format "[%s]" (symbol-name object))))
+ (t
+ (princ (symbol-name object)))))))
+
+(defun tomelr--print-key (key &optional type)
+ "Insert a TOML key representation of KEY at point.
+
+TYPE is set to `table' if KEY is a TOML Table key.
-(defun tomelr--print-key (object)
- "Insert a TOML key representation of OBJECT at point.
Signal `tomelr-key-format' if it cannot be encoded as a string."
- (or (tomelr--print-stringlike object)
- (signal 'tomelr-key-format (list object))))
+ (or (tomelr--print-stringlike key type)
+ (signal 'tomelr-key-format (list key))))
;;;; Objects
(defun tomelr--print-pair (key val)
"Insert TOML representation of KEY-VAL pair at point."
- ;; (message "[tomelr--print-pair DBG] key = %S, val = %S" key val)
- (when val ;Don't print the key if val is nil
- (tomelr--print-indentation) ;Newline before each key in a key-value pair
- (tomelr--print-key key)
- (insert tomelr--print-keyval-separator)
- (tomelr--print val)))
+ (let ((type (cond
+ ;; TODO: Need to find a robust way of detecting TOML tables.
+ ((and (mapp val)
+ (consp val) ; val = ((KEY . VAL)) <- cons
+ (consp (car val)) ;(car val) = (KEY . VAL) <- also cons
+ )
+ 'table)
+ (t
+ nil))))
+ ;; (message "[tomelr--print-pair DBG] key = %S, val = %S, type = %S"
+ ;; key val type)
+ ;; (message "[tomelr--print-pair DBG] val type = %S" (type-of val))
+ (when val ;Don't print the key if val is nil
+ (tomelr--print-indentation) ;Newline before each key in a key-value pair
+ (tomelr--print-key key type)
+ ;; Skip putting the separator if `type' has a non-nil value like
+ ;; `table'.
+ (unless type
+ (insert tomelr--print-keyval-separator))
+ (tomelr--print val))))
(defun tomelr--print-map (map)
"Insert TOML object representation of MAP at point.
This works for any MAP satisfying `mapp'."
+ ;; (message "[tomelr--print-map DBG] map = %S" map)
(unless (map-empty-p map)
(tomelr--with-indentation
(map-do #'tomelr--print-pair map))))
- [elpa] externals/tomelr 8bc506af5a 02/84: Add s-exp->toml examples and spec, (continued)
- [elpa] externals/tomelr 8bc506af5a 02/84: Add s-exp->toml examples and spec, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 7d8d41f15b 19/84: feat: Encode to multi-line TOML string automatically, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 0ba5f2ff69 24/84: chore(readme): Move mixed type array example to the 'medley' example, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ca9245038a 35/84: fix: Attempt to make tomelr--toml-table-p more robust, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 000067cf7d 14/84: chore(readme): Update, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 69217d47a6 22/84: feat: Skip converting keys whose values are nil, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr af40c0b40f 11/84: fix: Require subr-x for older Emacs versions, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 060c38b934 16/84: chore(readme): Limit up to 3 levels of headings for Org exports, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 20bf9e6c54 12/84: chore(doc): Add GHA and license badges, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr f37841cc78 25/84: test: Add test for TOML Array of Arrays, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr cedb75df72 29/84: feat: Support basic TOML tables,
ELPA Syncer <=
- [elpa] externals/tomelr 91800b26b8 21/84: feat: Recognize RFC 3339 formatted date-time + offset, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 83e924559e 13/84: chore(doc): Fix typo, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 98c9b8c1fc 26/84: fix: Use `=` and `length` separately instead of `length=`, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr c0962ba15f 33/84: fix: Don't run plist to TOML conversion test on emacs 26.3 and older, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 3c068fb9d9 34/84: refactor: Move TOML Table detection logic to a separate fn, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ad8366d904 36/84: feat: Support basic TOML Table Arrays, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 55fefb0fa8 40/84: doc: Add few links, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 38160ef271 41/84: fix: Stricter condition before starting TOML table array check, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr a7b3a57037 48/84: feat: Make a very basic nested array of TTA work, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 3aa4dc1dbd 54/84: chore: Add Package-Requires and other info in the header comment, ELPA Syncer, 2022/05/03