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