Inside the current buffer
The simplest way is to create a function that does those two things for you.
When you press (C-c C-e h h
) the function org-html-export-to-html
is run
in the current buffer.
When you press (C-c C-v t
) the function org-babel-tangle
is run in the
current buffer.
Therefor you can use the following function to do both for you:
(defun export-tangle ()
"Shortcut for exporting and tangling the current org-mode buffer."
(interactive)
(org-html-export-to-html)
(org-babel-tangle))
You can then bind this function to Alt+F2
using:
(add-hook
'org-mode-hook
(lambda ()
(local-set-key (kbd "M-<f2>") 'export-tangle)))
In a script
Sometimes you want to do this on files instead of the currently open buffer.
You can then create a similar function that opens the file for you and runs
the functions for you:
(defun export-tangle-file (filepath)
"Shortcut for exporting and tangling the org-mode file at `filepath'."
(find-file (expand-file-name filepath))
(org-html-export-to-html)
(org-babel-tangle)
(kill-buffer))
You can then use that function in a script for example. Like this:
(export-tangle-file "article.org")
For all files in a directory
You can even setup org-mode to publish and tangle a whole directory tree full
of org-mode files for you using the setting org-publish-project-alist
.
Some people use this to write blogs in org-mode and then publish the whole
blog using (C-c C-e P p
).
With org-publish-project-alist
you create components that does different
types of publishing.
Here's an example of how you can set up copying static assets, publishing
org-mode articles and tangling org-mode articles:
(setq org-publish-project-alist
'(("assets"
:base-directory "~/blog/articles/assets/"
:base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf"
:publishing-directory "~/blog/output/assets/"
:recursive t
:publishing-function org-publish-attachment)
("articles"
:base-directory "~/blog/articles/"
:publishing-directory "~/blog/output/"
:recursive t
:publishing-function org-html-publish-to-html)
("tangles"
:base-directory "~/blog/articles/"
:publishing-directory "~/blog/output/tangles/"
:recursive t
:publishing-function org-babel-tangle-publish)
("notebook"
:components ("assets" "tangles" "articles"))))
Note: org-table-tangle-publish
will put all tangles in the root of the publish-directory
.
So if you have setup tangling with a directory structure, it will be flattened.
I also encountered an error when the directory publish-directory
didn't exist.
After creating the directory it worked.