* Tangling with cross-references coming from included files
@ 2017-03-28 7:20 s
2017-03-28 14:06 ` Nick Dokos
2017-03-29 14:41 ` Nicolas Goaziou
0 siblings, 2 replies; 4+ messages in thread
From: s @ 2017-03-28 7:20 UTC (permalink / raw)
To: Emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 3935 bytes --]
Hi
I am trying to achieve the tangling of an org-file which is composed by
multiple (included) org-files.
I am using Emacs 25.1.1 and Org mode 9.0.5.
Here an example of what I am trying to achieve.
——————Example starts here——————
==== File A.org ====
[Text here …]
#+BEGIN_SRC python :tangle src/a.py :noweb tangle
class Foo(object):
<<file-b-method-foo>>
#+END
[More text here ...]
#+INCLUDE: “./b.org"
==== File b.org ====
[Text …]
#+BEGIN_SRC python :exports none
def foo(self):
print(“bar”)
#+END
——————Example ends here——————
My problem is, that the included parts are not being tangled. When
having all the parts in one file, there is no problem at all, but having only file is not an option.
An easy approach would be, as I thought at first, to use "org-org-export-as-org", which does include all into one buffer.
Then one could simply tangle that buffer. The problem with that approach is however, that all arguments
get stripped from #+BEGIN_SRC and therefore “:tangle …” too.
So, looking at the list’s archive and on org mode hacks, I found a workaround, which uses a hook
when exporting ([1] and [2]):
#+begin_src emacs-lisp
(defun ded/org-export-as-org-to-buffer ()
(interactive)
(let* ((tmp-file (make-temp-file "org-tangle-with-include"))
(org-export-preprocess-after-include-files-hook
`((lambda () (let ((s (buffer-string)))
(with-temp-file ,tmp-file (insert s)))))))
(save-window-excursion (org-export-as-html-to-buffer nil))
(switch-to-buffer
(get-buffer-create "*Org Org Export*"))
(insert-file-contents tmp-file))
(org-mode))
(defun ded/tangle-with-include-files ()
(interactive)
(save-window-excursion
(ded/org-export-as-org-to-buffer)
(org-babel-tangle)))
#+end_src
That is to use the hook to make a copy of the buffer right before exporting. This buffer, which has all files tied
together, gets written to a temp. file and this file is then tangled. This is done using the string “buffer-string”
generated by the exporter.
The problem with that solution is, that it concerns org versions < 9, using the “old” exporter.
Within the new exporter there is no such hook and therefore no string “buffer-string” holding
the complete buffer (as far as I saw).
Instead there is org-before-parsing-hook [3], wich does essentially the same (as far as I understood).
But instead using a string for copying the buffer for exporting, the exporter uses a temporary buffer,
“buf-copy”.
I tried to get the contents of that buffer within the hook like so:
#+begin_src emacs-lisp
(defun ded/org-export-as-org-to-buffer ()
(interactive)
(let* ((tmp-file (make-temp-file "org-tangle-with-include"))
(org-before-parsing-hook
`((lambda () (setq ((b (get-buffer ,buf-copy)))
(with-temp-file ,tmp-file (insert b)))))))
(save-window-excursion (org-export-as-html-to-buffer nil))
(switch-to-buffer
(get-buffer-create "*Org Org Export*"))
(insert-file-contents tmp-file))
(org-mode))
(defun ded/tangle-with-include-files ()
(interactive)
(save-window-excursion
(ded/org-export-as-org-to-buffer)
(org-babel-tangle)))
#+end_src
But it does not work as buf-copy is void. What am I doing wrong? How do I get the buffer “buf-copy”?
I am assuming that it is “buf-copy” as the call of the hook happens within the context of “org-export-with-buffer-copy”.
Is my assumption wrong?
Any help is greatly appreciated.
Sven
[1]: http://orgmode.org/worg/org-hacks.html#sec-1-10-2
[2]: https://lists.gnu.org/archive/html/emacs-orgmode/2010-09/msg00771.html
[3]: http://orgmode.org/worg/doc.html#org-export-before-parsing-hook
[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Tangling with cross-references coming from included files
2017-03-28 7:20 Tangling with cross-references coming from included files s
@ 2017-03-28 14:06 ` Nick Dokos
2017-03-28 15:15 ` Sven Osterwalder
2017-03-29 14:41 ` Nicolas Goaziou
1 sibling, 1 reply; 4+ messages in thread
From: Nick Dokos @ 2017-03-28 14:06 UTC (permalink / raw)
To: emacs-orgmode
"s@1042.ch" <s@1042.ch> writes:
> ...
> Instead there is org-before-parsing-hook [3], wich does essentially the same (as far as I understood).
> But instead using a string for copying the buffer for exporting, the exporter uses a temporary buffer,
> “buf-copy”.
>
> I tried to get the contents of that buffer within the hook like so:
>
> #+begin_src emacs-lisp
> (defun ded/org-export-as-org-to-buffer ()
> (interactive)
> (let* ((tmp-file (make-temp-file "org-tangle-with-include"))
> (org-before-parsing-hook
> `((lambda () (setq ((b (get-buffer ,buf-copy)))
> (with-temp-file ,tmp-file (insert b)))))))
> (save-window-excursion (org-export-as-html-to-buffer nil))
> (switch-to-buffer
> (get-buffer-create "*Org Org Export*"))
> (insert-file-contents tmp-file))
> (org-mode))
>
> (defun ded/tangle-with-include-files ()
> (interactive)
> (save-window-excursion
> (ded/org-export-as-org-to-buffer)
> (org-babel-tangle)))
> #+end_src
>
> But it does not work as buf-copy is void. What am I doing wrong? How do I get the buffer “buf-copy”?
> I am assuming that it is “buf-copy” as the call of the hook happens
> within the context of “org-export-with-buffer-copy”.
> Is my assumption wrong?
>
The hook is called 'org-export-before-parsing-hook', but whether that solves your problem or not,
I don't know.
--
Nick
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Tangling with cross-references coming from included files
2017-03-28 14:06 ` Nick Dokos
@ 2017-03-28 15:15 ` Sven Osterwalder
0 siblings, 0 replies; 4+ messages in thread
From: Sven Osterwalder @ 2017-03-28 15:15 UTC (permalink / raw)
To: Emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 712 bytes --]
Hi Nick
> On 28 Mar 2017, at 16:06, Nick Dokos <ndokos@gmail.com> wrote:
>
> The hook is called 'org-export-before-parsing-hook', but whether that solves your problem or not,
> I don't know.
>
> --
> Nick
Thank you for your answer. Yes, that was a typo, I actually used that hook (as you
wrote it).
Furthermore I forgot to mention, that the second code snippet, the one in file B, is named:
==== File b.org ====
[Text …]
#+NAME: file-b-method-foo
#+BEGIN_SRC python :exports none
def foo(self):
print(“bar”)
#+END
I even tried to use CUSTOM_ID (and ID as well), but babel does not seem to resolve
links as [[file:b.org::file-b-method-foo]] when tangling.
Sven
[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Tangling with cross-references coming from included files
2017-03-28 7:20 Tangling with cross-references coming from included files s
2017-03-28 14:06 ` Nick Dokos
@ 2017-03-29 14:41 ` Nicolas Goaziou
1 sibling, 0 replies; 4+ messages in thread
From: Nicolas Goaziou @ 2017-03-29 14:41 UTC (permalink / raw)
To: s@1042.ch; +Cc: Emacs-orgmode
Hello,
"s@1042.ch" <s@1042.ch> writes:
> I am trying to achieve the tangling of an org-file which is composed by
> multiple (included) org-files.
>
> I am using Emacs 25.1.1 and Org mode 9.0.5.
>
> Here an example of what I am trying to achieve.
>
> ——————Example starts here——————
> ==== File A.org ====
>
> [Text here …]
>
> #+BEGIN_SRC python :tangle src/a.py :noweb tangle
> class Foo(object):
> <<file-b-method-foo>>
> #+END
>
> [More text here ...]
>
> #+INCLUDE: ./b.org"
>
> ==== File b.org ====
>
> [Text &]
>
> #+BEGIN_SRC python :exports none
> def foo(self):
> print(bar)
> #+END
> \x14\x14\x14Example ends here\x14\x14\x14
>
> My problem is, that the included parts are not being tangled. When
> having all the parts in one file, there is no problem at all, but having only file is not an option.
>
> An easy approach would be, as I thought at first, to use "org-org-export-as-org", which does include all into one buffer.
> Then one could simply tangle that buffer. The problem with that approach is however, that all arguments
> get stripped from #+BEGIN_SRC and therefore :tangle & too.
Would calling
(org-export-expand-include-keyword)
in the master document solve your issue?
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-03-29 14:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-28 7:20 Tangling with cross-references coming from included files s
2017-03-28 14:06 ` Nick Dokos
2017-03-28 15:15 ` Sven Osterwalder
2017-03-29 14:41 ` 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).