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 Answer 1

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
3
  • 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!)
    – Beetle B.
    Commented Dec 19, 2018 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. Commented Dec 19, 2018 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.
    – Beetle B.
    Commented Dec 22, 2018 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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