[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/yaml 964ef3b105 014/124: process literal and folded
From: |
ELPA Syncer |
Subject: |
[elpa] externals/yaml 964ef3b105 014/124: process literal and folded |
Date: |
Fri, 29 Nov 2024 15:59:54 -0500 (EST) |
branch: externals/yaml
commit 964ef3b1054f4e77fdb9142004a0a617bc73e88a
Author: Zachary Romero <zacromero@posteo.net>
Commit: Zachary Romero <zacromero@posteo.net>
process literal and folded
---
yaml-tests.el | 40 ++++++++++++++++++++++++++++++++++++++++
yaml.el | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-------
2 files changed, 88 insertions(+), 7 deletions(-)
diff --git a/yaml-tests.el b/yaml-tests.el
index a7362a653c..d7d4603525 100644
--- a/yaml-tests.el
+++ b/yaml-tests.el
@@ -281,6 +281,40 @@
(should (equal 1.0e+INF (yaml--resolve-scalar-tag ".Inf")))
(should (equal "hello world" (yaml--resolve-scalar-tag "hello world"))))
+(ert-deftest yaml-process-literal-text-test ()
+ "Test processing literal strings"
+ (should (equal (yaml--process-literal-text "\n abc\n def")
+ "abc\ndef\n"))
+ (should (equal (yaml--process-literal-text "\n abc\n\n def")
+ "abc\n\ndef\n"))
+ (should (equal (yaml--process-literal-text "1\n abc\n def")
+ " abc\n def"))
+ (should (equal (yaml--process-literal-text "2\n abc\n def")
+ " abc\n def\n"))
+ (should (equal (yaml--process-literal-text "-\n abc\n def\n \n \n
\n")
+ "abc\ndef"))
+ (should (equal (yaml--process-literal-text "\n abc\n def\n \n \n
\n")
+ "abc\ndef\n"))
+ (should (equal (yaml--process-literal-text "+\n abc\n def\n \n \n
\n")
+ "abc\ndef\n\n\n\n")))
+
+(ert-deftest yaml-process-folded-text-test ()
+ "Test processing literal strings"
+ (should (equal (yaml--process-folded-text "\n abc\n def")
+ "abc def\n"))
+ (should (equal (yaml--process-folded-text "\n abc\n\n def")
+ "abc\ndef\n"))
+ (should (equal (yaml--process-folded-text "1\n abc\n def")
+ " abc\n def\n"))
+ (should (equal (yaml--process-folded-text "2\n abc\n def")
+ " abc\n def\n"))
+ (should (equal (yaml--process-folded-text "-\n abc\n def\n \n \n
\n")
+ "abc def"))
+ (should (equal (yaml--process-folded-text "\n abc\n def\n \n \n
\n")
+ "abc def\n"))
+ (should (equal (yaml--process-folded-text "+\n abc\n def\n \n \n
\n")
+ "abc def\n\n\n\n")))
+
(ert-deftest yaml-parsing-yaml ()
"Tests YAML parsing."
(should (yaml--hash-table-equal
@@ -306,6 +340,12 @@
;; }")))
)
+(yaml-parse-string
+ "abc:
+ def: >
+ value
+ value")
+
;; (yaml-parse-string "apiVersion: v1
;; description: A Helm chart for bidder canary
;; home: https://github.com/travelaudience/bidder-bidder
diff --git a/yaml.el b/yaml.el
index 1779599b5b..9e7f17a4b3 100644
--- a/yaml.el
+++ b/yaml.el
@@ -184,17 +184,58 @@ This flag is intended for development purposes.")
"The state that the YAML parser is with regards to incoming events.")
(defvar yaml--root nil)
+(defun yaml--parse-block-header (header)
+ "Parse the HEADER string returning chomping style and indent count."
+ (let* ((pos 0)
+ (chomp-indicator :clip)
+ (indentation-indicator nil)
+ (char (and (< pos (length header)) (aref header pos))))
+ (cond
+ ((equal char ?\n) (list chomp-indicator indentation-indicator))
+ ((equal char ?\-) (progn (setq chomp-indicator :strip) (setq pos (1+
pos))))
+ ((equal char ?\+) (progn (setq chomp-indicator :keep) (setq pos (1+
pos)))))
+ (let ((char (and (< pos (length header)) (aref header pos))))
+ (when char
+ (cond
+ ((< ?0 char ?9)
+ (setq indentation-indicator (- char ?0)))))
+ (list chomp-indicator indentation-indicator))))
+
+(defun yaml--chomp-text (text-body chomp)
+ "Change the ending newline of TEXT-BODY based on CHOMP."
+ (cond ((eq :clip chomp) (concat (string-trim-right text-body "\n*") "\n"))
+ ((eq :strip chomp) (string-trim-right text-body "\n*"))
+ ((eq :keep chomp) text-body)))
+
(defun yaml--process-folded-text (text)
"Remvoe the header line for a folded match and return TEXT body properly
formatted with INDENTATION stripped."
- (let* ((header-line (substring text 0 (string-match "\n" text)))
- (text-body (substring text (1+ (string-match "\n" text)))))
- text-body))
+ (let* ((text (yaml--process-literal-text text))
+ (done))
+ (while (not done)
+ (let ((replaced (replace-regexp-in-string "\\([^\n]\\)\n\\([^\n ]\\)"
+ "\\1 \\2"
+ text)))
+ (when (equal replaced text)
+ (setq done t))
+ (setq text replaced)))
+ (replace-regexp-in-string "\n\\(\n+\\)\\([^\n]\\)" "\\1\\2" text)))
(defun yaml--process-literal-text (text)
"Remvoe the header line for a folded match and return TEXT body properly
formatted with INDENTATION stripped."
(let* ((header-line (substring text 0 (string-match "\n" text)))
- (text-body (substring text (1+ (string-match "\n" text)))))
- text-body))
+ (text-body (substring text (1+ (string-match "\n" text))))
+ (parsed-header (yaml--parse-block-header header-line))
+ (chomp (car parsed-header))
+ (starting-spaces (or (and (cadr parsed-header)
+ (make-string (cadr parsed-header) ?\s))
+ (let ((_ (string-match "^ *" text-body)))
+ (match-string 0 text-body))))
+ (lines (split-string text-body "\n"))
+ (striped-lines (seq-map (lambda (l)
+ (string-remove-prefix starting-spaces l))
+ lines))
+ (text-body (string-join striped-lines "\n")))
+ (yaml--chomp-text text-body chomp)))
(defun yaml--resolve-scalar-tag (scalar)
(cond
@@ -486,10 +527,10 @@ This flag is intended for development purposes.")
(replaced (substring replaced 1 (1- (length
replaced)))))
(yaml--add-event (yaml--scalar-event "double"
replaced)))))
("c-l+literal" . (lambda (text)
- (let* ((processed-text (yaml--process-literal-text text
indentation)))
+ (let* ((processed-text (yaml--process-literal-text
text)))
(yaml--add-event (yaml--scalar-event "folded"
processed-text)))))
("c-l+folded" . (lambda (text)
- (let* ((processed-text (yaml--process-folded-text text
indentation)))
+ (let* ((processed-text (yaml--process-folded-text text)))
(yaml--add-event (yaml--scalar-event "folded"
processed-text)))))
("e-scalar" . (lambda (text)
(yaml--add-event (yaml--scalar-event "plain" ""))))
- [elpa] externals/yaml e2fe7e2e57 042/124: Merge pull request #3 from zkry/add-yaml-spec-json, (continued)
- [elpa] externals/yaml e2fe7e2e57 042/124: Merge pull request #3 from zkry/add-yaml-spec-json, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 6d52649383 076/124: Fix list encoding indentation #29, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml bda9a00090 058/124: return alist key as a symbol as a default, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 45aa819a4f 068/124: Add MELPA tag to README, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 37c176fc5c 086/124: Fix the anchor parse event turning maps into lists, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml f546dac2a8 082/124: Initial implementation of position storage, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml c07cc6d0f3 089/124: Merge pull request #35 from zkry/fix-yaml-anchor-match-definition, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 14e6e4bc3d 095/124: Merge pull request #37 from zkry/fix-folded-block-parsing-error, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 0ac7f365bb 104/124: Merge pull request #41 from zkry/fix-property-prop-bug, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 900ca55584 119/124: Fix failing test for parsing scalars, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 964ef3b105 014/124: process literal and folded,
ELPA Syncer <=
- [elpa] externals/yaml a1d6e3211c 007/124: Initial working yaml-parser, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 5c52737f17 005/124: Start on event handling, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml aba7c19c47 020/124: Remove yaml spec, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 7c6c1e1abf 017/124: Rename names according to elisp standards, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 9a8ae65986 016/124: Reduce stack usage, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 9e18a714f5 006/124: completing tracer to parse basic key-value, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 0c51603d6d 018/124: Add tracing; fix linting errors, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 49f4ebd8ab 011/124: Minor bug fixes, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml 70429bd21b 025/124: Fix s-l+block-indented bug; fix auto-detect-indent bug, ELPA Syncer, 2024/11/29
- [elpa] externals/yaml ed9dded709 027/124: Fix l+block-mapping auto-detect-indent bug, ELPA Syncer, 2024/11/29