5 use syntax::feature_gate::UnstableFeatures;
6 use html::markdown::{IdMap, ErrorCodes, Markdown};
7 use std::cell::RefCell;
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
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,
26 -> Option<ExternalHtml> {
27 let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
28 load_external_files(in_header, diag)
30 load_external_files(before_content, diag)
34 load_external_files(md_before_content, diag)
36 format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), codes))))
39 load_external_files(after_content, diag)
40 .map(|ac| (ih, bc, ac))
42 .and_then(|(ih, bc, ac)|
43 load_external_files(md_after_content, diag)
45 format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), codes))))
57 pub enum LoadStringError {
62 pub fn load_string<P: AsRef<Path>>(file_path: P, diag: &errors::Handler)
63 -> Result<String, LoadStringError>
65 let file_path = file_path.as_ref();
66 let contents = match fs::read(file_path) {
69 diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
70 return Err(LoadStringError::ReadFail);
73 match str::from_utf8(&contents) {
74 Ok(s) => Ok(s.to_string()),
76 diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
77 Err(LoadStringError::BadUtf8)
82 fn load_external_files(names: &[String], diag: &errors::Handler) -> Option<String> {
83 let mut out = String::new();
85 let s = match load_string(name, diag) {
87 Err(_) => return None,