@@ -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
0 commit comments