emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Code snippet for Message Sequence Diagram export
@ 2010-05-16 20:18 Juan
  2010-05-17  8:13 ` Eric S Fraga
  0 siblings, 1 reply; 6+ messages in thread
From: Juan @ 2010-05-16 20:18 UTC (permalink / raw)
  To: Emacs-orgmode

Hi,

The following code adds an export block of type 'mscgen'. The block
body will be processed by the mscgen application. Sort of works, not
very tested yet.

;;; org-exp-mscgen.el --- message sequence chart support for org-export

;; Copyright (C) 2010
;;   Free Software Foundation, Inc.

;; Author: Juan Pechiar
;; Version: 0.1 2010-05-16

;; This is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This software is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This software provides EMACS org-mode export support for message
;; sequence charts. The mscgen utility is used for processing the
;; sequence definition, and must therefore be installed in the system.
;;
;; The code is a direct modification of org-export-blocks-format-ditaa
;; from the org-mode distribution.
;;
;; Mscgen is available and documented at http://www.mcternan.me.uk/mscgen/index.html
;;
;; Customize path to mscgen utility with org-mscgen-path variable
;;
;; Example:
;;
;; #+begin_mscgen example1
;;  A,B;
;;  --- [ label = "start of sequence" ];
;;  A -> B [ label = "send message" ];
;; #+end_mscgen
;;
;; TODO: check cleanup of temp files
;; TODO: fix automatic filename generation when name not given
;; TODO: export in EPS format for LaTeX (need to convert EPS to PDF for pdflatex to use)

(defvar org-mscgen-path
  "/opt/local/bin/mscgen"
  "Complete path to the mscgen executable")

(defun org-export-blocks-format-mscgen (body &rest headers)
  "Pass block BODY to the mscgen utility creating an image.
Enclosing tags \"msc{\" and \"}\" must not appear in BODY.

Specify the path at which the image should be saved as the first
element of headers. Additional header elements are ignored and
for future use."
  (message "mscgen-formatting...")
  (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " "))) ;; for building hash
         (data-file (make-temp-file "org-mscgen"))                         ;; temp storage for BODY
         (hash (sha1 (prin1-to-string (list body args))))                  ;; hash (body + args)
         (raw-out-file (if headers (car headers) "out_mscgen" ))           ;; output file
         (out-file-parts (cons raw-out-file "png"))
         (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts)))
    (unless (file-exists-p org-mscgen-path)
      (error (format "Could not find mscgen at %s" org-mscgen-path)))
    (cond
     ((or latexp htmlp docbookp)
      ;; HTML, LaTeX and DocBook export
      (unless (file-exists-p out-file)
        (mapc ;; remove old hashed versions of this file
         (lambda (file)
           (when (and (string-match (concat (regexp-quote (car out-file-parts))
                                            "_\\([[:alnum:]]+\\)\\."
                                            (regexp-quote (cdr out-file-parts)))
                                    file)
                      (= (length (match-string 1 out-file)) 40))
             (delete-file (expand-file-name file
                                            (file-name-directory out-file)))))
         (directory-files (or (file-name-directory out-file)
                              default-directory)))
        (with-temp-file data-file (insert (concat "msc {\n" body "\n}\n")))
        (message (concat org-mscgen-path " -T png  -o " out-file " -i " data-file))
        (shell-command (concat org-mscgen-path " -T png  -o " out-file " -i " data-file)))
      (cond
       (latexp
        (format "\n#+ATTR_LaTeX: width=0.85\\textwidth\n[[file:%s]]\n" out-file))
       (t
        (format "\n[[file:%s]]\n" out-file))))
     ;; export to other formats: keep body as is
     (t (concat
	 "\n#+BEGIN_EXAMPLE\n"
	 body (if (string-match "\n$" body) "" "\n")
	 "#+END_EXAMPLE\n")))))

(org-export-blocks-add-block '(mscgen org-export-blocks-format-mscgen nil))

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-05-23 22:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-16 20:18 Code snippet for Message Sequence Diagram export Juan
2010-05-17  8:13 ` Eric S Fraga
2010-05-17 20:04   ` Juan
2010-05-17 23:04     ` Dan Davison
2010-05-18  1:24       ` Juan
2010-05-23 22:43         ` Juan

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).