Skip to content

Compilation

Jude Payne edited this page Jan 28, 2023 · 7 revisions

The dictim.compile namespace exposes one public function, d2 which will convert dictim syntax to a d2 string.

user=> (use 'dictim.compile)
nil

;; compiling a multiple connection element
user> (d2 [:a "->" :b "<-" :c "--" :d "hi!" {:a 1}])
"a -> b <- c -- d: hi! {\n  a: 1\n}"

;; two shapes
user> (d2 [:gerbil] [:hamster "Hamster" {:shape "person"}])
"gerbil: \nhamster: Hamster {\n  shape: person\n}"

;; a container for the above
user> (d2 [:rod "Pets" [:gerbil] [:hamster "Hamster" {:shape "person"}]])
"rod: Pets {\n  gerbil: \n  hamster: Hamster {\n    shape: person\n  }\n}"

Larger pieces of dictim have multiple elements, so remember to apply!

user=>> (apply d2 '([:cocker "Cocker Spaniel" {:stlye.fill "green"}]
                    [:springer "Springer Spaniel" {:style.fill "'#d6903a'"}]))
"cocker: Cocker Spaniel  {\n  stlye.fill: green\n}\nspringer: Springer Spaniel  {\n  style.fill: '#d6903a'\n}"
user=>> ;; hex colors in dictim (& d2) must be quoted. Single quotes are easiest

multi-line labels

For multi-line labels on any dictim element, remember to escape, so \n should become \\n

(apply d2 '([:cocker "Cocker Spaniel" {:style.fill "green"}]
            [:springer "Springer\\nSpaniel" {:style.fill "'#d6903a'"}]))

Compiling dictim is fast, supporting the generation of new diagrams quickly.

Please have a look or run recipe2 in the dictim cookbook to see more complex dictim being generated. The tests directory also has more examples.

validation

Before compilation to d2, the dictim supplied to the d2 function is validated and an error thrown for the first term that fails validation.

Clone this wiki locally