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