]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/externalfiles.rs
Remove the x86_64-rumprun-netbsd target
[rust.git] / src / librustdoc / externalfiles.rs
1 use crate::html::markdown::{ErrorCodes, IdMap, Markdown, Playground};
2 use crate::rustc_span::edition::Edition;
3 use std::fs;
4 use std::path::Path;
5 use std::str;
6
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,
18 }
19
20 impl ExternalHtml {
21     crate fn load(
22         in_header: &[String],
23         before_content: &[String],
24         after_content: &[String],
25         md_before_content: &[String],
26         md_after_content: &[String],
27         nightly_build: bool,
28         diag: &rustc_errors::Handler,
29         id_map: &mut IdMap,
30         edition: Edition,
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)?;
37         let bc = format!(
38             "{}{}",
39             bc,
40             Markdown(&m_bc, &[], id_map, codes, edition, playground).into_string()
41         );
42         let ac = load_external_files(after_content, diag)?;
43         let m_ac = load_external_files(md_after_content, diag)?;
44         let ac = format!(
45             "{}{}",
46             ac,
47             Markdown(&m_ac, &[], id_map, codes, edition, playground).into_string()
48         );
49         Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
50     }
51 }
52
53 crate enum LoadStringError {
54     ReadFail,
55     BadUtf8,
56 }
57
58 crate fn load_string<P: AsRef<Path>>(
59     file_path: P,
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) {
64         Ok(bytes) => bytes,
65         Err(e) => {
66             diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
67             return Err(LoadStringError::ReadFail);
68         }
69     };
70     match str::from_utf8(&contents) {
71         Ok(s) => Ok(s.to_string()),
72         Err(_) => {
73             diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
74             Err(LoadStringError::BadUtf8)
75         }
76     }
77 }
78
79 fn load_external_files(names: &[String], diag: &rustc_errors::Handler) -> Option<String> {
80     let mut out = String::new();
81     for name in names {
82         let s = match load_string(name, diag) {
83             Ok(s) => s,
84             Err(_) => return None,
85         };
86         out.push_str(&s);
87         out.push('\n');
88     }
89     Some(out)
90 }