clojurecup2015-mailhead/src/mailhead/visualize.clj

45 lines
1.2 KiB
Clojure
Raw Normal View History

2015-12-05 23:01:54 +01:00
(ns mailhead.visualize
(:use [tangle.core])
(:require [mailhead.parser :as parser]))
2015-12-06 00:46:25 +01:00
(defn edge-path [receive-path]
(let [path (reverse receive-path)
chain (concat (map :sender (butlast path)) [(:receiver (last path))])
cleaned-chain (map clojure.string/lower-case chain)]
(partition 2 (interleave cleaned-chain (rest cleaned-chain)))))
2015-12-05 23:01:54 +01:00
2015-12-06 00:46:25 +01:00
(defn edges [path]
2015-12-06 15:08:20 +01:00
(let [p (map-indexed
#(concat %2 [{:label (str (inc %1)) :fillcolor "gray" :color "gray"}])
2015-12-06 11:13:11 +01:00
(edge-path path))]
2015-12-06 00:46:25 +01:00
(into [] (map #(into [] %)) p)))
2015-12-05 23:01:54 +01:00
(defn draw-path [receive-path]
2015-12-06 11:13:11 +01:00
(graph->dot
[]
(edges receive-path)
{:node {:shape :box
:fontname "Arial"
:fontsize 12
:style "filled"
:fillcolor "darkgoldenrod1"
:color "darkgoldenrod3"}
:edge {:fontname "Arial"
:fontsize 10}
:directed? true
:graph {:rankdir :LR
:color "transparent"}}))
2015-12-05 23:01:54 +01:00
2015-12-06 11:13:11 +01:00
(defn save-image [dot token]
(let [path (str "generated-images/" token ".svg")]
(clojure.java.io/make-parents path)
(spit path (dot->svg dot))))
2015-12-05 23:01:54 +01:00
2015-12-06 11:13:11 +01:00
(defn next-token []
(str (java.util.UUID/randomUUID)))
2015-12-05 23:01:54 +01:00
(defn draw [parsed-email]
(let [token (next-token)]
(save-image (draw-path parsed-email) token)
token))