0

(Follow-up to Is there a way to use org-mode to render generated code?)

#+BEGIN_SRC sh :exports results :results output :wrap SRC haskell
sed '/^elts/,$p' -n Heap03.hs
#+END_SRC

lets me extract Haskell code from a file to show in org-mode's export. It's effectively a safer and more idiomatic version of the hack I came up with:

#+BEGIN_SRC sh :exports results :results output verbatim raw
echo '#+BEGIN_SRC haskell'
sed '/^elts/,$p' -n Heap03.hs
echo '#+END_SRC'
#+END_SRC

Now I want more abstraction. For example:

#+NAME: inline
#+BEGIN_SRC sh :exports none :results output verbatim raw
echo '#+BEGIN_SRC haskell'
echo "[[./$FILE][$FILE]]"
sed '/^elts/,$p' -n $FILE
echo '#+END_SRC'
#+END_SRC

#+CALL: inline(FILE="Heap01.hs")
#+CALL: inline(FILE="Heap02.hs")

The two criteria I'm trying to satisfy (and having trouble) are:

  1. I should have a link to the file automatically inserted above the Haskell code.
  2. The call should be as minimal as possible.

I haven't figured out how to get the first criterion satisfied using :wrap, and the best I can do toward the second criterion is #+CALL: inline(FILE="Heap01.hs") :wrap SRC haskell, which is just a bit too heavyweight for my taste.

Thanks!

1

How about (note : after name, and the :var definition):

#+name: inline
#+BEGIN_SRC sh :exports none :results output verbatim raw :var FILE=""
echo "[[./$FILE][$FILE]]"
echo '#+BEGIN_SRC haskell'
sed '/^elts/,$p' -n $FILE
cat $FILE
echo '\n#+END_SRC'
#+END_SRC

And then (with results raw):

#+CALL: inline(FILE="Heap01.hs") :results raw
  • (The lack of a colon was just a transcription error on my part) This will break if the Haskell file contains "#+END_SRC" in the section being extracted, right? I tried to write an org-mode function that calls an org-mode function, but I couldn't get that to work. I'm wondering if I should just throw some elisp at the problem. – Alex R Feb 24 '16 at 15:40
  • you would want to escape the #+END_SRC as ,#+END_SRC. Also escape any lines beginning with * as ,*. It seems you are going the opposite way of tangling. Why not put your haskell code in org, and use tangle to get out, rather than the opposite? – John Kitchin Feb 24 '16 at 16:09
  • No good reason. Partially, I'm exploring org-mode's functionality; partially, my first instinct was to make a bunch of similar Haskell files to demonstrate a progression, but only display parts of them inline, and I'm just following it. – Alex R Feb 24 '16 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.