[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tomelr 334b7cba54 76/84: refactor: Don't attempt to tri
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tomelr 334b7cba54 76/84: refactor: Don't attempt to triple-quote TOML keys |
Date: |
Tue, 3 May 2022 09:58:15 -0400 (EDT) |
branch: externals/tomelr
commit 334b7cba54001708e6819b9df0abf0c553c0d0a2
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>
refactor: Don't attempt to triple-quote TOML keys
Triple-quote only when the `type' input of `tomelr--print-stringlike'
is nil.
---
tomelr.el | 53 +++++++++++++++++++++++++++++------------------------
1 file changed, 29 insertions(+), 24 deletions(-)
diff --git a/tomelr.el b/tomelr.el
index 08f877d562..858da82d89 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -148,17 +148,18 @@ set to `keyword'.
Return the same STRING passed as input."
;; (message "[tomelr--print-string DBG] string = `%s'" string)
- (setq string (string-trim string "\"" "\""))
(let ((special-chars '((?b . ?\b) ;U+0008
(?f . ?\f) ;U+000C
(?\\ . ?\\)))
- (special-chars-re (rx (in ?\" ?\\ cntrl ?\u007F))) ;cntrl is same as
(?\u0000 . ?\u001F)
+ (special-chars-re (rx (in cntrl ?\u007F)))
begin-q end-q)
(cond
- ((equal type 'keyword))
+ ;; plist keywords, normal keys, table keys, table-array keys
+ (type)
((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"""
+ ;; Use multi-line string quotation if the string contains a "
+ ;; char or a newline - """STRING""", and only if the string
+ ;; doesn't have a "key type".
((string-match-p "\n\\|\"" string)
;; From https://toml.io/en/v1.0.0#string, Any Unicode
;; character may be used except those that must be escaped:
@@ -173,6 +174,7 @@ Return the same STRING passed as input."
(setq begin-q "\"\"\"\n")
(setq end-q "\"\"\""))
(t ;Basic quotation "STRING"
+ (setq special-chars-re (rx (in ?\" ?\\ cntrl ?\u007F))) ;cntrl is same
as (?\u0000 . ?\u001F)
(push '(?\" . ?\") special-chars)
(push '(?t . ?\t) special-chars) ;U+0009
(push '(?n . ?\n) special-chars) ;U+000A
@@ -195,10 +197,11 @@ Return the same STRING passed as input."
(defun tomelr--print-stringlike (object &optional type)
"Insert OBJECT encoded as a TOML string at point.
-Possible value of TYPE are `table', `table-array' or nil.
+Possible values of TYPE are `normal-key', `table-key',
+`table-array-key', `keyword', or nil.
Return nil if OBJECT cannot be encoded as a TOML string."
- (let ((sym-name (cond ((stringp object)
+ (let ((sym-name (cond ((and type (stringp object))
;; https://toml.io/en/v1.0.0#keys
;; Bare keys may only contain ASCII letters, ASCII
digits,
;; underscores, and dashes (A-Za-z0-9_-).
@@ -207,12 +210,14 @@ Return nil if OBJECT cannot be encoded as a TOML string."
;; Wrap string in double-quotes if it
;; doesn't contain only A-Za-z0-9_- chars.
(format "\"%s\"" object)))
+ ((and (null type) (stringp object))
+ object)
;; Symbol beginning with `:', like `:some_key'
((keywordp object)
(string-trim-left (symbol-name object) ":"))
((symbolp object)
(symbol-name object)))))
- (when type
+ (when (member type '(table-key table-array-key))
;; (message "[tomelr--print-stringlike DBG] %S is symbol, type = %S,
depth = %d"
;; object type tomelr--print-indentation-depth)
(if (null (nth tomelr--print-indentation-depth
tomelr--print-table-hierarchy))
@@ -230,26 +235,27 @@ Return nil if OBJECT cannot be encoded as a TOML string."
;; tomelr--print-table-hierarchy)
)
(cond
- ((equal type 'table)
+ ((equal type 'table-key)
(princ (format "[%s]" (string-join tomelr--print-table-hierarchy "."))))
- ((equal type 'table-array)
+ ((equal type 'table-array-key)
(princ (format "[[%s]]" (string-join tomelr--print-table-hierarchy
"."))))
((stringp object)
;; (message "[tomelr--print-stringlike DBG] %S is string" object)
- (tomelr--print-string sym-name))
+ (tomelr--print-string sym-name type))
((keywordp object)
;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
(tomelr--print-string sym-name 'keyword))
(sym-name
(princ sym-name)))))
-(defun tomelr--print-key (key &optional type)
+(defun tomelr--print-key (key &optional key-type)
"Insert a TOML key representation of KEY at point.
-TYPE is set to `table' if KEY is a TOML Table key.
+KEY-TYPE represents the type of key: `normal-key', `table-key' or
+`table-array-key'.
Signal `tomelr-key-format' if it cannot be encoded as a string."
- (or (tomelr--print-stringlike key type)
+ (or (tomelr--print-stringlike key key-type)
(signal 'tomelr-key-format (list key))))
;;;; Objects
@@ -283,18 +289,17 @@ Examples:
(defun tomelr--print-pair (key val)
"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)
+ (let ((key-type (cond
+ ((tomelr--toml-table-p val) 'table-key)
+ ((tomelr--toml-table-array-p val) 'table-array-key)
+ (t 'normal-key))))
+ ;; (message "[tomelr--print-pair DBG] key = %S, val = %S, key-type = %S"
+ ;; key val key-type)
(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
+ (tomelr--print-key key key-type)
+ ;; Skip putting the separator for table and table array keys.
+ (unless (member key-type '(table-key table-array-key))
(insert tomelr--print-keyval-separator))
(tomelr--print val))))
- [elpa] externals/tomelr f3b6951cfe 53/84: doc: Add note that the scalars need to be earlier in the order, (continued)
- [elpa] externals/tomelr f3b6951cfe 53/84: doc: Add note that the scalars need to be earlier in the order, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 192cb06a23 57/84: chore(style): Whitespace change only, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 6d2be83669 69/84: chore: package-lint fixes, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 41ccea4ebe 64/84: refactor: Remove unnecessary tomelr-encode-keyword, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 406f4922a8 58/84: test: Add tests for json.el functions used in tomelr, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr b3b6a28d15 68/84: doc: Add more examples, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr f9d670e165 56/84: refactor: Clean up unused code, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 511240765b 71/84: chore: Ignore .elc files, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ed13b73e9b 74/84: feat: Support string keys, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr de661716af 72/84: style: Remove space from after and before [ and ] brackets, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 334b7cba54 76/84: refactor: Don't attempt to triple-quote TOML keys,
ELPA Syncer <=
- [elpa] externals/tomelr c2d1328c44 80/84: fix: Boolean coercing when value is a symbol true or false, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ebe5959174 75/84: feat: Auto-coerce string to boolean, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr a676192b43 81/84: fix: Integer coercing of a number strings with underscores, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 8da825067b 15/84: chore(readme): Update, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 65653d53ee 30/84: chore(doc): M-x checkdoc fixes, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr bcaea16ec0 39/84: chore(doc): Clean up old code block, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 45542fb234 67/84: chore: Bump version, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 7c004af90d 66/84: style: Remove unnecessary tomelr-encode-* functions, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 568de5efb2 83/84: chore: Bump version, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 0d4674f782 42/84: test: Test `tomelr--toml-table-p`, ELPA Syncer, 2022/05/03