[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tomelr 2810504e84 32/84: feat: Add basic support for S-
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tomelr 2810504e84 32/84: feat: Add basic support for S-exp plists -> TOML conversion |
Date: |
Tue, 3 May 2022 09:58:10 -0400 (EDT) |
branch: externals/tomelr
commit 2810504e840d8038b9a06fff732889f0f8cc73c8
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>
feat: Add basic support for S-exp plists -> TOML conversion
Support added for scalars and lists.
Pending: tables, arrays of tables, etc.
---
README.org | 25 ++++++++++++++-----------
test/all-tests.el | 1 +
test/tplist.el | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
tomelr.el | 15 +++++++++------
4 files changed, 80 insertions(+), 17 deletions(-)
diff --git a/README.org b/README.org
index 6a43d4df76..6e45a61a8f 100644
--- a/README.org
+++ b/README.org
@@ -960,26 +960,29 @@ contributors = [
:key2 "klm")])
#+end_src
**** TOML
+#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
+(tomelr-encode
+ <<p-list>>)
+#+end_src
+
+#+RESULTS:
#+begin_src toml
-int = 123.0
+int = 123
str = "abc"
bool_false = false
bool_true = true
-int_list = [1.0, 2.0, 3.0]
-str_list = ["a", "b", "c"]
-bool_list = [true, false, true, false]
-list_of_lists = [ [1.0, 2.0],
- [3.0, 4.0, 5.0] ]
-
+int_list = [ 1, 2, 3 ]
+str_list = [ "a", "b", "c" ]
+bool_list = [ true, false, true, false ]
+list_of_lists = [ [ 1, 2 ], [ 3, 4, 5 ] ]
[map]
- key1 = 123.0
+ key1 = 123
key2 = "xyz"
-
[[list_of_maps]]
- key1 = 123.0
+ key1 = 123
key2 = "xyz"
[[list_of_maps]]
- key1 = 567.0
+ key1 = 567
key2 = "klm"
#+end_src
**** JSON Reference
diff --git a/test/all-tests.el b/test/all-tests.el
index a28528c2c0..8a19f4a81a 100644
--- a/test/all-tests.el
+++ b/test/all-tests.el
@@ -25,3 +25,4 @@
(require 'tnil)
(require 'tarray)
(require 'ttable)
+(require 'tplist)
diff --git a/test/tplist.el b/test/tplist.el
new file mode 100644
index 0000000000..db2e7152c2
--- /dev/null
+++ b/test/tplist.el
@@ -0,0 +1,56 @@
+;; -*- 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 of S-exp plists to TOML.
+
+;;; Code:
+(require 'tomelr)
+
+;;;; S-exp objects as plists
+(ert-deftest test-plist ()
+ (let ((inp '((:int 123
+ :remove_this_key nil
+ :str "abc"
+ :bool_false :false
+ :bool_true t
+ :int_list (1 2 3)
+ :str_list ("a" "b" "c")
+ :bool_list (t :false t :false)
+ :list_of_lists [(1 2) (3 4 5)]
+ ;; TODO plist specification of TOML tables is not yet
supported.
+ ;; :map (:key1 123
+ ;; :key2 "xyz")
+ )))
+ (ref '("int = 123
+str = \"abc\"
+bool_false = false
+bool_true = true
+int_list = [ 1, 2, 3 ]
+str_list = [ \"a\", \"b\", \"c\" ]
+bool_list = [ true, false, true, false ]
+list_of_lists = [ [ 1, 2 ], [ 3, 4, 5 ] ]"))
+ out)
+ (dolist (el inp)
+ (push (tomelr-encode el) out))
+ (should (equal ref (nreverse out)))))
+
+
+(provide 'tplist)
diff --git a/tomelr.el b/tomelr.el
index 55b3b07d73..fca625c9f0 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -144,11 +144,12 @@ Return nil if KEYWORD is not recognized as a TOML
keyword."
(and keyword (insert keyword))))
;;;; Strings
-(defun tomelr--print-string (string &optional trim-init-chars)
+(defun tomelr--print-string (string &optional type)
"Insert a TOML representation of STRING at point.
-If TRIM-INIT-CHARS is positive, those many initial characters
-of the STRING are not inserted.
+Optional TYPE arg gives more information about the input STRING.
+For example, if the string is the name of a TOML key, it will be
+set to `keyword'.
Return the same STRING passed as input. See
`tomelr-encode-string' instead if you need a function that
@@ -160,6 +161,7 @@ returns the TOML representation as a string."
(special-chars-re (rx (in ?\" ?\\ cntrl ?\u007F))) ;cntrl is same as
(?\u0000 . ?\u001F)
begin-q end-q)
(cond
+ ((equal type 'keyword))
((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"""
@@ -185,7 +187,6 @@ returns the TOML representation as a string."
(setq end-q 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)
@@ -210,9 +211,11 @@ Return nil if OBJECT cannot be encoded as a TOML string."
(cond ((stringp object)
;; (message "[tomelr--print-stringlike DBG] %S is string" object)
(tomelr--print-string object))
- ((keywordp object)
+ ((keywordp object) ;Symbol beginning with `:', like `:some_key'
;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
- (tomelr--print-string (symbol-name object) 1))
+ (tomelr--print-string
+ (string-trim-left (symbol-name object) ":")
+ 'keyword))
((symbolp object)
(let ((sym-name (symbol-name object)))
;; (message "[tomelr--print-stringlike DBG] %S is symbol, type =
%S, depth = %d"
- [elpa] externals/tomelr 28642f2e78 05/84: fix: dates will be strings in Lisp, (continued)
- [elpa] externals/tomelr 28642f2e78 05/84: fix: dates will be strings in Lisp, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 52dc93201d 08/84: feat: First cut -- Port json-encode from json.el to tomelr-encode, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 7cd15e79b8 10/84: tool(ci): Add GHA setup, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr c872e9efc1 17/84: test: Add test for integer scalar key-value pairs, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr a1f434f03a 31/84: feat: Support nested TOML tables, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 6bbe740e52 28/84: test: Test that 'false is also considered as boolean false in TOML, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 9c91e0dc07 18/84: test: Add test for float scalar key-value pairs, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 96c890a68b 23/84: feat: Convert Lisp lists to TOML arrays, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr bb85106ee9 27/84: doc: Add spec for nested tables and arrays of tables, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 1d65064ffa 20/84: feat: Recognize local date format YYYY-MM-DD, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 2810504e84 32/84: feat: Add basic support for S-exp plists -> TOML conversion,
ELPA Syncer <=
- [elpa] externals/tomelr 0f4e7b4f2c 47/84: fix: Better detection of nested TTA, but still wip, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr cff1f8aa89 38/84: fix: Support TOML tables arrays specified as plist vector, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 4c419bcee2 37/84: fix: Support TOML tables specified as plists, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr baf81228bc 45/84: fix: Correct the spec for nested array of tables, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 4dda8e6be3 46/84: chore: Style edits in a test, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 5959b90ffa 44/84: fix: Don't let TOML tables be recognized as TOML tables arrays, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr b64eb07e99 49/84: fix: Detect TT with sub-tables correctly, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 4386d99a85 65/84: refactor(minor): Use `tomelr--toml-table-p`, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 26f1fc2f3c 55/84: doc: Update the medley example, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr e2b313ca3b 52/84: feat: Implement everything planned in the initial spec, ELPA Syncer, 2022/05/03