1 use std::path::PathBuf;
3 use rustc_data_structures::fx::FxHashMap;
5 use crate::externalfiles::ExternalHtml;
6 use crate::html::escape::Escape;
7 use crate::html::format::{Buffer, Print};
8 use crate::html::render::{ensure_trailing_slash, StylePath};
13 crate favicon: String,
14 crate external_html: ExternalHtml,
15 crate default_settings: FxHashMap<String, String>,
17 /// The given user css file which allow to customize the generated
18 /// documentation theme.
19 crate css_file_extension: Option<PathBuf>,
20 /// If false, the `select` element to have search filtering by crates on rendered docs
21 /// won't be generated.
22 crate generate_search_filter: bool,
25 crate struct Page<'a> {
27 crate css_class: &'a str,
28 crate root_path: &'a str,
29 crate static_root_path: Option<&'a str>,
30 crate description: &'a str,
31 crate keywords: &'a str,
32 crate resource_suffix: &'a str,
33 crate extra_scripts: &'a [&'a str],
34 crate static_extra_scripts: &'a [&'a str],
38 crate fn get_static_root_path(&self) -> &str {
39 self.static_root_path.unwrap_or(self.root_path)
43 crate fn render<T: Print, S: Print>(
48 style_files: &[StylePath],
50 let static_root_path = page.get_static_root_path();
55 <meta charset=\"utf-8\">\
56 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
57 <meta name=\"generator\" content=\"rustdoc\">\
58 <meta name=\"description\" content=\"{description}\">\
59 <meta name=\"keywords\" content=\"{keywords}\">\
60 <title>{title}</title>\
61 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
62 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
63 id=\"mainThemeStyle\">\
65 <script id=\"default-settings\"{default_settings}></script>\
66 <script src=\"{static_root_path}storage{suffix}.js\"></script>\
67 <script src=\"{root_path}crates{suffix}.js\"></script>\
68 <noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
72 <style type=\"text/css\">\
73 #crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\
76 <body class=\"rustdoc {css_class}\">\
78 <div class=\"warning\">\
79 This old browser is unsupported and will most likely display funky \
84 <nav class=\"sidebar\">\
85 <div class=\"sidebar-menu\" role=\"button\">☰</div>\
89 <div class=\"theme-picker\">\
90 <button id=\"theme-picker\" aria-label=\"Pick another theme!\" aria-haspopup=\"menu\">\
91 <img src=\"{static_root_path}brush{suffix}.svg\" \
92 width=\"18\" height=\"18\" \
93 alt=\"Pick another theme!\">\
95 <div id=\"theme-choices\" role=\"menu\"></div>\
98 <form class=\"search-form\">\
99 <div class=\"search-container\">\
100 <div>{filter_crates}\
101 <input class=\"search-input\" name=\"search\" \
103 autocomplete=\"off\" \
104 spellcheck=\"false\" \
105 placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
108 <button type=\"button\" id=\"help-button\">?</button>
109 <a id=\"settings-menu\" href=\"{root_path}settings.html\">\
110 <img src=\"{static_root_path}wheel{suffix}.svg\" \
111 width=\"18\" height=\"18\" \
112 alt=\"Change settings\">\
117 <section id=\"main\" class=\"content\">{content}</section>\
118 <section id=\"search\" class=\"content hidden\"></section>\
120 <div id=\"rustdoc-vars\" data-root-path=\"{root_path}\" data-current-crate=\"{krate}\" \
121 data-search-index-js=\"{root_path}search-index{suffix}.js\" \
122 data-search-js=\"{static_root_path}search{suffix}.js\"></div>\
123 <script src=\"{static_root_path}main{suffix}.js\"></script>\
127 css_extension = if layout.css_file_extension.is_some() {
129 "<link rel=\"stylesheet\" \
131 href=\"{static_root_path}theme{suffix}.css\">",
132 static_root_path = static_root_path,
133 suffix = page.resource_suffix
138 content = Buffer::html().to_display(t),
139 static_root_path = static_root_path,
140 root_path = page.root_path,
141 css_class = page.css_class,
143 if layout.logo.is_empty() {
145 "<a href='{root}{path}index.html'>\
146 <div class='logo-container rust-logo'>\
147 <img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>",
148 root = page.root_path,
149 path = ensure_trailing_slash(&layout.krate),
150 static_root_path = static_root_path,
151 suffix = page.resource_suffix
155 "<a href='{root}{path}index.html'>\
156 <div class='logo-container'><img src='{logo}' alt='logo'></div></a>",
157 root = page.root_path,
158 path = ensure_trailing_slash(&layout.krate),
164 description = page.description,
165 keywords = page.keywords,
166 favicon = if layout.favicon.is_empty() {
168 r##"<link rel="icon" type="image/svg+xml" href="{static_root_path}favicon{suffix}.svg">
169 <link rel="alternate icon" type="image/png" href="{static_root_path}favicon-16x16{suffix}.png">
170 <link rel="alternate icon" type="image/png" href="{static_root_path}favicon-32x32{suffix}.png">"##,
171 static_root_path = static_root_path,
172 suffix = page.resource_suffix
175 format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
177 in_header = layout.external_html.in_header,
178 before_content = layout.external_html.before_content,
179 after_content = layout.external_html.after_content,
180 sidebar = Buffer::html().to_display(sidebar),
181 krate = layout.krate,
182 default_settings = layout
185 .map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-', "_"), Escape(v)))
186 .collect::<String>(),
187 style_files = style_files
190 if let Some(stem) = t.path.file_stem() { Some((stem, t.disabled)) } else { None }
193 if let Some(path) = t.0.to_str() { Some((path, t.1)) } else { None }
196 r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#,
197 Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)),
198 if t.1 { "disabled" } else { "" },
199 if t.0 == "light" { "id=\"themeStyle\"" } else { "" }
201 .collect::<String>(),
202 suffix = page.resource_suffix,
204 .static_extra_scripts
208 "<script src=\"{static_root_path}{extra_script}.js\"></script>",
209 static_root_path = static_root_path,
213 .chain(page.extra_scripts.iter().map(|e| {
215 "<script src=\"{root_path}{extra_script}.js\"></script>",
216 root_path = page.root_path,
220 .collect::<String>(),
221 filter_crates = if layout.generate_search_filter {
222 "<select id=\"crate-search\">\
223 <option value=\"All crates\">All crates</option>\
231 crate fn redirect(url: &str) -> String {
232 // <script> triggers a redirect before refresh, so this is fine.
237 <meta http-equiv="refresh" content="0;URL={url}">
238 <title>Redirection</title>
241 <p>Redirecting to <a href="{url}">{url}</a>...</p>
242 <script>location.replace("{url}" + location.search + location.hash);</script>