Skip to content

Commit 7018022

Browse files
committed
Do not crash on invalid assets, simply don't link them, closes #1924
1 parent 1e630ba commit 7018022

File tree

4 files changed

+12
-25
lines changed

4 files changed

+12
-25
lines changed

lib/ex_doc/formatter/epub.ex

+4-5
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ defmodule ExDoc.Formatter.EPUB do
7676

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

8584
content = Templates.content_template(config, nodes, uuid, datetime, static_files)
8685
File.write("#{config.output}/OEBPS/content.opf", content)

lib/ex_doc/formatter/epub/templates.ex

+2-3
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,10 @@ defmodule ExDoc.Formatter.EPUB.Templates do
129129
|> Enum.each(fn line ->
130130
[extension, media] = String.split(line, ",")
131131

132-
defp media_type("." <> unquote(extension)) do
132+
def media_type("." <> unquote(extension)) do
133133
unquote(media)
134134
end
135135
end)
136136

137-
defp media_type(arg),
138-
do: raise("asset with extension #{inspect(arg)} is not supported by EPUB format")
137+
def media_type(_arg), do: nil
139138
end

lib/ex_doc/formatter/epub/templates/content_template.eex

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
<%= for filter <- [:modules, :tasks], node <- nodes[filter] do %>
2424
<item id="<%= URI.encode node.id %>" href="<%= URI.encode node.id %>.xhtml" media-type="application/xhtml+xml" properties="scripted"/>
2525
<% end %>
26-
<%= for static_file <- static_files do %>
27-
<item id="<%= static_file_to_id(static_file) %>" href="<%= static_file %>" media-type="<%= media_type(Path.extname(static_file)) %>"/>
26+
<%= for {static_file, media_type} <- static_files do %>
27+
<item id="<%= static_file_to_id(static_file) %>" href="<%= static_file %>" media-type="<%= media_type %>"/>
2828
<% end %>
2929
<%= if config.cover do %>
30-
<item id="cover-image" href="assets/cover<%= Path.extname(config.cover) %>" media-type="<%= media_type(Path.extname(config.cover))%>"/>
30+
<item id="cover-image" href="assets/cover<%= Path.extname(config.cover) %>" media-type="<%= media_type(Path.extname(config.cover)) %>"/>
3131
<% end %>
3232
<%= if config.logo do %>
33-
<item id="logo" href="assets/logo<%= Path.extname(config.logo) %>" media-type="<%= media_type(Path.extname(config.logo))%>"/>
33+
<item id="logo" href="assets/logo<%= Path.extname(config.logo) %>" media-type="<%= media_type(Path.extname(config.logo)) %>"/>
3434
<% end %>
3535
</manifest>
3636
<spine>

test/ex_doc/formatter/epub_test.exs

+2-13
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,6 @@ defmodule ExDoc.Formatter.EPUBTest do
6767
assert content =~ ~r{<dc:creator id="author2">Jane Doe</dc:creator>}
6868
end
6969

70-
test "raises when assets are invalid", context do
71-
File.mkdir_p!("test/tmp/epub_assets/hello")
72-
File.touch!("test/tmp/epub_assets/hello/world.pdf")
73-
74-
assert_raise(
75-
RuntimeError,
76-
~s{asset with extension ".pdf" is not supported by EPUB format},
77-
fn -> generate_docs(doc_config(context, assets: %{"test/tmp/epub_assets" => "assets"})) end
78-
)
79-
after
80-
File.rm_rf!("test/tmp/epub_assets")
81-
end
82-
8370
test "generates an EPUB file in the default directory", %{tmp_dir: tmp_dir} = context do
8471
generate_docs(doc_config(context))
8572
assert File.regular?(tmp_dir <> "/epub/#{doc_config(context)[:project]}.epub")
@@ -232,6 +219,7 @@ defmodule ExDoc.Formatter.EPUBTest do
232219
test "assets required by the user end up in the right place", %{tmp_dir: tmp_dir} = context do
233220
File.mkdir_p!("test/tmp/epub_assets/hello")
234221
File.touch!("test/tmp/epub_assets/hello/world.png")
222+
File.touch!("test/tmp/epub_assets/hello/world.pdf")
235223

236224
generate_docs_and_unzip(
237225
context,
@@ -243,6 +231,7 @@ defmodule ExDoc.Formatter.EPUBTest do
243231
)
244232

245233
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/hello/world.png")
234+
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/hello/world.pdf")
246235
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/logo.png")
247236
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/cover.png")
248237
after

0 commit comments

Comments
 (0)