Following-up on the issue I reported some time ago with tangling of
nested source blocks, I would like to propose a patch for review.

Before the patch, the following file:

,----
| #+PROPERTY: header-args :tangle output.org
| 
| #+BEGIN_SRC org
| 
| ,* Test
| 
| ,#+BEGIN_SRC org
| ,,#+BEGIN_SRC emacs-lisp
| '(1 2 3)
| ,,#+END_SRC
| ,#+END_SRC
| 
| #+END_SRC
`----

is tangled to:

,---- output.org
| * Test
| 
| #+BEGIN_SRC org
| #+BEGIN_SRC emacs-lisp
| '(1 2 3)
| #+END_SRC
| #+END_SRC
`----

which misses the escaping (commas) for the inner source block.

Tangling after applying the proposed patch results in:

,---- output.org
| * Test
| 
| #+BEGIN_SRC org
| ,#+BEGIN_SRC emacs-lisp
| '(1 2 3)
| ,#+END_SRC
| #+END_SRC
`----

which is properly escaped.

The patch also contains a simple test to validate tangling of nested
blocks.

Could this be considered for a merge?

Thanks in advance

thibault

>From 6300856f8b9623b1ac0a40b7fe62751805159558 Mon Sep 17 00:00:00 2001
From: thibault <thibault.ma...@gmx.com>
Date: Fri, 20 Oct 2017 22:20:35 -0500
Subject: [PATCH] ob-tangle.el: Fix tangling of org block with nested source
 block

* lisp/ob-tangle.el (org-babel-tangle-single-block): Prevent double unescaping
of source block by removing unnecessary call to `org-unescape-code-in-string'.

* testing/lisp/test-ob-tangle.el (ob-tangle/nested-block) New function.  Test
tangle of org block with nested source block.
---
 lisp/ob-tangle.el              |  7 +++----
 testing/lisp/test-ob-tangle.el | 25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index adc680676..09d011fc3 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -494,10 +494,9 @@ non-nil, return the full association list to be used by
 		  link)
 		source-name
 		params
-		(org-unescape-code-in-string
-		 (if org-src-preserve-indentation
-		     (org-trim body t)
-		   (org-trim (org-remove-indentation body))))
+		(if org-src-preserve-indentation
+		    (org-trim body t)
+		  (org-trim (org-remove-indentation body)))
 		comment)))
     (if only-this-block
 	(list (cons src-lang (list result)))
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index 06a73f063..73b75323e 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -197,6 +197,31 @@ another block
           (org-babel-tangle-jump-to-org)
           (buffer-string)))))))
 
+(ert-deftest ob-tangle/nested-block ()
+  "Test tangling of org file with nested block."
+  (should
+   (string=
+    "#+begin_src org
+,#+begin_src emacs-lisp
+1
+,#+end_src
+#+end_src
+"
+    (org-test-with-temp-text-in-file
+        "#+header: :tangle \"test-ob-tangle.org\"
+#+begin_src org
+,#+begin_src org
+,,#+begin_src emacs-lisp
+1
+,,#+end_src
+,#+end_src
+#+end_src"
+      (unwind-protect
+          (progn (org-babel-tangle)
+                 (with-temp-buffer (insert-file-contents "test-ob-tangle.org")
+                                   (buffer-string)))
+        (delete-file "test-ob-tangle.org"))))))
+
 (provide 'test-ob-tangle)
 
 ;;; test-ob-tangle.el ends here
-- 
2.14.1

Reply via email to