Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xcapaldi committed Dec 27, 2022
0 parents commit b37338e
Show file tree
Hide file tree
Showing 6 changed files with 826 additions and 0 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# ob-d2

## Introduction
`ob-go` enables [Org-Babel](http://orgmode.org/worg/org-contrib/babel/intro.html) support for evaluating [d2](https://d2lang.com/tour/intro/) code.
It was created based on the usage of [ob-ditaa](https://orgmode.org/worg//org-contrib/babel/languages/ob-doc-ditaa.html).
The d2 code is compiled via the `d2` command.

```
#+BEGIN_ d2 :file hello.png
x -> y: hello world
#+END_SRC
```

<div>
<img height="500px" alt="hello world" src="hello.png">
</div>

## Language Specific Header Arguments

In addition to the normal header arguments for Babel, d2 uses the `:flags` header to pass additional flags to the `d2` command.
`:file` is required since the output of d2 is an image.

## Additional Examples
All courtesy of Terrastruct, Inc. who published these in their d2 documentation.

### Flags to specify theme and sketch format

```
#+BEGIN_SRC d2 :file flags.png :flags -t 101 -s
High Mem Instance -> EC2 <- High CPU Instance: Hosted By
#+END_SRC
```

<div>
<img height="500px" alt="flags" src="flags.png">
</div>

### Complex example

```
#+BEGIN_SRC d2 :file complex.png
clouds: {
aws: {
load_balancer -> api
api -> db
}
gcloud: {
auth -> db
}
gcloud -> aws
}
#+END_SRC
```

<div>
<img height="700px" alt="complex" src="complex.png">
</div>

Binary file added complex.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added flags.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hello.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions ob-d2.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
;;; ob-d2.el --- org-babel functions for d2

;; Copyright (C) 2022-2023 Xavier Capaldi

;; Author: Xavier Capaldi
;; Keywords: d2, literate programming, reproducible research
;; Homepage: http://orgmode.org
;; Version: 0.01

;;; License:

;; This program 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, or (at your option)
;; any later version.
;;
;; This program 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; Org-Babel support for evaluating d2 diagram scripting source code.
;;
;; d2 differs from most standard languages in that:
;;
;; 1) there is no such thing as a "session" in d2
;;
;; 2) we are only going to return results of type "file"
;;
;; 3) you can specify `:flags' headers to be passed to the `d2' command
;;
;; 4) there are no variables (at least for now)

;;; Requirements:

;; - You must have d2 installed and d2 should be in your `exec-path'. If not,
;; feel free to modify `org-babel-d2-command' to the location of your d2
;; command.
;;
;; - `d2-mode' is also recommended for syntax highlighting and formatting,
;; however it is not required.

;;; TODO

;; - Provide better error feedback.

;;; Code:
(require 'ob)
(require 'org-compat)

;; optionally define a file extension for this language
(add-to-list 'org-babel-tangle-lang-exts '("d2" . "d2"))

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

(defvar org-babel-d2-command "d2"
"The d2 command to use to compile and run the d2 code.")

(defun org-babel-execute:d2 (body params)
"Execute a block of d2 code with org-babel.
This function is called by `org-babel-execute-src-block'."
(let* ((out-file (or (cdr (assq :file params))
(error
"d2 code block requires :file header argument")))
(flags (cdr (assq :flags params)))
(in-file (org-babel-temp-file "d2-src-" ".d2"))
(cmd (concat org-babel-d2-command
" " flags
" " (org-babel-process-file-name in-file)
" " (org-babel-process-file-name out-file))))

(with-temp-file in-file (insert body))
(message cmd)
(shell-command cmd)
nil))

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

(provide 'ob-d2)

;;; ob-d2.el ends here

0 comments on commit b37338e

Please sign in to comment.