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