Skip to content

Commit f4a46cb

Browse files
committed
Deprecate assets as a binary in favor of a map
1 parent f03cd9a commit f4a46cb

File tree

6 files changed

+48
-42
lines changed

6 files changed

+48
-42
lines changed

lib/ex_doc/config.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule ExDoc.Config do
1414
defstruct annotations_for_docs: &__MODULE__.annotations_for_docs/1,
1515
api_reference: true,
1616
apps: [],
17-
assets: nil,
17+
assets: %{},
1818
authors: nil,
1919
before_closing_body_tag: &__MODULE__.before_closing_body_tag/1,
2020
before_closing_footer_tag: &__MODULE__.before_closing_footer_tag/1,
@@ -54,7 +54,7 @@ defmodule ExDoc.Config do
5454
annotations_for_docs: (map() -> list()),
5555
api_reference: boolean(),
5656
apps: [atom()],
57-
assets: nil | String.t(),
57+
assets: %{binary() => binary()},
5858
authors: nil | [String.t()],
5959
before_closing_body_tag: (atom() -> String.t()) | mfa() | map(),
6060
before_closing_footer_tag: (atom() -> String.t()) | mfa() | map(),

lib/ex_doc/formatter/epub.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule ExDoc.Formatter.EPUB do
22
@moduledoc false
33

44
@mimetype "application/epub+zip"
5+
@assets_dir "OEBPS/assets"
56
alias __MODULE__.{Assets, Templates}
67
alias ExDoc.Formatter.HTML
78

@@ -30,10 +31,9 @@ defmodule ExDoc.Formatter.EPUB do
3031

3132
config = %{config | extras: extras}
3233

33-
assets_dir = "OEBPS/assets"
34-
static_files = HTML.generate_assets(config, assets_dir, default_assets(config))
35-
HTML.generate_logo(assets_dir, config)
36-
HTML.generate_cover(assets_dir, config)
34+
static_files = HTML.generate_assets("OEBPS", default_assets(config), config)
35+
HTML.generate_logo(@assets_dir, config)
36+
HTML.generate_cover(@assets_dir, config)
3737

3838
uuid = "urn:uuid:#{uuid4()}"
3939
datetime = format_datetime()

lib/ex_doc/formatter/html.ex

+33-29
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule ExDoc.Formatter.HTML do
2222
extras = build_extras(config, ".html")
2323

2424
# Generate search early on without api reference in extras
25-
static_files = generate_assets(config, @assets_dir, default_assets(config))
25+
static_files = generate_assets(".", default_assets(config), config)
2626
search_data = generate_search_data(project_nodes, extras, config)
2727

2828
# TODO: Move this categorization to the language
@@ -284,37 +284,41 @@ defmodule ExDoc.Formatter.HTML do
284284
@doc """
285285
Generate assets from configs with the given default assets.
286286
"""
287-
def generate_assets(config, assets_dir, defaults) do
288-
write_default_assets(config, defaults) ++ copy_assets(config, assets_dir)
289-
end
290-
291-
defp copy_assets(config, assets_dir) do
292-
if path = config.assets do
293-
path
294-
|> Path.join("**/*")
295-
|> Path.wildcard()
296-
|> Enum.map(fn source ->
297-
filename = Path.join(assets_dir, Path.relative_to(source, path))
298-
target = Path.join(config.output, filename)
299-
File.mkdir(Path.dirname(target))
300-
File.copy(source, target)
301-
filename
302-
end)
303-
else
304-
[]
305-
end
306-
end
287+
def generate_assets(namespace, defaults, %{output: output, assets: assets}) do
288+
namespaced_assets =
289+
if is_map(assets) do
290+
Enum.map(assets, fn {source, target} -> {source, Path.join(namespace, target)} end)
291+
else
292+
IO.warn(
293+
"giving a binary to :assets is deprecated, please give a map from source to target instead"
294+
)
307295

308-
defp write_default_assets(config, sources) do
309-
Enum.flat_map(sources, fn {files, dir} ->
310-
target_dir = Path.join(config.output, dir)
296+
[{assets, Path.join(namespace, "assets")}]
297+
end
298+
299+
Enum.flat_map(defaults ++ namespaced_assets, fn {dir_or_files, relative_target_dir} ->
300+
target_dir = Path.join(output, relative_target_dir)
311301
File.mkdir_p!(target_dir)
312302

313-
Enum.map(files, fn {name, content} ->
314-
target = Path.join(target_dir, name)
315-
File.write(target, content)
316-
Path.relative_to(target, config.output)
317-
end)
303+
cond do
304+
is_list(dir_or_files) ->
305+
Enum.map(dir_or_files, fn {name, content} ->
306+
target = Path.join(target_dir, name)
307+
File.write(target, content)
308+
Path.relative_to(target, output)
309+
end)
310+
311+
is_binary(dir_or_files) and File.dir?(dir_or_files) ->
312+
dir_or_files
313+
|> File.cp_r!(target_dir)
314+
|> Enum.map(&Path.relative_to(&1, output))
315+
316+
is_binary(dir_or_files) ->
317+
[]
318+
319+
true ->
320+
raise ":assets must be a map of source directories to target directories"
321+
end
318322
end)
319323
end
320324

lib/mix/tasks/docs.ex

+2-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ defmodule Mix.Tasks.Docs do
6969
* `:api_reference` - Whether to generate `api-reference.html`; default: `true`.
7070
If this is set to false, `:main` must also be set.
7171
72-
* `:assets` - Path to a directory that will be copied as is to the "assets"
73-
directory in the output path. Its entries may be referenced in your docs
74-
under "assets/ASSET.EXTENSION"; defaults to no assets directory.
72+
* `:assets` - A map of source => target directories that will be copied as is to
73+
the output path. It defaults to an empty map.
7574
7675
* `:authors` - List of authors for the generated docs or epub.
7776

test/ex_doc/formatter/epub_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ defmodule ExDoc.Formatter.EPUBTest do
7474
assert_raise(
7575
RuntimeError,
7676
~s{asset with extension ".pdf" is not supported by EPUB format},
77-
fn -> generate_docs(doc_config(context, assets: "test/tmp/epub_assets")) end
77+
fn -> generate_docs(doc_config(context, assets: %{"test/tmp/epub_assets" => "assets"})) end
7878
)
7979
after
8080
File.rm_rf!("test/tmp/epub_assets")
@@ -236,7 +236,7 @@ defmodule ExDoc.Formatter.EPUBTest do
236236
generate_docs_and_unzip(
237237
context,
238238
doc_config(context,
239-
assets: "test/tmp/epub_assets",
239+
assets: %{"test/tmp/epub_assets" => "assets"},
240240
logo: "test/fixtures/elixir.png",
241241
cover: "test/fixtures/elixir.png"
242242
)

test/ex_doc/formatter/html_test.exs

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule ExDoc.Formatter.HTMLTest do
2828
project: "Elixir",
2929
version: "1.0.1",
3030
formatter: "html",
31-
assets: "test/tmp/html_assets",
31+
assets: %{"test/tmp/html_assets" => "assets"},
3232
output: tmp_dir <> "/html",
3333
source_beam: "test/tmp/beam",
3434
source_url: "https://github.com/elixir-lang/elixir",
@@ -745,7 +745,10 @@ defmodule ExDoc.Formatter.HTMLTest do
745745
File.touch!("test/tmp/html_assets/hello/world")
746746

747747
generate_docs(
748-
doc_config(context, assets: "test/tmp/html_assets", logo: "test/fixtures/elixir.png")
748+
doc_config(context,
749+
assets: %{"test/tmp/html_assets" => "assets"},
750+
logo: "test/fixtures/elixir.png"
751+
)
749752
)
750753

751754
assert File.regular?(tmp_dir <> "/html/assets/logo.png")

0 commit comments

Comments
 (0)