|

Add links to code block during orgmode export

Yann Esposito

on Yann Esposito's blog - source - txt - pdf - §permalink
Add links to code block during orgmode export.

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 elisp :tangle foo.el

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:

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

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:

label + pre {margin-top: 0;}

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.

Any comment? Click on my email below and I'll add it.
authorYann Esposito <yann@esposito.host>
tags#org-mode #blog
date2020-02-29
rssRSS (validate)
size
17K (html 4.6K, css 12K)
gz
5.3K (html 2.0K, css 3.4K)
generated2020-06-27T13:41:04.281897Z