emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Generate BEGIN_EXAMPLE block dynamically during export
@ 2016-02-16 19:10 Kaushal Modi
  2016-02-16 20:02 ` Thomas S. Dye
  0 siblings, 1 reply; 11+ messages in thread
From: Kaushal Modi @ 2016-02-16 19:10 UTC (permalink / raw)
  To: emacs-org list

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

Hi all,

I'd like to have an EXAMPLE block in my org file whose value is set using
something like an org macro at the time of export.

So, something like:

#+MACRO TAR_FILE some_file.tar.gz

#+BEGIN_EXAMPLE
> tar xvzf {{{TAR_FILE}}}
#+END_EXAMPLE

As the above is not possible, what would be a good way to generate the
EXAMPLE block on the fly during export with the value of {{{TAR_FILE}}}
replaced based on the macro definition?

--
Kaushal Modi

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

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 19:10 Generate BEGIN_EXAMPLE block dynamically during export Kaushal Modi
@ 2016-02-16 20:02 ` Thomas S. Dye
  2016-02-16 20:51   ` Kaushal Modi
  0 siblings, 1 reply; 11+ messages in thread
From: Thomas S. Dye @ 2016-02-16 20:02 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list

Aloha Kaushal,

Kaushal Modi writes:

> Hi all,
>
> I'd like to have an EXAMPLE block in my org file whose value is set using
> something like an org macro at the time of export.
>
> So, something like:
>
> #+MACRO TAR_FILE some_file.tar.gz
>
> #+BEGIN_EXAMPLE
>> tar xvzf {{{TAR_FILE}}}
> #+END_EXAMPLE
>
> As the above is not possible, what would be a good way to generate the
> EXAMPLE block on the fly during export with the value of {{{TAR_FILE}}}
> replaced based on the macro definition?

You might use babel instead of a macro:

,---------------------------------------------------------------------------
| #+name: tar-eg                                                            
| #+header: :var tar-file=""                                                
| #+begin_src emacs-lisp                                                    
| (format "> tar xzvf %s" tar-file)                                         
| #+end_src                                                                 
|                                                                           
| #+CALL: tar-eg(tar-file="some_file.tar.gz") :wrap example :exports results
|                                                                           
| #+results:                                                                
| #+BEGIN_example                                                           
| > tar xzvf some_file.tar.gz                                               
| #+END_example                                                             
`---------------------------------------------------------------------------

hth,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 20:02 ` Thomas S. Dye
@ 2016-02-16 20:51   ` Kaushal Modi
  2016-02-16 21:03     ` Thomas S. Dye
  0 siblings, 1 reply; 11+ messages in thread
From: Kaushal Modi @ 2016-02-16 20:51 UTC (permalink / raw)
  To: Thomas S. Dye; +Cc: emacs-org list

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

Thanks Tom.

Your solution almost gets me there but I still need to manually update the
tar-file argument value in the #+CALL lines.

What I am looking for is for something like below to work (but it isn't). I
have multiple such #+CALL lines and I would like to not manually update the
tar-file arg in all those.
Below does not work because the {{{TAR_FILE}}} macro does not get expanded
in the #+CALL line.

#+MACRO: TAR_FILE some_file.tar.gz

#+NAME: tar-eg
#+HEADER: :var tar-file="" :exports none
#+BEGIN_SRC emacs-lisp
(format "> tar xzvf %s" tar-file)
#+END_SRC

#+CALL: tar-eg(tar-file="{{{TAR_FILE}}}") :wrap example :exports results

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

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 20:51   ` Kaushal Modi
@ 2016-02-16 21:03     ` Thomas S. Dye
  2016-02-16 21:16       ` Kaushal Modi
  0 siblings, 1 reply; 11+ messages in thread
From: Thomas S. Dye @ 2016-02-16 21:03 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list

Aloha Kaushal,

Kaushal Modi writes:

> Thanks Tom.
>
> Your solution almost gets me there but I still need to manually update the
> tar-file argument value in the #+CALL lines.
>
> What I am looking for is for something like below to work (but it isn't). I
> have multiple such #+CALL lines and I would like to not manually update the
> tar-file arg in all those.
> Below does not work because the {{{TAR_FILE}}} macro does not get expanded
> in the #+CALL line.
>
> #+MACRO: TAR_FILE some_file.tar.gz
>
> #+NAME: tar-eg
> #+HEADER: :var tar-file="" :exports none
> #+BEGIN_SRC emacs-lisp
> (format "> tar xzvf %s" tar-file)
> #+END_SRC
>
> #+CALL: tar-eg(tar-file="{{{TAR_FILE}}}") :wrap example :exports results

Perhaps this?

,--------------------------------------------------------------
| #+PROPERTY: header-args:emacs-lisp :var tar-file="bar.tar.gz"
|                                                              
| #+name: tar-eg                                               
| #+begin_src emacs-lisp                                       
| (format "> tar xzvf %s" tar-file)                            
| #+end_src                                                    
|                                                              
| #+CALL: tar-eg() :wrap example :exports results              
|                                                              
| #+results:                                                   
| #+BEGIN_example                                              
| > tar xzvf bar.tar.gz                                        
| #+END_example                                                
`--------------------------------------------------------------

hth,
Tom


-- 
Thomas S. Dye
http://www.tsdye.com

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 21:03     ` Thomas S. Dye
@ 2016-02-16 21:16       ` Kaushal Modi
  2016-02-16 21:47         ` Kaushal Modi
  0 siblings, 1 reply; 11+ messages in thread
From: Kaushal Modi @ 2016-02-16 21:16 UTC (permalink / raw)
  To: Thomas S. Dye; +Cc: emacs-org list

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

That works perfectly! Thank you!

Just to add that I had to have "#+HEADER: :exports none" as I did not want
the elisp code to be visible in the exported documents.

#+PROPERTY: header-args:emacs-lisp :var tar-file="bar.tar.gz"

#+NAME: tar-eg
#+HEADER: :exports none
#+BEGIN_SRC emacs-lisp
(format "> tar xzvf %s" tar-file)
#+END_SRC

#+CALL: tar-eg() :wrap example :exports results

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

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 21:16       ` Kaushal Modi
@ 2016-02-16 21:47         ` Kaushal Modi
  2016-02-16 22:18           ` Thomas S. Dye
  2016-02-16 22:28           ` Nick Dokos
  0 siblings, 2 replies; 11+ messages in thread
From: Kaushal Modi @ 2016-02-16 21:47 UTC (permalink / raw)
  To: Thomas S. Dye; +Cc: emacs-org list

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

OK, now I have just one follow up question. I would like to tangle the
result to a separate file.

So I am looking for a way to save the result "> tar xvzf bar.tar.gz" to a
separate file. I tried adding ":tangle filename" to #+CALL/#+BEGIN_SRC but
that did not help. Adding to #+CALL does not do anything. And adding
":tangle file" to #+BEGIN_SRC block exports the code (not results) to the
file as expected.

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

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 21:47         ` Kaushal Modi
@ 2016-02-16 22:18           ` Thomas S. Dye
  2016-02-16 22:20             ` Kaushal Modi
  2016-02-16 22:28           ` Nick Dokos
  1 sibling, 1 reply; 11+ messages in thread
From: Thomas S. Dye @ 2016-02-16 22:18 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list

Aloha Kaushal,

Kaushal Modi writes:

> OK, now I have just one follow up question. I would like to tangle the
> result to a separate file.
>
> So I am looking for a way to save the result "> tar xvzf bar.tar.gz" to a
> separate file. I tried adding ":tangle filename" to #+CALL/#+BEGIN_SRC but
> that did not help. Adding to #+CALL does not do anything. And adding
> ":tangle file" to #+BEGIN_SRC block exports the code (not results) to the
> file as expected.

Can't you export to the separate file?  

All the best,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 22:18           ` Thomas S. Dye
@ 2016-02-16 22:20             ` Kaushal Modi
  0 siblings, 0 replies; 11+ messages in thread
From: Kaushal Modi @ 2016-02-16 22:20 UTC (permalink / raw)
  To: Thomas S. Dye; +Cc: emacs-org list

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

I finally have this working!

#+PROPERTY: header-args:emacs-lisp :var tar-file="bar.tar.gz" :exports none
:file "results.txt"

#+NAME: tar-eg
#+BEGIN_SRC emacs-lisp
(format "> tar xzvf %s" tar-file)
#+END_SRC

#+CALL: tar-eg() :wrap example :exports none

#+CAPTION: =results.txt=
#+NAME: code__results
#+INCLUDE: "results.txt" :src text

I need to export to a separate file and also embed the results in the
exported document. So above is what I came up with.

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

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 21:47         ` Kaushal Modi
  2016-02-16 22:18           ` Thomas S. Dye
@ 2016-02-16 22:28           ` Nick Dokos
  2016-02-16 22:40             ` Kaushal Modi
  1 sibling, 1 reply; 11+ messages in thread
From: Nick Dokos @ 2016-02-16 22:28 UTC (permalink / raw)
  To: emacs-orgmode

Kaushal Modi <kaushal.modi@gmail.com> writes:

> OK, now I have just one follow up question. I would like to tangle the result to a separate file.
>
> So I am looking for a way to save the result "> tar xvzf bar.tar.gz" to a separate file. I tried adding
> ":tangle filename" to #+CALL/#+BEGIN_SRC but that did not help. Adding to #+CALL does not do anything.
>And adding ":tangle file" to #+BEGIN_SRC block exports the code (not results) to the file as expected.

:file foo

perhaps?
--
Nick

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 22:28           ` Nick Dokos
@ 2016-02-16 22:40             ` Kaushal Modi
  2016-02-16 23:10               ` Kaushal Modi
  0 siblings, 1 reply; 11+ messages in thread
From: Kaushal Modi @ 2016-02-16 22:40 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-org list

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

This is turning out to be more complicated than I anticipated ..

I need to
(1) Export the evaluated emacs-lisp block to a separate file, and
(2) also include the result in the same document

I can do that but it is a multi-step process:

(1) First I need to export the result to the file only. I cannot include it
as that file hasn't yet been created.

===== org buffer =====
#+AUTHOR:
#+OPTIONS: toc:nil
#+PROPERTY: header-args:emacs-lisp :var tar-file="bar.tar.gz" :exports
results :file "results.txt"

#+NAME: tar-eg
#+BEGIN_SRC emacs-lisp
(format "> tar xzvf %s" tar-file)
#+END_SRC

# #+CAPTION: =results.txt=
# #+NAME: code__results
# #+INCLUDE: "results.txt" :src text
=====

So "C-c C-e t A" gives me:

===== org ascii export =====
[file:results.txt]
=====

(2) But I do not need that file link in the export.. I need the whole
exported result embedded verbatim in the exported document too.

So I need to export the document the second time with the :exports in
#+PROPERTY set to none and the CAPTION/NAME/INCLUDE lines uncommented.

===== org buffer =====
#+AUTHOR:
#+OPTIONS: toc:nil
#+PROPERTY: header-args:emacs-lisp :var tar-file="bar.tar.gz" :exports none
:file "results.txt"

#+NAME: tar-eg
#+BEGIN_SRC emacs-lisp
(format "> tar xzvf %s" tar-file)
#+END_SRC

#+CAPTION: =results.txt=
#+NAME: code__results
#+INCLUDE: "results.txt" :src text
=====

Then I get the desired output on doing "C-c C-e t A".

===== org ascii export =====
,----
| > tar xzvf bar.tar.gz
`----
Listing 1: `results.txt'
=====

Is there a way to export results of a code block evaluation to a file and
also embed them in the exported document?

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

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

* Re: Generate BEGIN_EXAMPLE block dynamically during export
  2016-02-16 22:40             ` Kaushal Modi
@ 2016-02-16 23:10               ` Kaushal Modi
  0 siblings, 0 replies; 11+ messages in thread
From: Kaushal Modi @ 2016-02-16 23:10 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-org list

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

This seems to work from my brief testing:

#+AUTHOR:
#+OPTIONS: toc:nil
#+PROPERTY: header-args:emacs-lisp :var tar-file="bar.tar.gz" :exports none

#+NAME: tar-eg
#+BEGIN_SRC emacs-lisp
  (let ((contents (format "> tar xzvf %s" tar-file)))
    (with-temp-buffer
      (insert contents)
      (write-file "results.txt"))
    contents)
#+END_SRC

#+CALL: tar-eg() :wrap example :exports results

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

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

end of thread, other threads:[~2016-02-16 23:10 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-16 19:10 Generate BEGIN_EXAMPLE block dynamically during export Kaushal Modi
2016-02-16 20:02 ` Thomas S. Dye
2016-02-16 20:51   ` Kaushal Modi
2016-02-16 21:03     ` Thomas S. Dye
2016-02-16 21:16       ` Kaushal Modi
2016-02-16 21:47         ` Kaushal Modi
2016-02-16 22:18           ` Thomas S. Dye
2016-02-16 22:20             ` Kaushal Modi
2016-02-16 22:28           ` Nick Dokos
2016-02-16 22:40             ` Kaushal Modi
2016-02-16 23:10               ` Kaushal Modi

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