1 use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, Playground};
2 use crate::rustc_span::edition::Edition;
9 #[derive(Clone, Debug, Serialize)]
10 crate struct ExternalHtml {
11 /// Content that will be included inline in the `<head>` section of a
12 /// rendered Markdown file or generated documentation
13 crate in_header: String,
14 /// Content that will be included inline between `<body>` and the content of
15 /// a rendered Markdown file or generated documentation
16 crate before_content: String,
17 /// Content that will be included inline between the content and `</body>` of
18 /// a rendered Markdown file or generated documentation
19 crate after_content: String,
25 before_content: &[String],
26 after_content: &[String],
27 md_before_content: &[String],
28 md_after_content: &[String],
30 diag: &rustc_errors::Handler,
33 playground: &Option<Playground>,
34 ) -> Option<ExternalHtml> {
35 let codes = ErrorCodes::from(nightly_build);
36 let ih = load_external_files(in_header, diag)?;
37 let bc = load_external_files(before_content, diag)?;
38 let m_bc = load_external_files(md_before_content, diag)?;
49 heading_offset: HeadingOffset::H2,
53 let ac = load_external_files(after_content, diag)?;
54 let m_ac = load_external_files(md_after_content, diag)?;
65 heading_offset: HeadingOffset::H2,
69 Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
73 crate enum LoadStringError {
78 crate fn load_string<P: AsRef<Path>>(
80 diag: &rustc_errors::Handler,
81 ) -> Result<String, LoadStringError> {
82 let file_path = file_path.as_ref();
83 let contents = match fs::read(file_path) {
86 diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
87 return Err(LoadStringError::ReadFail);
90 match str::from_utf8(&contents) {
91 Ok(s) => Ok(s.to_string()),
93 diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
94 Err(LoadStringError::BadUtf8)
99 fn load_external_files(names: &[String], diag: &rustc_errors::Handler) -> Option<String> {
100 let mut out = String::new();
102 let s = match load_string(name, diag) {
104 Err(_) => return None,