0

I'm trying to find a way how to conveniently edit(and pass) multi-line arguments to org-babel block.

In the example below, ${name} variable is pretty convenient to edit as inline variable but for ${storyDescription} it becomes very annoying because it could contain dozens of lines(separated by \n) and I would like to manage it like a regular text.

#+BEGIN_SRC http :pretty :var projectId="123" :var name="Test story name"
POST https://www.pivotaltracker.com/services/v5/projects/${projectId}/stories
X-TrackerToken: ${token}
Content-Type: application/json

{
  "name": ${name},
  "description": ${storyDescription},
  "current_state": "unstarted",
  "estimate": 1
}
#+END_SRC

Any hints are greatly appreciated.


Update.

For now, I'm using the following(dirty) hack in Ruby:

#+name: storyDescription

#+BEGIN_SRC ruby :results value
<<-EOL.gsub("\n", "\\n")
first line
second line
EOL
#+END_SRC

#+RESULTS: storyDescription
: first line\nsecond line\n

it works but I'm happy to accept more clean and idiomatic solution to this.

I'm wondering why there isn't some kind of BEGIN_SRC text(org-babel "text" language that's used just for setting long content blocks of plain text).

2
  • 1
    Why do you think that your solution is a (dirty) hack? It seems like a pretty elegant solution to me (although I would write it in elisp or python, but that does not make much difference).
    – NickD
    Jul 12, 2021 at 23:57
  • @NickD thanks for your comment. If you want to post elisp version I'll gladly accept it as an answer to this question. Jul 14, 2021 at 10:03

1 Answer 1

1

I think the given (Ruby) solution is fine: I don't consider it a dirty hack. The same thing can be done in just about any language of course. Particularly, as in my case, if one does not speak Ruby.

Here's a code block in emacs lisp (using the string library s.el, also available from MELPA):

#+name: desc-lisp
#+begin_src elisp :results drawer
(setq x "first line
second line
third line
")

(s-replace "\n" "\\n" x)
#+end_src

or in python (with :results value):

#+name: desc-python-value
#+begin_src python :results value
x="""first line
second line
third line
"""

return x.replace("\n", "\\n")
#+end_src

or in python with :results output:

#+name: desc-python-output
#+begin_src python :results output drawer
x="""first line
second line
third line
"""

print(x.replace("\n", "\\n"))
#+end_src

or even Ruby using a string to make it congruent to the ones above:

#+name: desc-ruby-value
#+begin_src ruby :results value drawer
x = "first line
second line
third line
"
    x.gsub("\n", "\\n")
#+end_src

In all cases, we define a string and then apply some transformation to it before returning it (in this case, translating newlines to the two-byte sequence <backslash>, <n>). Any transformation could be applied (including the identity transformation). In all the above cases, the output is first line\nsecond line\nthird line\n.

An alternative organization that may be slightly more modular is to just return the string untransformed from the code block: any transformation could then be applied in another code block that uses the string, E.g.:

#+name: desc-lisp
#+begin_src elisp :results drawer
"first line
second line
third line
"
#+end_src

#+name: desc-transformed
#+begin_src elisp :results drawer :var story=desc-lisp
(s-replace "\n" "\\n" story)
#+end_src


#+name: post-story
#+begin_src shell :var story=desc-transformed
echo $story
#+end_src

But all of these are minor variations on the theme which you established in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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