]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/html/layout.rs
New upstream version 1.45.0+dfsg1
[rustc.git] / src / librustdoc / html / layout.rs
CommitLineData
2c00a5a8 1use std::path::PathBuf;
1a4d82fc 2
9fa01778 3use crate::externalfiles::ExternalHtml;
60c5eb7d 4use crate::html::escape::Escape;
e1599b0c 5use crate::html::format::{Buffer, Print};
dfeec247 6use crate::html::render::ensure_trailing_slash;
1a4d82fc
JJ
7
8#[derive(Clone)]
9pub struct Layout {
10 pub logo: String,
11 pub favicon: String,
12 pub external_html: ExternalHtml,
13 pub krate: String,
e1599b0c
XL
14 /// The given user css file which allow to customize the generated
15 /// documentation theme.
16 pub css_file_extension: Option<PathBuf>,
17 /// If false, the `select` element to have search filtering by crates on rendered docs
18 /// won't be generated.
19 pub generate_search_filter: bool,
1a4d82fc
JJ
20}
21
22pub struct Page<'a> {
23 pub title: &'a str,
9e0c209e 24 pub css_class: &'a str,
1a4d82fc 25 pub root_path: &'a str,
0731742a 26 pub static_root_path: Option<&'a str>,
1a4d82fc 27 pub description: &'a str,
54a0048b 28 pub keywords: &'a str,
0531ce1d 29 pub resource_suffix: &'a str,
0731742a
XL
30 pub extra_scripts: &'a [&'a str],
31 pub static_extra_scripts: &'a [&'a str],
1a4d82fc
JJ
32}
33
e1599b0c 34pub fn render<T: Print, S: Print>(
0731742a 35 layout: &Layout,
9fa01778 36 page: &Page<'_>,
e1599b0c
XL
37 sidebar: S,
38 t: T,
0731742a 39 themes: &[PathBuf],
e1599b0c 40) -> String {
0731742a 41 let static_root_path = page.static_root_path.unwrap_or(page.root_path);
dfeec247
XL
42 format!(
43 "<!DOCTYPE html>\
83c7162d
XL
44<html lang=\"en\">\
45<head>\
46 <meta charset=\"utf-8\">\
47 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
48 <meta name=\"generator\" content=\"rustdoc\">\
49 <meta name=\"description\" content=\"{description}\">\
50 <meta name=\"keywords\" content=\"{keywords}\">\
51 <title>{title}</title>\
0731742a
XL
52 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
53 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
83c7162d
XL
54 id=\"mainThemeStyle\">\
55 {themes}\
0731742a
XL
56 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}dark{suffix}.css\">\
57 <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}light{suffix}.css\" \
83c7162d 58 id=\"themeStyle\">\
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\">\
82 <button id=\"theme-picker\" aria-label=\"Pick another theme!\">\
0731742a
XL
83 <img src=\"{static_root_path}brush{suffix}.svg\" \
84 width=\"18\" \
85 alt=\"Pick another theme!\">\
83c7162d
XL
86 </button>\
87 <div id=\"theme-choices\"></div>\
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>\
83c7162d 101 <a id=\"settings-menu\" href=\"{root_path}settings.html\">\
0731742a
XL
102 <img src=\"{static_root_path}wheel{suffix}.svg\" \
103 width=\"18\" \
104 alt=\"Change settings\">\
83c7162d
XL
105 </a>\
106 </div>\
107 </form>\
108 </nav>\
109 <section id=\"main\" class=\"content\">{content}</section>\
110 <section id=\"search\" class=\"content hidden\"></section>\
111 <section class=\"footer\"></section>\
83c7162d
XL
112 {after_content}\
113 <script>\
114 window.rootPath = \"{root_path}\";\
115 window.currentCrate = \"{krate}\";\
116 </script>\
0731742a
XL
117 <script src=\"{static_root_path}main{suffix}.js\"></script>\
118 {static_extra_scripts}\
a1dfa0c6 119 {extra_scripts}\
48663c56 120 <script defer src=\"{root_path}search-index{suffix}.js\"></script>\
83c7162d
XL
121</body>\
122</html>",
dfeec247
XL
123 css_extension = if layout.css_file_extension.is_some() {
124 format!(
125 "<link rel=\"stylesheet\" \
0731742a
XL
126 type=\"text/css\" \
127 href=\"{static_root_path}theme{suffix}.css\">",
128 static_root_path = static_root_path,
dfeec247
XL
129 suffix = page.resource_suffix
130 )
131 } else {
132 String::new()
133 },
134 content = Buffer::html().to_display(t),
135 static_root_path = static_root_path,
136 root_path = page.root_path,
137 css_class = page.css_class,
138 logo = {
139 let p = format!("{}{}", page.root_path, layout.krate);
140 let p = ensure_trailing_slash(&p);
141 if layout.logo.is_empty() {
142 format!(
143 "<a href='{path}index.html'>\
48663c56
XL
144 <div class='logo-container'>\
145 <img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>",
dfeec247
XL
146 path = p,
147 static_root_path = static_root_path,
148 suffix = page.resource_suffix
149 )
150 } else {
151 format!(
152 "<a href='{}index.html'>\
48663c56 153 <div class='logo-container'><img src='{}' alt='logo'></div></a>",
dfeec247
XL
154 p, 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="shortcut icon" href="{static_root_path}favicon{suffix}.ico">"#,
164 static_root_path = static_root_path,
165 suffix = page.resource_suffix
166 )
167 } else {
168 format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
169 },
170 in_header = layout.external_html.in_header,
171 before_content = layout.external_html.before_content,
172 after_content = layout.external_html.after_content,
173 sidebar = Buffer::html().to_display(sidebar),
174 krate = layout.krate,
175 themes = themes
176 .iter()
177 .filter_map(|t| t.file_stem())
178 .filter_map(|t| t.to_str())
179 .map(|t| format!(
180 r#"<link rel="stylesheet" type="text/css" href="{}.css">"#,
181 Escape(&format!("{}{}{}", static_root_path, t, page.resource_suffix))
182 ))
183 .collect::<String>(),
184 suffix = page.resource_suffix,
185 static_extra_scripts = page
186 .static_extra_scripts
187 .iter()
188 .map(|e| {
189 format!(
190 "<script src=\"{static_root_path}{extra_script}.js\"></script>",
191 static_root_path = static_root_path,
192 extra_script = e
193 )
194 })
195 .collect::<String>(),
196 extra_scripts = page
197 .extra_scripts
198 .iter()
199 .map(|e| {
200 format!(
201 "<script src=\"{root_path}{extra_script}.js\"></script>",
202 root_path = page.root_path,
203 extra_script = e
204 )
205 })
206 .collect::<String>(),
207 filter_crates = if layout.generate_search_filter {
208 "<select id=\"crate-search\">\
0731742a
XL
209 <option value=\"All crates\">All crates</option>\
210 </select>"
dfeec247
XL
211 } else {
212 ""
213 },
1a4d82fc
JJ
214 )
215}
216
e1599b0c 217pub fn redirect(url: &str) -> String {
1a4d82fc 218 // <script> triggers a redirect before refresh, so this is fine.
e1599b0c 219 format!(
dfeec247 220 r##"<!DOCTYPE html>
1a4d82fc
JJ
221<html lang="en">
222<head>
223 <meta http-equiv="refresh" content="0;URL={url}">
224</head>
225<body>
226 <p>Redirecting to <a href="{url}">{url}</a>...</p>
227 <script>location.replace("{url}" + location.search + location.hash);</script>
228</body>
229</html>"##,
dfeec247 230 url = url,
1a4d82fc
JJ
231 )
232}