emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: James Harkins <jamshark70@gmail.com>
To: John Kitchin <jkitchin@andrew.cmu.edu>
Cc: Emacs-orgmode@gnu.org
Subject: Re: Extract source code /with/ captions
Date: Mon, 13 Jan 2014 10:52:15 +0800	[thread overview]
Message-ID: <3cbde2bd-2dba-4c2f-85b7-62fd2a9daffc@dewdrop-world.net> (raw)
In-Reply-To: <CAJ51ETp3sjLaCXecpNJmiN7vtcqK5i5SEuZbzazPZgq3mbcnLw@mail.gmail.com>

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	other threads:[~2014-01-13  2:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-12 14:49 Extract source code /with/ captions James Harkins
2014-01-12 17:19 ` John Kitchin
2014-01-13  2:52   ` James Harkins [this message]
2014-01-13 17:06     ` Nick Dokos
2014-01-13 17:46       ` Nick Dokos
2014-01-13 18:01       ` Nick Dokos
2014-01-18  2:20         ` James Harkins
2014-01-18  3:04           ` James Harkins
2014-01-12 18:34 ` Charles Berry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3cbde2bd-2dba-4c2f-85b7-62fd2a9daffc@dewdrop-world.net \
    --to=jamshark70@gmail.com \
    --cc=Emacs-orgmode@gnu.org \
    --cc=jkitchin@andrew.cmu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).