使用 emacs org-mode 画图

Posted on 2013-01-27 15:43 chenfanyu 阅读(...) 评论(...) 编辑 收藏

概述

org画图主要有以下相关模块

  1. artist-mode/picture-mode: 生成 ascii 图
  2. ditaa: 根据ascii图生成图片
  3. graphviz: 生成关系图
  4. plantuml: 基于graphiz之上,生成uml图

当采用 org-mode 时,可以使用 #+BEGIN_SRC #+END_SRC 标签生成ditaa,graphiz,plantuml图

配置及相关准备工作

语言设置

设置 org-babel-load-languages 变量 增加允许语言设置, ditaa 为 t

(org-babel-do-load-languages
  'org-babel-load-languages
  '(;; other Babel languages
    (ditaa . t)
    (plantuml . t)
    (dot . t)
    (xxx . t)
    ))

这样设置后,可以按照以下方式调用

#+BEGIN_SRC ditaa :file some_filename.png :cmdline -r -s 0.8
#+BEGIN_SRC plantuml :file export-file-name :cmdline -charset UTF-8

如果不进行以上设置,通过begin_src将不会成功,需要按照以下方式调用:

#+begin_ditaa test.png -r -s 0.8

不进行安全性提示

生成图像时不予提示

(setq org-confirm-babel-evaluate nil)

建立 yas 快捷键

  1. dita
#+BEGIN_SRC ditaa :file ${1:export-file-name} :cmdline -r -s 0.8 
${0}
#+END_SRC 
  1. dot
#+BEGIN_SRC dot :file ${1:export-file-name}.png :cmdline -Kdot -Tpng
title ${0}
#+END_SRC 
  1. uml
#+BEGIN_SRC plantuml :file ${1:export-file-name} :cmdline -charset UTF-8
title ${0}
#+END_SRC

jar 包路径设置

(setq org-plantuml-jar-path
      (expand-file-name "~/.emacs.d/scripts/plantuml.jar"))
(setq org-ditaa-jar-path (format "%s%s" (if *cygwin* "c:/cygwin" "")
                                       (expand-file-name "~/.emacs.d/elpa/contrib/scripts/ditaa.jar")) )

预览图像

(add-hook 'org-babel-after-execute-hook 'bh/display-inline-images 'append)

; Make babel results blocks lowercase
(setq org-babel-results-keyword "results")

(defun bh/display-inline-images ()
  (condition-case nil
      (org-display-inline-images)
    (error nil)))

此时输入 C-c C-c 生成图像, C-c C-x C-v 开关内联图像显示

ditaa

实现原理

先通过artist-mode或者picture-mode生成ascii文本,然后

通过调用java 包 ditaa.jar 生成 图片,并将图片插入到 html 代码中. 如以下示例,实际调用的脚本是

java -Dfile.encoding=UTF-8 -jar /path/ditaa.jar -r -s 0.8 c\:/Users/ADMINI\~1/AppData/Local/Temp/babel-124Lpi/ditaa-124xjt e\:/gitroot/doc/org/share/emacs/ditaa_test.png

当前版本为 0.9

ditaa使用说明

参数说明
paramlong paramdesc
-e–encoding指定编码
-E–no-separation嵌套矩形是否分隔,缺省有分隔,设置后无
-r–round-corners圆角矩形
-s–scale矩形大小 ,比如 0.8
-o–overwrite如果有同名文件覆盖
-S–no-shadows
-A–no-antialias
-v–verbose
-h–help
-t–tabs
-h–html

图形规则

  1. 圆角矩形: 用/ \ 作为图形的四个顶角
  2. color
    • cxxx xxx别表示RGB
    • 也可以采用预设的值: cRED cGRE cBLK cBLU cPNK cYEL
  3. tags 在矩形内部标记:
    • {d} document
    • {s} storage
    • {io} input/output
  4. dashed lines 虚线 竖虚线 :: : 横虚线 :: =
  5. *
  6. 矩形内的文本: o :: · 号

示例

#+begin_src ditaa :file test_ditaa.png :cmdline -r -s 0.8

    +-----------+        +---------+
    |    PLC    |        |         |
    |  Network  +<------>+   PLC   +<---=---------+
    |    cRED   |        |  c707   |              |
    +-----------+        +----+----+              |
                              ^                   |
                              |                   |
                              |  +----------------|-----------------+
                              |  |                |                 |
                              v  v                v                 v
      +----------+       +----+--+--+      +-------+---+      +-----+-----+       Windows clients
      |          |       |          |      |           |      |           |      +----+      +----+
      | Database +<----->+  Shared  +<---->+ Executive +<-=-->+ Operator  +<---->|cYEL| . . .|cYEL|
      |   c707   |       |  Memory  |      |   c707    |      | Server    |      |    |      |    |
      +--+----+--+       |{d} cGRE  |      +------+----+      |   c707    |      +----+      +----+
         ^    ^          +----------+             ^           +-------+---+
         |    |                                   |
         |    +--------=--------------------------+
         v
+--------+--------+
|                 |
| Millwide System |            -------- Data ---------
| cBLU            |            --=----- Signals ---=--
+-----------------+

#+END_SRC

生成结果

https://images0.cnblogs.com/blog/485361/201301/27161349-3c9f33a1613947078d7c1037246ba62a.jpg

graphviz

  1. homepage 当前版本 2.28.0

示例

#+BEGIN_SRC dot :file test_graphviz.png :cmdline -Kdot -Tpng
digraph G {
  size="8,6"
  ratio=expand
  edge [dir=both]
  plcnet [shape=box, label="PLC Network"]
  subgraph cluster_wrapline {
    label="Wrapline Control System"
    color=purple
    subgraph {
    rank=same
    exec
    sharedmem [style=filled, fillcolor=lightgrey, shape=box]
    }
    edge[style=dotted, dir=none]
    exec -> opserver
    exec -> db
    plc -> exec
    edge [style=line, dir=both]
    exec -> sharedmem
    sharedmem -> db
    plc -> sharedmem
    sharedmem -> opserver
  }
  plcnet -> plc [constraint=false]
  millwide [shape=box, label="Millwide System"]
  db -> millwide

  subgraph cluster_opclients {
    color=blue
    label="Operator Clients"
    rankdir=LR
    labelloc=b
    node[label=client]
    opserver -> client1
    opserver -> client2
    opserver -> client3
  }
}

#+end_src

https://images0.cnblogs.com/blog/485361/201301/27161352-25d17a200fdf4b2c9049799d71dbf9df.jpg

plantuml

  1. homepage 当前版本 7952

配置

示例

#+BEGIN_SRC plantuml :file test_uml.png  :cmdline -charset UTF-8
title Example Sequence Diagram
activate Client
Client -> Server: Session Initiation
note right: Client requests new session
activate Server
Client <-- Server: Authorization Request
note left: Server requires authentication
Client -> Server: Authorization Response
note right: Client provides authentication details
Server --> Client: Session Token
note left: Session established
deactivate Server
Client -> Client: Saves token
deactivate Client
#+END_SRC

https://images0.cnblogs.com/blog/485361/201301/27161355-092b3993d45c4577ab92050c1c052236.jpg

致谢

感谢 Open Source 提供的 cnblogs 插件, 并非常热心的及时解决了图片的发送问题!