From mboxrd@z Thu Jan 1 00:00:00 1970 From: James Harkins Subject: Re: Extract source code /with/ captions Date: Mon, 13 Jan 2014 10:52:15 +0800 Message-ID: <3cbde2bd-2dba-4c2f-85b7-62fd2a9daffc@dewdrop-world.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:40828) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W2Xdi-0008JJ-LK for Emacs-orgmode@gnu.org; Sun, 12 Jan 2014 21:52:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W2Xdb-00031I-Ph for Emacs-orgmode@gnu.org; Sun, 12 Jan 2014 21:52:34 -0500 Received: from mail-pa0-x236.google.com ([2607:f8b0:400e:c03::236]:52958) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W2Xdb-00031C-Hb for Emacs-orgmode@gnu.org; Sun, 12 Jan 2014 21:52:27 -0500 Received: by mail-pa0-f54.google.com with SMTP id fa1so125914pad.27 for ; Sun, 12 Jan 2014 18:52:26 -0800 (PST) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: John Kitchin Cc: Emacs-orgmode@gnu.org 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=20 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=20 thing is, I don't need links in the LaTeX output -- hence, no need to tie=20 it to LaTeX export. So, after a night's sleep, I remembered something about org-element and=20 took a look at some docstrings. A clever comment about "The (almost)=20 almighty `org-element-map'" attracted particular attention :) and indeed,=20= 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) =09(message "\n\n\nELEMENT:") =09(print (plist-get (car (cdr element)) :caption)))))) I pulled one frame with two src blocks out of the presentation, put it in a=20= separate file, and running this function from the buffer produces this in=20 the messages buffer (omitting some blank lines, which I had inserted while=20= 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=20 get the code string. Here, I'm hung up on some (large?) gaps in my elisp knowledge. I have no=20 idea what #(...) signifies, or what functions I can use to get the string=20 out of it. "#" Is not an especially useful search term in google, bing=20 etc... Can anyone help with my next step? Also, big thanks to Nicolas for org-element. The fact that an elisp novice=20= can extract captions for source blocks in about half an hour of tinkering=20 is nothing short of criminally easy. Spectacular. hjh