emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: official orgmode parser


From: Przemysław Kamiński
Subject: Re: official orgmode parser
Date: Wed, 16 Sep 2020 14:09:42 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0

On 9/15/20 2:37 PM, tomas@tuxteam.de wrote:
On Tue, Sep 15, 2020 at 01:15:56PM +0200, Przemysław Kamiński wrote:

[...]

There's the org-json (or ox-json) package but for some reason I
wasn't able to run it successfully. I guess export to S-exps would
be best here. But yes I'll check that out.

If that's your route, perhaps the "Org element API" [1] might be
helpful. Especially `org-element-parse-buffer' gives you a Lisp
data structure which is supposed to be a parse of your Org buffer.

 From there to S-expression can be trivial (e.g. `print' or `pp'),
depending on what you want to do.

Walking the structure should be nice in Lisp, too.

The topic of (non-Emacs) parsing of Org comes up regularly, and
there is a good (but AFAIK not-quite-complete) Org syntax spec
in Worg [2], but there are a couple of difficulties to be mastered
before such a thing can become really enjoyable and useful.

The loose specification of Org's format (arguably its second
or third strongest asset, the first two being its incredible
community and Emacs itself) is something which makes this
problem "interesting". People have invented lots of usages
which might be broken should Org change to a strict formal
spec. You don't want to break those people.

But yes, perhaps some day someone nails it. Perhaps it's you :)

Cheers

[1] https://orgmode.org/worg/dev/org-element-api.html
[2] https://orgmode.org/worg/dev/org-syntax.html

  - t


So I looked at (pp (org-element-parse-buffer)) however it does print out recursive stuff which other schemes have trouble parsing.

My code looks more or less like this:

(defun org-parse (f)
  (with-temp-buffer
    (find-file f)
    (let* ((parsed (org-element-parse-buffer))
           (all (append org-element-all-elements org-element-all-objects))
           (mapped (org-element-map parsed all
                     (lambda (item)
                       (strip-parent item)))))
      (pp mapped))))


strip-parent is basically (plist-put props :parent nil) for elements properties. However it turns out there are more recursive objects, like

:title
          #("Headline 1" 0 10
            (:parent
             (headline #2
                           (section

So I'm wondering do I have to do it by hand for all cases or is there some way to output only a simple AST without those nested objects?

Best,
Przemek



reply via email to

[Prev in Thread] Current Thread [Next in Thread]