emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Put result output in different type of code block than original
@ 2011-10-10 15:44 Niels Giesen
  2011-10-11  8:55 ` Rasmus Pank Roulund
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Niels Giesen @ 2011-10-10 15:44 UTC (permalink / raw)
  To: emacs-orgmode list

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

Hi,

Say I've got a sh code block invoking curl to some json api, is it possible
some way to specify that the format of the output when :results output code
is in a json code block and *not* a sh code block?

#+begin_src sh :results output code :exports both
curl
https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
#+end_src

#+results:
#+BEGIN_SRC sh
{"userinfo" : {"id" : "QNCNFQUKKBCTTMAOIUFNOQVLDUFAJV", "fullname" : "Gekke
Dirk", "role" : "user"}}
#+END_SRC

whereas I'd like it to be

#+results:
#+BEGIN_SRC json
{"userinfo" : {"id" : "QNCNFQUKKBCTTMAOIUFNOQVLDUFAJV", "fullname" : "Gekke
Dirk", "role" : "user"}}
#+END_SRC

Any thoughts on how to go about this? Do other people have this same issue
and should there come an option in babel that handles this (one language
outputting source code for another?), or is there a way to specify ones own
outputter?

-- 
http://pft.github.com

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

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

* Re: Put result output in different type of code block than original
  2011-10-10 15:44 Put result output in different type of code block than original Niels Giesen
@ 2011-10-11  8:55 ` Rasmus Pank Roulund
  2011-10-11  9:06 ` Rainer M Krug
  2011-10-11 14:32 ` Eric Schulte
  2 siblings, 0 replies; 8+ messages in thread
From: Rasmus Pank Roulund @ 2011-10-11  8:55 UTC (permalink / raw)
  To: Niels Giesen; +Cc: emacs-orgmode list

Niels Giesen <niels.giesen@gmail.com> writes:

> Hi,
>
> Say I've got a sh code block invoking curl to some json api, is it possible
> some way to specify that the format of the output when :results output code
> is in a json code block and *not* a sh code block?

Does this help you?  (Lacking a relevant example)

#+begin_src R :results output latex
1+1
#+end_src

#+results:
#+BEGIN_LaTeX
[1] 2
#+END_LaTeX

–Rasmus

-- 
Sent from my Emacs

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

* Re: Put result output in different type of code block than original
  2011-10-10 15:44 Put result output in different type of code block than original Niels Giesen
  2011-10-11  8:55 ` Rasmus Pank Roulund
@ 2011-10-11  9:06 ` Rainer M Krug
  2011-10-11 14:04   ` Sebastien Vauban
  2011-10-11 14:32 ` Eric Schulte
  2 siblings, 1 reply; 8+ messages in thread
From: Rainer M Krug @ 2011-10-11  9:06 UTC (permalink / raw)
  To: Niels Giesen; +Cc: emacs-orgmode list

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

On Mon, Oct 10, 2011 at 5:44 PM, Niels Giesen <niels.giesen@gmail.com>wrote:

> Hi,
>
> Say I've got a sh code block invoking curl to some json api, is it possible
> some way to specify that the format of the output when :results output code
> is in a json code block and *not* a sh code block?
>
> #+begin_src sh :results output code :exports both
> curl
> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
> #+end_src
>
>
Does

#+begin_src sh :results output :exports both
echo #+BEGIN_SRC json
curl
https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
echo #+END_SRC
#+end_src

help?

Rainer


> #+results:
> #+BEGIN_SRC sh
> {"userinfo" : {"id" : "QNCNFQUKKBCTTMAOIUFNOQVLDUFAJV", "fullname" : "Gekke
> Dirk", "role" : "user"}}
> #+END_SRC
>
> whereas I'd like it to be
>
> #+results:
> #+BEGIN_SRC json
> {"userinfo" : {"id" : "QNCNFQUKKBCTTMAOIUFNOQVLDUFAJV", "fullname" : "Gekke
> Dirk", "role" : "user"}}
> #+END_SRC
>
> Any thoughts on how to go about this? Do other people have this same issue
> and should there come an option in babel that handles this (one language
> outputting source code for another?), or is there a way to specify ones own
> outputter?
>
> --
> http://pft.github.com
>



-- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology,
UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax (F):       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer@krugs.de

Skype:      RMkrug

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

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

* Re: Put result output in different type of code block than original
  2011-10-11  9:06 ` Rainer M Krug
@ 2011-10-11 14:04   ` Sebastien Vauban
  2011-10-11 16:01     ` Burton Samograd
  0 siblings, 1 reply; 8+ messages in thread
From: Sebastien Vauban @ 2011-10-11 14:04 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hi Rainer and Niels,

Rainer M Krug wrote:
> On Mon, Oct 10, 2011 at 5:44 PM, Niels Giesen <niels.giesen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:
>> Say I've got a sh code block invoking curl to some json api, is it possible
>> some way to specify that the format of the output when :results output code
>> is in a json code block and *not* a sh code block?

FYI, this is a need I also have, for example AWK outputting SQL code. There
has been some threads about this, but nothing is implemented so far in this
direction.

>> #+begin_src sh :results output code :exports both
>> curl
>> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
>> #+end_src
>
> Does
>
> #+begin_src sh :results output :exports both
> echo #+BEGIN_SRC json
> curl
> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
> echo #+END_SRC
> #+end_src
>
> help?

I like very much the idea of your workaround. Great thought!

Though, nope, it does not help the OP, because the echo lines don't output
anything here, as # is the symbol to begin a comment (in shell scripts). So,
the first line simply outputs a blank line...

Best regards,
  Seb

-- 
Sebastien Vauban

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

* Re: Put result output in different type of code block than original
  2011-10-10 15:44 Put result output in different type of code block than original Niels Giesen
  2011-10-11  8:55 ` Rasmus Pank Roulund
  2011-10-11  9:06 ` Rainer M Krug
@ 2011-10-11 14:32 ` Eric Schulte
  2 siblings, 0 replies; 8+ messages in thread
From: Eric Schulte @ 2011-10-11 14:32 UTC (permalink / raw)
  To: Niels Giesen; +Cc: emacs-orgmode list

Niels Giesen <niels.giesen@gmail.com> writes:

> Hi,
>
> Say I've got a sh code block invoking curl to some json api, is it possible
> some way to specify that the format of the output when :results output code
> is in a json code block and *not* a sh code block?
>

Hi Niels,

Currently there is no way to specify the type of returned source code
block.  Hopefully one of Rainer or Rasmus' suggestions to this thread
should serve as a sufficient work around.

I would augment Rainer's solution changing it to the following...

#+begin_src sh :results scalar raw
  echo "#+begin_src json"
    curl https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
  echo "#+end_src json"
#+end_src

which works for me -- or presumably would work for me if our-service
could getstfff for dirk with password catsbirthday.

Cheers -- Eric

>
> #+begin_src sh :results output code :exports both
> curl
> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
> #+end_src
>
> #+results:
> #+BEGIN_SRC sh
> {"userinfo" : {"id" : "QNCNFQUKKBCTTMAOIUFNOQVLDUFAJV", "fullname" : "Gekke
> Dirk", "role" : "user"}}
> #+END_SRC
>
> whereas I'd like it to be
>
> #+results:
> #+BEGIN_SRC json
> {"userinfo" : {"id" : "QNCNFQUKKBCTTMAOIUFNOQVLDUFAJV", "fullname" : "Gekke
> Dirk", "role" : "user"}}
> #+END_SRC
>
> Any thoughts on how to go about this? Do other people have this same issue
> and should there come an option in babel that handles this (one language
> outputting source code for another?), or is there a way to specify ones own
> outputter?

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

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

* Re: Put result output in different type of code block than original
  2011-10-11 14:04   ` Sebastien Vauban
@ 2011-10-11 16:01     ` Burton Samograd
  2011-10-11 16:52       ` Sebastien Vauban
  0 siblings, 1 reply; 8+ messages in thread
From: Burton Samograd @ 2011-10-11 16:01 UTC (permalink / raw)
  To: emacs-orgmode

"Sebastien Vauban"
<wxhgmqzgwmuf@spammotel.com> writes:

> Hi Rainer and Niels,
>
> Rainer M Krug wrote:
>> On Mon, Oct 10, 2011 at 5:44 PM, Niels Giesen <niels.giesen@gmail.com>wrote:
>>> Say I've got a sh code block invoking curl to some json api, is it possible
>>> some way to specify that the format of the output when :results output code
>>> is in a json code block and *not* a sh code block?
>
> FYI, this is a need I also have, for example AWK outputting SQL code. There
> has been some threads about this, but nothing is implemented so far in this
> direction.
>
>>> #+begin_src sh :results output code :exports both
>>> curl
>>> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
>>> #+end_src
>>
>> Does
>>
>> #+begin_src sh :results output :exports both
>> echo #+BEGIN_SRC json
>> curl
>> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
>> echo #+END_SRC
>> #+end_src
>>
>> help?
>
> I like very much the idea of your workaround. Great thought!
>
> Though, nope, it does not help the OP, because the echo lines don't output
> anything here, as # is the symbol to begin a comment (in shell scripts). So,
> the first line simply outputs a blank line...

Could you escape the comment character by using \#?

--
Burton Samograd

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

* Re: Put result output in different type of code block than original
  2011-10-11 16:01     ` Burton Samograd
@ 2011-10-11 16:52       ` Sebastien Vauban
  2011-10-11 20:55         ` Niels Giesen
  0 siblings, 1 reply; 8+ messages in thread
From: Sebastien Vauban @ 2011-10-11 16:52 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hi Burton,

Burton Samograd wrote:
> "Sebastien Vauban" <wxhgmqzgwmuf-geNee64TY+gS+FvcfC7Uqw@public.gmane.org> writes:
>> Hi Rainer and Niels,
>>
>> Rainer M Krug wrote:
>>> On Mon, Oct 10, 2011 at 5:44 PM, Niels Giesen <niels.giesen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:
>>>> Say I've got a sh code block invoking curl to some json api, is it possible
>>>> some way to specify that the format of the output when :results output code
>>>> is in a json code block and *not* a sh code block?
>>
>> FYI, this is a need I also have, for example AWK outputting SQL code. There
>> has been some threads about this, but nothing is implemented so far in this
>> direction.
>>
>>>> #+begin_src sh :results output code :exports both
>>>> curl
>>>> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
>>>> #+end_src
>>>
>>> Does
>>>
>>> #+begin_src sh :results output :exports both
>>> echo #+BEGIN_SRC json
>>> curl
>>> https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
>>> echo #+END_SRC
>>> #+end_src
>>>
>>> help?
>>
>> I like very much the idea of your workaround. Great thought!
>>
>> Though, nope, it does not help the OP, because the echo lines don't output
>> anything here, as # is the symbol to begin a comment (in shell scripts). So,
>> the first line simply outputs a blank line...
>
> Could you escape the comment character by using \#?

Yes, that does work as well (alternative to Eric's quoting method):

#+begin_src sh :results output raw :exports both
echo \#+BEGIN_SRC json
echo curl
echo https://our-service.org/getstuff?username=dirk\&password=catsbrithday\&fmt=json
echo \#+END_SRC
#+end_src

but my point was intended to be more general: this workaround may be difficult
to use for differnet languages to marry (my own need, in the past, was for AWK
outputting SQL + another I've forgotten about right now).

Anyway, this currently can be made to work, as shown.

Best regards,
  Seb

PS- I was expecting that multiple evaluations of the code block would lead to
    multiple results blocks, because of the raw specifier. It's not the case.
    Is is perfectly normal and explainable, or a corner case which favors --
    in this case -- this workaround?

-- 
Sebastien Vauban

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

* Re: Put result output in different type of code block than original
  2011-10-11 16:52       ` Sebastien Vauban
@ 2011-10-11 20:55         ` Niels Giesen
  0 siblings, 0 replies; 8+ messages in thread
From: Niels Giesen @ 2011-10-11 20:55 UTC (permalink / raw)
  To: emacs-orgmode

To all people in this thread,

Thank you for all the responses.

The echoing of #+begin_src and #end_src is indeed a workaround that I
had not thought of myself, though it is not that general, and puts stuff
in the source that really oughtn't be there.

In the mean time I came up with a piece of advise to
`org-babel-insert-result' that does the trick in a general way.

#+begin_src emacs-lisp
  (defadvice org-babel-insert-result (around pft/output-type)
    (let* ((all-params (caddr info))
           (lang (or (cdr (assoc :out all-params)) lang)))
      ad-do-it))

(ad-activate 'org-babel-insert-result)
#+end_src

Use it with :out your-choice-of-lang

And while I was at it, I added indentation:

#+begin_src elisp
(defadvice org-babel-insert-result (around pft/output-indent)
  (let* ((all-params (caddr info))
         (indent-after (assoc :indent-after all-params)))
    ad-do-it
    (when indent-after
     (save-excursion
       (when (re-search-forward
              "#\\+begin_src " nil t)
         (beginning-of-line)
         (org-edit-special)
         (indent-region (point-min) (point-max))
         (org-edit-src-exit))))))
#+end_src

so... this it what it does:

#+begin_src sh :results output code :out json :indent-after
  echo "{\"peul\":\"erwt\",\n\"graan\":\"rijst\"}"
#+end_src

#+results:
#+BEGIN_SRC json
  {"peul":"erwt",
   "graan":"rijst"}
#+END_SRC

I do not know whether it plays nice with all other input forms and
:result parameters, as there seem to be some special handlers for e.g.
LaTeX and emacs-lisp, but I guess this would help the AWK->SQL case too.

If this works well enough, these pieces of advise may serve as a basis
for a patch to `org-babel-insert-result'. What do you think?

-- 
http://pft.github.com/

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

end of thread, other threads:[~2011-10-11 20:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-10 15:44 Put result output in different type of code block than original Niels Giesen
2011-10-11  8:55 ` Rasmus Pank Roulund
2011-10-11  9:06 ` Rainer M Krug
2011-10-11 14:04   ` Sebastien Vauban
2011-10-11 16:01     ` Burton Samograd
2011-10-11 16:52       ` Sebastien Vauban
2011-10-11 20:55         ` Niels Giesen
2011-10-11 14:32 ` 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).