]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/html/layout.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / src / librustdoc / html / layout.rs
CommitLineData
2c00a5a8 1use std::path::PathBuf;
1a4d82fc 2
5869c6ff
XL
3use rustc_data_structures::fx::FxHashMap;
4
9fa01778 5use crate::externalfiles::ExternalHtml;
60c5eb7d 6use crate::html::escape::Escape;
e1599b0c 7use crate::html::format::{Buffer, Print};
3dfed10e 8use crate::html::render::{ensure_trailing_slash, StylePath};
1a4d82fc
JJ
9
10#[derive(Clone)]
fc512014
XL
11crate struct Layout {
12 crate logo: String,
13 crate favicon: String,
14 crate external_html: ExternalHtml,
5869c6ff 15 crate default_settings: FxHashMap<String, String>,
fc512014 16 crate krate: String,
e1599b0c
XL
17 /// The given user css file which allow to customize the generated
18 /// documentation theme.
fc512014 19 crate css_file_extension: Option<PathBuf>,
e1599b0c
XL
20 /// If false, the `select` element to have search filtering by crates on rendered docs
21 /// won't be generated.
fc512014 22 crate generate_search_filter: bool,
1a4d82fc
JJ
23}
24
fc512014
XL
25crate struct Page<'a> {
26 crate title: &'a str,
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],
1a4d82fc
JJ
35}
36
fc512014 37crate fn render<T: Print, S: Print>(
0731742a 38 layout: &Layout,
9fa01778 39 page: &Page<'_>,
e1599b0c
XL
40 sidebar: S,
41 t: T,
3dfed10e 42 style_files: &[StylePath],
e1599b0c 43) -> String {
0731742a 44 let static_root_path = page.static_root_path.unwrap_or(page.root_path);
dfeec247
XL
45 format!(
46 "<!DOCTYPE html>\
83c7162d
XL
47<html lang=\"en\">\
48<head>\
49 <meta charset=\"utf-8\">\
50 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
51 <meta name=\"generator\" content=\"rustdoc\">\
52 <meta name=\"description\" content=\"{description}\">\
53 <meta name=\"keywords\" content=\"{keywords}\">\
54 <title>{title}</title>\
0731742a
XL
55 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
56 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
83c7162d 57 id=\"mainThemeStyle\">\
3dfed10e 58 {style_files}\
29967ef6 59 <script id=\"default-settings\"{default_settings}></script>\
0731742a
XL
60 <script src=\"{static_root_path}storage{suffix}.js\"></script>\
61 <noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
83c7162d
XL
62 {css_extension}\
63 {favicon}\
64 {in_header}\
0731742a
XL
65 <style type=\"text/css\">\
66 #crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\
67 </style>\
83c7162d
XL
68</head>\
69<body class=\"rustdoc {css_class}\">\
70 <!--[if lte IE 8]>\
71 <div class=\"warning\">\
72 This old browser is unsupported and will most likely display funky \
73 things.\
74 </div>\
75 <![endif]-->\
76 {before_content}\
77 <nav class=\"sidebar\">\
78 <div class=\"sidebar-menu\">&#9776;</div>\
79 {logo}\
80 {sidebar}\
81 </nav>\
82 <div class=\"theme-picker\">\
29967ef6 83 <button id=\"theme-picker\" aria-label=\"Pick another theme!\" aria-haspopup=\"menu\">\
0731742a
XL
84 <img src=\"{static_root_path}brush{suffix}.svg\" \
85 width=\"18\" \
86 alt=\"Pick another theme!\">\
83c7162d 87 </button>\
29967ef6 88 <div id=\"theme-choices\" role=\"menu\"></div>\
83c7162d 89 </div>\
0731742a 90 <script src=\"{static_root_path}theme{suffix}.js\"></script>\
83c7162d 91 <nav class=\"sub\">\
60c5eb7d 92 <form class=\"search-form\">\
83c7162d 93 <div class=\"search-container\">\
0731742a
XL
94 <div>{filter_crates}\
95 <input class=\"search-input\" name=\"search\" \
60c5eb7d 96 disabled \
0731742a
XL
97 autocomplete=\"off\" \
98 spellcheck=\"false\" \
99 placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
100 type=\"search\">\
101 </div>\
fc512014 102 <button type=\"button\" class=\"help-button\">?</button>
83c7162d 103 <a id=\"settings-menu\" href=\"{root_path}settings.html\">\
0731742a
XL
104 <img src=\"{static_root_path}wheel{suffix}.svg\" \
105 width=\"18\" \
106 alt=\"Change settings\">\
83c7162d
XL
107 </a>\
108 </div>\
109 </form>\
110 </nav>\
111 <section id=\"main\" class=\"content\">{content}</section>\
112 <section id=\"search\" class=\"content hidden\"></section>\
113 <section class=\"footer\"></section>\
83c7162d 114 {after_content}\
5869c6ff 115 <div id=\"rustdoc-vars\" data-root-path=\"{root_path}\" data-current-crate=\"{krate}\"></div>
0731742a 116 <script src=\"{static_root_path}main{suffix}.js\"></script>\
a1dfa0c6 117 {extra_scripts}\
48663c56 118 <script defer src=\"{root_path}search-index{suffix}.js\"></script>\
83c7162d
XL
119</body>\
120</html>",
dfeec247
XL
121 css_extension = if layout.css_file_extension.is_some() {
122 format!(
123 "<link rel=\"stylesheet\" \
0731742a
XL
124 type=\"text/css\" \
125 href=\"{static_root_path}theme{suffix}.css\">",
126 static_root_path = static_root_path,
dfeec247
XL
127 suffix = page.resource_suffix
128 )
129 } else {
130 String::new()
131 },
132 content = Buffer::html().to_display(t),
133 static_root_path = static_root_path,
134 root_path = page.root_path,
135 css_class = page.css_class,
136 logo = {
dfeec247
XL
137 if layout.logo.is_empty() {
138 format!(
5869c6ff 139 "<a href='{root}{path}index.html'>\
3dfed10e 140 <div class='logo-container rust-logo'>\
48663c56 141 <img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>",
5869c6ff
XL
142 root = page.root_path,
143 path = ensure_trailing_slash(&layout.krate),
dfeec247
XL
144 static_root_path = static_root_path,
145 suffix = page.resource_suffix
146 )
147 } else {
148 format!(
5869c6ff
XL
149 "<a href='{root}{path}index.html'>\
150 <div class='logo-container'><img src='{logo}' alt='logo'></div></a>",
151 root = page.root_path,
152 path = ensure_trailing_slash(&layout.krate),
153 logo = layout.logo
dfeec247
XL
154 )
155 }
156 },
157 title = page.title,
158 description = page.description,
159 keywords = page.keywords,
160 favicon = if layout.favicon.is_empty() {
161 format!(
1b1a35ee
XL
162 r##"<link rel="icon" type="image/svg+xml" href="{static_root_path}favicon{suffix}.svg">
163<link rel="alternate icon" type="image/png" href="{static_root_path}favicon-16x16{suffix}.png">
164<link rel="alternate icon" type="image/png" href="{static_root_path}favicon-32x32{suffix}.png">"##,
dfeec247
XL
165 static_root_path = static_root_path,
166 suffix = page.resource_suffix
167 )
168 } else {
169 format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
170 },
171 in_header = layout.external_html.in_header,
172 before_content = layout.external_html.before_content,
173 after_content = layout.external_html.after_content,
174 sidebar = Buffer::html().to_display(sidebar),
175 krate = layout.krate,
29967ef6
XL
176 default_settings = layout
177 .default_settings
178 .iter()
179 .map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-', "_"), Escape(v)))
180 .collect::<String>(),
3dfed10e 181 style_files = style_files
dfeec247 182 .iter()
3dfed10e
XL
183 .filter_map(|t| {
184 if let Some(stem) = t.path.file_stem() { Some((stem, t.disabled)) } else { None }
185 })
186 .filter_map(|t| {
187 if let Some(path) = t.0.to_str() { Some((path, t.1)) } else { None }
188 })
dfeec247 189 .map(|t| format!(
3dfed10e
XL
190 r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#,
191 Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)),
192 if t.1 { "disabled" } else { "" },
193 if t.0 == "light" { "id=\"themeStyle\"" } else { "" }
dfeec247
XL
194 ))
195 .collect::<String>(),
196 suffix = page.resource_suffix,
5869c6ff 197 extra_scripts = page
dfeec247
XL
198 .static_extra_scripts
199 .iter()
200 .map(|e| {
201 format!(
202 "<script src=\"{static_root_path}{extra_script}.js\"></script>",
203 static_root_path = static_root_path,
204 extra_script = e
205 )
206 })
5869c6ff 207 .chain(page.extra_scripts.iter().map(|e| {
dfeec247
XL
208 format!(
209 "<script src=\"{root_path}{extra_script}.js\"></script>",
210 root_path = page.root_path,
211 extra_script = e
212 )
5869c6ff 213 }))
dfeec247
XL
214 .collect::<String>(),
215 filter_crates = if layout.generate_search_filter {
216 "<select id=\"crate-search\">\
1b1a35ee
XL
217 <option value=\"All crates\">All crates</option>\
218 </select>"
dfeec247
XL
219 } else {
220 ""
221 },
1a4d82fc
JJ
222 )
223}
224
fc512014 225crate fn redirect(url: &str) -> String {
1a4d82fc 226 // <script> triggers a redirect before refresh, so this is fine.
e1599b0c 227 format!(
dfeec247 228 r##"<!DOCTYPE html>
1a4d82fc
JJ
229<html lang="en">
230<head>
231 <meta http-equiv="refresh" content="0;URL={url}">
232</head>
233<body>
234 <p>Redirecting to <a href="{url}">{url}</a>...</p>
235 <script>location.replace("{url}" + location.search + location.hash);</script>
236</body>
237</html>"##,
dfeec247 238 url = url,
1a4d82fc
JJ
239 )
240}