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