Skip to content

Commit

Permalink
Do not crash on invalid assets, simply don't link them, closes #1924
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jun 20, 2024
1 parent 1e630ba commit 7018022
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
9 changes: 4 additions & 5 deletions lib/ex_doc/formatter/epub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ defmodule ExDoc.Formatter.EPUB do

defp generate_content(config, nodes, uuid, datetime, static_files) do
static_files =
static_files
|> Enum.filter(fn name ->
String.contains?(name, "OEBPS") and config.output |> Path.join(name) |> File.regular?()
end)
|> Enum.map(&Path.relative_to(&1, "OEBPS"))
for name <- static_files,
String.contains?(name, "OEBPS"),
media_type = Templates.media_type(Path.extname(name)),
do: {Path.relative_to(name, "OEBPS"), media_type}

content = Templates.content_template(config, nodes, uuid, datetime, static_files)
File.write("#{config.output}/OEBPS/content.opf", content)
Expand Down
5 changes: 2 additions & 3 deletions lib/ex_doc/formatter/epub/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,10 @@ defmodule ExDoc.Formatter.EPUB.Templates do
|> Enum.each(fn line ->
[extension, media] = String.split(line, ",")

defp media_type("." <> unquote(extension)) do
def media_type("." <> unquote(extension)) do
unquote(media)
end
end)

defp media_type(arg),
do: raise("asset with extension #{inspect(arg)} is not supported by EPUB format")
def media_type(_arg), do: nil
end
8 changes: 4 additions & 4 deletions lib/ex_doc/formatter/epub/templates/content_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
<%= for filter <- [:modules, :tasks], node <- nodes[filter] do %>
<item id="<%= URI.encode node.id %>" href="<%= URI.encode node.id %>.xhtml" media-type="application/xhtml+xml" properties="scripted"/>
<% end %>
<%= for static_file <- static_files do %>
<item id="<%= static_file_to_id(static_file) %>" href="<%= static_file %>" media-type="<%= media_type(Path.extname(static_file)) %>"/>
<%= for {static_file, media_type} <- static_files do %>
<item id="<%= static_file_to_id(static_file) %>" href="<%= static_file %>" media-type="<%= media_type %>"/>
<% end %>
<%= if config.cover do %>
<item id="cover-image" href="assets/cover<%= Path.extname(config.cover) %>" media-type="<%= media_type(Path.extname(config.cover))%>"/>
<item id="cover-image" href="assets/cover<%= Path.extname(config.cover) %>" media-type="<%= media_type(Path.extname(config.cover)) %>"/>
<% end %>
<%= if config.logo do %>
<item id="logo" href="assets/logo<%= Path.extname(config.logo) %>" media-type="<%= media_type(Path.extname(config.logo))%>"/>
<item id="logo" href="assets/logo<%= Path.extname(config.logo) %>" media-type="<%= media_type(Path.extname(config.logo)) %>"/>
<% end %>
</manifest>
<spine>
Expand Down
15 changes: 2 additions & 13 deletions test/ex_doc/formatter/epub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,6 @@ defmodule ExDoc.Formatter.EPUBTest do
assert content =~ ~r{<dc:creator id="author2">Jane Doe</dc:creator>}
end

test "raises when assets are invalid", context do
File.mkdir_p!("test/tmp/epub_assets/hello")
File.touch!("test/tmp/epub_assets/hello/world.pdf")

assert_raise(
RuntimeError,
~s{asset with extension ".pdf" is not supported by EPUB format},
fn -> generate_docs(doc_config(context, assets: %{"test/tmp/epub_assets" => "assets"})) end
)
after
File.rm_rf!("test/tmp/epub_assets")
end

test "generates an EPUB file in the default directory", %{tmp_dir: tmp_dir} = context do
generate_docs(doc_config(context))
assert File.regular?(tmp_dir <> "/epub/#{doc_config(context)[:project]}.epub")
Expand Down Expand Up @@ -232,6 +219,7 @@ defmodule ExDoc.Formatter.EPUBTest do
test "assets required by the user end up in the right place", %{tmp_dir: tmp_dir} = context do
File.mkdir_p!("test/tmp/epub_assets/hello")
File.touch!("test/tmp/epub_assets/hello/world.png")
File.touch!("test/tmp/epub_assets/hello/world.pdf")

generate_docs_and_unzip(
context,
Expand All @@ -243,6 +231,7 @@ defmodule ExDoc.Formatter.EPUBTest do
)

assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/hello/world.png")
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/hello/world.pdf")
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/logo.png")
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/cover.png")
after
Expand Down

0 comments on commit 7018022

Please sign in to comment.