diff --git a/lib/ex_doc/formatter/epub.ex b/lib/ex_doc/formatter/epub.ex
index 0f9136198..94281d99e 100644
--- a/lib/ex_doc/formatter/epub.ex
+++ b/lib/ex_doc/formatter/epub.ex
@@ -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)
diff --git a/lib/ex_doc/formatter/epub/templates.ex b/lib/ex_doc/formatter/epub/templates.ex
index 93facbfeb..7fd10e2f3 100644
--- a/lib/ex_doc/formatter/epub/templates.ex
+++ b/lib/ex_doc/formatter/epub/templates.ex
@@ -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
diff --git a/lib/ex_doc/formatter/epub/templates/content_template.eex b/lib/ex_doc/formatter/epub/templates/content_template.eex
index de22444f2..ed7b01939 100644
--- a/lib/ex_doc/formatter/epub/templates/content_template.eex
+++ b/lib/ex_doc/formatter/epub/templates/content_template.eex
@@ -23,14 +23,14 @@
<%= for filter <- [:modules, :tasks], node <- nodes[filter] do %>
<% end %>
- <%= for static_file <- static_files do %>
-
+ <%= for {static_file, media_type} <- static_files do %>
+
<% end %>
<%= if config.cover do %>
-
+
<% end %>
<%= if config.logo do %>
-
+
<% end %>
diff --git a/test/ex_doc/formatter/epub_test.exs b/test/ex_doc/formatter/epub_test.exs
index 2bfdc3845..3f6c6e81d 100644
--- a/test/ex_doc/formatter/epub_test.exs
+++ b/test/ex_doc/formatter/epub_test.exs
@@ -67,19 +67,6 @@ defmodule ExDoc.Formatter.EPUBTest do
assert content =~ ~r{Jane Doe}
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")
@@ -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,
@@ -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