emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option
@ 2022-10-26 19:37 dziltener
  2022-10-28  2:19 ` Ihor Radchenko
  2022-10-30  4:17 ` Timothy
  0 siblings, 2 replies; 12+ messages in thread
From: dziltener @ 2022-10-26 19:37 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Daniel Ziltener

From: Daniel Ziltener <dziltener@lyrion.ch>

* ob-tangle.el, ob-core.el, test-ob-tangle.el, org-manual.org: Add a
"strip-tangle" noweb option to strip the noweb tags when tangling, but
keep and expand them otherwise.
---
 doc/org-manual.org             |  6 ++++++
 lisp/ob-core.el                |  6 +++---
 lisp/ob-tangle.el              |  4 +++-
 testing/lisp/test-ob-tangle.el | 27 +++++++++++++++++++++++++++
 4 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 18a050069..cb85f4702 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -18993,6 +18993,12 @@ tangled, or exported.
   Expansion of noweb syntax references in the body of the code block
   when tangling.  No expansion when evaluating or exporting.
 
+- =strip-tangle= ::
+
+  Expansion of noweb syntax references in the body of the code block
+  when evaluating or exporting. Removes noweb syntax references
+  when tangling.
+
 - =no-export= ::
 
   Expansion of noweb syntax references in the body of the code block
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 518831ec6..ae77182ef 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2880,9 +2880,9 @@ parameters when merging lists."
   "Check if PARAMS require expansion in CONTEXT.
 CONTEXT may be one of :tangle, :export or :eval."
   (let ((allowed-values (cl-case context
-			  (:tangle '("yes" "tangle" "no-export" "strip-export"))
-			  (:eval   '("yes" "no-export" "strip-export" "eval"))
-			  (:export '("yes")))))
+			  (:tangle '("yes" "tangle" "no-export" "strip-export" "strip-tangle"))
+			  (:eval   '("yes" "no-export" "strip-export" "eval" "strip-tangle"))
+			  (:export '("yes" "strip-tangle")))))
     (cl-some (lambda (v) (member v allowed-values))
 	     (split-string (or (cdr (assq :noweb params)) "")))))
 
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 2da92efaf..d9d847195 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -536,7 +536,9 @@ non-nil, return the full association list to be used by
 	 (body
 	  ;; Run the tangle-body-hook.
           (let ((body (if (org-babel-noweb-p params :tangle)
-			  (org-babel-expand-noweb-references info)
+                          (if (string= "strip-tangle" (cdr (assq :noweb (nth 2 info))))
+                              (replace-regexp-in-string (org-babel-noweb-wrap) "" (nth 1 info))
+			    (org-babel-expand-noweb-references info))
 			(nth 1 info))))
 	    (with-temp-buffer
 	      (insert
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index a0003ee40..455745e5c 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -510,6 +510,33 @@ another block
 		    (org-split-string (buffer-string))))
 	      (delete-file file))))))
 
+(ert-deftest ob-tangle/strip-tangle ()
+  "Test if strip-tangle works correctly when tangling noweb code blocks."
+  (should
+   (equal '("1")
+          (let ((file (make-temp-file "org-tangle-")))
+            (unwind-protect
+                (progn
+                  (org-test-with-temp-text-in-file
+                   (format "
+#+name: block1
+#+begin_src elisp
+2
+#+end_src
+
+#+begin_src elisp :noweb strip-tangle :tangle %s
+1<<block1>>
+#+end_src
+"
+                           file)
+                   (let ((org-babel-noweb-error-all-langs nil)
+                         (org-babel-noweb-error-langs nil))
+                     (org-babel-tangle)))
+                  (with-temp-buffer
+                    (insert-file-contents file)
+                    (org-split-string (buffer-string))))
+              (delete-file file))))))
+
 (ert-deftest ob-tangle/detangle-false-positive ()
   "Test handling of false positive link during detangle."
   (let (buffer)
-- 
2.35.3



^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-12-27 11:01 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-26 19:37 [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option dziltener
2022-10-28  2:19 ` Ihor Radchenko
2022-10-28  8:09   ` Daniel Ziltener
2022-10-29  3:49     ` Ihor Radchenko
2022-10-29 23:34       ` Daniel Ziltener
2022-10-30  4:12         ` Ihor Radchenko
2022-10-30 10:42           ` Daniel Ziltener
2022-11-02  6:17             ` Ihor Radchenko
2022-12-27 10:59               ` Ihor Radchenko
2022-10-30  4:17 ` Timothy
2022-10-31  3:24   ` Ihor Radchenko
2022-10-31  3:31     ` Timothy

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).