15

I'm writting a package which dowloads a list of healines, contents, and some other properties, which are to be displayed to the user. For the moment, an org-mode buffer seems like a good way of displaying these headlines.

Below is an example of how this list might be structured. It is merely illustrative, I can easily convert it to any other structure as necessary.

'(("One headline" "Some much longer content."
   (property1 . value)
   (property2 . value))
  ("Second headline" "More much longer content."
   (property1 . value)
   (property2 . value)))

Is there a function or package which prints such a list into an org-mode buffer?

Here's the desired output.

* One headline
  :PROPERTIES:
  :property1: value
  :property2: value
  :END:
Some much longer content.

* Second headline
  :PROPERTIES:
  :property1: value
  :property2: value
  :END:
More much longer content.

I could do this manually, I'm just wondering if there might be something out there.

  • Oh my, this would be very useful. +1. I would note that even Org does so manually. See org-insert-drawer. (That is, I would imagine if such a converter existed, this function would call that one with nil.) – Sean Allred Oct 30 '14 at 22:04
17

This is the job of org-element, the awesome(!) work of Nicolas Goaziou. If you don't know org-element and you care about org-development this is something you should look into. It is not only a great tool to work with, it is also increasingly powering org. Most notably the org-export (ox), but also functions in e.g. org.el.

To get the "lisp representation" of an element under point use org-element-at-point or org-element-context. To get the representation of the buffer use org-element-parse-buffer. While not directly relevant here, be aware of org-element-map.

To go from the "lisp representation" of an element, secondary string or parse tree back to the "Org syntax representation" use org-element-interpret-data. This is the (only) way to turn a "lisp representation" into a "Org syntax representation". You will probably not want to write this representation manually, though. Here's is a pretty small representation of your first headline

(org-element-interpret-data
 '(headline (:title "One headline" :level 1)
            (property-drawer nil ((node-property (:key "property1" :value "value1"))
                                  (node-property (:key "property2" :value "value2"))))
            (#("Some much longer content."))))

If you must add both headlines add a parse tree

(org-element-interpret-data
 '(org-data nil (headline (:title "One headline" :level 1)
                          (property-drawer nil ((node-property (:key "property1" :value "value1"))
                                                (node-property (:key "property2" :value "value2"))))
                          (#("Some much longer content.")))
            (headline (:title "Second headline" :level 1)
                          (property-drawer nil ((node-property (:key "property1" :value "value1"))
                                                (node-property (:key "property2" :value "value2"))))
                          (#("More much longer content.")))))

You may find that Thorsten Jolitz's org-dp library will aid you in such efforts (on MELPA).

The library org-dp is meant for programming at the local level, i.e. without any (contextual) information except those about the parsed element at point. It is designed to make using the Org parser/interpreter framework at the local level as convenient as using it at the global level (with a complete parse-tree produced by org-element-parse-buffer available)

A more complete description by Thorsten can be found here.

For further clarifications gmane.emacs.orgmode is really the appropriate forum.

  • This is a great answer, but I can't understand what org-dp brings beyond org-element. – Lyn Headley Dec 29 '17 at 1:14
  • org-dp is an alternative interface. AFAIR, it was written to create documents in the Org syntax/format from an "elementy" point of view. The goal of Org Element is to be a parser and a workhorse for Org. I am personally just fine with org-element, but it's great to have alternative interfaces. – rasmus Apr 11 '18 at 22:42
1

I've sort of tangentially looked at this issue. Take a look at the org-protocol.el. It's bundled with org-mode. Specifically, the org-protocol-do-capture function converts a list, "parts" (which you seem to already have), to org-mode properties using org-store-link-props function and then calls org-capture. This assumes that you have a capture template with placeholders such as %:link. You can define the properties to be whatever you like.

I've done something similar to scrape title, author, date, source, etc. from site APIs. If you end up looking at this code, be sure to also look at capture-templates.el.

If you're working with JSON data, the json.el and / or request.el might be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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