Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name = "Deneb"
uuid = "b5a61f88-a05b-4725-9526-4eca17cec623"
authors = ["brucala <brucala@gmail.com>"]
version = "0.4.3"
version = "0.5.0"

[deps]
DefaultApplication = "3f0dd361-4fe0-5fc6-8523-80b14ec94d85"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
MultilineStrings = "1e8d2bf6-9821-4900-9a2f-4d87552df2bd"
NodeJS_18_jll = "c1e1d063-8311-5f52-a749-c7b05e91ae37"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
VLConvert_jll = "f937dff4-8506-5d6a-ae08-1f9bcdfef0bf"

[weakdeps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Expand All @@ -26,4 +26,5 @@ JSON = "0.21"
MultilineStrings = "1"
NetworkLayout = "0.4.7"
Tables = "1"
VLConvert_jll = "1.8.0"
julia = "1.9"
2 changes: 1 addition & 1 deletion src/Deneb.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Deneb

using UUIDs
using NodeJS_18_jll
using VLConvert_jll
using JSON, Tables
using MultilineStrings: indent
using DefaultApplication
Expand Down
42 changes: 17 additions & 25 deletions src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,23 @@ end
### Convert to image
###

convert(s::AbstractSpec, fmt::Symbol) = read(
pipeline(vl2command(fmt), stdin=IOBuffer(json(s))),
String
)
function vl2command(fmt::Symbol)
fmt == :vega && (fmt = :vg)
valid_formats = (:vg, :png, :svg, :pdf)
function convert(spec::AbstractSpec, fmt::Symbol)
fname = tempname()
run_vlconvert(spec, fmt, fname)
return read(out_file, String)
Comment thread
brucala marked this conversation as resolved.
Outdated
end

function run_vlconvert(spec::AbstractSpec, fmt::Symbol, fname::String)
valid_formats = (:vg, :vega, :png, :svg, :pdf, :jpeg, :jpg)
Symbol(fmt) ∉ valid_formats && return @warn("Format must be any of $valid_formats")
deps = ["--yes", "-p", "vega", "-p", "vega-lite"]
fmt == :vg || append!(deps, ["-p", "canvas"])
Cmd(`$(node()) $npx $deps vl2$fmt`)
fmt == :vega && (fmt = :vg)
fmt == :jpg && (fmt = :jpeg)
spec_file = tempname()
write(spec_file, json(spec))
run(`$(vlconvert()) vl2$fmt -i $spec_file -o $fname`)
end


###
### HTML templates
###
Expand Down Expand Up @@ -210,7 +214,7 @@ html(spec;

"""
save(filename, spec)
Saves the spec as filename. Allowed formats are `.json`, `.html`, `.png`, `.svg`, `.pdf`.
Saves the spec as filename. Allowed formats are `.json`, `.html`, `.png`, `.svg`, `.pdf`, `.jpeg/jpg`, `.vg/vega`.
"""
function save(filename::AbstractString, mime::AbstractString, s::VegaLiteSpec)
showable(s, mime) || return
Expand All @@ -220,20 +224,8 @@ function save(filename::AbstractString, mime::AbstractString, s::VegaLiteSpec)
end
function save(filename::AbstractString, s::VegaLiteSpec)
file_ext = lowercase(splitext(filename)[2])
if file_ext == ".svg"
mime = "image/svg+xml"
elseif file_ext == ".pdf"
mime = "application/pdf"
elseif file_ext == ".png"
mime = "image/png"
elseif file_ext == ".json"
mime = "application/json"
elseif file_ext == ".html"
return write(filename, html(s))
else
throw(ArgumentError("Unknown file type."))
end
save(filename, mime, s)
file_ext == ".html" && return write(filename, html(s))
run_vlconvert(s, Symbol(file_ext[2:end]), filename)
end

###
Expand Down
Loading