From: "s@1042.ch" <s@1042.ch>
To: Emacs-orgmode@gnu.org
Subject: Tangling with cross-references coming from included files
Date: Tue, 28 Mar 2017 09:20:48 +0200 [thread overview]
Message-ID: <B2BDA194-943D-47A4-A852-697308054F8B@1042.ch> (raw)
[-- 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 --]
next reply other threads:[~2017-03-28 7:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-28 7:20 s [this message]
2017-03-28 14:06 ` Tangling with cross-references coming from included files Nick Dokos
2017-03-28 15:15 ` Sven Osterwalder
2017-03-29 14:41 ` Nicolas Goaziou
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=B2BDA194-943D-47A4-A852-697308054F8B@1042.ch \
--to=s@1042.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).