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