@@ -7,7 +7,7 @@ use crate::error_codes::error_codes;
7
7
8
8
use std:: env;
9
9
use std:: error:: Error ;
10
- use std:: fs:: { self , create_dir_all , File } ;
10
+ use std:: fs:: { self , File } ;
11
11
use std:: io:: Write ;
12
12
use std:: path:: Path ;
13
13
use std:: path:: PathBuf ;
@@ -65,44 +65,6 @@ fn render_markdown(output_path: &Path) -> Result<(), Box<dyn Error>> {
65
65
Ok ( ( ) )
66
66
}
67
67
68
- fn move_folder ( source : & Path , target : & Path ) -> Result < ( ) , Box < dyn Error > > {
69
- let entries =
70
- fs:: read_dir ( source) ?. map ( |res| res. map ( |e| e. path ( ) ) ) . collect :: < Result < Vec < _ > , _ > > ( ) ?;
71
-
72
- for entry in entries {
73
- let file_name = entry. file_name ( ) . expect ( "file_name() failed" ) . to_os_string ( ) ;
74
- let output = target. join ( file_name) ;
75
- if entry. is_file ( ) {
76
- fs:: rename ( entry, output) ?;
77
- } else {
78
- if !output. exists ( ) {
79
- create_dir_all ( & output) ?;
80
- }
81
- move_folder ( & entry, & output) ?;
82
- }
83
- }
84
-
85
- fs:: remove_dir ( & source) ?;
86
-
87
- Ok ( ( ) )
88
- }
89
-
90
- fn render_html ( output_path : & Path ) -> Result < ( ) , Box < dyn Error > > {
91
- // We need to render into a temporary folder to prevent `mdbook` from removing everything
92
- // in the output folder (including other completely unrelated things).
93
- let tmp_output = output_path. join ( "tmp" ) ;
94
-
95
- if !tmp_output. exists ( ) {
96
- create_dir_all ( & tmp_output) ?;
97
- }
98
-
99
- render_html_inner ( & tmp_output) ?;
100
-
101
- move_folder ( & tmp_output, output_path) ?;
102
-
103
- Ok ( ( ) )
104
- }
105
-
106
68
// By default, mdbook doesn't consider code blocks as Rust ones contrary to rustdoc so we have
107
69
// to manually add `rust` attribute whenever needed.
108
70
fn add_rust_attribute_on_codeblock ( explanation : & str ) -> String {
@@ -134,26 +96,22 @@ fn add_rust_attribute_on_codeblock(explanation: &str) -> String {
134
96
} )
135
97
}
136
98
137
- fn render_html_inner ( output_path : & Path ) -> Result < ( ) , Box < dyn Error > > {
138
- // We need to have a little difference between `summary` and `introduction` because the "draft"
139
- // chapters (the ones looking like `[a]()`) are not handled correctly when being put into a
140
- // `Chapter` directly: they generate a link whereas they shouldn't.
99
+ fn render_html ( output_path : & Path ) -> Result < ( ) , Box < dyn Error > > {
141
100
let mut introduction = format ! (
142
- "<script>{} </script>
101
+ "<script src='redirect.js'> </script>
143
102
# Rust error codes index
144
103
145
104
This page lists all the error codes emitted by the Rust compiler.
146
105
147
- " ,
148
- include_str!( "redirect.js" )
106
+ "
149
107
) ;
150
108
151
109
let err_codes = error_codes ( ) ;
152
110
let mut chapters = Vec :: with_capacity ( err_codes. len ( ) ) ;
153
111
154
112
for ( err_code, explanation) in err_codes. iter ( ) {
155
113
if let Some ( explanation) = explanation {
156
- introduction. push_str ( & format ! ( " * [{0}](./error_codes/ {0}.html)\n " , err_code) ) ;
114
+ introduction. push_str ( & format ! ( " * [{0}](./{0}.html)\n " , err_code) ) ;
157
115
158
116
let content = add_rust_attribute_on_codeblock ( explanation) ;
159
117
chapters. push ( BookItem :: Chapter ( Chapter {
@@ -162,7 +120,7 @@ This page lists all the error codes emitted by the Rust compiler.
162
120
number : None ,
163
121
sub_items : Vec :: new ( ) ,
164
122
// We generate it into the `error_codes` folder.
165
- path : Some ( PathBuf :: from ( & format ! ( "error_codes/ {}.html" , err_code) ) ) ,
123
+ path : Some ( PathBuf :: from ( & format ! ( "{}.html" , err_code) ) ) ,
166
124
source_path : None ,
167
125
parent_names : Vec :: new ( ) ,
168
126
} ) ) ;
@@ -172,7 +130,7 @@ This page lists all the error codes emitted by the Rust compiler.
172
130
}
173
131
174
132
let mut config = Config :: from_str ( include_str ! ( "book_config.toml" ) ) ?;
175
- config. build . build_dir = output_path. to_path_buf ( ) ;
133
+ config. build . build_dir = output_path. join ( "error_codes" ) . to_path_buf ( ) ;
176
134
let mut book = MDBook :: load_with_config_and_summary (
177
135
env ! ( "CARGO_MANIFEST_DIR" ) ,
178
136
config,
@@ -191,10 +149,27 @@ This page lists all the error codes emitted by the Rust compiler.
191
149
book. book . sections . push ( BookItem :: Chapter ( chapter) ) ;
192
150
book. build ( ) ?;
193
151
194
- // We don't need this file since it's handled by doc.rust-lang.org directly.
195
- let _ = fs:: remove_file ( output_path. join ( "404.html" ) ) ;
196
- // We don't want this file either because it would overwrite the already existing `index.html`.
197
- let _ = fs:: remove_file ( output_path. join ( "index.html" ) ) ;
152
+ // We can't put this content into another file, otherwise `mbdbook` will also put it into the
153
+ // output directory, making a duplicate.
154
+ fs:: write (
155
+ output_path. join ( "error-index.html" ) ,
156
+ r#"<!DOCTYPE html>
157
+ <html>
158
+ <head>
159
+ <title>Rust error codes index - Error codes index</title>
160
+ <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
161
+ <meta name="description" content="Book listing all Rust error codes">
162
+ <script src="error_codes/redirect.js"></script>
163
+ </head>
164
+ <body>
165
+ <div>If you are not automatically redirected to the error code index, please <a id="index-link" href="./error_codes/error-index.html">here</a>.
166
+ <script>document.getElementById("index-link").click()</script>
167
+ </body>
168
+ </html>"# ,
169
+ ) ?;
170
+
171
+ // No need for a 404 file, it's already handled by the server.
172
+ fs:: remove_file ( output_path. join ( "error_codes/404.html" ) ) ?;
198
173
199
174
Ok ( ( ) )
200
175
}
0 commit comments