1 use std
::path
::PathBuf
;
3 use rustc_data_structures
::fx
::FxHashMap
;
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}
;
13 crate favicon
: String
,
14 crate external_html
: ExternalHtml
,
15 crate default_settings
: FxHashMap
<String
, 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
,
25 crate struct Page
<'a
> {
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],
37 crate fn render
<T
: Print
, S
: Print
>(
42 style_files
: &[StylePath
],
44 let static_root_path
= page
.static_root_path
.unwrap_or(page
.root_path
);
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\">\
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>\
66 <style type=\"text/css\">\
67 #crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\
70 <body class=\"rustdoc {css_class}\">\
72 <div class=\"warning\">\
73 This old browser is unsupported and will most likely display funky \
78 <nav class=\"sidebar\">\
79 <div class=\"sidebar-menu\" role=\"button\">☰</div>\
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!\">\
89 <div id=\"theme-choices\" role=\"menu\"></div>\
91 <script src=\"{static_root_path}theme{suffix}.js\"></script>\
93 <form class=\"search-form\">\
94 <div class=\"search-container\">\
96 <input class=\"search-input\" name=\"search\" \
98 autocomplete=\"off\" \
99 spellcheck=\"false\" \
100 placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
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\">\
112 <section id=\"main\" class=\"content\">{content}</section>\
113 <section id=\"search\" class=\"content hidden\"></section>\
114 <section class=\"footer\"></section>\
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>\
122 css_extension
= if layout
.css_file_extension
.is_some() {
124 "<link rel=\"stylesheet\" \
126 href=\"{static_root_path}theme{suffix}.css\">",
127 static_root_path
= static_root_path
,
128 suffix
= page
.resource_suffix
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
,
138 if layout
.logo
.is_empty() {
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
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
),
159 description
= page
.description
,
160 keywords
= page
.keywords
,
161 favicon
= if layout
.favicon
.is_empty() {
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
170 format
!(r
#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
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
180 .map(|(k
, v
)| format
!(r
#" data-{}="{}""#, k.replace('-', "_"), Escape(v)))
181 .collect
::<String
>(),
182 style_files
= style_files
185 if let Some(stem
) = t
.path
.file_stem() { Some((stem, t.disabled)) }
else { None }
188 if let Some(path
) = t
.0.to_str() { Some((path, t.1)) }
else { None }
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 { "" }
196 .collect
::<String
>(),
197 suffix
= page
.resource_suffix
,
199 .static_extra_scripts
203 "<script src=\"{static_root_path}{extra_script}.js\"></script>",
204 static_root_path
= static_root_path
,
208 .chain(page
.extra_scripts
.iter().map(|e
| {
210 "<script src=\"{root_path}{extra_script}.js\"></script>",
211 root_path
= page
.root_path
,
215 .collect
::<String
>(),
216 filter_crates
= if layout
.generate_search_filter
{
217 "<select id=\"crate-search\">\
218 <option value=\"All crates\">All crates</option>\
226 crate fn redirect(url
: &str) -> String
{
227 // <script> triggers a redirect before refresh, so this is fine.
232 <meta http-equiv="refresh" content="0;URL={url}">
235 <p>Redirecting to <a href="{url}">{url}</a>...</p>
236 <script>location.replace("{url}" + location.search + location.hash);</script>