emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Org mode and "shunt" exporters?
@ 2013-12-05 20:30 Brett Viren
  2013-12-06  2:10 ` Eric Schulte
  2013-12-06 13:02 ` John Kitchin
  0 siblings, 2 replies; 16+ messages in thread
From: Brett Viren @ 2013-12-05 20:30 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 996 bytes --]

Has anyone written any new-style exporter which will produce a common
markup/data language format like JSON or YAML?  I'm looking for
something that fully preserves the original org document structure and
does no semantic interpretation along the way.

What I really want is to parse arbitrary org files in Python.  I've
looked at the entries at worg's "org-tool" node which do this but they
seem out of date or make assumptions about what org elements exist or
their URLs are not loading (NEO).  If any of that's a misrepresentation
please correct me.

In any case, using org's own exporter to produce JSON or YAML and then
relying on these format's Python modules for parsing seems like the best
way to go to let me author in org and process in Python.

I'm not very good with elisp (which is why I want to get org data into
Python) but I guess I can have a go at making such a "shunt" exporter.
Before I try, I just wanted to check if someone had this wheel already
spinning.

Thanks,
-Brett.

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-05 20:30 Org mode and "shunt" exporters? Brett Viren
@ 2013-12-06  2:10 ` Eric Schulte
  2013-12-09 17:38   ` Brett Viren
  2013-12-06 13:02 ` John Kitchin
  1 sibling, 1 reply; 16+ messages in thread
From: Eric Schulte @ 2013-12-06  2:10 UTC (permalink / raw)
  To: Brett Viren; +Cc: emacs-orgmode

Brett Viren <bv@bnl.gov> writes:

> Has anyone written any new-style exporter which will produce a common
> markup/data language format like JSON or YAML?  I'm looking for
> something that fully preserves the original org document structure and
> does no semantic interpretation along the way.
>
> What I really want is to parse arbitrary org files in Python.  I've
> looked at the entries at worg's "org-tool" node which do this but they
> seem out of date or make assumptions about what org elements exist or
> their URLs are not loading (NEO).  If any of that's a misrepresentation
> please correct me.
>
> In any case, using org's own exporter to produce JSON or YAML and then
> relying on these format's Python modules for parsing seems like the best
> way to go to let me author in org and process in Python.
>
> I'm not very good with elisp (which is why I want to get org data into
> Python) but I guess I can have a go at making such a "shunt" exporter.
> Before I try, I just wanted to check if someone had this wheel already
> spinning.
>
> Thanks,
> -Brett.

You can use `org-element-parse-buffer' to convert an Emacs Buffer to a
structured Emacs Lisp object.  At that point you can use existing tools
for converting lisp to JSON or YAML.  I've used cl-json for Common Lisp,
I would imagine something similar exists for Emacs Lisp.

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-05 20:30 Org mode and "shunt" exporters? Brett Viren
  2013-12-06  2:10 ` Eric Schulte
@ 2013-12-06 13:02 ` John Kitchin
  2013-12-12 16:30   ` Brett Viren
  1 sibling, 1 reply; 16+ messages in thread
From: John Kitchin @ 2013-12-06 13:02 UTC (permalink / raw)
  To: Brett Viren; +Cc: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 2000 bytes --]

that sounds like an interesting approach. xml seems like what you really
want, since looking at the parsetree there is a lot of information (e.g.
attributes, properties, etc...) that would be tricky to generate a fully
representative json scheme.

This page suggests at the bottom you could export to texinfo, and convert
that to docbook:
http://orgmode.org/worg/exporters/ox-overview.html


   - (1) DocBook export, available in previous Org-mode versions, has not
   currently been ported to the new exporter, however the new
ox-texinfobackend can generate DocBook format. Once
   file.texi is created via ox-texinfo, simply execute:

makeinfo --docbook file.texi




John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu



On Thu, Dec 5, 2013 at 3:30 PM, Brett Viren <bv@bnl.gov> wrote:

> Has anyone written any new-style exporter which will produce a common
> markup/data language format like JSON or YAML?  I'm looking for
> something that fully preserves the original org document structure and
> does no semantic interpretation along the way.
>
> What I really want is to parse arbitrary org files in Python.  I've
> looked at the entries at worg's "org-tool" node which do this but they
> seem out of date or make assumptions about what org elements exist or
> their URLs are not loading (NEO).  If any of that's a misrepresentation
> please correct me.
>
> In any case, using org's own exporter to produce JSON or YAML and then
> relying on these format's Python modules for parsing seems like the best
> way to go to let me author in org and process in Python.
>
> I'm not very good with elisp (which is why I want to get org data into
> Python) but I guess I can have a go at making such a "shunt" exporter.
> Before I try, I just wanted to check if someone had this wheel already
> spinning.
>
> Thanks,
> -Brett.
>

[-- Attachment #2: Type: text/html, Size: 2774 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-06  2:10 ` Eric Schulte
@ 2013-12-09 17:38   ` Brett Viren
  2013-12-09 19:55     ` Nicolas Goaziou
  0 siblings, 1 reply; 16+ messages in thread
From: Brett Viren @ 2013-12-09 17:38 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1437 bytes --]

Eric Schulte <schulte.eric@gmail.com> writes:

> You can use `org-element-parse-buffer' to convert an Emacs Buffer to a
> structured Emacs Lisp object.  At that point you can use existing tools
> for converting lisp to JSON or YAML.  I've used cl-json for Common Lisp,
> I would imagine something similar exists for Emacs Lisp.

Thanks for the suggestion.  I pursued that a bit this weekend.  The
resulting data structure is a Circular Object[1] due to the ":parent"
references.  It seems dealing with this kind of data structure is
somewhat uncommon (or my search-fu lacking), although I do find a recent
reference to it on this mailing list[2].  I also found a "cust-print"[3]
feature from Emacs 19 which has since been removed.  It shows a way to
deal with Circular Objects.  So far it has strongly taxed my poor elisp
skills but I plan to pursue this direction a bit more.

I did try throwing a JSON parser/generator[4] at the output of
org-element-parse-buffer but this failed due to exceeding emacs's
recursion limits.  I think this must be from the ":parent" references
getting recursed on forever.


-Brett.


[1] one must (setq print-circle t) to avoid emacs reader errors
http://www.gnu.org/software/emacs/manual/html_node/elisp/Circular-Objects.html

[2] http://comments.gmane.org/gmane.emacs.orgmode/65999

[3] http://web.mit.edu/dosathena/sandbox/emacs-19.28/lisp/cust-print.el

[4] http://edward.oconnor.cx/2006/03/json.el

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-09 17:38   ` Brett Viren
@ 2013-12-09 19:55     ` Nicolas Goaziou
  2013-12-13 18:26       ` Brett Viren
  0 siblings, 1 reply; 16+ messages in thread
From: Nicolas Goaziou @ 2013-12-09 19:55 UTC (permalink / raw)
  To: Brett Viren; +Cc: emacs-orgmode, Eric Schulte

Hello,

Brett Viren <bv@bnl.gov> writes:

> Eric Schulte <schulte.eric@gmail.com> writes:
>
>> You can use `org-element-parse-buffer' to convert an Emacs Buffer to a
>> structured Emacs Lisp object.  At that point you can use existing tools
>> for converting lisp to JSON or YAML.  I've used cl-json for Common Lisp,
>> I would imagine something similar exists for Emacs Lisp.
>
> Thanks for the suggestion.  I pursued that a bit this weekend.  The
> resulting data structure is a Circular Object[1] due to the ":parent"
> references.  It seems dealing with this kind of data structure is
> somewhat uncommon (or my search-fu lacking), although I do find a recent
> reference to it on this mailing list[2].  I also found a "cust-print"[3]
> feature from Emacs 19 which has since been removed.  It shows a way to
> deal with Circular Objects.  So far it has strongly taxed my poor elisp
> skills but I plan to pursue this direction a bit more.
>
> I did try throwing a JSON parser/generator[4] at the output of
> org-element-parse-buffer but this failed due to exceeding emacs's
> recursion limits.  I think this must be from the ":parent" references
> getting recursed on forever.

You can walk the tree, e.g. with `org-element-map', and remove
all :parent references if you don't need them.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-06 13:02 ` John Kitchin
@ 2013-12-12 16:30   ` Brett Viren
  2013-12-12 17:22     ` John Kitchin
  0 siblings, 1 reply; 16+ messages in thread
From: Brett Viren @ 2013-12-12 16:30 UTC (permalink / raw)
  To: John Kitchin; +Cc: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 2153 bytes --]

Hi John,

John Kitchin <jkitchin@andrew.cmu.edu> writes:

> that sounds like an interesting approach. xml seems like what you
> really want, since looking at the parsetree there is a lot of
> information (e.g. attributes, properties, etc...) that would be tricky
> to generate a fully representative json scheme. 

I see from your other thread that you are looking in to this idea of
dumping to JSON.  That's great!  If you want a tester of your JSON
exporter I'd be very happy to give it a try.  If it means I can abandon
my own stumbling around, that would make me even happier.

But, I am still slowly messing with this myself.  I plan to next follow
Nicolas's suggestion of simply removing the :parent parameter to get
over the hurdle that the circular object caused me.  I think if the
overall structure of the parse tree is preserved in the JSON then
parentage can be restored when it is read back.

I've also thought a bit about schema issues.  Regardless of how an "org
schema" might be represented, it would be best if it could be generated
From org instead of hand crafted.  This would need a kind of a "meta
export" feature.  I've not yet checked to see if there is some facility
in org to exploit to do this.  Maybe someone knows?

In the past I've expressed schema descriptions for JSON data in JSON
itself.  Internet searches now show this is not a novel approach so I
think there is some fruit to be found pursuing this direction.  Or, I
may just be trying too hard to avoid XML....

> This page suggests at the bottom you could export to texinfo, and
> convert that to docbook:
> http://orgmode.org/worg/exporters/ox-overview.html
>
> * (1) DocBook export, available in previous Org-mode versions, has not
>   currently been ported to the new exporter, however the new
>   ox-texinfo backend can generate DocBook format. Once file.texi is
>   created via ox-texinfo, simply execute: 
>
> makeinfo --docbook file.texi

Thanks.  I did try this but makeinfo failed on the texinfo file that was
produced.  I didn't pursue it enough to figure out why or if I was doing
something wrong.

-Brett.

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-12 16:30   ` Brett Viren
@ 2013-12-12 17:22     ` John Kitchin
  2013-12-12 19:24       ` Eric Schulte
  2013-12-12 19:50       ` Aaron Ecay
  0 siblings, 2 replies; 16+ messages in thread
From: John Kitchin @ 2013-12-12 17:22 UTC (permalink / raw)
  To: Brett Viren; +Cc: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 3101 bytes --]

I wouldn't hold your breath waiting for my solution, it's currently a
constructive procrastination project to see if it is possible ;)

I made this:

https://github.com/jkitchin/jmax/blob/prelude/ox-json.el

which does give some output that vaguely resembles json for a very simple
org-file (no quotes, no effort to make compliant json, or pretty print it,
for example). This handles headlines, italic, bold text, links, paragraphs,
and plain text.

I don't have much need for this right now, so it will probably not advance
much, but you might find it a starting point for you if the parse-tree
doesn't pan out.



John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu



On Thu, Dec 12, 2013 at 11:30 AM, Brett Viren <bv@bnl.gov> wrote:

> Hi John,
>
> John Kitchin <jkitchin@andrew.cmu.edu> writes:
>
> > that sounds like an interesting approach. xml seems like what you
> > really want, since looking at the parsetree there is a lot of
> > information (e.g. attributes, properties, etc...) that would be tricky
> > to generate a fully representative json scheme.
>
> I see from your other thread that you are looking in to this idea of
> dumping to JSON.  That's great!  If you want a tester of your JSON
> exporter I'd be very happy to give it a try.  If it means I can abandon
> my own stumbling around, that would make me even happier.
>
> But, I am still slowly messing with this myself.  I plan to next follow
> Nicolas's suggestion of simply removing the :parent parameter to get
> over the hurdle that the circular object caused me.  I think if the
> overall structure of the parse tree is preserved in the JSON then
> parentage can be restored when it is read back.
>
> I've also thought a bit about schema issues.  Regardless of how an "org
> schema" might be represented, it would be best if it could be generated
> From org instead of hand crafted.  This would need a kind of a "meta
> export" feature.  I've not yet checked to see if there is some facility
> in org to exploit to do this.  Maybe someone knows?
>
> In the past I've expressed schema descriptions for JSON data in JSON
> itself.  Internet searches now show this is not a novel approach so I
> think there is some fruit to be found pursuing this direction.  Or, I
> may just be trying too hard to avoid XML....
>
> > This page suggests at the bottom you could export to texinfo, and
> > convert that to docbook:
> > http://orgmode.org/worg/exporters/ox-overview.html
> >
> > * (1) DocBook export, available in previous Org-mode versions, has not
> >   currently been ported to the new exporter, however the new
> >   ox-texinfo backend can generate DocBook format. Once file.texi is
> >   created via ox-texinfo, simply execute:
> >
> > makeinfo --docbook file.texi
>
> Thanks.  I did try this but makeinfo failed on the texinfo file that was
> produced.  I didn't pursue it enough to figure out why or if I was doing
> something wrong.
>
> -Brett.
>

[-- Attachment #2: Type: text/html, Size: 4196 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-12 17:22     ` John Kitchin
@ 2013-12-12 19:24       ` Eric Schulte
  2013-12-12 19:50       ` Aaron Ecay
  1 sibling, 0 replies; 16+ messages in thread
From: Eric Schulte @ 2013-12-12 19:24 UTC (permalink / raw)
  To: John Kitchin; +Cc: Brett Viren, emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 244 bytes --]

Here's a quick shot at using Common Lisp's cl-json package as a bridge
between Emacs-lisp and JSON.  The Org-mode file with necessary the code
is attached (it requires a running lisp process w/cl-json loaded), as
well as it's json conversion.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-to-json.org --]
[-- Type: text/x-org, Size: 1192 bytes --]

#+Title: Org to JSON

Use Common Lisp as a bridge between Org-mode and JSON.

A three step process.

1. First we'll define an Emacs Lisp function to write an Org-mode
   buffer to a file as Emacs Lisp.
   #+begin_src emacs-lisp
     (defun write-org-to-emacs-lisp-file (&optional path)
       (interactive "Foutput file: ")
       (cl-flet ((obj-to-path (obj path)
                              (with-temp-file path
                                (prin1 obj (current-buffer)))))
         (obj-to-path (org-element-parse-buffer) path)))
   #+end_src

2. Small hack to fix the circular =:parent= issue.
   #+begin_src sh :var path="/tmp/org-to-json.el" :results none
     sed 's/#[0-9]\+/:not-the-parent/g' -i $path
   #+end_src

3. Finally use Common Lisp to read the hacked up Emacs Lisp and use
   [[http://common-lisp.net/projects/cl-json][cl-json]] to print that as JSON.
   #+name: emacs-lisp-to-json
   #+headers: :var in-path="/tmp/org-to-json.el"
   #+headers: :var out-path="/tmp/org-to-json.json"
   #+begin_src lisp :results none
     (with-open-file (in in-path)
       (with-open-file (out out-path :direction :output)
         (cl-json:encode-json (read in) out)))
   #+end_src

[-- Attachment #3: org-to-json.json --]
[-- Type: application/json, Size: 4421 bytes --]

[-- Attachment #4: Type: text/plain, Size: 63 bytes --]


-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-12 17:22     ` John Kitchin
  2013-12-12 19:24       ` Eric Schulte
@ 2013-12-12 19:50       ` Aaron Ecay
  2013-12-12 19:58         ` Matt Price
  1 sibling, 1 reply; 16+ messages in thread
From: Aaron Ecay @ 2013-12-12 19:50 UTC (permalink / raw)
  To: John Kitchin, Brett Viren; +Cc: emacs-orgmode@gnu.org

2013ko abenudak 12an, John Kitchin-ek idatzi zuen:
>
> I wouldn't hold your breath waiting for my solution, it's currently a
> constructive procrastination project to see if it is possible ;)
>
> I made this:
>
> https://github.com/jkitchin/jmax/blob/prelude/ox-json.el
>
> which does give some output that vaguely resembles json for a very simple
> org-file (no quotes, no effort to make compliant json, or pretty print it,
> for example). This handles headlines, italic, bold text, links, paragraphs,
> and plain text.

Emacs has a json library since 2008 (C-h f json-encode), and
pretty-printing in that library since last year, so you (or someone else
who has procrastination time on their hands...) should be able to solve the
quoting and formatting problems that way.

--
Aaron Ecay

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-12 19:50       ` Aaron Ecay
@ 2013-12-12 19:58         ` Matt Price
  2013-12-13  2:53           ` John Kitchin
  2013-12-13 19:02           ` Brett Viren
  0 siblings, 2 replies; 16+ messages in thread
From: Matt Price @ 2013-12-12 19:58 UTC (permalink / raw)
  To: John Kitchin, Brett Viren, emacs-orgmode@gnu.org

On Thu, Dec 12, 2013 at 2:50 PM, Aaron Ecay <aaronecay@gmail.com> wrote:
> 2013ko abenudak 12an, John Kitchin-ek idatzi zuen:
>>
>> I wouldn't hold your breath waiting for my solution, it's currently a
>> constructive procrastination project to see if it is possible ;)
>>
>> I made this:
>>
>> https://github.com/jkitchin/jmax/blob/prelude/ox-json.el
>>
>> which does give some output that vaguely resembles json for a very simple
>> org-file (no quotes, no effort to make compliant json, or pretty print it,
>> for example). This handles headlines, italic, bold text, links, paragraphs,
>> and plain text.
>
> Emacs has a json library since 2008 (C-h f json-encode), and
> pretty-printing in that library since last year, so you (or someone else
> who has procrastination time on their hands...) should be able to solve the
> quoting and formatting problems that way.
>
> --
> Aaron Ecay

I am pretty ignorant and may have missed a referene o this in the
thread, but this (very outdated) code is on the emacswiki:

http://www.emacswiki.org/emacs/org-json.el

not sure if it will actually help you.

Matt
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-12 19:58         ` Matt Price
@ 2013-12-13  2:53           ` John Kitchin
  2013-12-13 19:02           ` Brett Viren
  1 sibling, 0 replies; 16+ messages in thread
From: John Kitchin @ 2013-12-13  2:53 UTC (permalink / raw)
  To: Matt Price; +Cc: Brett Viren, emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 1566 bytes --]

I tried this code but I get a JSON readtable error even with the examples
in the code.

John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu



On Thu, Dec 12, 2013 at 2:58 PM, Matt Price <moptop99@gmail.com> wrote:

> On Thu, Dec 12, 2013 at 2:50 PM, Aaron Ecay <aaronecay@gmail.com> wrote:
> > 2013ko abenudak 12an, John Kitchin-ek idatzi zuen:
> >>
> >> I wouldn't hold your breath waiting for my solution, it's currently a
> >> constructive procrastination project to see if it is possible ;)
> >>
> >> I made this:
> >>
> >> https://github.com/jkitchin/jmax/blob/prelude/ox-json.el
> >>
> >> which does give some output that vaguely resembles json for a very
> simple
> >> org-file (no quotes, no effort to make compliant json, or pretty print
> it,
> >> for example). This handles headlines, italic, bold text, links,
> paragraphs,
> >> and plain text.
> >
> > Emacs has a json library since 2008 (C-h f json-encode), and
> > pretty-printing in that library since last year, so you (or someone else
> > who has procrastination time on their hands...) should be able to solve
> the
> > quoting and formatting problems that way.
> >
> > --
> > Aaron Ecay
>
> I am pretty ignorant and may have missed a referene o this in the
> thread, but this (very outdated) code is on the emacswiki:
>
> http://www.emacswiki.org/emacs/org-json.el
>
> not sure if it will actually help you.
>
> Matt
> >
>

[-- Attachment #2: Type: text/html, Size: 2455 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-09 19:55     ` Nicolas Goaziou
@ 2013-12-13 18:26       ` Brett Viren
  2013-12-13 19:05         ` Nicolas Goaziou
  0 siblings, 1 reply; 16+ messages in thread
From: Brett Viren @ 2013-12-13 18:26 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode, Eric Schulte

[-- Attachment #1: Type: text/plain, Size: 1961 bytes --]

Hi,

Nicolas Goaziou <n.goaziou@gmail.com> writes:

> You can walk the tree, e.g. with `org-element-map', and remove
> all :parent references if you don't need them.

I figured out how to follow this advice.  I can even make valid JSON
From the filtered parse tree by handing it to Edward O'Conner's
json.el (link in example below).

However this method only works for a very simple org document.  I'm
successfully filtering out the :parent properties of (most of) the
elements but as soon as my document produces a plain text element like:

  #("Text" 0 4 (:parent #1))

then two problems occcur:

First, I'm simply failing to see how to set this :parent property to nil
like I do with the others.  

Second, json.el throws a "Bad JSON Object" error.  I tried assuming that
the problem was it doesn't know what to do with this substring form.
Naively, I tried to follow some other recent advice in another thread
about using substring-no-properties to strip out the meta data from the
plain text elements.  But this apparently is a net no-op as I suspect
that the org-element-set-contents then puts them right back.

I feel like I'm pretty close.  Any more advice?

Thanks,
-Brett.


#+TITLE: The Title.

Blah blah blah.

* A heading.

This uses http://edward.oconnor.cx/2006/03/json.el

 - foo
 - bar
 - baz

#+BEGIN_SRC elisp
  (require 'json)
  (let* ((tree (org-element-parse-buffer 'object nil)))
    (org-element-map tree org-element-all-elements 
      (lambda (x) 
        (if (org-element-property :parent x)
            (org-element-put-property x :parent nil))
        ;; (if (eq (org-element-type x) 'plain-text)
        ;;     (org-element-set-contents x (substring-no-properties 
        ;;                                  (org-element-contents x))))
        ))
    (write-region
     ;(json-encode tree) 
     (prin1-to-string tree)
      nil "foo.txt"))
#+END_SRC

#+RESULTS:


[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-12 19:58         ` Matt Price
  2013-12-13  2:53           ` John Kitchin
@ 2013-12-13 19:02           ` Brett Viren
  1 sibling, 0 replies; 16+ messages in thread
From: Brett Viren @ 2013-12-13 19:02 UTC (permalink / raw)
  To: Matt Price; +Cc: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 491 bytes --]

Matt Price <moptop99@gmail.com> writes:

> I am pretty ignorant and may have missed a referene o this in the
> thread, but this (very outdated) code is on the emacswiki:
>
> http://www.emacswiki.org/emacs/org-json.el

Thanks.  My searches didn't find this.  It looks like this is parsing
the org buffer directly and only to the level of headings.  Ultimately I
want to emit JSON to get at the full detailed document structure so this
probably isn't in the direction I want to go.

-Brett.



[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-13 18:26       ` Brett Viren
@ 2013-12-13 19:05         ` Nicolas Goaziou
  2013-12-13 19:29           ` Eric Schulte
  0 siblings, 1 reply; 16+ messages in thread
From: Nicolas Goaziou @ 2013-12-13 19:05 UTC (permalink / raw)
  To: Brett Viren; +Cc: emacs-orgmode, Eric Schulte

Hello,

Brett Viren <bv@bnl.gov> writes:

> However this method only works for a very simple org document.  I'm
> successfully filtering out the :parent properties of (most of) the
> elements but as soon as my document produces a plain text element like:
>
>   #("Text" 0 4 (:parent #1))
>
> then two problems occcur:
>
> First, I'm simply failing to see how to set this :parent property to nil
> like I do with the others.  

Like any other construct, with `org-element-put-property'.

>   (let* ((tree (org-element-parse-buffer 'object nil)))
>     (org-element-map tree org-element-all-elements 

You're only mapping over elements. You need to add
`org-element-all-objects' and `plain-text'.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-13 19:05         ` Nicolas Goaziou
@ 2013-12-13 19:29           ` Eric Schulte
  2013-12-13 23:06             ` Brett Viren
  0 siblings, 1 reply; 16+ messages in thread
From: Eric Schulte @ 2013-12-13 19:29 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: Brett Viren, emacs-orgmode

This should work in a recent Emacs.

    (require 'json)
    (defun org-as-json-to-file (&optional path)
      "Export the current Org-mode buffer as JSON to the supplied PATH."
      (interactive "Fwrite to file: ")
      (let ((tree (org-element-parse-buffer)))
        (org-element-map tree
            (append org-element-all-objects org-element-all-elements)
          (lambda (el) (org-element-put-property el :parent nil)))
        (with-temp-file path
          (insert (json-encode tree)))))

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Org mode and "shunt" exporters?
  2013-12-13 19:29           ` Eric Schulte
@ 2013-12-13 23:06             ` Brett Viren
  0 siblings, 0 replies; 16+ messages in thread
From: Brett Viren @ 2013-12-13 23:06 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode, Nicolas Goaziou

[-- Attachment #1: Type: text/plain, Size: 2781 bytes --]

Hi Eric,

Eric Schulte <schulte.eric@gmail.com> writes:

> This should work in a recent Emacs.
>
>     (require 'json)
>     (defun org-as-json-to-file (&optional path)
>       "Export the current Org-mode buffer as JSON to the supplied PATH."
>       (interactive "Fwrite to file: ")
>       (let ((tree (org-element-parse-buffer)))
>         (org-element-map tree
>             (append org-element-all-objects org-element-all-elements)
>           (lambda (el) (org-element-put-property el :parent nil)))
>         (with-temp-file path
>           (insert (json-encode tree)))))

Thanks.  With this, Nicolas's and all the other input I've got something
working now.  There was still one small issue I found with this last
round.  The :structure property also causes an error inside json.el
like:

  json-encode-key: Bad JSON object key: 105

But, for now, nulling :structure in the same way as :parent let's me
chain org->JSON->Python!  The first elisp code block in the test doc
below works.

Thanks for all the patient help from everyone.  I've learned a lot.

-Brett.


#+TITLE: The Title.
Blah blah blah.
* A heading.
This uses http://edward.oconnor.cx/2006/03/json.el
 - foo
 - bar
 - baz
#+BEGIN_SRC elisp
  (require 'json)
  (let* ((tree (org-element-parse-buffer 'object nil)))
    (org-element-map tree (append org-element-all-elements
    org-element-all-objects '(plain-text))
      (lambda (x) 
        (if (org-element-property :parent x)
            (org-element-put-property x :parent "none"))
        (if (org-element-property :structure x)
            (org-element-put-property x :structure "none"))
        ;; (if (eq (org-element-type x) 'plain-text)
        ;;     (org-element-set-contents x (substring-no-properties 
        ;;                                  (org-element-contents x))))
        ))
    (write-region
     (json-encode tree) 
     ;(prin1-to-string tree)
      nil "foo.dat"))
#+END_SRC

#+RESULTS:

* From Eric Schultz
#+BEGIN_SRC elisp
  (require 'json)
  (defun org-as-json-to-file (&optional path)
    "Export the current Org-mode buffer as JSON to the supplied PATH."
    (interactive "Fwrite to file: ")
    (let ((tree (org-element-parse-buffer)))
      (org-element-map tree
          (append org-element-all-objects org-element-all-elements)
        (lambda (el) (org-element-put-property el :parent "none")))
      (with-temp-file path
        (insert (json-encode tree)))))
  (org-as-json-to-file "eric.txt")
#+END_SRC

* Try some hand written data
#+BEGIN_SRC elisp
  (require 'json)
  (with-current-buffer (find-file-noselect "foo.dat")
    (let ((tree (read (current-buffer))))
      (prin1-to-string (json-encode tree))))
#+END_SRC



 

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2013-12-13 23:06 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-05 20:30 Org mode and "shunt" exporters? Brett Viren
2013-12-06  2:10 ` Eric Schulte
2013-12-09 17:38   ` Brett Viren
2013-12-09 19:55     ` Nicolas Goaziou
2013-12-13 18:26       ` Brett Viren
2013-12-13 19:05         ` Nicolas Goaziou
2013-12-13 19:29           ` Eric Schulte
2013-12-13 23:06             ` Brett Viren
2013-12-06 13:02 ` John Kitchin
2013-12-12 16:30   ` Brett Viren
2013-12-12 17:22     ` John Kitchin
2013-12-12 19:24       ` Eric Schulte
2013-12-12 19:50       ` Aaron Ecay
2013-12-12 19:58         ` Matt Price
2013-12-13  2:53           ` John Kitchin
2013-12-13 19:02           ` Brett Viren

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).