Generate SVG images

This commit is contained in:
Aaron Fischer 2015-12-06 11:13:11 +01:00
parent 2286e75f0d
commit d96250b444
2 changed files with 35 additions and 20 deletions

View file

@ -1,5 +1,6 @@
(ns mailhead.routes.home
(:require [mailhead.layout :as layout]
[mailhead.visualize :as v]
[compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :refer [ok]]
[clojure.java.io :as io]))
@ -11,7 +12,11 @@
(layout/render "about.html"))
(defn analyze-mailheader [mailheader]
(layout/render "result.html" {:mailheader mailheader}))
(let [token (v/draw mailheader)]
;; TODO: Generate token
;; TODO: Generate image
;; TODO: Show image and other data on result.html
(layout/render "result.html" {:mailheader mailheader})))
(defroutes home-routes
(GET "/" [] (home-page))

View file

@ -9,28 +9,38 @@
(partition 2 (interleave cleaned-chain (rest cleaned-chain)))))
(defn edges [path]
(let [p (map #(concat % [{:label "ms" :fillcolor "gray" :color "gray"}]) (edge-path path))]
(let [p (map
#(concat % [{:label "time" :fillcolor "gray" :color "gray"}])
(edge-path path))]
(into [] (map #(into [] %)) p)))
(defn draw-path [receive-path]
(-> (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"}})
(dot->image "png")))
(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"}}))
(defn draw [filename]
(draw-path (parser/parse-from-file filename)))
(defn save-image [dot token]
(let [path (str "generated-images/" token ".svg")]
(clojure.java.io/make-parents path)
(spit path (dot->svg dot))))
(draw "/home/aaron/workbench/clojurecup2015/sample-data/header1.eml")
(defn next-token []
(str (java.util.UUID/randomUUID)))
;; TODO: Make it work with strings
(defn draw [mailheader]
(let [filename "/home/aaron/workbench/clojurecup2015/sample-data/header1.eml"
token (next-token)]
(save-image (draw-path (parser/parse-from-file filename)) token)
token))