2014-06-28 05:35:25 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::{old_io, str};
|
2014-06-28 05:35:25 -05:00
|
|
|
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Clone)]
|
2014-06-28 05:35:25 -05:00
|
|
|
pub struct ExternalHtml{
|
|
|
|
pub in_header: String,
|
|
|
|
pub before_content: String,
|
|
|
|
pub after_content: String
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExternalHtml {
|
|
|
|
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String])
|
|
|
|
-> Option<ExternalHtml> {
|
|
|
|
match (load_external_files(in_header),
|
|
|
|
load_external_files(before_content),
|
|
|
|
load_external_files(after_content)) {
|
|
|
|
(Some(ih), Some(bc), Some(ac)) => Some(ExternalHtml {
|
|
|
|
in_header: ih,
|
|
|
|
before_content: bc,
|
|
|
|
after_content: ac
|
|
|
|
}),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 18:31:00 -06:00
|
|
|
pub fn load_string(input: &Path) -> old_io::IoResult<Option<String>> {
|
|
|
|
let mut f = try!(old_io::File::open(input));
|
2014-06-28 05:35:25 -05:00
|
|
|
let d = try!(f.read_to_end());
|
2015-02-01 20:53:25 -06:00
|
|
|
Ok(str::from_utf8(&d).map(|s| s.to_string()).ok())
|
2014-06-28 05:35:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! load_or_return {
|
|
|
|
($input: expr, $cant_read: expr, $not_utf8: expr) => {
|
|
|
|
{
|
|
|
|
let input = Path::new($input);
|
|
|
|
match ::externalfiles::load_string(&input) {
|
|
|
|
Err(e) => {
|
2015-01-22 18:31:00 -06:00
|
|
|
let _ = writeln!(&mut old_io::stderr(),
|
2014-06-28 05:35:25 -05:00
|
|
|
"error reading `{}`: {}", input.display(), e);
|
|
|
|
return $cant_read;
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
2015-01-22 18:31:00 -06:00
|
|
|
let _ = writeln!(&mut old_io::stderr(),
|
2014-06-28 05:35:25 -05:00
|
|
|
"error reading `{}`: not UTF-8", input.display());
|
|
|
|
return $not_utf8;
|
|
|
|
}
|
|
|
|
Ok(Some(s)) => s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_external_files(names: &[String]) -> Option<String> {
|
|
|
|
let mut out = String::new();
|
2015-01-31 11:20:46 -06:00
|
|
|
for name in names {
|
2015-02-01 20:53:25 -06:00
|
|
|
out.push_str(&*load_or_return!(&name, None, None));
|
2014-09-22 10:28:35 -05:00
|
|
|
out.push('\n');
|
2014-06-28 05:35:25 -05:00
|
|
|
}
|
|
|
|
Some(out)
|
|
|
|
}
|