]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/externalfiles.rs
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / librustdoc / externalfiles.rs
CommitLineData
c295e0f8 1use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, Playground};
31ef2f64 2use rustc_span::edition::Edition;
2c00a5a8 3use std::fs;
c30ab7b3 4use std::path::Path;
c34b1796 5use std::str;
1a4d82fc 6
136023e0
XL
7use serde::Serialize;
8
9#[derive(Clone, Debug, Serialize)]
923072b8 10pub(crate) struct ExternalHtml {
3c0e092e 11 /// Content that will be included inline in the `<head>` section of a
c30ab7b3 12 /// rendered Markdown file or generated documentation
923072b8 13 pub(crate) in_header: String,
3c0e092e 14 /// Content that will be included inline between `<body>` and the content of
c30ab7b3 15 /// a rendered Markdown file or generated documentation
923072b8 16 pub(crate) before_content: String,
3c0e092e 17 /// Content that will be included inline between the content and `</body>` of
c30ab7b3 18 /// a rendered Markdown file or generated documentation
923072b8 19 pub(crate) after_content: String,
1a4d82fc
JJ
20}
21
22impl ExternalHtml {
923072b8 23 pub(crate) fn load(
dfeec247
XL
24 in_header: &[String],
25 before_content: &[String],
26 after_content: &[String],
27 md_before_content: &[String],
28 md_after_content: &[String],
fc512014 29 nightly_build: bool,
4b012472 30 dcx: &rustc_errors::DiagCtxt,
dfeec247
XL
31 id_map: &mut IdMap,
32 edition: Edition,
33 playground: &Option<Playground>,
34 ) -> Option<ExternalHtml> {
fc512014 35 let codes = ErrorCodes::from(nightly_build);
4b012472
FG
36 let ih = load_external_files(in_header, dcx)?;
37 let bc = load_external_files(before_content, dcx)?;
38 let m_bc = load_external_files(md_before_content, dcx)?;
dfeec247 39 let bc = format!(
add651ee 40 "{bc}{}",
c295e0f8
XL
41 Markdown {
42 content: &m_bc,
43 links: &[],
44 ids: id_map,
45 error_codes: codes,
46 edition,
47 playground,
48 heading_offset: HeadingOffset::H2,
49 }
50 .into_string()
dfeec247 51 );
4b012472
FG
52 let ac = load_external_files(after_content, dcx)?;
53 let m_ac = load_external_files(md_after_content, dcx)?;
dfeec247 54 let ac = format!(
add651ee 55 "{ac}{}",
c295e0f8
XL
56 Markdown {
57 content: &m_ac,
58 links: &[],
59 ids: id_map,
60 error_codes: codes,
61 edition,
62 playground,
63 heading_offset: HeadingOffset::H2,
64 }
65 .into_string()
dfeec247
XL
66 );
67 Some(ExternalHtml { in_header: ih, before_content: bc, after_content: ac })
1a4d82fc
JJ
68 }
69}
70
923072b8 71pub(crate) enum LoadStringError {
c30ab7b3
SL
72 ReadFail,
73 BadUtf8,
1a4d82fc
JJ
74}
75
923072b8 76pub(crate) fn load_string<P: AsRef<Path>>(
dfeec247 77 file_path: P,
4b012472 78 dcx: &rustc_errors::DiagCtxt,
dfeec247 79) -> Result<String, LoadStringError> {
c30ab7b3 80 let file_path = file_path.as_ref();
2c00a5a8
XL
81 let contents = match fs::read(file_path) {
82 Ok(bytes) => bytes,
83 Err(e) => {
4b012472 84 dcx.struct_err(format!(
add651ee
FG
85 "error reading `{file_path}`: {e}",
86 file_path = file_path.display()
87 ))
88 .emit();
2c00a5a8
XL
89 return Err(LoadStringError::ReadFail);
90 }
91 };
c30ab7b3
SL
92 match str::from_utf8(&contents) {
93 Ok(s) => Ok(s.to_string()),
94 Err(_) => {
c0240ec0 95 dcx.err(format!("error reading `{}`: not UTF-8", file_path.display()));
c30ab7b3 96 Err(LoadStringError::BadUtf8)
1a4d82fc
JJ
97 }
98 }
99}
100
4b012472 101fn load_external_files(names: &[String], dcx: &rustc_errors::DiagCtxt) -> Option<String> {
1a4d82fc 102 let mut out = String::new();
85aaf69f 103 for name in names {
4b012472 104 let Ok(s) = load_string(name, dcx) else { return None };
c30ab7b3 105 out.push_str(&s);
1a4d82fc
JJ
106 out.push('\n');
107 }
108 Some(out)
109}