Skip to content

Commit fb2813b

Browse files
Add index page
1 parent ca2639e commit fb2813b

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

src/bootstrap/doc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ impl Step for Standalone {
405405
cmd.arg("--html-after-content").arg(&footer)
406406
.arg("--html-before-content").arg(&version_info)
407407
.arg("--html-in-header").arg(&favicon)
408+
.arg("--index-page").arg("src/doc/index.md")
408409
.arg("--markdown-playground-url")
409410
.arg("https://play.rust-lang.org/")
410411
.arg("-o").arg(&out)

src/librustdoc/html/render.rs

+76-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ use html::{highlight, layout};
8080

8181
use minifier;
8282

83+
use pulldown_cmark;
84+
8385
/// A pair of name and its optional document.
8486
pub type NameDoc = (String, Option<String>);
8587

@@ -106,6 +108,8 @@ struct Context {
106108
/// The map used to ensure all generated 'id=' attributes are unique.
107109
id_map: Rc<RefCell<IdMap>>,
108110
pub shared: Arc<SharedContext>,
111+
pub enable_index_page: bool,
112+
pub index_page: Option<PathBuf>,
109113
}
110114

111115
struct SharedContext {
@@ -501,7 +505,10 @@ pub fn run(mut krate: clean::Crate,
501505
sort_modules_alphabetically: bool,
502506
themes: Vec<PathBuf>,
503507
enable_minification: bool,
504-
id_map: IdMap) -> Result<(), Error> {
508+
id_map: IdMap,
509+
enable_index_page: bool,
510+
index_page: Option<PathBuf>,
511+
) -> Result<(), Error> {
505512
let src_root = match krate.src {
506513
FileName::Real(ref p) => match p.parent() {
507514
Some(p) => p.to_path_buf(),
@@ -572,6 +579,8 @@ pub fn run(mut krate: clean::Crate,
572579
codes: ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()),
573580
id_map: Rc::new(RefCell::new(id_map)),
574581
shared: Arc::new(scx),
582+
enable_index_page,
583+
index_page,
575584
};
576585

577586
// Crawl the crate to build various caches used for the output
@@ -902,8 +911,9 @@ themePicker.onblur = handleThemeButtonsBlur;
902911
write(cx.dst.join("COPYRIGHT.txt"),
903912
include_bytes!("static/COPYRIGHT.txt"))?;
904913

905-
fn collect(path: &Path, krate: &str, key: &str) -> io::Result<Vec<String>> {
914+
fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec<String>, Vec<String>)> {
906915
let mut ret = Vec::new();
916+
let mut krates = Vec::new();
907917
if path.exists() {
908918
for line in BufReader::new(File::open(path)?).lines() {
909919
let line = line?;
@@ -914,9 +924,13 @@ themePicker.onblur = handleThemeButtonsBlur;
914924
continue;
915925
}
916926
ret.push(line.to_string());
927+
krates.push(line[key.len() + 2..].split('"')
928+
.next()
929+
.map(|s| s.to_owned())
930+
.unwrap_or_else(|| String::new()));
917931
}
918932
}
919-
Ok(ret)
933+
Ok((ret, krates))
920934
}
921935

922936
fn show_item(item: &IndexItem, krate: &str) -> String {
@@ -931,7 +945,7 @@ themePicker.onblur = handleThemeButtonsBlur;
931945

932946
let dst = cx.dst.join("aliases.js");
933947
{
934-
let mut all_aliases = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
948+
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
935949
let mut w = try_err!(File::create(&dst), &dst);
936950
let mut output = String::with_capacity(100);
937951
for (alias, items) in &cache.aliases {
@@ -955,7 +969,7 @@ themePicker.onblur = handleThemeButtonsBlur;
955969

956970
// Update the search index
957971
let dst = cx.dst.join("search-index.js");
958-
let mut all_indexes = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst);
972+
let (mut all_indexes, mut krates) = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst);
959973
all_indexes.push(search_index);
960974
// Sort the indexes by crate so the file will be generated identically even
961975
// with rustdoc running in parallel.
@@ -969,6 +983,61 @@ themePicker.onblur = handleThemeButtonsBlur;
969983
}
970984
try_err!(writeln!(&mut w, "initSearch(searchIndex);"), &dst);
971985

986+
<<<<<<< HEAD
987+
if cx.disable_index_page == false {
988+
let dst = cx.dst.join("index.html");
989+
=======
990+
if cx.enable_index_page == true {
991+
>>>>>>> a2642cf... f
992+
if let Some(ref index_page) = cx.index_page {
993+
let mut content = Vec::with_capacity(100000);
994+
995+
let mut f = try_err!(File::open(&index_page), &index_page);
996+
try_err!(f.read_to_end(&mut content), &index_page);
997+
let content = match String::from_utf8(content) {
998+
Ok(c) => c,
999+
Err(_) => return Err(Error::new(
1000+
io::Error::new(
1001+
io::ErrorKind::Other, "invalid markdown"),
1002+
&index_page)),
1003+
};
1004+
let parser = pulldown_cmark::Parser::new(&content);
1005+
let mut html_buf = String::new();
1006+
pulldown_cmark::html::push_html(&mut html_buf, parser);
1007+
let mut f = try_err!(File::create(&dst), &dst);
1008+
try_err!(f.write_all(html_buf.as_bytes()), &dst);
1009+
} else {
1010+
let mut w = BufWriter::new(try_err!(File::create(&dst), &dst));
1011+
let page = layout::Page {
1012+
title: "Index of crates",
1013+
css_class: "mod",
1014+
root_path: "./",
1015+
description: "List of crates",
1016+
keywords: BASIC_KEYWORDS,
1017+
resource_suffix: &cx.shared.resource_suffix,
1018+
};
1019+
krates.push(krate.name.clone());
1020+
krates.sort();
1021+
krates.dedup();
1022+
1023+
let content = format!(
1024+
"<h1 class='fqn'>\
1025+
<span class='in-band'>List of all crates</span>\
1026+
</h1><ul class='mod'>{}</ul>",
1027+
krates
1028+
.iter()
1029+
.map(|s| {
1030+
format!("<li><a href=\"{}/index.html\">{}</li>", s, s)
1031+
})
1032+
.collect::<String>());
1033+
try_err!(layout::render(&mut w, &cx.shared.layout,
1034+
&page, &(""), &content,
1035+
cx.shared.css_file_extension.is_some(),
1036+
&cx.shared.themes), &dst);
1037+
try_err!(w.flush(), &dst);
1038+
}
1039+
}
1040+
9721041
// Update the list of all implementors for traits
9731042
let dst = cx.dst.join("implementors");
9741043
for (&did, imps) in &cache.implementors {
@@ -1022,7 +1091,8 @@ themePicker.onblur = handleThemeButtonsBlur;
10221091
remote_item_type.css_class(),
10231092
remote_path[remote_path.len() - 1]));
10241093

1025-
let mut all_implementors = try_err!(collect(&mydst, &krate.name, "implementors"), &mydst);
1094+
let (mut all_implementors, _) = try_err!(collect(&mydst, &krate.name, "implementors"),
1095+
&mydst);
10261096
all_implementors.push(implementors);
10271097
// Sort the implementors by crate so the file will be generated
10281098
// identically even with rustdoc running in parallel.

src/librustdoc/lib.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ fn opts() -> Vec<RustcOptGroup> {
334334
"LEVEL",
335335
)
336336
}),
337+
unstable("index-page", |o| {
338+
o.optopt("",
339+
"index-page",
340+
"Markdown file to be used as index page",
341+
"PATH")
342+
}),
343+
unstable("enable-index-page", |o| {
344+
o.optflag("",
345+
"enable-index-page",
346+
"To enable generation of the index page")
347+
}),
337348
]
338349
}
339350

@@ -534,6 +545,8 @@ fn main_args(args: &[String]) -> isize {
534545
let linker = matches.opt_str("linker").map(PathBuf::from);
535546
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
536547
let resource_suffix = matches.opt_str("resource-suffix");
548+
let index_page = matches.opt_str("index-page").map(|s| PathBuf::from(&s));
549+
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
537550
let enable_minification = !matches.opt_present("disable-minification");
538551

539552
let edition = matches.opt_str("edition").unwrap_or("2015".to_string());
@@ -544,6 +557,12 @@ fn main_args(args: &[String]) -> isize {
544557
return 1;
545558
}
546559
};
560+
if let Some(ref index_page) = index_page {
561+
if !index_page.is_file() {
562+
diag.struct_err("option `--index-page` argument must be a file").emit();
563+
return 1;
564+
}
565+
}
547566

548567
let cg = build_codegen_options(&matches, ErrorOutputType::default());
549568

@@ -580,7 +599,10 @@ fn main_args(args: &[String]) -> isize {
580599
renderinfo,
581600
sort_modules_alphabetically,
582601
themes,
583-
enable_minification, id_map)
602+
enable_minification, id_map,
603+
enable_index_page, index_page,
604+
&matches,
605+
&diag)
584606
.expect("failed to generate documentation");
585607
0
586608
}

0 commit comments

Comments
 (0)