Source sidebar improvements
This commit is contained in:
parent
93520d2ad1
commit
e87f8cc49b
5 changed files with 86 additions and 60 deletions
|
@ -859,9 +859,11 @@ themePicker.onblur = handleThemeButtonsBlur;
|
|||
write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
|
||||
static_files::SETTINGS_JS,
|
||||
options.enable_minification)?;
|
||||
write_minify(cx.dst.join(&format!("source-script{}.js", cx.shared.resource_suffix)),
|
||||
include_str!("static/source-script.js"),
|
||||
options.enable_minification)?;
|
||||
if cx.shared.include_sources {
|
||||
write_minify(cx.dst.join(&format!("source-script{}.js", cx.shared.resource_suffix)),
|
||||
static_files::sidebar::SOURCE_SCRIPT,
|
||||
options.enable_minification)?;
|
||||
}
|
||||
|
||||
{
|
||||
let mut data = format!("var resourcesSuffix = \"{}\";\n",
|
||||
|
@ -990,8 +992,8 @@ themePicker.onblur = handleThemeButtonsBlur;
|
|||
}
|
||||
}
|
||||
|
||||
fn to_string(&self) -> String {
|
||||
let mut subs: Vec<&Hierarchy> = self.children.iter().map(|(_, v)| v).collect();
|
||||
fn to_json_string(&self) -> String {
|
||||
let mut subs: Vec<&Hierarchy> = self.children.values().collect();
|
||||
subs.sort_unstable_by(|a, b| a.elem.cmp(&b.elem));
|
||||
let mut files = self.elems.iter()
|
||||
.map(|s| format!("\"{}\"",
|
||||
|
@ -1002,46 +1004,52 @@ themePicker.onblur = handleThemeButtonsBlur;
|
|||
// FIXME(imperio): we could avoid to generate "dirs" and "files" if they're empty.
|
||||
format!("{{\"name\":\"{name}\",\"dirs\":[{subs}],\"files\":[{files}]}}",
|
||||
name=self.elem.to_str().expect("invalid osstring conversion"),
|
||||
subs=subs.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(","),
|
||||
subs=subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(","),
|
||||
files=files.join(","))
|
||||
}
|
||||
}
|
||||
|
||||
use std::path::Component;
|
||||
if cx.shared.include_sources {
|
||||
use std::path::Component;
|
||||
|
||||
let mut hierarchy = Hierarchy::new(OsString::new());
|
||||
for source in cx.shared.local_sources.iter()
|
||||
.filter_map(|p| p.0.strip_prefix(&cx.shared.src_root)
|
||||
.ok()) {
|
||||
let mut h = &mut hierarchy;
|
||||
let mut elems = source.components()
|
||||
.filter_map(|s| {
|
||||
match s {
|
||||
Component::Normal(s) => Some(s.to_owned()),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.peekable();
|
||||
loop {
|
||||
let cur_elem = elems.next().expect("empty file path");
|
||||
if elems.peek().is_none() {
|
||||
h.elems.insert(cur_elem);
|
||||
break;
|
||||
} else {
|
||||
let e = cur_elem.clone();
|
||||
h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
|
||||
h = h.children.get_mut(&cur_elem).expect("not found child");
|
||||
let mut hierarchy = Hierarchy::new(OsString::new());
|
||||
for source in cx.shared.local_sources.iter()
|
||||
.filter_map(|p| p.0.strip_prefix(&cx.shared.src_root)
|
||||
.ok()) {
|
||||
let mut h = &mut hierarchy;
|
||||
let mut elems = source.components()
|
||||
.filter_map(|s| {
|
||||
match s {
|
||||
Component::Normal(s) => Some(s.to_owned()),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.peekable();
|
||||
loop {
|
||||
let cur_elem = elems.next().expect("empty file path");
|
||||
if elems.peek().is_none() {
|
||||
h.elems.insert(cur_elem);
|
||||
break;
|
||||
} else {
|
||||
let e = cur_elem.clone();
|
||||
h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
|
||||
h = h.children.get_mut(&cur_elem).expect("not found child");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let dst = cx.dst.join("source-files.js");
|
||||
let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst);
|
||||
all_sources.push(format!("sourcesIndex['{}'] = {};", &krate.name, hierarchy.to_string()));
|
||||
all_sources.sort();
|
||||
let mut w = try_err!(File::create(&dst), &dst);
|
||||
try_err!(writeln!(&mut w, "var N = null;var sourcesIndex = {{}};\n{}", all_sources.join("\n")),
|
||||
&dst);
|
||||
let dst = cx.dst.join("source-files.js");
|
||||
let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst);
|
||||
all_sources.push(format!("sourcesIndex['{}'] = {};",
|
||||
&krate.name,
|
||||
hierarchy.to_json_string()));
|
||||
all_sources.sort();
|
||||
let mut w = try_err!(File::create(&dst), &dst);
|
||||
try_err!(writeln!(&mut w,
|
||||
"var N = null;var sourcesIndex = {{}};\n{}",
|
||||
all_sources.join("\n")),
|
||||
&dst);
|
||||
}
|
||||
|
||||
// Update the search index
|
||||
let dst = cx.dst.join("search-index.js");
|
||||
|
@ -1367,7 +1375,7 @@ impl<'a> SourceCollector<'a> {
|
|||
layout::render(&mut w, &self.scx.layout,
|
||||
&page, &(""), &Source(contents),
|
||||
self.scx.css_file_extension.is_some(),
|
||||
&self.scx.themes, &["source-files.js", "source-script.js"])?;
|
||||
&self.scx.themes, &["source-files", "source-script"])?;
|
||||
w.flush()?;
|
||||
self.scx.local_sources.insert(p.clone(), href);
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue