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