From ee102ded065adf0e08915ca978ce789b14c04ddb Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 14:35:21 +0800 Subject: [PATCH 01/11] Add rx dep --- origami-parsers.el | 1 + 1 file changed, 1 insertion(+) diff --git a/origami-parsers.el b/origami-parsers.el index 95ae526..1019f5e 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -37,6 +37,7 @@ (require 'cl-lib) (require 'dash) +(require 'rx) (require 's) (require 'subr-x) From 4c300a67ce0309b5032efa2f926fa1eca946e72a Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 14:48:37 +0800 Subject: [PATCH 02/11] Exclude all, 1 --- origami-parsers.el | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 1019f5e..46897a7 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -789,24 +789,7 @@ expressions." (not (string-match-p ignored-tags-regex (car pos)))))) (build-nodes (content) (rx-let - ((beg-tag - (seq "<" - ;; elements start with letter or _, don't match preamble - (any word "_") - (zero-or-more - (or - ;; anything but closing tag or attribute - (not (any ">" - ;; ignore self-closing tags - "/" - ;; attribute values require their own matching - "\"")) - ;; attribute value - (seq "\"" - (zero-or-more (not "\"")) - "\""))) - ">")) - (end-tag + ((end-tag (seq "")) ">"))) ;; no need to care for comments/CDATA, these pos are filtered by face ;; in valid-pos-p From 1c9d6de8041b0b65b143f9c9d7293998a611d5f6 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 14:51:36 +0800 Subject: [PATCH 03/11] Print build form --- origami-parsers.el | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/origami-parsers.el b/origami-parsers.el index 46897a7..24e1e68 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -782,6 +782,7 @@ expressions." (defun origami-xml-base-parser (create &optional remove-leaves ignored-tags-regex) "Base parser for xml style markup." + (message "%s" rx--builtin-forms) (cl-labels ((valid-pos-p (pos) (and (origami-filter-code-face pos) @@ -789,7 +790,24 @@ expressions." (not (string-match-p ignored-tags-regex (car pos)))))) (build-nodes (content) (rx-let - ((end-tag + ((beg-tag + (seq "<" + ;; elements start with letter or _, don't match preamble + (any word "_") + (zero-or-more + (or + ;; anything but closing tag or attribute + (not (any ">" + ;; ignore self-closing tags + "/" + ;; attribute values require their own matching + "\"")) + ;; attribute value + (seq "\"" + (zero-or-more (not "\"")) + "\""))) + ">")) + (end-tag (seq "")) ">"))) ;; no need to care for comments/CDATA, these pos are filtered by face ;; in valid-pos-p From 794bf66fa5bf3b892a33dfa492a3f101fcb20105 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 14:55:25 +0800 Subject: [PATCH 04/11] Print build form --- origami-parsers.el | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/origami-parsers.el b/origami-parsers.el index 24e1e68..a7a06ab 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -780,9 +780,12 @@ expressions." (lambda (match &rest _) (+ (point) (length match) 1)))))) +(eval-when-compile + (dolist (form rx--builtin-forms) + (message "> %s" form))) + (defun origami-xml-base-parser (create &optional remove-leaves ignored-tags-regex) "Base parser for xml style markup." - (message "%s" rx--builtin-forms) (cl-labels ((valid-pos-p (pos) (and (origami-filter-code-face pos) From 000d4fda7889cd8bbe9490679bfe17c6fd903a95 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 14:57:30 +0800 Subject: [PATCH 05/11] Print build form --- origami-parsers.el | 1 + 1 file changed, 1 insertion(+) diff --git a/origami-parsers.el b/origami-parsers.el index a7a06ab..8352a2c 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -781,6 +781,7 @@ expressions." (+ (point) (length match) 1)))))) (eval-when-compile + (require 'rx) (dolist (form rx--builtin-forms) (message "> %s" form))) From aabafd9a345749701b6295b8c3ac9f2a467d300f Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 15:03:56 +0800 Subject: [PATCH 06/11] Test operation --- origami-parsers.el | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 8352a2c..7a72598 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -780,11 +780,6 @@ expressions." (lambda (match &rest _) (+ (point) (length match) 1)))))) -(eval-when-compile - (require 'rx) - (dolist (form rx--builtin-forms) - (message "> %s" form))) - (defun origami-xml-base-parser (create &optional remove-leaves ignored-tags-regex) "Base parser for xml style markup." (cl-labels @@ -794,7 +789,10 @@ expressions." (not (string-match-p ignored-tags-regex (car pos)))))) (build-nodes (content) (rx-let - ((beg-tag + ((other-tag-1 (seq "<")) + (other-tag-2 (any word "_")) + (other-tag-3 (zero-or-more "_")) + (beg-tag (seq "<" ;; elements start with letter or _, don't match preamble (any word "_") From d8513d466d99304e09f669f12df0f0dd7677a747 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 15:07:42 +0800 Subject: [PATCH 07/11] Remove whole beg-tag --- origami-parsers.el | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 7a72598..5b13d86 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -789,26 +789,7 @@ expressions." (not (string-match-p ignored-tags-regex (car pos)))))) (build-nodes (content) (rx-let - ((other-tag-1 (seq "<")) - (other-tag-2 (any word "_")) - (other-tag-3 (zero-or-more "_")) - (beg-tag - (seq "<" - ;; elements start with letter or _, don't match preamble - (any word "_") - (zero-or-more - (or - ;; anything but closing tag or attribute - (not (any ">" - ;; ignore self-closing tags - "/" - ;; attribute values require their own matching - "\"")) - ;; attribute value - (seq "\"" - (zero-or-more (not "\"")) - "\""))) - ">")) + ((beg-tag "<") (end-tag (seq "")) ">"))) ;; no need to care for comments/CDATA, these pos are filtered by face From 15dd4fdb135d28048bf769010dc05325c65cbc3d Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 15:11:18 +0800 Subject: [PATCH 08/11] Rename beg-tag --- origami-parsers.el | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/origami-parsers.el b/origami-parsers.el index 5b13d86..4bf9413 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -789,7 +789,23 @@ expressions." (not (string-match-p ignored-tags-regex (car pos)))))) (build-nodes (content) (rx-let - ((beg-tag "<") + ((beg-tag-2 + (seq "<" + ;; elements start with letter or _, don't match preamble + (any word "_") + (zero-or-more + (or + ;; anything but closing tag or attribute + (not (any ">" + ;; ignore self-closing tags + "/" + ;; attribute values require their own matching + "\"")) + ;; attribute value + (seq "\"" + (zero-or-more (not "\"")) + "\""))) + ">")) (end-tag (seq "")) ">"))) ;; no need to care for comments/CDATA, these pos are filtered by face From 8c105bc353c7736dce806a7b91d22a2365485da9 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 15:14:32 +0800 Subject: [PATCH 09/11] Remove whole function --- origami-parsers.el | 63 ---------------------------------------------- 1 file changed, 63 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 4bf9413..0c8d664 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -780,69 +780,6 @@ expressions." (lambda (match &rest _) (+ (point) (length match) 1)))))) -(defun origami-xml-base-parser (create &optional remove-leaves ignored-tags-regex) - "Base parser for xml style markup." - (cl-labels - ((valid-pos-p (pos) - (and (origami-filter-code-face pos) - (or (null ignored-tags-regex) - (not (string-match-p ignored-tags-regex (car pos)))))) - (build-nodes (content) - (rx-let - ((beg-tag-2 - (seq "<" - ;; elements start with letter or _, don't match preamble - (any word "_") - (zero-or-more - (or - ;; anything but closing tag or attribute - (not (any ">" - ;; ignore self-closing tags - "/" - ;; attribute values require their own matching - "\"")) - ;; attribute value - (seq "\"" - (zero-or-more (not "\"")) - "\""))) - ">")) - (end-tag - (seq "")) ">"))) - ;; no need to care for comments/CDATA, these pos are filtered by face - ;; in valid-pos-p - (let* ((rx-beg-tag (rx beg-tag)) - (rx-end-tag (rx end-tag)) - (rx-all (rx (or beg-tag end-tag))) - (positions (origami-get-positions content rx-all #'valid-pos-p))) - (origami-build-pair-tree create rx-beg-tag rx-end-tag nil positions)))) - (trim (nodes) - ;; trims leaves, if required - ;; no artificial root node yet, so base level is a list of nodes - (if (not remove-leaves) - nodes - (let ((trimmed-nodes - ;; remove base level leaves - (seq-remove (lambda (node) (null (origami-fold-children node))) - nodes))) - (if (seq-empty-p trimmed-nodes) - trimmed-nodes - ;; recurse to remove deeper level leaves - (-map #'trim-recurse trimmed-nodes))))) - (trim-recurse (tree) - (if (origami-fold-children tree) - ;; has children - recurse for children, and don't remove this node - (let ((trimmed-childs - (-map #'trim-recurse (origami-fold-children tree)))) - ;; return copy of node with filtered childs - (origami-fold-children-set tree - (seq-remove #'null trimmed-childs))) - ;; no childrens, remove this node - nil))) - (lambda (content) - (-some-> content - build-nodes - trim)))) - (defcustom origami-xml-skip-leaf-nodes t "In xml files, only elements with child elements will be foldable, not if they contain text only." From d509e9f8baa3ab61d402ec8d8faa0b5f705d7fd8 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 15:17:14 +0800 Subject: [PATCH 10/11] Remove all rx --- origami-parsers.el | 9 --------- 1 file changed, 9 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 0c8d664..d792947 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -790,15 +790,6 @@ foldable, not if they contain text only." "Parser for xml." (origami-xml-base-parser create origami-xml-skip-leaf-nodes)) -(defun origami-html-parser (create) - "Parser for html." - (rx-let ((ignore-tags (&rest tags) (seq "<" (or tags) word-end))) - ;; Self-closing tags (void elements) without closing slash would throw off parser, ignore - (let ((ignore-tags-rx - (rx (ignore-tags "area" "base" "br" "col" "command" "embed" "hr" "img" "input" - "keygen" "link" "menuitem" "meta" "param" "source" "track" "wbr")))) - (origami-xml-base-parser create nil ignore-tags-rx)))) - (defun origami-json-parser (create) "Parser for JSON." (lambda (content) From f2a24c556c99998c19532bc08e5adbe501d0e60c Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 29 Mar 2021 15:22:34 +0800 Subject: [PATCH 11/11] Revert all --- origami-parsers.el | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/origami-parsers.el b/origami-parsers.el index d792947..1019f5e 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -780,6 +780,69 @@ expressions." (lambda (match &rest _) (+ (point) (length match) 1)))))) +(defun origami-xml-base-parser (create &optional remove-leaves ignored-tags-regex) + "Base parser for xml style markup." + (cl-labels + ((valid-pos-p (pos) + (and (origami-filter-code-face pos) + (or (null ignored-tags-regex) + (not (string-match-p ignored-tags-regex (car pos)))))) + (build-nodes (content) + (rx-let + ((beg-tag + (seq "<" + ;; elements start with letter or _, don't match preamble + (any word "_") + (zero-or-more + (or + ;; anything but closing tag or attribute + (not (any ">" + ;; ignore self-closing tags + "/" + ;; attribute values require their own matching + "\"")) + ;; attribute value + (seq "\"" + (zero-or-more (not "\"")) + "\""))) + ">")) + (end-tag + (seq "")) ">"))) + ;; no need to care for comments/CDATA, these pos are filtered by face + ;; in valid-pos-p + (let* ((rx-beg-tag (rx beg-tag)) + (rx-end-tag (rx end-tag)) + (rx-all (rx (or beg-tag end-tag))) + (positions (origami-get-positions content rx-all #'valid-pos-p))) + (origami-build-pair-tree create rx-beg-tag rx-end-tag nil positions)))) + (trim (nodes) + ;; trims leaves, if required + ;; no artificial root node yet, so base level is a list of nodes + (if (not remove-leaves) + nodes + (let ((trimmed-nodes + ;; remove base level leaves + (seq-remove (lambda (node) (null (origami-fold-children node))) + nodes))) + (if (seq-empty-p trimmed-nodes) + trimmed-nodes + ;; recurse to remove deeper level leaves + (-map #'trim-recurse trimmed-nodes))))) + (trim-recurse (tree) + (if (origami-fold-children tree) + ;; has children - recurse for children, and don't remove this node + (let ((trimmed-childs + (-map #'trim-recurse (origami-fold-children tree)))) + ;; return copy of node with filtered childs + (origami-fold-children-set tree + (seq-remove #'null trimmed-childs))) + ;; no childrens, remove this node + nil))) + (lambda (content) + (-some-> content + build-nodes + trim)))) + (defcustom origami-xml-skip-leaf-nodes t "In xml files, only elements with child elements will be foldable, not if they contain text only." @@ -790,6 +853,15 @@ foldable, not if they contain text only." "Parser for xml." (origami-xml-base-parser create origami-xml-skip-leaf-nodes)) +(defun origami-html-parser (create) + "Parser for html." + (rx-let ((ignore-tags (&rest tags) (seq "<" (or tags) word-end))) + ;; Self-closing tags (void elements) without closing slash would throw off parser, ignore + (let ((ignore-tags-rx + (rx (ignore-tags "area" "base" "br" "col" "command" "embed" "hr" "img" "input" + "keygen" "link" "menuitem" "meta" "param" "source" "track" "wbr")))) + (origami-xml-base-parser create nil ignore-tags-rx)))) + (defun origami-json-parser (create) "Parser for JSON." (lambda (content)