fix double extensions in savefig

This commit is contained in:
Daniel Schwabeneder 2020-04-22 23:37:31 +02:00
parent 8b5fc95443
commit df6fa6fd68

View File

@ -88,7 +88,6 @@ const _savemap = Dict(
"tex" => tex, "tex" => tex,
"json" => json, "json" => json,
"html" => html, "html" => html,
"tex" => tex,
"tikz" => tex, "tikz" => tex,
"txt" => txt, "txt" => txt,
) )
@ -104,15 +103,12 @@ function getExtension(fn::AbstractString)
end end
function addExtension(fn::AbstractString, ext::AbstractString) function addExtension(fn::AbstractString, ext::AbstractString)
try oldfn, oldext = splitext(fn)
oldext = getExtension(fn) oldext = chop(oldext, head = 1, tail = 0)
if get(_extesion_map, oldext, oldext) == ext if get(_extension_map, oldext, oldext) == ext
return fn return fn
else else
return "$fn.$ext" return string(fn, ".", ext)
end
catch
return "$fn.$ext"
end end
end end
@ -125,21 +121,21 @@ file types, some also support svg, ps, eps, html and tex.
""" """
function savefig(plt::Plot, fn::AbstractString) function savefig(plt::Plot, fn::AbstractString)
fn = abspath(expanduser(fn)) fn = abspath(expanduser(fn))
# get the extension # get the extension
local ext fn, ext = splitext(fn)
try ext = chop(ext, head = 1, tail = 0)
ext = getExtension(fn) if isempty(ext)
catch
# if we couldn't extract the extension, add the default
ext = defaultOutputFormat(plt) ext = defaultOutputFormat(plt)
fn = addExtension(fn, ext)
end end
# save it # save it
func = get(_savemap, ext) do if haskey(_savemap, ext)
error("Unsupported extension $ext with filename ", fn) func = _savemap[ext]
return func(plt, fn)
else
error("Invalid file extension: ", fn)
end end
func(plt, fn)
end end
savefig(fn::AbstractString) = savefig(current(), fn) savefig(fn::AbstractString) = savefig(current(), fn)