emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Export tangle filename with source block


From: Thibault Marin
Subject: [O] Export tangle filename with source block
Date: Sat, 30 Jul 2016 22:45:42 -0500
User-agent: mu4e 0.9.16; emacs 24.5.1

Hi list,

I have an org file that I am tangling into multiple files, and exporting
to html.  What I would like to do is to label each source block in the
exported html with the filename used for tangling this specific block.
I don't have a strong opinion about the actual appearance of the label
(adding a comment at the top of the source block, hover in the code
textarea, other?).

I couldn't find any option to do that, so I initially though of using
`org-export-filter-src-block-functions' (following
http://orgmode.org/manual/Advanced-configuration.html).  I was not able
to get the org-element object for the current block from the parameter
received by the filter.  It looks like the third parameter (`info') may
have more information but I currently don't see how to get (1) the
current source block, and (2) the tangling filename.

I then tried to define a derived backend with custom handling of source
blocks as described in the documentation.

I currently have the following, where I try to get the tangle filename
using `org-babel-get-src-block-info', and later insert it into the html
content using an ugly regexp.

(defun my-html-src-block (src-block contents info)
  "Transcode a SRC-BLOCK element from Org to HTML.
     CONTENTS is nil.  INFO is a plist used as a communication
     channel."
  (let* ((lang (org-element-property :language src-block))
         (src-info (org-babel-get-src-block-info t src-block))
         (tang (cdr (assq :tangle (nth 2 src-info))))
         (export-out (org-export-with-backend 'html src-block contents info))
         )
    (if (and (string= lang "lua") (and tang (> (length tang) 0)))
        (progn
          (let ((src-start-pat "\\(<pre class=\"src src-lua\" 
id=\"[^\"]+*\">\\)"))
            (replace-regexp-in-string src-start-pat
                                      (concat "\\1"
                                              "\n-- ["
                                              tang
                                              "]\n\n") export-out)
            )
          )
      export-out
      )
    )
  )

(org-export-define-derived-backend 'my-html 'html
  :translate-alist '((src-block . my-html-src-block)))

Besides feeling wrong, this always gets the tangle name from the top
level org option ("#+PROPERTY: tangle main-file" at the top of the file)
instead of the one overridden by individual blocks "#+BEGIN_SRC :tangle
other-file".  Moreover the added comment is not syntax highlighted and
this feels really wrong.

Does anybody have any idea on how to achieve this?

Thanks in advance.

thibault



reply via email to

[Prev in Thread] Current Thread [Next in Thread]