Skip to content

Commit

Permalink
Merge pull request #23 from ASML-Labs/media-fix
Browse files Browse the repository at this point in the history
Fix for templates that already have an existing media directory
  • Loading branch information
matthijscox-asml authored Feb 10, 2023
2 parents cc0cbc2 + 2a2b816 commit 0e15364
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PPTX"
uuid = "14a86994-10a4-4a7d-b9ad-ef6f3b1fac6a"
authors = ["Xander de Vries", "Matthijs Cox"]
version = "0.5.1"
version = "0.5.2"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
41 changes: 18 additions & 23 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ function add_title_shape!(doc::EzXML.Document, slide::Slide, unzipped_ppt_dir::S
end

function write_shapes!(pres::Presentation)
mkdir("media")
if !isdir("media")
mkdir("media")
end
for slide in slides(pres)
for shape in shapes(slide)
if shape isa Picture
Expand Down Expand Up @@ -139,33 +141,25 @@ function Base.write(
end
end

origin = pwd()
try
mktempdir() do tmpdir
cd(tmpdir)
mktempdir() do tmpdir
cd(tmpdir) do
cp(template_path, template_name)
unzipped_dir = template_name
if template_isfile
unzip(template_name)
unzipped_dir = first(splitext(template_name)) # remove .pptx
end
ppt_dir = joinpath(unzipped_dir, "ppt")
cd(ppt_dir)
write_relationships!(p)
write_presentation!(p)
write_slides!(p)
write_shapes!(p)
update_table_style!()
cd(tmpdir)
cd(ppt_dir) do
write_relationships!(p)
write_presentation!(p)
write_slides!(p)
write_shapes!(p)
update_table_style!()
end
zip(unzipped_dir, filename)
cp(filename, filepath)
# need to cd out of folder, else mktempdir cannot cleanup
cd(origin)
end
catch e
rethrow(e)
finally
cd(origin)
end
if open_ppt
try
Expand Down Expand Up @@ -195,12 +189,13 @@ end
function zip(folder::String, filename::String)
zip_ext_filename = split(filename, ".")[begin] * ".zip"
origin = pwd()
cd(folder)
for f in readdir(".")
run_silent_pipeline(`$(exe7z()) a $zip_ext_filename $f`)
cd(folder) do
for f in readdir(".")
run_silent_pipeline(`$(exe7z()) a $zip_ext_filename $f`)
end
mv(zip_ext_filename, joinpath(origin, filename))
end
mv(zip_ext_filename, joinpath(origin, filename))
return cd(origin)
return nothing
end

# silent, unless we error
Expand Down
43 changes: 34 additions & 9 deletions test/testWriting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ function write_and_remove(fname::String, p::Presentation)
end

@testset "zipping/unzipping" begin
origin = @__DIR__

# simple zip/unzip test
mktempdir() do tmpdir
cd(tmpdir)
a_folder = abspath(joinpath(PPTX.TEMPLATE_DIR,"no-slides"))
cp(a_folder, abspath(joinpath(".","no-slides")))
PPTX.zip("no-slides", "zipfile.pptx")
@test isfile("zipfile.pptx")
PPTX.unzip("zipfile.pptx")
@test isdir("zipfile")
cd(origin)
cd(tmpdir) do
a_folder = abspath(joinpath(PPTX.TEMPLATE_DIR,"no-slides"))
cp(a_folder, abspath(joinpath(".","no-slides")))
PPTX.zip("no-slides", "zipfile.pptx")
@test isfile("zipfile.pptx")
PPTX.unzip("zipfile.pptx")
@test isdir("zipfile")
end
end
end

Expand Down Expand Up @@ -100,3 +99,29 @@ end
@test contains(str, "<a:clrScheme name=\"Office Theme\">")
end
end

@testset "custom template with media dir" begin
# test for issue https://github.com/ASML-Labs/PPTX.jl/issues/20
mktempdir() do tmpdir
template_name = "no-slides"
original_template_path = joinpath(PPTX.TEMPLATE_DIR, template_name)
edited_template_path = joinpath(tmpdir, template_name)
cp(original_template_path, edited_template_path)

# add an existing media directory
media_dir = joinpath(edited_template_path, "ppt", "media")
mkdir(media_dir)

pres = Presentation(;title="My Presentation")
s1 = Slide()
julia_logo = Picture(joinpath(PPTX.ASSETS_DIR,"julia_logo.png"), top = 110, left = 110)
push!(s1, julia_logo)
push!(pres, s1)

# originally this threw IOError: mkdir("media"; mode=0o777): file already exists (EEXIST)
pptx_path = joinpath(tmpdir, "example.pptx")
write(pptx_path, pres; open_ppt=false, template_path=edited_template_path)

@test isfile(pptx_path)
end
end

2 comments on commit 0e15364

@matthijscox-asml
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Fix for custom templates that already contain an existing media directory

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/77405

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.2 -m "<description of version>" 0e15364e4597d26d19a342c53931e2a5251e28a2
git push origin v0.5.2

Please sign in to comment.