Replace writeln!/write! with push_str
This commit is contained in:
parent
57243b74b1
commit
75528f266f
1 changed files with 70 additions and 82 deletions
|
@ -65,7 +65,7 @@ use crate::docfs::{DocFS, ErrorStorage, PathError};
|
||||||
use crate::doctree;
|
use crate::doctree;
|
||||||
use crate::fold::DocFolder;
|
use crate::fold::DocFolder;
|
||||||
use crate::html::escape::Escape;
|
use crate::html::escape::Escape;
|
||||||
use crate::html::format::{AsyncSpace, ConstnessSpace};
|
use crate::html::format::{Buffer, AsyncSpace, ConstnessSpace};
|
||||||
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace, DefaultSpace};
|
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace, DefaultSpace};
|
||||||
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
|
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
|
||||||
use crate::html::format::fmt_impl_for_trait_page;
|
use crate::html::format::fmt_impl_for_trait_page;
|
||||||
|
@ -1025,12 +1025,12 @@ themePicker.onblur = handleThemeButtonsBlur;
|
||||||
}
|
}
|
||||||
all_aliases.push(format!("ALIASES[\"{}\"] = {{{}}};", krate.name, output));
|
all_aliases.push(format!("ALIASES[\"{}\"] = {{{}}};", krate.name, output));
|
||||||
all_aliases.sort();
|
all_aliases.sort();
|
||||||
let mut v = Vec::new();
|
let mut v = Buffer::html();
|
||||||
try_err!(writeln!(&mut v, "var ALIASES = {{}};"), &dst);
|
writeln!(&mut v, "var ALIASES = {{}};");
|
||||||
for aliases in &all_aliases {
|
for aliases in &all_aliases {
|
||||||
try_err!(writeln!(&mut v, "{}", aliases), &dst);
|
writeln!(&mut v, "{}", aliases);
|
||||||
}
|
}
|
||||||
cx.shared.fs.write(&dst, &v)?;
|
cx.shared.fs.write(&dst, v.into_inner().into_bytes())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
@ -1114,12 +1114,9 @@ themePicker.onblur = handleThemeButtonsBlur;
|
||||||
&krate.name,
|
&krate.name,
|
||||||
hierarchy.to_json_string()));
|
hierarchy.to_json_string()));
|
||||||
all_sources.sort();
|
all_sources.sort();
|
||||||
let mut v = Vec::new();
|
let v = format!("var N = null;var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();\n",
|
||||||
try_err!(writeln!(&mut v,
|
all_sources.join("\n"));
|
||||||
"var N = null;var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();",
|
cx.shared.fs.write(&dst, v.as_bytes())?;
|
||||||
all_sources.join("\n")),
|
|
||||||
&dst);
|
|
||||||
cx.shared.fs.write(&dst, &v)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the search index
|
// Update the search index
|
||||||
|
@ -1134,14 +1131,11 @@ themePicker.onblur = handleThemeButtonsBlur;
|
||||||
// with rustdoc running in parallel.
|
// with rustdoc running in parallel.
|
||||||
all_indexes.sort();
|
all_indexes.sort();
|
||||||
{
|
{
|
||||||
let mut v = Vec::new();
|
let mut v = String::from("var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={};\n");
|
||||||
try_err!(writeln!(&mut v, "var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={{}};"), &dst);
|
v.push_str(&minify_replacer(
|
||||||
try_err!(write_minify_replacer(
|
|
||||||
&mut v,
|
|
||||||
&format!("{}\n{}", variables.join(""), all_indexes.join("\n")),
|
&format!("{}\n{}", variables.join(""), all_indexes.join("\n")),
|
||||||
options.enable_minification),
|
options.enable_minification));
|
||||||
&dst);
|
v.push_str("initSearch(searchIndex);addSearchOptions(searchIndex);");
|
||||||
try_err!(write!(&mut v, "initSearch(searchIndex);addSearchOptions(searchIndex);"), &dst);
|
|
||||||
cx.shared.fs.write(&dst, &v)?;
|
cx.shared.fs.write(&dst, &v)?;
|
||||||
}
|
}
|
||||||
if options.enable_index_page {
|
if options.enable_index_page {
|
||||||
|
@ -1247,19 +1241,18 @@ themePicker.onblur = handleThemeButtonsBlur;
|
||||||
// identically even with rustdoc running in parallel.
|
// identically even with rustdoc running in parallel.
|
||||||
all_implementors.sort();
|
all_implementors.sort();
|
||||||
|
|
||||||
let mut v = Vec::new();
|
let mut v = String::from("(function() {var implementors = {}};\n");
|
||||||
try_err!(writeln!(&mut v, "(function() {{var implementors = {{}};"), &mydst);
|
|
||||||
for implementor in &all_implementors {
|
for implementor in &all_implementors {
|
||||||
try_err!(writeln!(&mut v, "{}", *implementor), &mydst);
|
v.push_str(&format!("{}", *implementor));
|
||||||
}
|
}
|
||||||
try_err!(writeln!(&mut v, "{}", r"
|
v.push_str(r"
|
||||||
if (window.register_implementors) {
|
if (window.register_implementors) {
|
||||||
window.register_implementors(implementors);
|
window.register_implementors(implementors);
|
||||||
} else {
|
} else {
|
||||||
window.pending_implementors = implementors;
|
window.pending_implementors = implementors;
|
||||||
}
|
}
|
||||||
"), &mydst);
|
\n");
|
||||||
try_err!(writeln!(&mut v, r"}})()"), &mydst);
|
v.push_str("})()");
|
||||||
cx.shared.fs.write(&mydst, &v)?;
|
cx.shared.fs.write(&mydst, &v)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1279,68 +1272,65 @@ fn write_minify(fs:&DocFS, dst: PathBuf, contents: &str, enable_minification: bo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_minify_replacer<W: Write>(
|
fn minify_replacer(
|
||||||
dst: &mut W,
|
|
||||||
contents: &str,
|
contents: &str,
|
||||||
enable_minification: bool,
|
enable_minification: bool,
|
||||||
) -> io::Result<()> {
|
) -> String {
|
||||||
use minifier::js::{simple_minify, Keyword, ReservedChar, Token, Tokens};
|
use minifier::js::{simple_minify, Keyword, ReservedChar, Token, Tokens};
|
||||||
|
|
||||||
if enable_minification {
|
if enable_minification {
|
||||||
writeln!(dst, "{}",
|
let tokens: Tokens<'_> = simple_minify(contents)
|
||||||
{
|
.into_iter()
|
||||||
let tokens: Tokens<'_> = simple_minify(contents)
|
.filter(|(f, next)| {
|
||||||
.into_iter()
|
// We keep backlines.
|
||||||
.filter(|(f, next)| {
|
minifier::js::clean_token_except(f, next, &|c: &Token<'_>| {
|
||||||
// We keep backlines.
|
c.get_char() != Some(ReservedChar::Backline)
|
||||||
minifier::js::clean_token_except(f, next, &|c: &Token<'_>| {
|
|
||||||
c.get_char() != Some(ReservedChar::Backline)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.map(|(f, _)| {
|
|
||||||
minifier::js::replace_token_with(f, &|t: &Token<'_>| {
|
|
||||||
match *t {
|
|
||||||
Token::Keyword(Keyword::Null) => Some(Token::Other("N")),
|
|
||||||
Token::String(s) => {
|
|
||||||
let s = &s[1..s.len() -1]; // The quotes are included
|
|
||||||
if s.is_empty() {
|
|
||||||
Some(Token::Other("E"))
|
|
||||||
} else if s == "t" {
|
|
||||||
Some(Token::Other("T"))
|
|
||||||
} else if s == "u" {
|
|
||||||
Some(Token::Other("U"))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.into();
|
|
||||||
tokens.apply(|f| {
|
|
||||||
// We add a backline after the newly created variables.
|
|
||||||
minifier::js::aggregate_strings_into_array_with_separation_filter(
|
|
||||||
f,
|
|
||||||
"R",
|
|
||||||
Token::Char(ReservedChar::Backline),
|
|
||||||
// This closure prevents crates' names from being aggregated.
|
|
||||||
//
|
|
||||||
// The point here is to check if the string is preceded by '[' and
|
|
||||||
// "searchIndex". If so, it means this is a crate name and that it
|
|
||||||
// shouldn't be aggregated.
|
|
||||||
|tokens, pos| {
|
|
||||||
pos < 2 ||
|
|
||||||
!tokens[pos - 1].eq_char(ReservedChar::OpenBracket) ||
|
|
||||||
tokens[pos - 2].get_other() != Some("searchIndex")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.to_string()
|
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
.map(|(f, _)| {
|
||||||
|
minifier::js::replace_token_with(f, &|t: &Token<'_>| {
|
||||||
|
match *t {
|
||||||
|
Token::Keyword(Keyword::Null) => Some(Token::Other("N")),
|
||||||
|
Token::String(s) => {
|
||||||
|
let s = &s[1..s.len() -1]; // The quotes are included
|
||||||
|
if s.is_empty() {
|
||||||
|
Some(Token::Other("E"))
|
||||||
|
} else if s == "t" {
|
||||||
|
Some(Token::Other("T"))
|
||||||
|
} else if s == "u" {
|
||||||
|
Some(Token::Other("U"))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.into();
|
||||||
|
let o = tokens.apply(|f| {
|
||||||
|
// We add a backline after the newly created variables.
|
||||||
|
minifier::js::aggregate_strings_into_array_with_separation_filter(
|
||||||
|
f,
|
||||||
|
"R",
|
||||||
|
Token::Char(ReservedChar::Backline),
|
||||||
|
// This closure prevents crates' names from being aggregated.
|
||||||
|
//
|
||||||
|
// The point here is to check if the string is preceded by '[' and
|
||||||
|
// "searchIndex". If so, it means this is a crate name and that it
|
||||||
|
// shouldn't be aggregated.
|
||||||
|
|tokens, pos| {
|
||||||
|
pos < 2 ||
|
||||||
|
!tokens[pos - 1].eq_char(ReservedChar::OpenBracket) ||
|
||||||
|
tokens[pos - 2].get_other() != Some("searchIndex")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.to_string();
|
||||||
|
format!("{}\n", o)
|
||||||
} else {
|
} else {
|
||||||
writeln!(dst, "{}", contents)
|
format!("{}\n", contents)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2073,9 +2063,7 @@ impl Context {
|
||||||
if !self.render_redirect_pages {
|
if !self.render_redirect_pages {
|
||||||
let items = self.build_sidebar_items(&m);
|
let items = self.build_sidebar_items(&m);
|
||||||
let js_dst = self.dst.join("sidebar-items.js");
|
let js_dst = self.dst.join("sidebar-items.js");
|
||||||
let mut v = Vec::new();
|
let v = format!("initSidebarItems({});", as_json(&items));
|
||||||
try_err!(write!(&mut v, "initSidebarItems({});",
|
|
||||||
as_json(&items)), &js_dst);
|
|
||||||
scx.fs.write(&js_dst, &v)?;
|
scx.fs.write(&js_dst, &v)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue