|
36 | 36 | use std::fmt;
|
37 | 37 | use std::local_data;
|
38 | 38 | use std::io;
|
39 |
| -use std::io::{fs, File, BufferedWriter}; |
| 39 | +use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader}; |
40 | 40 | use std::str;
|
41 | 41 | use std::vec;
|
42 | 42 | use std::vec_ng::Vec;
|
@@ -283,48 +283,75 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
|
283 | 283 | };
|
284 | 284 | }
|
285 | 285 |
|
286 |
| - // Add all the static files |
287 |
| - let mut dst = cx.dst.join(krate.name.as_slice()); |
288 |
| - try!(mkdir(&dst)); |
289 |
| - try!(write(dst.join("jquery.js"), |
290 |
| - include_str!("static/jquery-2.1.0.min.js"))); |
291 |
| - try!(write(dst.join("main.js"), include_str!("static/main.js"))); |
292 |
| - try!(write(dst.join("main.css"), include_str!("static/main.css"))); |
293 |
| - try!(write(dst.join("normalize.css"), |
294 |
| - include_str!("static/normalize.css"))); |
295 |
| - |
296 | 286 | // Publish the search index
|
297 |
| - { |
298 |
| - dst.push("search-index.js"); |
299 |
| - let mut w = BufferedWriter::new(File::create(&dst).unwrap()); |
300 |
| - let w = &mut w as &mut Writer; |
301 |
| - try!(write!(w, "var searchIndex = [")); |
| 287 | + let index = { |
| 288 | + let mut w = MemWriter::new(); |
| 289 | + try!(write!(&mut w, "searchIndex['{}'] = [", krate.name)); |
302 | 290 | for (i, item) in cache.search_index.iter().enumerate() {
|
303 | 291 | if i > 0 {
|
304 |
| - try!(write!(w, ",")); |
| 292 | + try!(write!(&mut w, ",")); |
305 | 293 | }
|
306 |
| - try!(write!(w, "\\{ty:\"{}\",name:\"{}\",path:\"{}\",desc:{}", |
307 |
| - item.ty, item.name, item.path, |
308 |
| - item.desc.to_json().to_str())); |
| 294 | + try!(write!(&mut w, "\\{ty:\"{}\",name:\"{}\",path:\"{}\",desc:{}", |
| 295 | + item.ty, item.name, item.path, |
| 296 | + item.desc.to_json().to_str())); |
309 | 297 | match item.parent {
|
310 | 298 | Some(id) => {
|
311 |
| - try!(write!(w, ",parent:'{}'", id)); |
| 299 | + try!(write!(&mut w, ",parent:'{}'", id)); |
312 | 300 | }
|
313 | 301 | None => {}
|
314 | 302 | }
|
315 |
| - try!(write!(w, "\\}")); |
| 303 | + try!(write!(&mut w, "\\}")); |
316 | 304 | }
|
317 |
| - try!(write!(w, "];")); |
318 |
| - try!(write!(w, "var allPaths = \\{")); |
| 305 | + try!(write!(&mut w, "];")); |
| 306 | + try!(write!(&mut w, "allPaths['{}'] = \\{", krate.name)); |
319 | 307 | for (i, (&id, &(ref fqp, short))) in cache.paths.iter().enumerate() {
|
320 | 308 | if i > 0 {
|
321 |
| - try!(write!(w, ",")); |
| 309 | + try!(write!(&mut w, ",")); |
322 | 310 | }
|
323 |
| - try!(write!(w, "'{}':\\{type:'{}',name:'{}'\\}", |
324 |
| - id, short, *fqp.last().unwrap())); |
| 311 | + try!(write!(&mut w, "'{}':\\{type:'{}',name:'{}'\\}", |
| 312 | + id, short, *fqp.last().unwrap())); |
325 | 313 | }
|
326 |
| - try!(write!(w, "\\};")); |
327 |
| - try!(w.flush()); |
| 314 | + try!(write!(&mut w, "\\};")); |
| 315 | + |
| 316 | + str::from_utf8_owned(w.unwrap()).unwrap() |
| 317 | + }; |
| 318 | + |
| 319 | + // Write out the shared files. Note that these are shared among all rustdoc |
| 320 | + // docs placed in the output directory, so this needs to be a synchronized |
| 321 | + // operation with respect to all other rustdocs running around. |
| 322 | + { |
| 323 | + try!(mkdir(&cx.dst)); |
| 324 | + let _lock = ::flock::Lock::new(&cx.dst.join(".lock")); |
| 325 | + |
| 326 | + // Add all the static files. These may already exist, but we just |
| 327 | + // overwrite them anyway to make sure that they're fresh and up-to-date. |
| 328 | + try!(write(cx.dst.join("jquery.js"), |
| 329 | + include_str!("static/jquery-2.1.0.min.js"))); |
| 330 | + try!(write(cx.dst.join("main.js"), include_str!("static/main.js"))); |
| 331 | + try!(write(cx.dst.join("main.css"), include_str!("static/main.css"))); |
| 332 | + try!(write(cx.dst.join("normalize.css"), |
| 333 | + include_str!("static/normalize.css"))); |
| 334 | + |
| 335 | + // Update the search index |
| 336 | + let dst = cx.dst.join("search-index.js"); |
| 337 | + let mut all_indexes = Vec::new(); |
| 338 | + all_indexes.push(index); |
| 339 | + if dst.exists() { |
| 340 | + for line in BufferedReader::new(File::open(&dst)).lines() { |
| 341 | + let line = try!(line); |
| 342 | + if !line.starts_with("searchIndex") { continue } |
| 343 | + if line.starts_with(format!("searchIndex['{}']", krate.name)) { |
| 344 | + continue |
| 345 | + } |
| 346 | + all_indexes.push(line); |
| 347 | + } |
| 348 | + } |
| 349 | + let mut w = try!(File::create(&dst)); |
| 350 | + try!(writeln!(&mut w, r"var searchIndex = \{\}; var allPaths = \{\};")); |
| 351 | + for index in all_indexes.iter() { |
| 352 | + try!(writeln!(&mut w, "{}", *index)); |
| 353 | + } |
| 354 | + try!(writeln!(&mut w, "initSearch(searchIndex);")); |
328 | 355 | }
|
329 | 356 |
|
330 | 357 | // Render all source files (this may turn into a giant no-op)
|
|
0 commit comments