Skip to content

Commit

Permalink
Fix and test the svgwriter
Browse files Browse the repository at this point in the history
This will help ensure that IJulia continues to work
  • Loading branch information
timholy committed Aug 30, 2018
1 parent 5b8801c commit 407a053
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
23 changes: 14 additions & 9 deletions src/ProfileView.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
__precompile__()

module ProfileView

using Profile
using Profile, UUIDs
using Colors

import Base: isequal, show
Expand Down Expand Up @@ -159,14 +157,21 @@ function prepare_image(bt, uip, counts, lidict, lkup, C, colorgc, combine,
img, lidict, imgtags
end

function svgwrite(filename::AbstractString, data, lidict; C = false, colorgc = true, fontsize = 12, combine = true, pruned = [])
function svgwrite(io::IO, data, lidict; C = false, colorgc = true, fontsize = 12, combine = true, pruned = [])
img, lidict, imgtags = prepare(data, C=C, lidict=lidict, colorgc=colorgc, combine=combine, pruned=pruned)
pd = ProfileData(img, lidict, imgtags, fontsize)
show(io, "image/svg+xml", pd)
end
function svgwrite(filename::AbstractString, data, lidict; kwargs...)
open(filename, "w") do file
show(file, "image/svg+xml", pd)
svgwrite(file, data, lidict; kwargs...)
end
nothing
end
function svgwrite(io::IO; kwargs...)
data, lidict = Profile.retrieve()
svgwrite(io, data, lidict; kwargs...)
end
function svgwrite(filename::AbstractString; kwargs...)
data, lidict = Profile.retrieve()
svgwrite(filename, data, lidict; kwargs...)
Expand All @@ -192,9 +197,9 @@ function show(f::IO, ::MIME"image/svg+xml", pd::ProfileData)
ystep = (height - (topmargin + botmargin)) / nrows
avgcharwidth = 6 # for Verdana 12 pt font
function eschtml(str)
s = replace(str, '<', "&lt;")
s = replace(s, '>', "&gt;")
s = replace(s, '&', "&amp;")
s = replace(str, '<' => "&lt;")
s = replace(s, '>' => "&gt;")
s = replace(s, '&' => "&amp;")
s
end
function printrec(f, samples, xstart, xend, y, tag, rgb)
Expand All @@ -219,7 +224,7 @@ function show(f::IO, ::MIME"image/svg+xml", pd::ProfileData)
# end
end

fig_id = string("fig-", replace(string(Base.Random.uuid4()), "-", ""))
fig_id = string("fig-", replace(string(uuid4()), "-" => ""))
svgheader(f, fig_id, width=width, height=height)
# rectangles are on a grid and split across multiple columns (must span similar adjacent ones together)
for r in 1:nrows
Expand Down
2 changes: 0 additions & 2 deletions src/ProfileViewSVG.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
__precompile__()

module ProfileViewSVG

function __init__()
Expand Down
8 changes: 4 additions & 4 deletions src/svgwriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const snapsvgjs = joinpath(@__DIR__, "..", "templates", "snap.svg-min.js")
const viewerjs = joinpath(@__DIR__, "viewer.js")

function escape_script(js::AbstractString)
return replace(js, "]]", "] ]")
return replace(js, "]]" => "] ]")
end

function svgheader(f::IO, fig_id::AbstractString; width=1200, height=706, font="Verdana")
Expand Down Expand Up @@ -37,9 +37,9 @@ end
function svgfinish(f::IO, fig_id)
print(f, """
</g></g>
<script><![CDATA[$(escape_script(readstring(snapsvgjs)))]]></script>
<script><![CDATA[$(escape_script(read(snapsvgjs, String)))]]></script>
<script><![CDATA[
$(escape_script(readstring(viewerjs)))
$(escape_script(read(viewerjs, String)))
(function (glob, factory) {
if (typeof require === "function" && typeof define === "function" && define.amd) {
require(["Snap.svg", "ProfileView"], function (Snap, ProfileView) {
Expand All @@ -64,7 +64,7 @@ function svgfinish(f::IO, fig_id)
fig.clip = Snap.select('#$fig_id-clip-rect');
fig.clip_width = fig.clip.getBBox().w;
fig.clip_middle = fig.clip.getBBox().cy;
fig.details = document.getElementById("$fig_id-details").firstChild;
fig.details = document.getElementById("$fig_id-details").firstChild;
fig.scale = 1.0;
fig.shift = fig.viewport_cx;
Expand Down
4 changes: 4 additions & 0 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ profile_unstable_test(1, 1)
Profile.clear()
@profile profile_unstable_test(10, 10^6)
ProfileView.view()

mktemp() do filename, io
ProfileView.svgwrite(io)
end

0 comments on commit 407a053

Please sign in to comment.