emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Tangling org file with nested org source block
@ 2017-09-28  2:49 Thibault Marin
  2017-10-21  4:05 ` Thibault Marin
  0 siblings, 1 reply; 3+ messages in thread
From: Thibault Marin @ 2017-09-28  2:49 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org


Hi list,

I am trying to tangle the following org 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
`----

(note the double commas at the beginning of the inner-most block).
I am working on master (Org mode version 9.1.1 (release_9.1.1-79-g731e1c.dirty @
.../org-mode/lisp/)), and I get the following:

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

The inner block does not get escaped which causes issues when exporting.  Trying
to investigate this, I ended up in the ~org-babel-tangle-single-block~ function
in =ob-tangle.el=.  The second comma is removed by a call to
~org-unescape-code-in-string~ (l. 497).  Since the incoming string has already
been cleaned-up (i.e. the first comma removed) at that point I wonder why this
call is necessary.  When I remove that call to ~org-unescape-code-in-string~,
tangling produces the output I expect:

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

where the inner source block is properly escaped.

So, is the call to ~org-unescape-code-in-string~ required?  Does anyone know if
there is a way to get the desired output?

Thanks in advance,
thibault

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

* Re: Tangling org file with nested org source block
  2017-09-28  2:49 Tangling org file with nested org source block Thibault Marin
@ 2017-10-21  4:05 ` Thibault Marin
  2017-10-22 10:43   ` Nicolas Goaziou
  0 siblings, 1 reply; 3+ messages in thread
From: Thibault Marin @ 2017-10-21  4:05 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 909 bytes --]


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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-tangle.el-Fix-tangling-of-org-block-with-nested-s.patch --]
[-- Type: text/x-diff, Size: 2252 bytes --]

From 6300856f8b9623b1ac0a40b7fe62751805159558 Mon Sep 17 00:00:00 2001
From: thibault <thibault.marin@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


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

* Re: Tangling org file with nested org source block
  2017-10-21  4:05 ` Thibault Marin
@ 2017-10-22 10:43   ` Nicolas Goaziou
  0 siblings, 0 replies; 3+ messages in thread
From: Nicolas Goaziou @ 2017-10-22 10:43 UTC (permalink / raw)
  To: Thibault Marin; +Cc: emacs-orgmode@gnu.org

Hello,

Thibault Marin <thibault.marin@gmx.com> writes:


> From 6300856f8b9623b1ac0a40b7fe62751805159558 Mon Sep 17 00:00:00 2001
> From: thibault <thibault.marin@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

It looks good. Applied. Thank you.

Regards,

-- 
Nicolas Goaziou

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

end of thread, other threads:[~2017-10-22 10:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-28  2:49 Tangling org file with nested org source block Thibault Marin
2017-10-21  4:05 ` Thibault Marin
2017-10-22 10:43   ` Nicolas Goaziou

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).