|
11 | 11 | use std::fs::File;
|
12 | 12 | use std::io::prelude::*;
|
13 | 13 | use std::io;
|
14 |
| -use std::path::{PathBuf, Path}; |
| 14 | +use std::path::Path; |
15 | 15 | use std::str;
|
16 | 16 |
|
17 | 17 | #[derive(Clone)]
|
18 | 18 | pub struct ExternalHtml{
|
| 19 | + /// Content that will be included inline in the <head> section of a |
| 20 | + /// rendered Markdown file or generated documentation |
19 | 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 |
20 | 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 |
21 | 27 | pub after_content: String
|
22 | 28 | }
|
23 | 29 |
|
24 | 30 | impl ExternalHtml {
|
25 | 31 | pub fn load(in_header: &[String], before_content: &[String], after_content: &[String])
|
26 | 32 | -> 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 { |
31 |
| - in_header: ih, |
32 |
| - before_content: bc, |
33 |
| - after_content: ac |
34 |
| - }), |
35 |
| - _ => None |
36 |
| - } |
| 33 | + load_external_files(in_header) |
| 34 | + .and_then(|ih| |
| 35 | + load_external_files(before_content) |
| 36 | + .map(|bc| (ih, bc)) |
| 37 | + ) |
| 38 | + .and_then(|(ih, bc)| |
| 39 | + load_external_files(after_content) |
| 40 | + .map(|ac| (ih, bc, ac)) |
| 41 | + ) |
| 42 | + .map(|(ih, bc, ac)| |
| 43 | + ExternalHtml { |
| 44 | + in_header: ih, |
| 45 | + before_content: bc, |
| 46 | + after_content: ac, |
| 47 | + } |
| 48 | + ) |
37 | 49 | }
|
38 | 50 | }
|
39 | 51 |
|
40 |
| -pub fn load_string(input: &Path) -> io::Result<Option<String>> { |
41 |
| - let mut f = File::open(input)?; |
42 |
| - let mut d = Vec::new(); |
43 |
| - f.read_to_end(&mut d)?; |
44 |
| - Ok(str::from_utf8(&d).map(|s| s.to_string()).ok()) |
| 52 | +pub enum LoadStringError { |
| 53 | + ReadFail, |
| 54 | + BadUtf8, |
45 | 55 | }
|
46 | 56 |
|
47 |
| -macro_rules! load_or_return { |
48 |
| - ($input: expr, $cant_read: expr, $not_utf8: expr) => { |
49 |
| - { |
50 |
| - let input = PathBuf::from(&$input[..]); |
51 |
| - match ::externalfiles::load_string(&input) { |
52 |
| - Err(e) => { |
53 |
| - let _ = writeln!(&mut io::stderr(), |
54 |
| - "error reading `{}`: {}", input.display(), e); |
55 |
| - return $cant_read; |
56 |
| - } |
57 |
| - Ok(None) => { |
58 |
| - let _ = writeln!(&mut io::stderr(), |
59 |
| - "error reading `{}`: not UTF-8", input.display()); |
60 |
| - return $not_utf8; |
61 |
| - } |
62 |
| - Ok(Some(s)) => s |
63 |
| - } |
| 57 | +pub fn load_string<P: AsRef<Path>>(file_path: P) -> Result<String, LoadStringError> { |
| 58 | + let file_path = file_path.as_ref(); |
| 59 | + let mut contents = vec![]; |
| 60 | + let result = File::open(file_path) |
| 61 | + .and_then(|mut f| f.read_to_end(&mut contents)); |
| 62 | + if let Err(e) = result { |
| 63 | + let _ = writeln!(&mut io::stderr(), |
| 64 | + "error reading `{}`: {}", |
| 65 | + file_path.display(), e); |
| 66 | + return Err(LoadStringError::ReadFail); |
| 67 | + } |
| 68 | + match str::from_utf8(&contents) { |
| 69 | + Ok(s) => Ok(s.to_string()), |
| 70 | + Err(_) => { |
| 71 | + let _ = writeln!(&mut io::stderr(), |
| 72 | + "error reading `{}`: not UTF-8", |
| 73 | + file_path.display()); |
| 74 | + Err(LoadStringError::BadUtf8) |
64 | 75 | }
|
65 | 76 | }
|
66 | 77 | }
|
67 | 78 |
|
68 |
| -pub fn load_external_files(names: &[String]) -> Option<String> { |
| 79 | +fn load_external_files(names: &[String]) -> Option<String> { |
69 | 80 | let mut out = String::new();
|
70 | 81 | for name in names {
|
71 |
| - out.push_str(&*load_or_return!(&name, None, None)); |
| 82 | + let s = match load_string(name) { |
| 83 | + Ok(s) => s, |
| 84 | + Err(_) => return None, |
| 85 | + }; |
| 86 | + out.push_str(&s); |
72 | 87 | out.push('\n');
|
73 | 88 | }
|
74 | 89 | Some(out)
|
|
0 commit comments