Skip to content

Commit 22357bf

Browse files
committed
refactor: restructure EPUB, HTML, and Markdown output directories
- Updated output directory structure for EPUB, HTML, and Markdown formats to use a consistent `.build` subdirectory. - Introduced `output_setup` and `generate_build` functions to manage build file generation and cleanup. - Adjusted file writing logic to ensure proper paths are used for generated files. - Enhanced test cases to reflect changes in the output directory structure.
1 parent 83c8172 commit 22357bf

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

lib/ex_doc/formatter/epub.ex

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ defmodule ExDoc.Formatter.EPUB do
1111
"""
1212
@spec run([ExDoc.ModuleNode.t()], [ExDoc.ModuleNode.t()], ExDoc.Config.t()) :: String.t()
1313
def run(project_nodes, filtered_modules, config) when is_map(config) do
14+
original_output = Path.expand(config.output)
1415
config = normalize_config(config)
15-
File.rm_rf!(config.output)
16+
build_dir = Path.join(original_output, ".build/epub")
17+
build = Path.join(build_dir, ".build")
18+
output_setup(build, config)
1619
File.mkdir_p!(Path.join(config.output, "OEBPS"))
1720

1821
project_nodes =
@@ -40,12 +43,15 @@ defmodule ExDoc.Formatter.EPUB do
4043
uuid = "urn:uuid:#{uuid4()}"
4144
datetime = format_datetime()
4245

43-
generate_content(config, nodes_map, uuid, datetime, static_files)
44-
generate_nav(config, nodes_map)
45-
generate_title(config)
46-
generate_extras(config)
47-
generate_list(config, nodes_map.modules)
48-
generate_list(config, nodes_map.tasks)
46+
content_files = generate_content(config, nodes_map, uuid, datetime, static_files)
47+
nav_files = generate_nav(config, nodes_map)
48+
title_files = generate_title(config)
49+
extra_files = generate_extras(config)
50+
module_files = generate_list(config, nodes_map.modules)
51+
task_files = generate_list(config, nodes_map.tasks)
52+
53+
all_files = List.flatten([content_files, nav_files, title_files, extra_files, module_files, task_files])
54+
generate_build(all_files, build)
4955

5056
{:ok, epub} = generate_epub(config.output)
5157
File.rm_rf!(config.output)
@@ -65,14 +71,16 @@ defmodule ExDoc.Formatter.EPUB do
6571
for {_title, extras} <- config.extras,
6672
node <- extras,
6773
not is_map_key(node, :url) do
68-
output = "#{config.output}/OEBPS/#{node.id}.xhtml"
74+
filename = "OEBPS/#{node.id}.xhtml"
75+
output = "#{config.output}/#{filename}"
6976
html = Templates.extra_template(config, node)
7077

7178
if File.regular?(output) do
7279
Utils.warn("file #{Path.relative_to_cwd(output)} already exists", [])
7380
end
7481

7582
File.write!(output, html)
83+
filename
7684
end
7785
end
7886

@@ -84,7 +92,9 @@ defmodule ExDoc.Formatter.EPUB do
8492
do: {Path.relative_to(name, "OEBPS"), media_type}
8593

8694
content = Templates.content_template(config, nodes, uuid, datetime, static_files)
87-
File.write("#{config.output}/OEBPS/content.opf", content)
95+
filename = "OEBPS/content.opf"
96+
File.write("#{config.output}/#{filename}", content)
97+
[filename]
8898
end
8999

90100
defp generate_nav(config, nodes) do
@@ -94,12 +104,16 @@ defmodule ExDoc.Formatter.EPUB do
94104
end)
95105

96106
content = Templates.nav_template(config, nodes)
97-
File.write("#{config.output}/OEBPS/nav.xhtml", content)
107+
filename = "OEBPS/nav.xhtml"
108+
File.write("#{config.output}/#{filename}", content)
109+
[filename]
98110
end
99111

100112
defp generate_title(config) do
101113
content = Templates.title_template(config)
102-
File.write("#{config.output}/OEBPS/title.xhtml", content)
114+
filename = "OEBPS/title.xhtml"
115+
File.write("#{config.output}/#{filename}", content)
116+
[filename]
103117
end
104118

105119
defp generate_list(config, nodes) do
@@ -126,6 +140,31 @@ defmodule ExDoc.Formatter.EPUB do
126140
)
127141
end
128142

143+
defp output_setup(build, config) do
144+
if File.exists?(build) do
145+
build
146+
|> File.read!()
147+
|> String.split("\n", trim: true)
148+
|> Enum.map(&Path.join(config.output, &1))
149+
|> Enum.each(&File.rm/1)
150+
151+
File.rm(build)
152+
else
153+
File.rm_rf!(config.output)
154+
end
155+
end
156+
157+
defp generate_build(files, build) do
158+
entries =
159+
files
160+
|> Enum.uniq()
161+
|> Enum.sort()
162+
|> Enum.map(&[&1, "\n"])
163+
164+
File.mkdir_p!(Path.dirname(build))
165+
File.write!(build, entries)
166+
end
167+
129168
## Helpers
130169

131170
defp default_assets(config) do
@@ -159,7 +198,9 @@ defmodule ExDoc.Formatter.EPUB do
159198

160199
defp generate_module_page(module_node, config) do
161200
content = Templates.module_page(config, module_node)
162-
File.write("#{config.output}/OEBPS/#{module_node.id}.xhtml", content)
201+
filename = "OEBPS/#{module_node.id}.xhtml"
202+
File.write("#{config.output}/#{filename}", content)
203+
filename
163204
end
164205

165206
@two_power_16 65536

lib/ex_doc/formatter/html.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ defmodule ExDoc.Formatter.HTML do
1515
config = normalize_config(config)
1616
config = %{config | output: Path.expand(config.output)}
1717

18-
build = Path.join(config.output, ".build")
18+
build_dir = Path.join(config.output, ".build/html")
19+
build = Path.join(build_dir, ".build")
1920
output_setup(build, config)
2021

2122
project_nodes = Formatter.render_all(project_nodes, filtered_modules, ".html", config, [])
@@ -79,6 +80,7 @@ defmodule ExDoc.Formatter.HTML do
7980
|> Enum.sort()
8081
|> Enum.map(&[&1, "\n"])
8182

83+
File.mkdir_p!(Path.dirname(build))
8284
File.write!(build, entries)
8385
end
8486

lib/ex_doc/formatter/markdown.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ defmodule ExDoc.Formatter.Markdown do
1313
Utils.unset_warned()
1414

1515
config = normalize_config(config)
16-
build = Path.join(config.output, ".build-markdown")
16+
build_dir = Path.join(config.output, ".build/markdown")
17+
build = Path.join(build_dir, ".build")
1718
output_setup(build, config)
1819

1920
extras = Formatter.build_extras(config, ".md")
@@ -77,6 +78,7 @@ defmodule ExDoc.Formatter.Markdown do
7778
|> Enum.sort()
7879
|> Enum.map(&[&1, "\n"])
7980

81+
File.mkdir_p!(Path.dirname(build))
8082
File.write!(build, entries)
8183
end
8284

test/ex_doc/formatter/html_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ defmodule ExDoc.Formatter.HTMLTest do
967967
generate_docs(config)
968968

969969
# Verify necessary files in .build
970-
content = File.read!(tmp_dir <> "/html/.build")
970+
content = File.read!(tmp_dir <> "/html/.build/html/.build")
971971
assert content =~ ~r(^readme\.html$)m
972972
assert content =~ ~r(^api-reference\.html$)m
973973
assert content =~ ~r(^dist/sidebar_items-[\w]{8}\.js$)m
@@ -995,7 +995,7 @@ defmodule ExDoc.Formatter.HTMLTest do
995995
File.touch!(keep)
996996
generate_docs(config)
997997
assert File.exists?(keep)
998-
content = File.read!(tmp_dir <> "/html/.build")
998+
content = File.read!(tmp_dir <> "/html/.build/html/.build")
999999
refute content =~ ~r{keep}
10001000
end
10011001
end

0 commit comments

Comments
 (0)