]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
Remove inline script tags
[rust.git] / src / librustdoc / html / layout.rs
1 use std::collections::HashMap;
2 use std::path::PathBuf;
3
4 use crate::externalfiles::ExternalHtml;
5 use crate::html::escape::Escape;
6 use crate::html::format::{Buffer, Print};
7 use crate::html::render::{ensure_trailing_slash, StylePath};
8
9 #[derive(Clone)]
10 crate 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,
16     /// The given user css file which allow to customize the generated
17     /// documentation theme.
18     crate css_file_extension: Option<PathBuf>,
19     /// If false, the `select` element to have search filtering by crates on rendered docs
20     /// won't be generated.
21     crate generate_search_filter: bool,
22 }
23
24 crate 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],
34 }
35
36 crate fn render<T: Print, S: Print>(
37     layout: &Layout,
38     page: &Page<'_>,
39     sidebar: S,
40     t: T,
41     style_files: &[StylePath],
42 ) -> String {
43     let static_root_path = page.static_root_path.unwrap_or(page.root_path);
44     format!(
45         "<!DOCTYPE html>\
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>\
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\" \
56           id=\"mainThemeStyle\">\
57     {style_files}\
58     <script id=\"default-settings\"{default_settings}></script>\
59     <script src=\"{static_root_path}storage{suffix}.js\"></script>\
60     <noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
61     {css_extension}\
62     {favicon}\
63     {in_header}\
64     <style type=\"text/css\">\
65     #crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\
66     </style>\
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!\" aria-haspopup=\"menu\">\
83             <img src=\"{static_root_path}brush{suffix}.svg\" \
84                  width=\"18\" \
85                  alt=\"Pick another theme!\">\
86         </button>\
87         <div id=\"theme-choices\" role=\"menu\"></div>\
88     </div>\
89     <script src=\"{static_root_path}theme{suffix}.js\"></script>\
90     <nav class=\"sub\">\
91         <form class=\"search-form\">\
92             <div class=\"search-container\">\
93                 <div>{filter_crates}\
94                     <input class=\"search-input\" name=\"search\" \
95                            disabled \
96                            autocomplete=\"off\" \
97                            spellcheck=\"false\" \
98                            placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
99                            type=\"search\">\
100                 </div>\
101                 <button type=\"button\" class=\"help-button\">?</button>
102                 <a id=\"settings-menu\" href=\"{root_path}settings.html\">\
103                     <img src=\"{static_root_path}wheel{suffix}.svg\" \
104                          width=\"18\" \
105                          alt=\"Change settings\">\
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>\
113     {after_content}\
114     <div id=\"rustdoc-vars\" data-root-path=\"{root_path}\" data-current-crate=\"{krate}\"></div>
115     <script src=\"{static_root_path}main{suffix}.js\"></script>\
116     {static_extra_scripts}\
117     {extra_scripts}\
118     <script defer src=\"{root_path}search-index{suffix}.js\"></script>\
119 </body>\
120 </html>",
121         css_extension = if layout.css_file_extension.is_some() {
122             format!(
123                 "<link rel=\"stylesheet\" \
124                        type=\"text/css\" \
125                        href=\"{static_root_path}theme{suffix}.css\">",
126                 static_root_path = static_root_path,
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'>\
142                      <div class='logo-container rust-logo'>\
143                      <img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>",
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'>\
151                      <div class='logo-container'><img src='{}' alt='logo'></div></a>",
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!(
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">"##,
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         default_settings = layout
176             .default_settings
177             .iter()
178             .map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-', "_"), Escape(v)))
179             .collect::<String>(),
180         style_files = style_files
181             .iter()
182             .filter_map(|t| {
183                 if let Some(stem) = t.path.file_stem() { Some((stem, t.disabled)) } else { None }
184             })
185             .filter_map(|t| {
186                 if let Some(path) = t.0.to_str() { Some((path, t.1)) } else { None }
187             })
188             .map(|t| format!(
189                 r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#,
190                 Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)),
191                 if t.1 { "disabled" } else { "" },
192                 if t.0 == "light" { "id=\"themeStyle\"" } else { "" }
193             ))
194             .collect::<String>(),
195         suffix = page.resource_suffix,
196         static_extra_scripts = page
197             .static_extra_scripts
198             .iter()
199             .map(|e| {
200                 format!(
201                     "<script src=\"{static_root_path}{extra_script}.js\"></script>",
202                     static_root_path = static_root_path,
203                     extra_script = e
204                 )
205             })
206             .collect::<String>(),
207         extra_scripts = page
208             .extra_scripts
209             .iter()
210             .map(|e| {
211                 format!(
212                     "<script src=\"{root_path}{extra_script}.js\"></script>",
213                     root_path = page.root_path,
214                     extra_script = e
215                 )
216             })
217             .collect::<String>(),
218         filter_crates = if layout.generate_search_filter {
219             "<select id=\"crate-search\">\
220                  <option value=\"All crates\">All crates</option>\
221              </select>"
222         } else {
223             ""
224         },
225     )
226 }
227
228 crate fn redirect(url: &str) -> String {
229     // <script> triggers a redirect before refresh, so this is fine.
230     format!(
231         r##"<!DOCTYPE html>
232 <html lang="en">
233 <head>
234     <meta http-equiv="refresh" content="0;URL={url}">
235 </head>
236 <body>
237     <p>Redirecting to <a href="{url}">{url}</a>...</p>
238     <script>location.replace("{url}" + location.search + location.hash);</script>
239 </body>
240 </html>"##,
241         url = url,
242     )
243 }