branch: externals/yaml
commit 920f71b0c12f6036846f3590a32a71a310ed369f
Merge: 3fbeaee97d 6f4d1fb993
Author: Zachary Romero <[email protected]>
Commit: GitHub <[email protected]>

    Merge pull request #64 from ehrt74/master
    
    added encoding to kyaml-compact and kyaml-pretty
---
 yaml-tests.el | 29 ++++++++++++++++++++++------
 yaml.el       | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 77 insertions(+), 13 deletions(-)

diff --git a/yaml-tests.el b/yaml-tests.el
index 5c102f4ad1..03c6e34292 100644
--- a/yaml-tests.el
+++ b/yaml-tests.el
@@ -551,12 +551,16 @@ keep: |+
 
 (defun yaml-test-round-trip (o)
   "Test (equal (decode (encode o)) o)"
-  (let* ((encoded (yaml-encode o))
-         (parsed (yaml-parse-string encoded
-                                    :object-type 'alist
-                                    :sequence-type 'list))
-         (encoded-2 (yaml-encode o)))
-    (equal encoded encoded-2)))
+  (seq-doseq (yaml-dialect '(:auto :kyaml-compact :kyaml-pretty))
+    (seq-doseq (yaml-indent-width '(2 4 8))
+      (let ((yaml-dialect yaml-dialect)
+           (yaml-indent-width yaml-indent-width))
+       (let* ((encoded (yaml-encode o))
+               (parsed (yaml-parse-string encoded
+                                         :object-type 'alist
+                                         :sequence-type 'list))
+               (encoded-2 (yaml-encode o)))
+       (equal encoded encoded-2))))))
 
 
 (ert-deftest yaml-encode-tests ()
@@ -627,9 +631,22 @@ keep: |+
   (should (equal
            (yaml-encode `[((foo . bar) (baz . bax))])
            "- foo: bar\n  baz: bax"))
+  (should (equal
+          (let ((yaml-dialect :kyaml-compact))
+            (yaml-encode `((deeper . [((foo . "bar") (baz . "bax"))]))))
+          "{deeper: [{foo: \"bar\", baz: \"bax\", }, ], }"))
+  (should (equal
+          (let ((yaml-dialect :kyaml-pretty)
+                (yaml-indent-width 4))
+            (yaml-encode `((deeper . [((foo . "bar") (baz . "bax"))]))))
+          "{\n    deeper: [\n        {\n            foo: \"bar\",\n            
baz: \"bax\",\n        },\n    ],\n}"))
   (should (equal
            (yaml-encode `((deeper . [((foo . bar) (baz . bax))])))
            "deeper: \n- foo: bar\n  baz: bax"))
+  (should (equal
+          (let ((yaml-indent-width 4))
+            (yaml-encode `((deeper . [((foo . bar) (baz . bax))]))))
+           "deeper: \n-   foo: bar\n    baz: bax"))
   (should (equal (yaml-parse-string
                   (yaml-encode [1 [2 [3] 2] 1])
                   :object-type 'alist
diff --git a/yaml.el b/yaml.el
index f2f6693335..141eb7eb48 100644
--- a/yaml.el
+++ b/yaml.el
@@ -45,6 +45,10 @@
 
 (defconst yaml-parser-version "0.5.1")
 
+
+(defvar yaml-dialect :auto "Yaml-dialect. one of :auto, :kyaml-pretty, 
:kyaml-compact")
+(defvar yaml-indent-width 2 "Number of spaces per indentation. Valid values 
>=2")
+
 (defvar yaml--encode-use-flow-sequence t
   "Turn on encoding sequence of scalars as flow sequence.")
 
@@ -2651,7 +2655,8 @@ auto-detecting the indentation.  Functionality defers to
      (t (insert (symbol-name s)))))
    ((numberp s) (insert (number-to-string s)))
    ((stringp s)
-    (if (string-match "\\`[-_a-zA-Z0-9]+\\'" s)
+    (if (and (string-match "\\`[-_a-zA-Z0-9]+\\'" s)
+            (eql yaml-dialect :auto))
         (insert s)
       (insert "\"" (yaml--encode-escape-string s) "\"")))))
 
@@ -2677,8 +2682,28 @@ auto-detecting the indentation"
            (yaml--encode-hash-table ht indent auto-indent))
           ((zerop (length l))
            (insert "[]"))
+         ((eql yaml-dialect :kyaml-pretty)
+          (let* ((indent-string (make-string indent ?\s))
+                 (next-indent (+ indent yaml-indent-width))
+                 (next-indent-string (make-string next-indent ?\s)))
+            (insert "[" "\n")
+            (seq-do (lambda (object)
+                      (insert next-indent-string)
+                      (yaml--encode-object object next-indent)
+                      (insert "," "\n"))
+                    l)
+            (insert indent-string "]")))
+
+         ((eql yaml-dialect :kyaml-compact)
+          (insert "[")
+          (seq-do (lambda (object)
+                    (yaml--encode-object object nil)
+                    (insert ", "))
+                  l)
+          (insert "]"))
+
           ((and yaml--encode-use-flow-sequence
-                (seq-every-p #'yaml--scalarp l))
+                    (seq-every-p #'yaml--scalarp l))
            (insert "[")
            (yaml--encode-object (car l) 0)
            (seq-do (lambda (object)
@@ -2688,9 +2713,9 @@ auto-detecting the indentation"
            (insert "]"))
           (t
            (when (zerop indent)
-             (setq indent 2))
+             (setq indent yaml-indent-width))
            (let* ((first t)
-                  (indent-string (make-string (- indent 2) ?\s)))
+                  (indent-string (make-string (- indent yaml-indent-width) 
?\s)))
              (seq-do
               (lambda (object)
                 (if (not first)
@@ -2703,7 +2728,7 @@ auto-detecting the indentation"
                 (if (or (hash-table-p object)
                         (yaml--alist-to-hash-table object))
                     (yaml--encode-object object indent t)
-                  (yaml--encode-object object (+ indent 2) nil)))
+                  (yaml--encode-object object (+ indent yaml-indent-width) 
nil)))
               l))))))
 
 (defun yaml--encode-auto-detect-indent ()
@@ -2717,6 +2742,28 @@ If AUTO-INDENT is non-nil, auto-detect the indent on the 
current
 line and insert accordingly."
   (cond ((zerop (hash-table-size m))
          (insert "{}"))
+       ((eql yaml-dialect :kyaml-pretty)
+        (let* ((indent-string (make-string indent ?\s))
+               (next-indent (+ indent yaml-indent-width))
+               (next-indent-string (make-string next-indent ?\s)))
+          (insert "{" "\n")
+          (maphash
+           (lambda (k v)
+             (insert next-indent-string)
+             (yaml--encode-object k next-indent)
+             (insert ": ")
+             (yaml--encode-object v next-indent)
+             (insert ",\n")) m)
+          (insert indent-string "}")))
+       ((eql yaml-dialect :kyaml-compact)
+        (insert "{")
+        (maphash
+         (lambda (k v)
+           (yaml--encode-object k nil)
+           (insert ": ")
+           (yaml--encode-object v nil)
+           (insert ", ")) m)
+        (insert "}"))
         (t
          (let ((first t)
                (indent-string (make-string indent ?\s)))
@@ -2727,13 +2774,13 @@ line and insert accordingly."
                 (if auto-indent
                     (let ((curr-indent (yaml--encode-auto-detect-indent)))
                       (when (> curr-indent indent)
-                        (setq indent (+ curr-indent 1)))
+                        (setq indent (+ curr-indent yaml-indent-width)))
                       (insert (make-string (- indent curr-indent) ?\s)))
                   (insert "\n" indent-string))
                 (setq first nil))
               (yaml--encode-object k indent nil)
               (insert ": ")
-              (yaml--encode-object v (+ indent 2)))
+              (yaml--encode-object v (+ indent yaml-indent-width)))
             m)))))
 
 (provide 'yaml)

Reply via email to