]>
Commit | Line | Data |
---|---|---|
2c00a5a8 | 1 | use std::path::PathBuf; |
1a4d82fc | 2 | |
9fa01778 | 3 | use crate::externalfiles::ExternalHtml; |
60c5eb7d | 4 | use crate::html::escape::Escape; |
e1599b0c | 5 | use crate::html::format::{Buffer, Print}; |
3dfed10e | 6 | use crate::html::render::{ensure_trailing_slash, StylePath}; |
1a4d82fc JJ |
7 | |
8 | #[derive(Clone)] | |
9 | pub 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 | ||
22 | pub 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 | 34 | pub fn render<T: Print, S: Print>( |
0731742a | 35 | layout: &Layout, |
9fa01778 | 36 | page: &Page<'_>, |
e1599b0c XL |
37 | sidebar: S, |
38 | t: T, | |
3dfed10e | 39 | style_files: &[StylePath], |
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 | 54 | id=\"mainThemeStyle\">\ |
3dfed10e | 55 | {style_files}\ |
0731742a XL |
56 | <script src=\"{static_root_path}storage{suffix}.js\"></script>\ |
57 | <noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\ | |
83c7162d XL |
58 | {css_extension}\ |
59 | {favicon}\ | |
60 | {in_header}\ | |
0731742a XL |
61 | <style type=\"text/css\">\ |
62 | #crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\ | |
63 | </style>\ | |
83c7162d XL |
64 | </head>\ |
65 | <body class=\"rustdoc {css_class}\">\ | |
66 | <!--[if lte IE 8]>\ | |
67 | <div class=\"warning\">\ | |
68 | This old browser is unsupported and will most likely display funky \ | |
69 | things.\ | |
70 | </div>\ | |
71 | <![endif]-->\ | |
72 | {before_content}\ | |
73 | <nav class=\"sidebar\">\ | |
74 | <div class=\"sidebar-menu\">☰</div>\ | |
75 | {logo}\ | |
76 | {sidebar}\ | |
77 | </nav>\ | |
78 | <div class=\"theme-picker\">\ | |
79 | <button id=\"theme-picker\" aria-label=\"Pick another theme!\">\ | |
0731742a XL |
80 | <img src=\"{static_root_path}brush{suffix}.svg\" \ |
81 | width=\"18\" \ | |
82 | alt=\"Pick another theme!\">\ | |
83c7162d XL |
83 | </button>\ |
84 | <div id=\"theme-choices\"></div>\ | |
85 | </div>\ | |
0731742a | 86 | <script src=\"{static_root_path}theme{suffix}.js\"></script>\ |
83c7162d | 87 | <nav class=\"sub\">\ |
60c5eb7d | 88 | <form class=\"search-form\">\ |
83c7162d | 89 | <div class=\"search-container\">\ |
0731742a XL |
90 | <div>{filter_crates}\ |
91 | <input class=\"search-input\" name=\"search\" \ | |
60c5eb7d | 92 | disabled \ |
0731742a XL |
93 | autocomplete=\"off\" \ |
94 | spellcheck=\"false\" \ | |
95 | placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \ | |
96 | type=\"search\">\ | |
97 | </div>\ | |
3dfed10e | 98 | <span class=\"help-button\">?</span> |
83c7162d | 99 | <a id=\"settings-menu\" href=\"{root_path}settings.html\">\ |
0731742a XL |
100 | <img src=\"{static_root_path}wheel{suffix}.svg\" \ |
101 | width=\"18\" \ | |
102 | alt=\"Change settings\">\ | |
83c7162d XL |
103 | </a>\ |
104 | </div>\ | |
105 | </form>\ | |
106 | </nav>\ | |
107 | <section id=\"main\" class=\"content\">{content}</section>\ | |
108 | <section id=\"search\" class=\"content hidden\"></section>\ | |
109 | <section class=\"footer\"></section>\ | |
83c7162d XL |
110 | {after_content}\ |
111 | <script>\ | |
112 | window.rootPath = \"{root_path}\";\ | |
113 | window.currentCrate = \"{krate}\";\ | |
114 | </script>\ | |
0731742a XL |
115 | <script src=\"{static_root_path}main{suffix}.js\"></script>\ |
116 | {static_extra_scripts}\ | |
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 = { | |
137 | let p = format!("{}{}", page.root_path, layout.krate); | |
138 | let p = ensure_trailing_slash(&p); | |
139 | if layout.logo.is_empty() { | |
140 | format!( | |
141 | "<a href='{path}index.html'>\ | |
3dfed10e | 142 | <div class='logo-container rust-logo'>\ |
48663c56 | 143 | <img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>", |
dfeec247 XL |
144 | path = p, |
145 | static_root_path = static_root_path, | |
146 | suffix = page.resource_suffix | |
147 | ) | |
148 | } else { | |
149 | format!( | |
150 | "<a href='{}index.html'>\ | |
48663c56 | 151 | <div class='logo-container'><img src='{}' alt='logo'></div></a>", |
dfeec247 XL |
152 | p, layout.logo |
153 | ) | |
154 | } | |
155 | }, | |
156 | title = page.title, | |
157 | description = page.description, | |
158 | keywords = page.keywords, | |
159 | favicon = if layout.favicon.is_empty() { | |
160 | format!( | |
1b1a35ee XL |
161 | r##"<link rel="icon" type="image/svg+xml" href="{static_root_path}favicon{suffix}.svg"> |
162 | <link rel="alternate icon" type="image/png" href="{static_root_path}favicon-16x16{suffix}.png"> | |
163 | <link rel="alternate icon" type="image/png" href="{static_root_path}favicon-32x32{suffix}.png">"##, | |
dfeec247 XL |
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, | |
3dfed10e | 175 | style_files = style_files |
dfeec247 | 176 | .iter() |
3dfed10e XL |
177 | .filter_map(|t| { |
178 | if let Some(stem) = t.path.file_stem() { Some((stem, t.disabled)) } else { None } | |
179 | }) | |
180 | .filter_map(|t| { | |
181 | if let Some(path) = t.0.to_str() { Some((path, t.1)) } else { None } | |
182 | }) | |
dfeec247 | 183 | .map(|t| format!( |
3dfed10e XL |
184 | r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#, |
185 | Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)), | |
186 | if t.1 { "disabled" } else { "" }, | |
187 | if t.0 == "light" { "id=\"themeStyle\"" } else { "" } | |
dfeec247 XL |
188 | )) |
189 | .collect::<String>(), | |
190 | suffix = page.resource_suffix, | |
191 | static_extra_scripts = page | |
192 | .static_extra_scripts | |
193 | .iter() | |
194 | .map(|e| { | |
195 | format!( | |
196 | "<script src=\"{static_root_path}{extra_script}.js\"></script>", | |
197 | static_root_path = static_root_path, | |
198 | extra_script = e | |
199 | ) | |
200 | }) | |
201 | .collect::<String>(), | |
202 | extra_scripts = page | |
203 | .extra_scripts | |
204 | .iter() | |
205 | .map(|e| { | |
206 | format!( | |
207 | "<script src=\"{root_path}{extra_script}.js\"></script>", | |
208 | root_path = page.root_path, | |
209 | extra_script = e | |
210 | ) | |
211 | }) | |
212 | .collect::<String>(), | |
213 | filter_crates = if layout.generate_search_filter { | |
214 | "<select id=\"crate-search\">\ | |
1b1a35ee XL |
215 | <option value=\"All crates\">All crates</option>\ |
216 | </select>" | |
dfeec247 XL |
217 | } else { |
218 | "" | |
219 | }, | |
1a4d82fc JJ |
220 | ) |
221 | } | |
222 | ||
e1599b0c | 223 | pub fn redirect(url: &str) -> String { |
1a4d82fc | 224 | // <script> triggers a redirect before refresh, so this is fine. |
e1599b0c | 225 | format!( |
dfeec247 | 226 | r##"<!DOCTYPE html> |
1a4d82fc JJ |
227 | <html lang="en"> |
228 | <head> | |
229 | <meta http-equiv="refresh" content="0;URL={url}"> | |
230 | </head> | |
231 | <body> | |
232 | <p>Redirecting to <a href="{url}">{url}</a>...</p> | |
233 | <script>location.replace("{url}" + location.search + location.hash);</script> | |
234 | </body> | |
235 | </html>"##, | |
dfeec247 | 236 | url = url, |
1a4d82fc JJ |
237 | ) |
238 | } |