A new version of ob-plantuml

Great news: ob-plantuml has been accepted as a part of org-babel in its git repository.

Here is the new version of it:

;;; ob-plantuml.el --- org-babel functions for plantuml evaluation

;; Author: Zhang Weize

;;; Commentary:

;; Org-Babel support for evaluating plantuml script.
;;
;; Inspired by Ian Yang's org-export-blocks-format-plantuml
;; http://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el

;;; Code:
(require 'ob)

(defvar org-babel-default-header-args:plantuml
  '((:results . "file") (:exports . "results"))
  "Default arguments for evaluating a plantuml source block.")

(defun org-babel-expand-body:plantuml (body params &optional processed-params)
  "Expand BODY according to PARAMS, return the expanded body." body)

(defvar org-plantuml-jar-path)
(defun org-babel-execute:plantuml (body params)
  "Execute a block of plantuml code with org-babel.
This function is called by `org-babel-execute-src-block'."
  (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
        (out-file (cdr (assoc :file params)))
        (cmdline (cdr (assoc :cmdline params)))
        (in-file (make-temp-file "org-babel-plantuml")))
    (unless (file-exists-p org-plantuml-jar-path)
      (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
    (with-temp-file in-file (insert (concat "@startuml\n" body "\n@enduml")))
    (message (concat "java -jar " org-plantuml-jar-path
                     " -p " cmdline " < " in-file " > " out-file))
    (shell-command (concat "java -jar " (shell-quote-argument org-plantuml-jar-path)
                           " -p " cmdline " < " in-file " > " out-file))
    ; The method below will produce error when exporting the buffer.
    ;; (with-temp-buffer
    ;;   (call-process-shell-command
    ;;    (concat "java -jar " org-plantuml-jar-path " -p " cmdline)
    ;;    in-file
    ;;    '(t nil))
    ;;   (write-region nil nil out-file))
    out-file))

(defun org-babel-prep-session:plantuml (session params)
  "Return an error because plantuml does not support sessions."
  (error "Plantuml does not support sessions"))

(provide 'ob-plantuml)

;;; ob-plantuml.el ends here
About these ads
This entry was posted in emacs and tagged , , . Bookmark the permalink.

3 Responses to A new version of ob-plantuml

  1. Arnaud says:

    Hi,

    Great job!
    Anyway, I’ve noticed that in your source code, you have hardcoded PlantUML keywords.
    This may be an issue, because PlantUML language change quite often.

    To turn arround this, I’ve added an option “-language” in the command line:
    java -jar plantuml.jar -language

    This prints on the standard output a list of keywords/type/color. So instead of hardcoding
    those data, you can retrieve them dynamically. So when PlantUML.jar will be updated,
    new keywords will be automatically recognized in emacs.

    Do you thing it’s useful ?

    Regards,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s