@@ -22,7 +22,7 @@ defmodule ExDoc.Formatter.HTML do
22
22
extras = build_extras ( config , ".html" )
23
23
24
24
# 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 )
26
26
search_data = generate_search_data ( project_nodes , extras , config )
27
27
28
28
# TODO: Move this categorization to the language
@@ -284,37 +284,41 @@ defmodule ExDoc.Formatter.HTML do
284
284
@ doc """
285
285
Generate assets from configs with the given default assets.
286
286
"""
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
+ )
307
295
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 )
311
301
File . mkdir_p! ( target_dir )
312
302
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
318
322
end )
319
323
end
320
324
0 commit comments