From d938e25503324ccdbb6a353e62064c39fda3a439 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Sat, 29 Feb 2020 17:08:05 +0100 Subject: [PATCH] add tangle to code block --- src/posts/0011-export-tangle-names/index.org | 53 +++++++++++++++++++ .../org_html_export_show_tangle.el | 9 ++++ 2 files changed, 62 insertions(+) create mode 100644 src/posts/0011-export-tangle-names/index.org create mode 100644 src/posts/0011-export-tangle-names/org_html_export_show_tangle.el diff --git a/src/posts/0011-export-tangle-names/index.org b/src/posts/0011-export-tangle-names/index.org new file mode 100644 index 0000000..1f5f150 --- /dev/null +++ b/src/posts/0011-export-tangle-names/index.org @@ -0,0 +1,53 @@ +#+title: Add links to code block during orgmode export +#+date: [2020-02-29 Sat] +#+author: Yann Esposito +#+EMAIL: yann@esposito.host +#+keywords: org-mode, blog +#+DESCRIPTION: add links to code block during orgmode export +#+OPTIONS: auto-id:t toc:t +#+STARTUP: overview + +I wanted to add a link to the file I export with org tangle. +And it was surprisingly difficult to find. +Apparently I am one of the few people that use orgmode the way I do. + +Using orgmode file as markdown to blog. +And exporting to a different file some code block. +So I often endup writing something like: + +#+begin_src org +..begin_src elisp :tangle foo.el +#+end_src + +I tangle the source code that export the code block to an external file. +Then I use this hook during HTML export to add a caption with the link the +file I tangled: + +#+begin_src elisp :tangle org_html_export_show_tangle.el +(defun my-add-link-to-tangled-files (backend) + "Add a link just before source code block with tangled files. +BACKEND is the export backend. Used as symbol." + (while ;; (re-search-forward ) + (re-search-forward "^\\( *\\)#\\+begin_src .*:tangle \\([^\s\n]*\\)" nil t) + (replace-match "\\1#+CAPTION: [[./\\2][=\\2=]]\n\\&"))) + +(add-hook 'org-export-before-processing-hook + 'my-add-link-to-tangled-files) +#+end_src + +And this article is an example of the result. +The link with the listing is generated automatically for me. + +A small note regarding CSS. +My =pre= have a =margin-top=. +But I wanted to get rid of it when the previous block was a =label=. +This is achievable with: + +#+begin_src css +label + pre {margin-top: 0;} +#+end_src + +That's it. +It took me really a long time to just think about using caption, and not +trying something smarter like injecting html code, etc... +So I hope it could help someone. diff --git a/src/posts/0011-export-tangle-names/org_html_export_show_tangle.el b/src/posts/0011-export-tangle-names/org_html_export_show_tangle.el new file mode 100644 index 0000000..5b65a90 --- /dev/null +++ b/src/posts/0011-export-tangle-names/org_html_export_show_tangle.el @@ -0,0 +1,9 @@ +(defun my-add-link-to-tangled-files (backend) + "Add a link just before source code block with tangled files. +BACKEND is the export backend. Used as symbol." + (while ;; (re-search-forward ) + (re-search-forward "^\\( *\\)#\\+begin_src .*:tangle \\([^\s\n]*\\)" nil t) + (replace-match "\\1#+CAPTION: [[./\\2][=\\2=]]\n\\&"))) + +(add-hook 'org-export-before-processing-hook + 'my-add-link-to-tangled-files)