Skip to content

Commit 5113929

Browse files
authored
Merge pull request #284 from emil0r/feature/graphviz
graphviz (js) added
2 parents cc9684a + 9ff62c6 commit 5113929

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

notebooks/clay_book/examples.clj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,20 @@ flowchart TD
195195
C -->|Three| F[fa:fa-car Car]
196196
")
197197

198+
;; ## graphviz
199+
200+
(kind/graphviz "digraph D {
201+
202+
A [shape=diamond]
203+
B [shape=box]
204+
C [shape=circle]
205+
206+
A -> B [style=dashed, color=grey]
207+
A -> C [color=\"black:invis:black\"]
208+
A -> D [penwidth=5, arrowhead=none]
209+
210+
}")
211+
198212
;; ## Code
199213

200214
;; Clojure code can be annotated with `kind/code`.

src/scicloj/clay/v2/item.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@
294294
(first value)]
295295
:deps [:mermaid]})
296296

297+
(defn graphviz [{:as _context
298+
:keys [value]}]
299+
{:hiccup [:div
300+
;; create a local closure and then execute it immediately
301+
[:script {:type "text/javascript"}
302+
(format "(function() {
303+
const el = document.currentScript.parentElement
304+
Viz.instance().then(viz => {
305+
try {
306+
const svg = viz.renderSVGElement(`%s`);
307+
308+
el.appendChild(svg);
309+
} catch (e) {
310+
console.error(e);
311+
}
312+
})})
313+
();" (first value))]]
314+
:deps [:graphviz]})
315+
297316
(defn plotly [{:as context
298317
:keys [kindly/options value]
299318
{:keys [data layout config]

src/scicloj/clay/v2/page.clj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
[clj-yaml.core :as yaml]
55
[clojure.java.io :as io]
66
[clojure.java.shell :as shell]
7-
[clojure.string :as string]
7+
[clojure.string :as str]
88
[hiccup.core :as hiccup]
99
[hiccup.page]
10+
[scicloj.clay.v2.files :as files]
11+
[scicloj.clay.v2.item :as item]
1012
[scicloj.clay.v2.prepare :as prepare]
1113
[scicloj.clay.v2.styles :as styles]
1214
[scicloj.clay.v2.util.portal :as portal]
13-
[scicloj.clay.v2.util.resource :as resource]
14-
[scicloj.clay.v2.files :as files]
15-
[scicloj.clay.v2.item :as item]
16-
[clojure.string :as str]))
15+
[scicloj.clay.v2.util.resource :as resource]))
1716

1817
(def special-lib-resources
1918
{:vega {:js {:from-local-copy
@@ -26,6 +25,8 @@
2625
["https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css"]}}
2726
:mermaid {:js {:from-the-web
2827
["https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js"]}}
28+
:graphviz {:js {:from-the-web
29+
["https://cdn.jsdelivr.net/npm/@viz-js/[email protected]/dist/viz-global.min.js"]}}
2930
:echarts {:js {:from-local-copy
3031
["https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"]}}
3132
:cytoscape {:js {:from-local-copy
@@ -295,7 +296,7 @@
295296
(map-indexed
296297
(fn [i item]
297298
(prepare/item->md item)))
298-
(string/join "\n\n")))))
299+
(str/join "\n\n")))))
299300

300301

301302
(defn gfm [{:as spec
@@ -305,7 +306,7 @@
305306
(remove nil?)
306307
(mapcat #(str/split % #"\n"))
307308
(remove #(re-matches #":::.*" %))
308-
(string/join "\n")))
309+
(str/join "\n")))
309310

310311
(defn hiccup [{:as spec
311312
:keys [items title quarto]}]

src/scicloj/clay/v2/prepare.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@
543543
:kind/mermaid
544544
#'item/mermaid)
545545

546+
(add-preparer!
547+
:kind/graphviz
548+
#'item/graphviz)
549+
546550
(add-preparer!
547551
:kind/plotly
548552
#'item/plotly)

0 commit comments

Comments
 (0)