emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] Extract source code /with/ captions


From: James Harkins
Subject: Re: [O] Extract source code /with/ captions
Date: Mon, 13 Jan 2014 10:52:15 +0800
User-agent: Trojita/v0.3.96-git; Qt/4.8.1; X11; Linux; Ubuntu 12.04.3 LTS

On Monday, January 13, 2014 1:19:28 AM HKT, John Kitchin wrote:
I think I have done something like that before. What I did was make it so
each code block would be written out to a file, e.g.
course-notes/script-%d.py and a link would be put in the exported pdf
right
after that block. I do not know how you could get the captions though.

Thanks for the suggestion. I think it might be overkill for my case. One thing is, I don't need links in the LaTeX output -- hence, no need to tie it to LaTeX export.

So, after a night's sleep, I remembered something about org-element and took a look at some docstrings. A clever comment about "The (almost) almighty `org-element-map'" attracted particular attention :) and indeed, it turns out that it does almost all the hard work.

Some progress, then:

(defun hjh-print-src-blocks ()
 "Iterate src blocks from org-element and print them to *Messages*."
 (interactive)
 (let ((tree (org-element-parse-buffer)))
   (org-element-map tree 'src-block
     (lambda (element)
        (message "\n\n\nELEMENT:")
        (print (plist-get (car (cdr element)) :caption))))))

I pulled one frame with two src blocks out of the presentation, put it in a separate file, and running this function from the buffer produces this in the messages buffer (omitting some blank lines, which I had inserted while running this under edebug):

ELEMENT:
(((#("25% coin toss in SmallTalk" 0 26 (:parent #2)))))

ELEMENT:
(((#("25% coin toss in SuperCollider" 0 30 (:parent #2)))))

This is correct, and I also see that I can use (plist-get ... :value) to get the code string.

Here, I'm hung up on some (large?) gaps in my elisp knowledge. I have no idea what #(...) signifies, or what functions I can use to get the string out of it. "#" Is not an especially useful search term in google, bing etc...

Can anyone help with my next step?

Also, big thanks to Nicolas for org-element. The fact that an elisp novice can extract captions for source blocks in about half an hour of tinkering is nothing short of criminally easy. Spectacular.

hjh



reply via email to

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