0

I have defined expand and execute functions for 'text' source blocks as follows:

(defun org-babel-expand-body:text (body params)
  (org-trim body))

(defun org-babel-execute:text (body params)
  (org-trim body))

I do this so that I can create basic templates for things like css, html, and configuration files. I want to use clojure to manipulate the text defined in these blocks as a string. For example, here I want to turn the output of the "sample" block into a clojure string in the "clojure-sample" block:

#+NAME: sample
#+begin_src text
some


text
#+end_src

#+RESULTS: sample
: some
: 
: 
: text

#+NAME: clojure-sample
#+begin_src clojure :noweb yes :results pp
"<<sample()>>"
#+end_src

But when I run org-babel-expand-src-block on the "clojure-sample" block, it expands to:

"some
"
"
"text"

Why this is not being expanded into a single string and how I might fix it?

These are the expand and execute definitions for clojure source blocks.

(defun org-babel-expand-body:clojure (body params)
  "Expand BODY according to PARAMS, return the expanded body."
  (let* ((vars (org-babel--get-vars params))
         (result-params (cdr (assq :result-params params)))
         (print-level nil)
         (print-length nil)
         (body
          (org-trim
           (format "%s"
                   ;; Variables binding.
                   (if (null vars) (org-trim body)
                     (format "(let [%s]\n%s)"
                             (mapconcat
                              (lambda (var)
                                (format "%S (quote %S)" (car var) (cdr var)))
                              vars
                              "\n      ")
                             body))))))
    (if (or (member "code" result-params)
            (member "pp" result-params))
        (format "(clojure.pprint/pprint (do %s))" body)
      body)))

(defun org-babel-execute:clojure (body params)
  (let ((expanded (org-babel-expand-body:clojure body params))
    (response (cons 'dict nil)))
    (let ((result-params (cdr (assoc :result-params params))))
      (setq response (nrepl-sync-request:eval expanded
                          (cider-current-connection)
                          (cider-current-ns)))
      (setq result (concat (nrepl-dict-get response
                       (if (or
                        (member "output" result-params)
                        (member "pp" result-params))
                           "out"
                         "value"))
               (nrepl-dict-get response "ex")
               (nrepl-dict-get response "root-ex")
               (nrepl-dict-get response "err")))
      (org-babel-result-cond (cdr (assoc :result-params params))
    result
    (condition-case nil (org-babel-script-excape result)
      (error result))))))

1 Answer 1

1

I got what I wanted to work by changing the text expand function to

(defun org-babel-expand-body:text (body params)
  (replace-regexp-in-string (regexp-quote "\n") "\\n" (org-trim body) nil 'literal))

A clojure source block will then expand a text source block in the way where it can be interpreted as a string and can be spit to a file.

Your Answer

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

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