Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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?
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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. :file foo perhaps? -- Nick
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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.
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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.
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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
Re: [O] Generate BEGIN_EXAMPLE block dynamically during export
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