Org Mode Postscript Source Blocks

org ps

Table of Contents

Hacky org-babel execute function for generating images from PostScript source blocks in Org Mode .

(defun org-babel-execute:ps (body params)
  "Execute a block of PostScript code with org-babel.
This function is called by `org-babel-execute-src-block'."
  (let* ((org-babel-euler-lisp-cmd (or (cdr (assq :cmd params)) org-babel-euler-lisp-cmd))
         (session (cdr (assq :session params)))
         (size (cdr (assq :size params)))
         (parent-dir (file-name-directory (buffer-file-name)))
         (outfile (or (cdr (assq :outfile params)) "out.png"))
         (outfile-abs (concat parent-dir outfile))
         (result-type (cdr (assq :result-type params)))
         (full-body (org-babel-expand-body:generic
                     ;; TODO: Handle variables
                     body params nil))
         (result (let ((script-file (org-babel-temp-file "ghostscript-")))
                   (with-temp-file script-file
                     (insert full-body)
                     (unless (string-suffix-p "showpage" (string-trim-right full-body))
                       (insert "\nshowpage")))
                   (org-babel-eval
                    (if size
                        (format
                         "gs -g%s -sDEVICE=png16m -o %s %s"
                         size
                         outfile-abs
                         (org-babel-process-file-name script-file))
                      (format
                       "gs -sDEVICE=png16m -o %s %s"
                       outfile-abs
                       (org-babel-process-file-name script-file)))
                    ""))))
    (org-babel-result-cond (cdr (assq :result-params params)) outfile outfile)))

The PNG output device expects a showpage at the end of the code. This will be inserted automatically.

Usage

#+begin_src ps :results file :size 1000x1000 :outfile images/ps/lines.png :exports both
   % PostScript code
#+end_src

If you have an idea how this page could be improved or a comment send me a mail.