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.
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.
15 use html::markdown::Markdown;
18 pub struct ExternalHtml {
19 /// Content that will be included inline in the <head> section of a
20 /// rendered Markdown file or generated documentation
21 pub in_header: String,
22 /// Content that will be included inline between <body> and the content of
23 /// a rendered Markdown file or generated documentation
24 pub before_content: String,
25 /// Content that will be included inline between the content and </body> of
26 /// a rendered Markdown file or generated documentation
27 pub after_content: String
31 pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
32 md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler)
33 -> Option<ExternalHtml> {
34 load_external_files(in_header, diag)
36 load_external_files(before_content, diag)
40 load_external_files(md_before_content, diag)
41 .map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[]))))
44 load_external_files(after_content, diag)
45 .map(|ac| (ih, bc, ac))
47 .and_then(|(ih, bc, ac)|
48 load_external_files(md_after_content, diag)
49 .map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[]))))
61 pub enum LoadStringError {
66 pub fn load_string<P: AsRef<Path>>(file_path: P, diag: &errors::Handler)
67 -> Result<String, LoadStringError>
69 let file_path = file_path.as_ref();
70 let contents = match fs::read(file_path) {
73 diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
74 return Err(LoadStringError::ReadFail);
77 match str::from_utf8(&contents) {
78 Ok(s) => Ok(s.to_string()),
80 diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
81 Err(LoadStringError::BadUtf8)
86 fn load_external_files(names: &[String], diag: &errors::Handler) -> Option<String> {
87 let mut out = String::new();
89 let s = match load_string(name, diag) {
91 Err(_) => return None,