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