emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to pass a block of text to a code block as data?
@ 2013-02-08 17:06 Michael Baum
  2013-02-08 20:17 ` Sebastien Vauban
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Baum @ 2013-02-08 17:06 UTC (permalink / raw)
  To: emacs-orgmode

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

I haven't been able to figure this out from the org-mode documents yet.
What I would like to do is have a source code block in python or perl or
something similar. to which I can pass a reference to several lines of text
that the source code block, when evaluated, would then act on. Which would
involve iterating through the data text one line at a time and writing the
output to a buffer.

The text data could be indicated by a specific headline or a named block of
some sort or maybe an internal link, I'm not particularly fussy just so
there's a mechanism to do this.

Would appreciate any guidance.

-- 
====================================
Michael Baum <maabaum@gmail.com>

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

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

* Re: How to pass a block of text to a code block as data?
  2013-02-08 17:06 How to pass a block of text to a code block as data? Michael Baum
@ 2013-02-08 20:17 ` Sebastien Vauban
  2013-02-09  2:59   ` Michael Baum
  0 siblings, 1 reply; 6+ messages in thread
From: Sebastien Vauban @ 2013-02-08 20:17 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hi Michael,

Michael Baum wrote:
> I haven't been able to figure this out from the org-mode documents yet.
> What I would like to do is have a source code block in python or perl or
> something similar. to which I can pass a reference to several lines of text
> that the source code block, when evaluated, would then act on. Which would
> involve iterating through the data text one line at a time and writing the
> output to a buffer.
>
> The text data could be indicated by a specific headline or a named block of
> some sort or maybe an internal link, I'm not particularly fussy just so
> there's a mechanism to do this.
>
> Would appreciate any guidance.

#+name: lines-of-text
: I haven't been able to figure this out from the org-mode documents yet.
: What I would like to do is have a source code block in python or perl or
: something similar. to which I can pass a reference to several lines of text
: that the source code block, when evaluated, would then act on. Which would
: involve iterating through the data text one line at a time and writing the
: output to a buffer.

#+begin_src sh :stdin lines-of-text :results output
  grep would
#+end_src

#+results:
#+begin_example
What I would like to do is have a source code block in python or perl or
that the source code block, when evaluated, would then act on. Which would
#+end_example

Best regards,
  Seb

-- 
Sebastien Vauban

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

* Re: How to pass a block of text to a code block as data?
  2013-02-08 20:17 ` Sebastien Vauban
@ 2013-02-09  2:59   ` Michael Baum
  2013-02-09 18:49     ` Sean O'Halpin
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Baum @ 2013-02-09  2:59 UTC (permalink / raw)
  To: Sebastien Vauban; +Cc: emacs-orgmode

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

Sebastien:

Thanks! That helps a lot. I'm having trouble extending your example,
however. A couple of questions --

- What signals the end of the block of text to be used as data? I take it
that it's important that these all be comment lines staring with a colon
after the #+name label? Is there a way to do the same thing with a begin
and end block construction?

- In this line:
       #+begin_src sh :stdin lines-of-text :results output

does the flag :stdin mean that the following named block literally becomes
the STDIN stream for the code block? If I replace your shell/grep example
with this:

#+begin_src perl :stdin lines-of-text :results output
while (<>) {
   print $_;
}
#+end_src

...it doesn't work, although as far as I know that perl code snippet should
in fact simply print out the incoming lines from stdin.

Thanks again,

Michael

On Fri, Feb 8, 2013 at 3:17 PM, Sebastien Vauban <wxhgmqzgwmuf@spammotel.com
> wrote:

> Hi Michael,
>
> Michael Baum wrote:
> > I haven't been able to figure this out from the org-mode documents yet.
> > What I would like to do is have a source code block in python or perl or
> > something similar. to which I can pass a reference to several lines of
> text
> > that the source code block, when evaluated, would then act on. Which
> would
> > involve iterating through the data text one line at a time and writing
> the
> > output to a buffer.
> >
> > The text data could be indicated by a specific headline or a named block
> of
> > some sort or maybe an internal link, I'm not particularly fussy just so
> > there's a mechanism to do this.
> >
> > Would appreciate any guidance.
>
> #+name: lines-of-text
> : I haven't been able to figure this out from the org-mode documents yet.
> : What I would like to do is have a source code block in python or perl or
> : something similar. to which I can pass a reference to several lines of
> text
> : that the source code block, when evaluated, would then act on. Which
> would
> : involve iterating through the data text one line at a time and writing
> the
> : output to a buffer.
>
> #+begin_src sh :stdin lines-of-text :results output
>   grep would
> #+end_src
>
> #+results:
> #+begin_example
> What I would like to do is have a source code block in python or perl or
> that the source code block, when evaluated, would then act on. Which would
> #+end_example
>
> Best regards,
>   Seb
>
> --
> Sebastien Vauban
>
>
>


-- 
====================================
Michael Baum <maabaum@gmail.com>

You should never have your best trousers on when you go out
 to fight for freedom and truth. - Ibsen

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

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

* Re: How to pass a block of text to a code block as data?
  2013-02-09  2:59   ` Michael Baum
@ 2013-02-09 18:49     ` Sean O'Halpin
  2013-02-11 21:49       ` Michael Baum
  0 siblings, 1 reply; 6+ messages in thread
From: Sean O'Halpin @ 2013-02-09 18:49 UTC (permalink / raw)
  To: Michael Baum; +Cc: Sebastien Vauban, emacs-orgmode

On Sat, Feb 9, 2013 at 2:59 AM, Michael Baum <maabaum@gmail.com> wrote:
>
> - What signals the end of the block of text to be used as data? I take it
> that it's important that these all be comment lines staring with a colon
> after the #+name label? Is there a way to do the same thing with a begin and
> end block construction?
>
> - In this line:
>
>        #+begin_src sh :stdin lines-of-text :results output
>
> does the flag :stdin mean that the following named block literally becomes
> the STDIN stream for the code block? If I replace your shell/grep example
> with this:
>
> #+begin_src perl :stdin lines-of-text :results output
> while (<>) {
>    print $_;
> }
> #+end_src
>
> ...it doesn't work, although as far as I know that perl code snippet should
> in fact simply print out the incoming lines from stdin.
>
> Thanks again,
>
> Michael

Hi,

The :stdin option is only defined for shell and awk AFAIK. (Might be
an idea to add to other languages..)

You can pass in a variable referring to the block of text, as shown
below (example for ruby but should work for perl):

#+begin_src org

#+name: more-lines-of-text
#+begin_example
What signals the end of the block of text to be used as data? I take
it that it's important that these all be comment lines staring with a
colon after the #+name label? Is there a way to do the same thing with
a begin and end block construction?
#+end_example

#+begin_src ruby :var lines=more-lines-of-text :results output
  puts lines.reverse
#+end_src

#+RESULTS:
: ?noitcurtsnoc kcolb dne dna nigeb a
: htiw gniht emas eht od ot yaw a ereht sI ?lebal eman+# eht retfa noloc
: a htiw gnirats senil tnemmoc eb lla eseht taht tnatropmi s'ti taht ti
: ekat I ?atad sa desu eb ot txet fo kcolb eht fo dne eht slangis tahW

#+end_src

Regards,
Sean

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

* Re: How to pass a block of text to a code block as data?
  2013-02-09 18:49     ` Sean O'Halpin
@ 2013-02-11 21:49       ` Michael Baum
  2013-02-11 22:07         ` Eric Schulte
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Baum @ 2013-02-11 21:49 UTC (permalink / raw)
  To: Sean O'Halpin; +Cc: Sebastien Vauban, emacs-orgmode

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

Sean, that helps too, thank you. Now that you and Sebastien have gone to
all this trouble I found the part of the manual that sort of describes
this, but I clearly didn't understand it before. Possible needs a more
worked-out example for the slow among us, like self.

I've noticed one curious thing in trying a perl example. See first:

---------------------8<----------------------------------
#+name: wake
#+BEGIN_EXAMPLE
    riverrun, past Eve and Adam's, from swerve of shore to bend
of bay, brings us by a commodius vicus of recirculation back to
Howth Castle and Environs.
    Sir Tristram, violer d'amores, fr'over the short sea, had passen-
core rearrived from North Armorica on this side the scraggy
isthmus of Europe Minor to wielderfight his penisolate war: nor
had topsawyer's rocks by the stream Oconee exaggerated themselse
to Laurens County's gorgios while they went doublin their mumper
all the time: nor avoice from afire bellowsed mishe mishe to
#+END_EXAMPLE

#+begin_src perl :var inlines=wake :results output
  foreach $aln (split(/$/,$inlines)) {
       print $aln;
  }
#+end_src


#+results:
: riverrun, past Eve and Adam's, from swerve of shore to bend
: of bay, brings us by a commodius vicus of recirculation back to
: Howth Castle and Environs.
:     Sir Tristram, violer d'amores, fr'over the short sea, had passen-
: core rearrived from North Armorica on this side the scraggy
: isthmus of Europe Minor to wielderfight his penisolate war: nor
: had topsawyer's rocks by the stream Oconee exaggerated themselse
: to Laurens County's gorgios while they went doublin their mumper
: all the time: nor avoice from afire bellowsed mishe mishe to

---------------------8<----------------------------------

and then a more complicated block that's closer to my real task:

---------------------8<----------------------------------

#+NAME: job2
#+BEGIN_EXAMPLE
!START
!ID:7655
!DATE:02/10/2013
!CLOSE:03/15/2013
!UNTILFILLED:
!POSITION:Science Editor
!COMPANY:East Newark Times Herald News and World Defender
!BEGIN-DESCRIPTION
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut
wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero
eros et accumsan et iusto odio dignissim qui blandit praesent luptatum
zzril delenit augue duis dolore te feugait nulla facilisi.

!END-DESCRIPTION
!BEGIN-SPECIAL
Duis eget lorem ac odio lobortis suscipit nec et neque. Sed at quam ut
mauris scelerisque congue id eget dui. Quisque tellus lectus, tristique eu
posuere in, faucibus vitae urna. Duis vitae orci purus, quis euismod augue.
!END-SPECIAL
!SALARY:16.67 per hour
!BEGIN-CONTACT
Please submit online at  http://enthnawd.org/jobs
!END-CONTACT
!END
#+END_EXAMPLE


#+begin_src perl :var inlines=job2 :results output
  foreach $aln (split(/$/,$inlines)) {
       print $aln;
  }
#+end_src

#+results:
#+begin_example
!START
!ID:7655
!DATE:02/10/2013
!CLOSE:03/15/2013
!UNTILFILLED:
!POSITION:Science Editor
!COMPANY:East Newark Times Herald News and World Defender
!BEGIN-DESCRIPTION
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut
wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero
eros et accumsan et iusto odio dignissim qui blandit praesent luptatum
zzril delenit augue duis dolore te feugait nulla facilisi.

!END-DESCRIPTION
!BEGIN-SPECIAL
Duis eget lorem ac odio lobortis suscipit nec et neque. Sed at quam ut
mauris scelerisque congue id eget dui. Quisque tellus lectus, tristique eu
posuere in, faucibus vitae urna. Duis vitae orci purus, quis euismod augue.
!END-SPECIAL
!SALARY:16.67 per hour
!BEGIN-CONTACT
Please submit online at  http://enthnawd.org/jobs
!END-CONTACT
!END
#+end_example

---------------------8<----------------------------------

NOTICE THAT while both return the result as Example text, the first simple
prepends each line with a colon, simple Example form, and the second wraps
the result in an Example block without altering the lines.

Not sure why? Is this just a function of the number of lines of the text?

Michael


-- 
====================================
Michael Baum <maabaum@gmail.com>

You should never have your best trousers on when you go out
 to fight for freedom and truth. - Ibsen

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

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

* Re: How to pass a block of text to a code block as data?
  2013-02-11 21:49       ` Michael Baum
@ 2013-02-11 22:07         ` Eric Schulte
  0 siblings, 0 replies; 6+ messages in thread
From: Eric Schulte @ 2013-02-11 22:07 UTC (permalink / raw)
  To: Michael Baum; +Cc: Sebastien Vauban, emacs-orgmode

Michael Baum <maabaum@gmail.com> writes:

> Sean, that helps too, thank you. Now that you and Sebastien have gone to
> all this trouble I found the part of the manual that sort of describes
> this, but I clearly didn't understand it before. Possible needs a more
> worked-out example for the slow among us, like self.
>

Patches are welcome, especially documentation patches.

[...]
>
> NOTICE THAT while both return the result as Example text, the first simple
> prepends each line with a colon, simple Example form, and the second wraps
> the result in an Example block without altering the lines.
>
> Not sure why? Is this just a function of the number of lines of the text?
>

Yes, this is a function of the number of lines in the output text.  You
can control where this switch is made by changing the value of
org-babel-min-lines-for-block-output.

Best,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

end of thread, other threads:[~2013-02-11 22:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-08 17:06 How to pass a block of text to a code block as data? Michael Baum
2013-02-08 20:17 ` Sebastien Vauban
2013-02-09  2:59   ` Michael Baum
2013-02-09 18:49     ` Sean O'Halpin
2013-02-11 21:49       ` Michael Baum
2013-02-11 22:07         ` Eric Schulte

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