]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/externalfiles.rs
Rollup merge of #60766 - vorner:weak-into-raw, r=sfackler
[rust.git] / src / librustdoc / externalfiles.rs
1 use std::fs;
2 use std::path::Path;
3 use std::str;
4 use errors;
5 use crate::syntax::feature_gate::UnstableFeatures;
6 use crate::syntax::edition::Edition;
7 use crate::html::markdown::{IdMap, ErrorCodes, Markdown};
8
9 use std::cell::RefCell;
10
11 #[derive(Clone, Debug)]
12 pub struct ExternalHtml {
13     /// Content that will be included inline in the <head> section of a
14     /// rendered Markdown file or generated documentation
15     pub in_header: String,
16     /// Content that will be included inline between <body> and the content of
17     /// a rendered Markdown file or generated documentation
18     pub before_content: String,
19     /// Content that will be included inline between the content and </body> of
20     /// a rendered Markdown file or generated documentation
21     pub after_content: String
22 }
23
24 impl ExternalHtml {
25     pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
26                 md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler,
27                 id_map: &mut IdMap, edition: Edition)
28             -> Option<ExternalHtml> {
29         let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
30         load_external_files(in_header, diag)
31             .and_then(|ih|
32                 load_external_files(before_content, diag)
33                     .map(|bc| (ih, bc))
34             )
35             .and_then(|(ih, bc)|
36                 load_external_files(md_before_content, diag)
37                     .map(|m_bc| (ih,
38                             format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map),
39                                     codes, edition))))
40             )
41             .and_then(|(ih, bc)|
42                 load_external_files(after_content, diag)
43                     .map(|ac| (ih, bc, ac))
44             )
45             .and_then(|(ih, bc, ac)|
46                 load_external_files(md_after_content, diag)
47                     .map(|m_ac| (ih, bc,
48                             format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map),
49                                     codes, edition))))
50             )
51             .map(|(ih, bc, ac)|
52                 ExternalHtml {
53                     in_header: ih,
54                     before_content: bc,
55                     after_content: ac,
56                 }
57             )
58     }
59 }
60
61 pub enum LoadStringError {
62     ReadFail,
63     BadUtf8,
64 }
65
66 pub fn load_string<P: AsRef<Path>>(file_path: P, diag: &errors::Handler)
67     -> Result<String, LoadStringError>
68 {
69     let file_path = file_path.as_ref();
70     let contents = match fs::read(file_path) {
71         Ok(bytes) => bytes,
72         Err(e) => {
73             diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
74             return Err(LoadStringError::ReadFail);
75         }
76     };
77     match str::from_utf8(&contents) {
78         Ok(s) => Ok(s.to_string()),
79         Err(_) => {
80             diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
81             Err(LoadStringError::BadUtf8)
82         }
83     }
84 }
85
86 fn load_external_files(names: &[String], diag: &errors::Handler) -> Option<String> {
87     let mut out = String::new();
88     for name in names {
89         let s = match load_string(name, diag) {
90             Ok(s) => s,
91             Err(_) => return None,
92         };
93         out.push_str(&s);
94         out.push('\n');
95     }
96     Some(out)
97 }