Skip to content

Instantly share code, notes, and snippets.

@daemianmack
Last active May 11, 2020 18:18
Show Gist options
  • Save daemianmack/a10269d4f2e30f5c541eeceeaee85009 to your computer and use it in GitHub Desktop.
Save daemianmack/a10269d4f2e30f5c541eeceeaee85009 to your computer and use it in GitHub Desktop.
Generating a flow chart in org-mode
;; Using org-mode to describe state transitions with embedded elisp to
;; emit those state transitions as a graphical flow-chart via dot
;;
;; Modified with minor improvements from https://orgmode.org/worg/org-tutorials/org-dot-diagrams.html
;;
;; Place cursor in elisp code-block, eval with `C-c C-c`.
#+NAME: nodes
| *node* | *label* | *shape* | *fillcolor* | fg |
|-----------------------------+------------------------------------+---------+----------------+-------|
| get_user | get_user | | cornflowerblue | |
| resolve_or_create_user | resolve-or-create-user | | cornflowerblue | |
| user_in_datomic | is user in datomic? | diamond | yellow | |
| return_eid | return user EID | | | |
| return_follows | return follow info | circle | green | |
| upsert_editor_api_user | upsert-editor-api-user | | cornflowerblue | |
| call_editor_api | call-editor-api | | cornflowerblue | |
| editor_api_response_anomaly | editor-api response is anomaly? | diamond | yellow | |
| return_anomaly | return editor-api response anomaly | plain | firebrick1 | white |
| import_user | import-entity-tx-entity | | cornflowerblue | |
| import_anomaly | datomic response is anomaly? | diamond | yellow | |
| return_anomaly2 | return anomaly tx-result | plain | firebrick1 | white |
#+NAME: edges
| From | To | label |
|-----------------------------+-----------------------------+-------|
| get_user | resolve_or_create_user | |
| resolve_or_create_user | user_in_datomic | |
| user_in_datomic | return_eid | Y |
| return_eid | return_follows | |
| user_in_datomic | upsert_editor_api_user | N |
| upsert_editor_api_user | call_editor_api | |
| call_editor_api | editor_api_response_anomaly | |
| editor_api_response_anomaly | return_anomaly | Y |
| editor_api_response_anomaly | import_user | N |
| import_user | import_anomaly | |
| import_anomaly | return_anomaly2 | Y |
| import_anomaly | return_eid | N |
#+BEGIN_SRC elisp :imagename ~/get-user.png :results output raw :var nodes=nodes edges=edges
(defun write_nodes(x)
(format "%s [label=\"%s\" shape=%s style=\"filled\" fillcolor=\"%s\" fontcolor=\"%s\"]"
(nth 0 x)
(nth 1 x)
(if (string= "" (nth 2 x)) "box" (nth 2 x))
(if (string= "" (nth 3 x)) "none" (nth 3 x))
(if (string= "" (nth 4 x)) "black" (nth 4 x))))
(defun write_edges(x)
(format "%s -> %s [taillabel=\"%s\"]"
(nth 0 x)
(nth 1 x)
(nth 2 x)))
(defun dotgen(nodes edges)
(let ((dotbegin "digraph {\n")
(dotend "\n}"))
(concat dotbegin
(mapconcat #'write_nodes nodes "\n")
(mapconcat #'write_edges edges "\n")
dotend)))
(let* ((params (nth 2 (org-babel-get-src-block-info)))
(imagename (cdr (assq :imagename params))))
(assq-delete-all :imagename params)
(push `(:file . ,imagename) params)
(message "vvv")
(message (dotgen nodes edges))
(message "^^^")
(org-babel-execute:dot (dotgen nodes edges) params)
(princ (format "[[./%s]]" imagename)))
#+END_SRC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment