Skip to content

Commit

Permalink
feat: Add basic support for S-exp plists -> TOML conversion
Browse files Browse the repository at this point in the history
Support added for scalars and lists.
Pending: tables, arrays of tables, etc.
  • Loading branch information
kaushalmodi committed Apr 29, 2022
1 parent a1f434f commit 2810504
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
25 changes: 14 additions & 11 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/all-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
(require 'tnil)
(require 'tarray)
(require 'ttable)
(require 'tplist)
56 changes: 56 additions & 0 deletions test/tplist.el
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 9 additions & 6 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"""
Expand All @@ -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)
Expand All @@ -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"
Expand Down

0 comments on commit 2810504

Please sign in to comment.