]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
Auto merge of #66392 - estebank:trait-alias-ice, r=eddyb
[rust.git] / src / librustdoc / html / layout.rs
1 use std::path::PathBuf;
2
3 use crate::externalfiles::ExternalHtml;
4 use crate::html::escape::Escape;
5 use crate::html::render::ensure_trailing_slash;
6 use crate::html::format::{Buffer, Print};
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,
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,
20 }
21
22 pub struct Page<'a> {
23     pub title: &'a str,
24     pub css_class: &'a str,
25     pub root_path: &'a str,
26     pub static_root_path: Option<&'a str>,
27     pub description: &'a str,
28     pub keywords: &'a str,
29     pub resource_suffix: &'a str,
30     pub extra_scripts: &'a [&'a str],
31     pub static_extra_scripts: &'a [&'a str],
32 }
33
34 pub fn render<T: Print, S: Print>(
35     layout: &Layout,
36     page: &Page<'_>,
37     sidebar: S,
38     t: T,
39     themes: &[PathBuf],
40 ) -> String {
41     let static_root_path = page.static_root_path.unwrap_or(page.root_path);
42     format!("<!DOCTYPE html>\
43 <html lang=\"en\">\
44 <head>\
45     <meta charset=\"utf-8\">\
46     <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
47     <meta name=\"generator\" content=\"rustdoc\">\
48     <meta name=\"description\" content=\"{description}\">\
49     <meta name=\"keywords\" content=\"{keywords}\">\
50     <title>{title}</title>\
51     <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
52     <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
53           id=\"mainThemeStyle\">\
54     {themes}\
55     <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}dark{suffix}.css\">\
56     <link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}light{suffix}.css\" \
57           id=\"themeStyle\">\
58     <script src=\"{static_root_path}storage{suffix}.js\"></script>\
59     <noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
60     {css_extension}\
61     {favicon}\
62     {in_header}\
63     <style type=\"text/css\">\
64     #crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\
65     </style>\
66 </head>\
67 <body class=\"rustdoc {css_class}\">\
68     <!--[if lte IE 8]>\
69     <div class=\"warning\">\
70         This old browser is unsupported and will most likely display funky \
71         things.\
72     </div>\
73     <![endif]-->\
74     {before_content}\
75     <nav class=\"sidebar\">\
76         <div class=\"sidebar-menu\">&#9776;</div>\
77         {logo}\
78         {sidebar}\
79     </nav>\
80     <div class=\"theme-picker\">\
81         <button id=\"theme-picker\" aria-label=\"Pick another theme!\">\
82             <img src=\"{static_root_path}brush{suffix}.svg\" \
83                  width=\"18\" \
84                  alt=\"Pick another theme!\">\
85         </button>\
86         <div id=\"theme-choices\"></div>\
87     </div>\
88     <script src=\"{static_root_path}theme{suffix}.js\"></script>\
89     <nav class=\"sub\">\
90         <form class=\"search-form js-only\">\
91             <div class=\"search-container\">\
92                 <div>{filter_crates}\
93                     <input class=\"search-input\" name=\"search\" \
94                            autocomplete=\"off\" \
95                            spellcheck=\"false\" \
96                            placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
97                            type=\"search\">\
98                 </div>\
99                 <a id=\"settings-menu\" href=\"{root_path}settings.html\">\
100                     <img src=\"{static_root_path}wheel{suffix}.svg\" \
101                          width=\"18\" \
102                          alt=\"Change settings\">\
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>\
110     {after_content}\
111     <script>\
112         window.rootPath = \"{root_path}\";\
113         window.currentCrate = \"{krate}\";\
114     </script>\
115     <script src=\"{root_path}aliases{suffix}.js\"></script>\
116     <script src=\"{static_root_path}main{suffix}.js\"></script>\
117     {static_extra_scripts}\
118     {extra_scripts}\
119     <script defer src=\"{root_path}search-index{suffix}.js\"></script>\
120 </body>\
121 </html>",
122     css_extension = if layout.css_file_extension.is_some() {
123         format!("<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     } else {
129         String::new()
130     },
131     content   = Buffer::html().to_display(t),
132     static_root_path = static_root_path,
133     root_path = page.root_path,
134     css_class = page.css_class,
135     logo      = {
136         let p = format!("{}{}", page.root_path, layout.krate);
137         let p = ensure_trailing_slash(&p);
138         if layout.logo.is_empty() {
139             format!("<a href='{path}index.html'>\
140                      <div class='logo-container'>\
141                      <img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>",
142                     path=p,
143                     static_root_path=static_root_path,
144                     suffix=page.resource_suffix)
145         } else {
146             format!("<a href='{}index.html'>\
147                      <div class='logo-container'><img src='{}' alt='logo'></div></a>",
148                     p,
149                     layout.logo)
150         }
151     },
152     title     = page.title,
153     description = page.description,
154     keywords = page.keywords,
155     favicon   = if layout.favicon.is_empty() {
156         format!(r#"<link rel="shortcut icon" href="{static_root_path}favicon{suffix}.ico">"#,
157                 static_root_path=static_root_path,
158                 suffix=page.resource_suffix)
159     } else {
160         format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
161     },
162     in_header = layout.external_html.in_header,
163     before_content = layout.external_html.before_content,
164     after_content = layout.external_html.after_content,
165     sidebar   = Buffer::html().to_display(sidebar),
166     krate     = layout.krate,
167     themes = themes.iter()
168                    .filter_map(|t| t.file_stem())
169                    .filter_map(|t| t.to_str())
170                    .map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}.css">"#,
171                                     Escape(&format!("{}{}{}",
172                                                     static_root_path,
173                                                     t,
174                                                     page.resource_suffix))))
175                    .collect::<String>(),
176     suffix=page.resource_suffix,
177     static_extra_scripts=page.static_extra_scripts.iter().map(|e| {
178         format!("<script src=\"{static_root_path}{extra_script}.js\"></script>",
179                 static_root_path=static_root_path,
180                 extra_script=e)
181     }).collect::<String>(),
182     extra_scripts=page.extra_scripts.iter().map(|e| {
183         format!("<script src=\"{root_path}{extra_script}.js\"></script>",
184                 root_path=page.root_path,
185                 extra_script=e)
186     }).collect::<String>(),
187     filter_crates=if layout.generate_search_filter {
188         "<select id=\"crate-search\">\
189             <option value=\"All crates\">All crates</option>\
190         </select>"
191     } else {
192         ""
193     },
194     )
195 }
196
197 pub fn redirect(url: &str) -> String {
198     // <script> triggers a redirect before refresh, so this is fine.
199     format!(
200 r##"<!DOCTYPE html>
201 <html lang="en">
202 <head>
203     <meta http-equiv="refresh" content="0;URL={url}">
204 </head>
205 <body>
206     <p>Redirecting to <a href="{url}">{url}</a>...</p>
207     <script>location.replace("{url}" + location.search + location.hash);</script>
208 </body>
209 </html>"##,
210     url = url,
211     )
212 }