1

Given an org document, I'd like to traverse all the nodes (headlines) that have the following property ARTICLE_TYPE, and extract the body (excluding the drawers) for each one.

I'm struggling to find the appropriate org-element-map expression that will give me this.

Any ideas?

1

I think this does what you want. It does assume there is no nesting of the headlines.

#+BEGIN_SRC emacs-lisp :results code
(org-element-map (org-element-parse-buffer)
    'headline
  (lambda (hl)
    (when (org-element-property :ARTICLE_TYPE hl) 
      (buffer-substring-no-properties
       (save-excursion
     (goto-char (org-element-property :begin hl))
     (org-end-of-meta-data)
     (point))
       (org-element-property :contents-end hl)))))
#+END_SRC
  • Apparently this returns only the result for the first headline that matches (no idea why it doesn't return all). (Rant: Generally, for a package as good as org mode, I'm a bit shocked at how poor/nonexistent the API is. I would have expected a way to traverse all headlines in whatever order I like (e.g. BFS vs DFS), as well as things like "Return all children of this node" and so on). (BTW, learned a lot from your blog in the past - thanks!) – user1462309 Dec 19 '18 at 0:13
  • For me, this returns all the nodes. org-element-map has an optional argument that stops at the first match, but I don't use it in this example. – John Kitchin Dec 19 '18 at 1:18
  • Marking as correct, as it seems to work for you. Probably something wrong on my setup and I haven't had time to debug. – user1462309 Dec 22 '18 at 0:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.