1 use crate::html::markdown::{ErrorCodes, IdMap, Markdown, Playground};
2 use crate::rustc_span::edition::Edition;
7 #[derive(Clone, Debug)]
8 crate struct ExternalHtml {
9 /// Content that will be included inline in the <head> section of a
10 /// rendered Markdown file or generated documentation
11 crate in_header: String,
12 /// Content that will be included inline between <body> and the content of
13 /// a rendered Markdown file or generated documentation
14 crate before_content: String,
15 /// Content that will be included inline between the content and </body> of
16 /// a rendered Markdown file or generated documentation
17 crate after_content: String,
23 before_content: &[String],
24 after_content: &[String],
25 md_before_content: &[String],
26 md_after_content: &[String],
28 diag: &rustc_errors::Handler,
31 playground: &Option<Playground>,
32 ) -> Option<ExternalHtml> {
33 let codes = ErrorCodes::from(nightly_build);
34 let ih = load_external_files(in_header, diag)?;
35 let bc = load_external_files(before_content, diag)?;
36 let m_bc = load_external_files(md_before_content, diag)?;
40 Markdown(&m_bc, &[], id_map, codes, edition, playground).into_string()
42 let ac = load_external_files(after_content, diag)?;
43 let m_ac = load_external_files(md_after_content, diag)?;
47 Markdown(&m_ac, &[], id_map, codes, edition, playground).into_string()
49 Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
53 crate enum LoadStringError {
58 crate fn load_string<P: AsRef<Path>>(
60 diag: &rustc_errors::Handler,
61 ) -> Result<String, LoadStringError> {
62 let file_path = file_path.as_ref();
63 let contents = match fs::read(file_path) {
66 diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
67 return Err(LoadStringError::ReadFail);
70 match str::from_utf8(&contents) {
71 Ok(s) => Ok(s.to_string()),
73 diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
74 Err(LoadStringError::BadUtf8)
79 fn load_external_files(names: &[String], diag: &rustc_errors::Handler) -> Option<String> {
80 let mut out = String::new();
82 let s = match load_string(name, diag) {
84 Err(_) => return None,