]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/externalfiles.rs
Refactor mod/check (part vii)
[rust.git] / src / librustdoc / externalfiles.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::fs;
12 use std::path::Path;
13 use std::str;
14 use errors;
15 use syntax::feature_gate::UnstableFeatures;
16 use html::markdown::{IdMap, ErrorCodes, Markdown};
17 use std::cell::RefCell;
18
19 #[derive(Clone)]
20 pub struct ExternalHtml {
21     /// Content that will be included inline in the <head> section of a
22     /// rendered Markdown file or generated documentation
23     pub in_header: String,
24     /// Content that will be included inline between <body> and the content of
25     /// a rendered Markdown file or generated documentation
26     pub before_content: String,
27     /// Content that will be included inline between the content and </body> of
28     /// a rendered Markdown file or generated documentation
29     pub after_content: String
30 }
31
32 impl ExternalHtml {
33     pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
34                 md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler,
35                 id_map: &mut IdMap)
36             -> Option<ExternalHtml> {
37         let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
38         load_external_files(in_header, diag)
39             .and_then(|ih|
40                 load_external_files(before_content, diag)
41                     .map(|bc| (ih, bc))
42             )
43             .and_then(|(ih, bc)|
44                 load_external_files(md_before_content, diag)
45                     .map(|m_bc| (ih,
46                             format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), codes))))
47             )
48             .and_then(|(ih, bc)|
49                 load_external_files(after_content, diag)
50                     .map(|ac| (ih, bc, ac))
51             )
52             .and_then(|(ih, bc, ac)|
53                 load_external_files(md_after_content, diag)
54                     .map(|m_ac| (ih, bc,
55                             format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), codes))))
56             )
57             .map(|(ih, bc, ac)|
58                 ExternalHtml {
59                     in_header: ih,
60                     before_content: bc,
61                     after_content: ac,
62                 }
63             )
64     }
65 }
66
67 pub enum LoadStringError {
68     ReadFail,
69     BadUtf8,
70 }
71
72 pub fn load_string<P: AsRef<Path>>(file_path: P, diag: &errors::Handler)
73     -> Result<String, LoadStringError>
74 {
75     let file_path = file_path.as_ref();
76     let contents = match fs::read(file_path) {
77         Ok(bytes) => bytes,
78         Err(e) => {
79             diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
80             return Err(LoadStringError::ReadFail);
81         }
82     };
83     match str::from_utf8(&contents) {
84         Ok(s) => Ok(s.to_string()),
85         Err(_) => {
86             diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
87             Err(LoadStringError::BadUtf8)
88         }
89     }
90 }
91
92 fn load_external_files(names: &[String], diag: &errors::Handler) -> Option<String> {
93     let mut out = String::new();
94     for name in names {
95         let s = match load_string(name, diag) {
96             Ok(s) => s,
97             Err(_) => return None,
98         };
99         out.push_str(&s);
100         out.push('\n');
101     }
102     Some(out)
103 }