2021-10-04 20:54:00 -05:00
|
|
|
use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, Playground};
|
2020-01-01 12:40:49 -06:00
|
|
|
use crate::rustc_span::edition::Edition;
|
2018-01-10 10:58:39 -06:00
|
|
|
use std::fs;
|
2016-10-08 15:29:10 -05:00
|
|
|
use std::path::Path;
|
2015-02-26 23:00:43 -06:00
|
|
|
use std::str;
|
2019-02-23 01:40:07 -06:00
|
|
|
|
2021-06-08 19:19:03 -05:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize)]
|
2020-11-14 16:59:58 -06:00
|
|
|
crate struct ExternalHtml {
|
2021-11-19 14:54:05 -06:00
|
|
|
/// Content that will be included inline in the `<head>` section of a
|
2016-10-08 16:34:13 -05:00
|
|
|
/// rendered Markdown file or generated documentation
|
2020-11-14 16:59:58 -06:00
|
|
|
crate in_header: String,
|
2021-11-19 14:54:05 -06:00
|
|
|
/// Content that will be included inline between `<body>` and the content of
|
2016-10-08 16:34:13 -05:00
|
|
|
/// a rendered Markdown file or generated documentation
|
2020-11-14 16:59:58 -06:00
|
|
|
crate before_content: String,
|
2021-11-19 14:54:05 -06:00
|
|
|
/// Content that will be included inline between the content and `</body>` of
|
2016-10-08 16:34:13 -05:00
|
|
|
/// a rendered Markdown file or generated documentation
|
2020-11-14 16:59:58 -06:00
|
|
|
crate after_content: String,
|
2014-06-28 05:35:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ExternalHtml {
|
2020-11-14 16:59:58 -06:00
|
|
|
crate fn load(
|
2019-12-22 16:42:04 -06:00
|
|
|
in_header: &[String],
|
|
|
|
before_content: &[String],
|
|
|
|
after_content: &[String],
|
|
|
|
md_before_content: &[String],
|
|
|
|
md_after_content: &[String],
|
2020-10-10 13:27:52 -05:00
|
|
|
nightly_build: bool,
|
2020-01-09 04:18:47 -06:00
|
|
|
diag: &rustc_errors::Handler,
|
2019-12-22 16:42:04 -06:00
|
|
|
id_map: &mut IdMap,
|
|
|
|
edition: Edition,
|
|
|
|
playground: &Option<Playground>,
|
|
|
|
) -> Option<ExternalHtml> {
|
2020-10-10 13:27:52 -05:00
|
|
|
let codes = ErrorCodes::from(nightly_build);
|
2019-08-10 17:39:50 -05:00
|
|
|
let ih = load_external_files(in_header, diag)?;
|
|
|
|
let bc = load_external_files(before_content, diag)?;
|
|
|
|
let m_bc = load_external_files(md_before_content, diag)?;
|
2019-12-22 16:42:04 -06:00
|
|
|
let bc = format!(
|
|
|
|
"{}{}",
|
|
|
|
bc,
|
2021-10-04 20:08:58 -05:00
|
|
|
Markdown {
|
|
|
|
content: &m_bc,
|
|
|
|
links: &[],
|
|
|
|
ids: id_map,
|
|
|
|
error_codes: codes,
|
|
|
|
edition,
|
|
|
|
playground,
|
2021-10-04 20:54:00 -05:00
|
|
|
heading_offset: HeadingOffset::H2,
|
2021-10-04 20:08:58 -05:00
|
|
|
}
|
|
|
|
.into_string()
|
2019-12-22 16:42:04 -06:00
|
|
|
);
|
2019-08-10 17:39:50 -05:00
|
|
|
let ac = load_external_files(after_content, diag)?;
|
|
|
|
let m_ac = load_external_files(md_after_content, diag)?;
|
2019-12-22 16:42:04 -06:00
|
|
|
let ac = format!(
|
|
|
|
"{}{}",
|
|
|
|
ac,
|
2021-10-04 20:08:58 -05:00
|
|
|
Markdown {
|
|
|
|
content: &m_ac,
|
|
|
|
links: &[],
|
|
|
|
ids: id_map,
|
|
|
|
error_codes: codes,
|
|
|
|
edition,
|
|
|
|
playground,
|
2021-10-04 20:54:00 -05:00
|
|
|
heading_offset: HeadingOffset::H2,
|
2021-10-04 20:08:58 -05:00
|
|
|
}
|
|
|
|
.into_string()
|
2019-12-22 16:42:04 -06:00
|
|
|
);
|
|
|
|
Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
|
2014-06-28 05:35:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:59:58 -06:00
|
|
|
crate enum LoadStringError {
|
2016-10-08 21:55:40 -05:00
|
|
|
ReadFail,
|
|
|
|
BadUtf8,
|
2014-06-28 05:35:25 -05:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:59:58 -06:00
|
|
|
crate fn load_string<P: AsRef<Path>>(
|
2019-12-22 16:42:04 -06:00
|
|
|
file_path: P,
|
2020-01-09 04:18:47 -06:00
|
|
|
diag: &rustc_errors::Handler,
|
2019-12-22 16:42:04 -06:00
|
|
|
) -> Result<String, LoadStringError> {
|
2016-10-08 21:55:40 -05:00
|
|
|
let file_path = file_path.as_ref();
|
2018-01-10 10:58:39 -06:00
|
|
|
let contents = match fs::read(file_path) {
|
|
|
|
Ok(bytes) => bytes,
|
|
|
|
Err(e) => {
|
2018-05-08 13:42:53 -05:00
|
|
|
diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
|
2018-01-10 10:58:39 -06:00
|
|
|
return Err(LoadStringError::ReadFail);
|
|
|
|
}
|
|
|
|
};
|
2016-10-08 21:55:40 -05:00
|
|
|
match str::from_utf8(&contents) {
|
|
|
|
Ok(s) => Ok(s.to_string()),
|
|
|
|
Err(_) => {
|
2018-05-08 13:42:53 -05:00
|
|
|
diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
|
2016-10-08 21:55:40 -05:00
|
|
|
Err(LoadStringError::BadUtf8)
|
2014-06-28 05:35:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:18:47 -06:00
|
|
|
fn load_external_files(names: &[String], diag: &rustc_errors::Handler) -> Option<String> {
|
2014-06-28 05:35:25 -05:00
|
|
|
let mut out = String::new();
|
2015-01-31 11:20:46 -06:00
|
|
|
for name in names {
|
2018-05-08 13:42:53 -05:00
|
|
|
let s = match load_string(name, diag) {
|
2016-10-08 21:55:40 -05:00
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
|
|
|
out.push_str(&s);
|
2014-09-22 10:28:35 -05:00
|
|
|
out.push('\n');
|
2014-06-28 05:35:25 -05:00
|
|
|
}
|
|
|
|
Some(out)
|
|
|
|
}
|