1use std::fmt::Display;
2use std::path::PathBuf;
3
4use askama::Template;
5use rustc_data_structures::fx::FxIndexMap;
6
7use super::static_files::{STATIC_FILES, StaticFiles};
8use crate::externalfiles::ExternalHtml;
9use crate::html::render::{StylePath, ensure_trailing_slash};
10
11#[cfg(test)]
12mod tests;
13
14pub(crate) struct Layout {
15 pub(crate) logo: String,
16 pub(crate) favicon: String,
17 pub(crate) external_html: ExternalHtml,
18 pub(crate) default_settings: FxIndexMap<String, String>,
19 pub(crate) krate: String,
20 pub(crate) krate_version: String,
21 pub(crate) css_file_extension: Option<PathBuf>,
24 pub(crate) scrape_examples_extension: bool,
26}
27
28pub(crate) struct Page<'a> {
29 pub(crate) title: &'a str,
30 pub(crate) css_class: &'a str,
31 pub(crate) root_path: &'a str,
32 pub(crate) static_root_path: Option<&'a str>,
33 pub(crate) description: &'a str,
34 pub(crate) resource_suffix: &'a str,
35 pub(crate) rust_logo: bool,
36}
37
38impl Page<'_> {
39 pub(crate) fn get_static_root_path(&self) -> String {
40 match self.static_root_path {
41 Some(s) => s.to_string(),
42 None => format!("{}static.files/", self.root_path),
43 }
44 }
45}
46
47#[derive(Template)]
48#[template(path = "page.html")]
49struct PageLayout<'a> {
50 static_root_path: String,
51 page: &'a Page<'a>,
52 layout: &'a Layout,
53
54 files: &'static StaticFiles,
55
56 themes: Vec<String>,
57 sidebar: String,
58 content: String,
59 rust_channel: &'static str,
60 pub(crate) rustdoc_version: &'a str,
61 display_krate: &'a str,
69 display_krate_with_trailing_slash: String,
70 display_krate_version_number: &'a str,
71 display_krate_version_extra: &'a str,
72}
73
74impl PageLayout<'_> {
75 fn static_root_path_may_remove_crossorigin(&self) -> bool {
77 may_remove_crossorigin(&self.static_root_path)
78 }
79}
80
81pub(crate) use crate::html::render::sidebar::filters;
82
83pub(crate) fn render<T: Display, S: Display>(
84 layout: &Layout,
85 page: &Page<'_>,
86 sidebar: S,
87 t: T,
88 style_files: &[StylePath],
89) -> String {
90 let rustdoc_version = rustc_interface::util::version_str!().unwrap_or("unknown version");
91
92 let (display_krate, display_krate_version, display_krate_with_trailing_slash) =
93 if page.root_path == "./" {
94 ("Rustdoc", rustdoc_version, String::new())
96 } else {
97 let display_krate_with_trailing_slash =
98 ensure_trailing_slash(&layout.krate).to_string();
99 (&layout.krate[..], &layout.krate_version[..], display_krate_with_trailing_slash)
100 };
101 let static_root_path = page.get_static_root_path();
102
103 let (display_krate_version_number, display_krate_version_extra) =
105 display_krate_version.split_once([' ', '\t']).unwrap_or((display_krate_version, ""));
106
107 let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
108 themes.sort();
109
110 let content = t.to_string(); let sidebar = sidebar.to_string();
112 PageLayout {
113 static_root_path,
114 page,
115 layout,
116 files: &STATIC_FILES,
117 themes,
118 sidebar,
119 content,
120 display_krate,
121 display_krate_with_trailing_slash,
122 display_krate_version_number,
123 display_krate_version_extra,
124 rust_channel: *crate::clean::utils::RUSTDOC_VERSION,
125 rustdoc_version,
126 }
127 .render()
128 .unwrap()
129}
130
131pub(crate) fn redirect(url: &str) -> String {
132 format!(
134 r##"<!DOCTYPE html>
135<html lang="en">
136<head>
137 <meta http-equiv="refresh" content="0;URL={url}">
138 <title>Redirection</title>
139</head>
140<body>
141 <p>Redirecting to <a href="{url}">{url}</a>...</p>
142 <script>location.replace("{url}" + location.search + location.hash);</script>
143</body>
144</html>"##,
145 )
146}
147
148pub(crate) fn may_remove_crossorigin(href: &str) -> bool {
151 if href.starts_with("//") {
153 return false;
154 }
155 let has_scheme = href.split_once(':').is_some_and(|(scheme, _rest)| {
159 let mut chars = scheme.chars();
160 chars.next().is_some_and(|c| c.is_ascii_alphabetic())
161 && chars.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
162 });
163 !has_scheme
165}