emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Export tangle filename with source block
@ 2016-07-31  3:45 Thibault Marin
  2016-10-08 19:36 ` Thibault Marin
  0 siblings, 1 reply; 5+ messages in thread
From: Thibault Marin @ 2016-07-31  3:45 UTC (permalink / raw)
  To: Org Mode


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-10-12 23:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-31  3:45 Export tangle filename with source block Thibault Marin
2016-10-08 19:36 ` Thibault Marin
2016-10-11 18:57   ` Nicolas Goaziou
2016-10-12  4:39     ` Thibault Marin
2016-10-12 23:48       ` Thibault Marin

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