branch: externals/tomelr commit 2810504e840d8038b9a06fff732889f0f8cc73c8 Author: Kaushal Modi <kaushal.m...@gmail.com> Commit: Kaushal Modi <kaushal.m...@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.m...@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"