From mboxrd@z Thu Jan 1 00:00:00 1970 From: Brett Viren Subject: Re: Org mode and "shunt" exporters? Date: Fri, 13 Dec 2013 18:06:04 -0500 Message-ID: References: <87zjoe7mf2.fsf@gmail.com> <87k3fdu7dm.fsf@gmail.com> <877gb8woyz.fsf@gmail.com> <87mwk4mtv4.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:41455) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VrboY-0003el-WF for emacs-orgmode@gnu.org; Fri, 13 Dec 2013 18:06:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VrboQ-00055z-JW for emacs-orgmode@gnu.org; Fri, 13 Dec 2013 18:06:34 -0500 Received: from smtpgw.bnl.gov ([2620:10a:0:3::30]:29578 helo=iron4.sec.bnl.local) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VrboQ-000541-F2 for emacs-orgmode@gnu.org; Fri, 13 Dec 2013 18:06:26 -0500 In-Reply-To: <87mwk4mtv4.fsf@gmail.com> (Eric Schulte's message of "Fri, 13 Dec 2013 12:29:18 -0700") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Eric Schulte Cc: emacs-orgmode@gnu.org, Nicolas Goaziou --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Hi Eric, Eric Schulte 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. =2DBrett. #+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)=20 (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=20 ;; (org-element-contents x)))) )) (write-region (json-encode tree)=20 ;(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 =20 --=-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAlKrktwACgkQEixH2Z0dKCyvEQCfVlizntDY0O2tohZ5Ew6yYBuZ PXAAn2Ct73M5fQKyIkK0LozUQQLgyyCd =KMmP -----END PGP SIGNATURE----- --=-=-=--