5 use crate::syntax::feature_gate::UnstableFeatures;
6 use crate::html::markdown::{IdMap, ErrorCodes, Markdown};
8 use std::cell::RefCell;
10 #[derive(Clone, Debug)]
11 pub struct ExternalHtml {
12 /// Content that will be included inline in the <head> section of a
13 /// rendered Markdown file or generated documentation
14 pub in_header: String,
15 /// Content that will be included inline between <body> and the content of
16 /// a rendered Markdown file or generated documentation
17 pub before_content: String,
18 /// Content that will be included inline between the content and </body> of
19 /// a rendered Markdown file or generated documentation
20 pub after_content: String
24 pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
25 md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler,
27 -> Option<ExternalHtml> {
28 let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
29 load_external_files(in_header, diag)
31 load_external_files(before_content, diag)
35 load_external_files(md_before_content, diag)
37 format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), codes))))
40 load_external_files(after_content, diag)
41 .map(|ac| (ih, bc, ac))
43 .and_then(|(ih, bc, ac)|
44 load_external_files(md_after_content, diag)
46 format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), codes))))
58 pub enum LoadStringError {
63 pub fn load_string<P: AsRef<Path>>(file_path: P, diag: &errors::Handler)
64 -> Result<String, LoadStringError>
66 let file_path = file_path.as_ref();
67 let contents = match fs::read(file_path) {
70 diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
71 return Err(LoadStringError::ReadFail);
74 match str::from_utf8(&contents) {
75 Ok(s) => Ok(s.to_string()),
77 diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
78 Err(LoadStringError::BadUtf8)
83 fn load_external_files(names: &[String], diag: &errors::Handler) -> Option<String> {
84 let mut out = String::new();
86 let s = match load_string(name, diag) {
88 Err(_) => return None,