]>
Commit | Line | Data |
---|---|---|
2c00a5a8 | 1 | use std::fs; |
c30ab7b3 | 2 | use std::path::Path; |
c34b1796 | 3 | use std::str; |
94b46f34 | 4 | use errors; |
60c5eb7d | 5 | use rustc_feature::UnstableFeatures; |
48663c56 | 6 | use crate::syntax::edition::Edition; |
416331ca | 7 | use crate::html::markdown::{IdMap, ErrorCodes, Markdown, Playground}; |
1a4d82fc | 8 | |
a1dfa0c6 | 9 | #[derive(Clone, Debug)] |
2c00a5a8 | 10 | pub struct ExternalHtml { |
c30ab7b3 SL |
11 | /// Content that will be included inline in the <head> section of a |
12 | /// rendered Markdown file or generated documentation | |
1a4d82fc | 13 | pub in_header: String, |
c30ab7b3 SL |
14 | /// Content that will be included inline between <body> and the content of |
15 | /// a rendered Markdown file or generated documentation | |
1a4d82fc | 16 | pub before_content: String, |
c30ab7b3 SL |
17 | /// Content that will be included inline between the content and </body> of |
18 | /// a rendered Markdown file or generated documentation | |
1a4d82fc JJ |
19 | pub after_content: String |
20 | } | |
21 | ||
22 | impl ExternalHtml { | |
7cac9316 | 23 | pub fn load(in_header: &[String], before_content: &[String], after_content: &[String], |
b7449926 | 24 | md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler, |
416331ca | 25 | id_map: &mut IdMap, edition: Edition, playground: &Option<Playground>) |
1a4d82fc | 26 | -> Option<ExternalHtml> { |
b7449926 | 27 | let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()); |
416331ca XL |
28 | let ih = load_external_files(in_header, diag)?; |
29 | let bc = load_external_files(before_content, diag)?; | |
30 | let m_bc = load_external_files(md_before_content, diag)?; | |
31 | let bc = format!("{}{}", bc, Markdown(&m_bc, &[], id_map, | |
32 | codes, edition, playground).to_string()); | |
33 | let ac = load_external_files(after_content, diag)?; | |
34 | let m_ac = load_external_files(md_after_content, diag)?; | |
35 | let ac = format!("{}{}", ac, Markdown(&m_ac, &[], id_map, | |
36 | codes, edition, playground).to_string()); | |
37 | Some(ExternalHtml { | |
38 | in_header: ih, | |
39 | before_content: bc, | |
40 | after_content: ac, | |
41 | }) | |
1a4d82fc JJ |
42 | } |
43 | } | |
44 | ||
c30ab7b3 SL |
45 | pub enum LoadStringError { |
46 | ReadFail, | |
47 | BadUtf8, | |
1a4d82fc JJ |
48 | } |
49 | ||
94b46f34 XL |
50 | pub fn load_string<P: AsRef<Path>>(file_path: P, diag: &errors::Handler) |
51 | -> Result<String, LoadStringError> | |
52 | { | |
c30ab7b3 | 53 | let file_path = file_path.as_ref(); |
2c00a5a8 XL |
54 | let contents = match fs::read(file_path) { |
55 | Ok(bytes) => bytes, | |
56 | Err(e) => { | |
94b46f34 | 57 | diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit(); |
2c00a5a8 XL |
58 | return Err(LoadStringError::ReadFail); |
59 | } | |
60 | }; | |
c30ab7b3 SL |
61 | match str::from_utf8(&contents) { |
62 | Ok(s) => Ok(s.to_string()), | |
63 | Err(_) => { | |
94b46f34 | 64 | diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit(); |
c30ab7b3 | 65 | Err(LoadStringError::BadUtf8) |
1a4d82fc JJ |
66 | } |
67 | } | |
68 | } | |
69 | ||
94b46f34 | 70 | fn load_external_files(names: &[String], diag: &errors::Handler) -> Option<String> { |
1a4d82fc | 71 | let mut out = String::new(); |
85aaf69f | 72 | for name in names { |
94b46f34 | 73 | let s = match load_string(name, diag) { |
c30ab7b3 SL |
74 | Ok(s) => s, |
75 | Err(_) => return None, | |
76 | }; | |
77 | out.push_str(&s); | |
1a4d82fc JJ |
78 | out.push('\n'); |
79 | } | |
80 | Some(out) | |
81 | } |