From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jeff Filipovits Subject: Re: Get the text of a node Date: Wed, 23 Oct 2019 17:01:26 +0000 Message-ID: References: <877e4vj0zf.fsf@fastmail.fm> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000003a25fa059596da61" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:54171) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNJzl-0007Xm-Vg for emacs-orgmode@gnu.org; Wed, 23 Oct 2019 13:00:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNJzk-0006zK-Ft for emacs-orgmode@gnu.org; Wed, 23 Oct 2019 13:00:25 -0400 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]:45105) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iNJzk-0006yl-6m for emacs-orgmode@gnu.org; Wed, 23 Oct 2019 13:00:24 -0400 Received: by mail-wr1-x433.google.com with SMTP id q13so17941393wrs.12 for ; Wed, 23 Oct 2019 10:00:23 -0700 (PDT) In-Reply-To: <877e4vj0zf.fsf@fastmail.fm> 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" To: Joost Kremers Cc: emacs-orgmode@gnu.org --0000000000003a25fa059596da61 Content-Type: text/plain; charset="UTF-8" Sometimes giving a bad answer inspires someone else to give a better one, so here goes: It seems like the best way to get the contents programatically is using org-dp (https://github.com/tj64/org-dp). I don't see a way in org-element. The data returned will include the property drawer of the heading. It does not include subheadings. I wrote a quick and ugly function to strip out the property drawer (it also has to remove the properties list associated with the section element, hence excluding :begin), and then returns a string. (defun get-contents (data) "DATA is the data returned by (org-dp-contents)" (let ((contents) (exclusions '(property-drawer :begin))) (dolist (element (cdar data)) (unless (memq (car-safe element) exclusions) (push element contents))) (org-element-interpret-data (reverse contents)))) I am skeptical that this is a better way then the alternative you described, but do not know. Hopefully someone else can assist. On Wed, Oct 23, 2019 at 12:10 PM Joost Kremers wrote: > Hi all, > > I was wondering if there's a way to programmatically get the text > of a node in an Org buffer. Basically, I have a buffer that looks > something like this: > > #+BEGIN_SRC org > * Top header > ** Subheader > :PROPERTIES: > :Custom_ID: some_id > :END: > > Text starts here, possibly with additional subheaders > #+END_SRC > > What I would like to extract is the text below "Subheader", but > without the :PROPERTIES: block. > > I've looked at the org-element library, but I haven't been able to > figure out how to use it to extract just the plain text. > > I use the :Custom_ID: property to find the relevant subheading and > I know I can use (org-back-to-heading) to get point to the > Subheader containing the relevant :PROPERTIES: block. Obviously, I > could then narrow the buffer to the subheader, use a text search > to move point past the line containing :END: and then extract the > text from there until (point-max). > > I'm just wondering if this may break in unexpected circumstances > and whether there's a better way. > > TIA > > Joost > > > > -- > Joost Kremers > Life has its moments > > --0000000000003a25fa059596da61 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Sometimes giving a bad answer inspi= res someone else to give a better one, so here goes:

It seems like the best way to get the contents programatically is u= sing org-dp (h= ttps://github.com/tj64/org-dp). I don't see a way in org-element. <= /div>

The data returned will include the property drawer= of the heading. It does not include subheadings.

I wrote a quick and ugly function to strip out the property drawer (it als= o has to remove the properties list associated with the section element, he= nce excluding :begin), and then returns a string.

(defun get-contents (data)
"DATA = is the data returned by (org-dp-contents)"
=C2=A0 (let ((con= tents)
=C2=A0=C2=A0=C2=A0 (exclusions '(property-drawer :begin)))=C2=A0=C2=A0=C2=A0 (dolist (element (cdar data))
=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 (unless (memq (car-safe element) exclusions)
=C2=A0=C2=A0=C2= =A0 (push element contents)))
=C2=A0=C2=A0=C2=A0 (org-element-interpret-= data (reverse contents))))


I am skeptical that= this is a better way then the alternative you described, but do not know. = Hopefully someone else can assist.


=


On Wed, Oct 23, 2019 at 12:10 PM Joost Kremers <joostkremers@fastmai= l.fm> wrote:
Hi all,

I was wondering if there's a way to programmatically get the text
of a node in an Org buffer. Basically, I have a buffer that looks
something like this:

#+BEGIN_SRC org
* Top header
** Subheader
=C2=A0 =C2=A0:PROPERTIES:
=C2=A0 =C2=A0:Custom_ID: some_id
=C2=A0 =C2=A0:END:

=C2=A0 =C2=A0Text starts here, possibly with additional subheaders
#+END_SRC

What I would like to extract is the text below "Subheader", but <= br> without the :PROPERTIES: block.

I've looked at the org-element library, but I haven't been able to =
figure out how to use it to extract just the plain text.

I use the :Custom_ID: property to find the relevant subheading and
I know I can use (org-back-to-heading) to get point to the
Subheader containing the relevant :PROPERTIES: block. Obviously, I
could then narrow the buffer to the subheader, use a text search
to move point past the line containing :END: and then extract the
text from there until (point-max).

I'm just wondering if this may break in unexpected circumstances
and whether there's a better way.

TIA

Joost



--
Joost Kremers
Life has its moments

--0000000000003a25fa059596da61--