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.
12 use std::io::prelude::*;
14 use std::path::{PathBuf, Path};
18 pub struct ExternalHtml{
19 pub in_header: String,
20 pub before_content: String,
21 pub after_content: String
25 pub fn load(in_header: &[String], before_content: &[String], after_content: &[String])
26 -> Option<ExternalHtml> {
27 match (load_external_files(in_header),
28 load_external_files(before_content),
29 load_external_files(after_content)) {
30 (Some(ih), Some(bc), Some(ac)) => Some(ExternalHtml {
40 pub fn load_string(input: &Path) -> io::Result<Option<String>> {
41 let mut f = try!(File::open(input));
42 let mut d = Vec::new();
43 try!(f.read_to_end(&mut d));
44 Ok(str::from_utf8(&d).map(|s| s.to_string()).ok())
47 macro_rules! load_or_return {
48 ($input: expr, $cant_read: expr, $not_utf8: expr) => {
50 let input = PathBuf::from(&$input[..]);
51 match ::externalfiles::load_string(&input) {
53 let _ = writeln!(&mut io::stderr(),
54 "error reading `{}`: {}", input.display(), e);
58 let _ = writeln!(&mut io::stderr(),
59 "error reading `{}`: not UTF-8", input.display());
68 pub fn load_external_files(names: &[String]) -> Option<String> {
69 let mut out = String::new();
71 out.push_str(&*load_or_return!(&name, None, None));