emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Daniel Ziltener <dziltener@lyrion.ch>
To: emacs-orgmode@gnu.org
Subject: Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option
Date: Sun, 30 Oct 2022 11:42:01 +0100	[thread overview]
Message-ID: <a7253078-dc7f-ec5a-9c20-44d4cab551cf@lyrion.ch> (raw)
In-Reply-To: <8735b6ar1q.fsf@localhost>


[-- Attachment #1.1.1: Type: text/plain, Size: 1222 bytes --]


Am 30.10.22 um 05:12 schrieb Ihor Radchenko:
> Daniel Ziltener <dziltener@lyrion.ch> writes:
>
>> From: Daniel Ziltener <dziltener@lyrion.ch>
>>
>> * lisp/ob-tangle.el (org-babel-tangle-single-block): strip noweb tags
>> from block if :noweb has been set to strip-tangle.
>> * lisp/ob-core.el (org-babel-common-header-args-w-values): add
>> "strip-tangle" as new allowed value.
>> * testing/lisp/test-ob-tangle.el (ob-tangle/strip-tangle): add new test
>> case for strip-tangle.
>> * doc/org-manual.org (Noweb Reference Syntax): adjust documentation for
>> the noweb header argument.
>> * etc/ORG-NEWS: add entry for new header argument value.
>>
>> This patch adds the "strip-tangle" option for the :noweb header
>> argument. This strips the noweb tags before tangling the block. This can
>> be useful for e.g. testing purposes where one wants to use a block as
>> test case that can be both run inline as well as tangled into a file for
>> automated testing.
> Thanks, but it looks like your PGP signature corrupted the email patch.
> Could you please create a separate patch file and attach it in the
> reply?
Sure, here's the patch attached, as generated by "git format-patch 
origin/main".

[-- Attachment #1.1.2: 0001-lisp-ob-tangle.el-lisp-ob-core.el-Add-strip-tangle.patch --]
[-- Type: text/x-patch, Size: 4804 bytes --]

From 0133162ef78e007fe4918016a4aabf8bc3734488 Mon Sep 17 00:00:00 2001
From: Daniel Ziltener <dziltener@lyrion.ch>
Date: Sun, 30 Oct 2022 01:20:53 +0200
Subject: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle

* lisp/ob-tangle.el (org-babel-tangle-single-block): strip noweb tags
from block if :noweb has been set to strip-tangle.
* lisp/ob-core.el (org-babel-common-header-args-w-values): add
"strip-tangle" as new allowed value.
* testing/lisp/test-ob-tangle.el (ob-tangle/strip-tangle): add new test
case for strip-tangle.
* doc/org-manual.org (Noweb Reference Syntax): adjust documentation for
the noweb header argument.
* etc/ORG-NEWS: add entry for new header argument value.

This patch adds the "strip-tangle" option for the :noweb header
argument. This strips the noweb tags before tangling the block. This can
be useful for e.g. testing purposes where one wants to use a block as
test case that can be both run inline as well as tangled into a file for
automated testing.
---
 doc/org-manual.org             |  6 ++++++
 etc/ORG-NEWS                   |  5 +++++
 lisp/ob-core.el                |  2 +-
 lisp/ob-tangle.el              |  4 +++-
 testing/lisp/test-ob-tangle.el | 26 ++++++++++++++++++++++++++
 5 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 18a050069..064d51bcd 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 exporting.
+
 - =no-export= ::
 
   Expansion of noweb syntax references in the body of the code block
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 6e875deb6..2c66d2e45 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -228,6 +228,11 @@ commands.
 =:noweb-prefix= can be set to =no= to prevent the prefix characters
 from being repeated when expanding a multiline noweb reference.
 
+*** New =:noweb= babel header argument value =strip-tangle=
+
+=:noweb= can be set to =strip-tangle= to strip the noweb syntax references
+before tangling.
+
 *** New LaTeX source block backend using =engraved-faces-latex=
 
 When ~org-latex-src-block-backend~ is set to ~engraved~,
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 518831ec6..c52f113b4 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -414,7 +414,7 @@ then run `org-babel-switch-to-session'."
     (mkdirp	. ((yes no)))
     (no-expand)
     (noeval)
-    (noweb	. ((yes no tangle no-export strip-export)))
+    (noweb	. ((yes no tangle strip-tangle no-export strip-export)))
     (noweb-ref	. :any)
     (noweb-sep  . :any)
     (noweb-prefix . ((no yes)))
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..af2a72682 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -510,6 +510,32 @@ 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


[-- Attachment #1.1.3: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 1205 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

  reply	other threads:[~2022-10-30 10:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a7253078-dc7f-ec5a-9c20-44d4cab551cf@lyrion.ch \
    --to=dziltener@lyrion.ch \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).