]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
Auto merge of #45359 - arielb1:escaping-borrow, r=eddyb
[rust.git] / src / librustdoc / html / layout.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::fmt;
12 use std::io;
13
14 use externalfiles::ExternalHtml;
15
16 #[derive(Clone)]
17 pub struct Layout {
18     pub logo: String,
19     pub favicon: String,
20     pub external_html: ExternalHtml,
21     pub krate: String,
22 }
23
24 pub struct Page<'a> {
25     pub title: &'a str,
26     pub css_class: &'a str,
27     pub root_path: &'a str,
28     pub description: &'a str,
29     pub keywords: &'a str,
30 }
31
32 pub fn render<T: fmt::Display, S: fmt::Display>(
33     dst: &mut io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
34     css_file_extension: bool)
35     -> io::Result<()>
36 {
37     write!(dst,
38 r##"<!DOCTYPE html>
39 <html lang="en">
40 <head>
41     <meta charset="utf-8">
42     <meta name="viewport" content="width=device-width, initial-scale=1.0">
43     <meta name="generator" content="rustdoc">
44     <meta name="description" content="{description}">
45     <meta name="keywords" content="{keywords}">
46
47     <title>{title}</title>
48
49     <link rel="stylesheet" type="text/css" href="{root_path}normalize.css">
50     <link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css">
51     <link rel="stylesheet" type="text/css" href="{root_path}main.css">
52     {css_extension}
53
54     {favicon}
55     {in_header}
56 </head>
57 <body class="rustdoc {css_class}">
58     <!--[if lte IE 8]>
59     <div class="warning">
60         This old browser is unsupported and will most likely display funky
61         things.
62     </div>
63     <![endif]-->
64
65     {before_content}
66
67     <nav class="sidebar">
68         {logo}
69         {sidebar}
70     </nav>
71
72     <nav class="sub">
73         <form class="search-form js-only">
74             <div class="search-container">
75                 <input class="search-input" name="search"
76                        autocomplete="off"
77                        placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
78                        type="search">
79             </div>
80         </form>
81     </nav>
82
83     <section id='main' class="content">{content}</section>
84     <section id='search' class="content hidden"></section>
85
86     <section class="footer"></section>
87
88     <aside id="help" class="hidden">
89         <div>
90             <h1 class="hidden">Help</h1>
91
92             <div class="shortcuts">
93                 <h2>Keyboard Shortcuts</h2>
94
95                 <dl>
96                     <dt>?</dt>
97                     <dd>Show this help dialog</dd>
98                     <dt>S</dt>
99                     <dd>Focus the search field</dd>
100                     <dt>↑</dt>
101                     <dd>Move up in search results</dd>
102                     <dt>↓</dt>
103                     <dd>Move down in search results</dd>
104                     <dt>↹</dt>
105                     <dd>Switch tab</dd>
106                     <dt>&#9166;</dt>
107                     <dd>Go to active search result</dd>
108                     <dt>+</dt>
109                     <dd>Collapse/expand all sections</dd>
110                 </dl>
111             </div>
112
113             <div class="infos">
114                 <h2>Search Tricks</h2>
115
116                 <p>
117                     Prefix searches with a type followed by a colon (e.g.
118                     <code>fn:</code>) to restrict the search to a given type.
119                 </p>
120
121                 <p>
122                     Accepted types are: <code>fn</code>, <code>mod</code>,
123                     <code>struct</code>, <code>enum</code>,
124                     <code>trait</code>, <code>type</code>, <code>macro</code>,
125                     and <code>const</code>.
126                 </p>
127
128                 <p>
129                     Search functions by type signature (e.g.
130                     <code>vec -> usize</code> or <code>* -> vec</code>)
131                 </p>
132             </div>
133         </div>
134     </aside>
135
136     {after_content}
137
138     <script>
139         window.rootPath = "{root_path}";
140         window.currentCrate = "{krate}";
141     </script>
142     <script src="{root_path}main.js"></script>
143     <script defer src="{root_path}search-index.js"></script>
144 </body>
145 </html>"##,
146     css_extension = if css_file_extension {
147         format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme.css\">",
148                 root_path = page.root_path)
149     } else {
150         "".to_owned()
151     },
152     content   = *t,
153     root_path = page.root_path,
154     css_class = page.css_class,
155     logo      = if layout.logo.is_empty() {
156         "".to_string()
157     } else {
158         format!("<a href='{}{}/index.html'>\
159                  <img src='{}' alt='logo' width='100'></a>",
160                 page.root_path, layout.krate,
161                 layout.logo)
162     },
163     title     = page.title,
164     description = page.description,
165     keywords = page.keywords,
166     favicon   = if layout.favicon.is_empty() {
167         "".to_string()
168     } else {
169         format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
170     },
171     in_header = layout.external_html.in_header,
172     before_content = layout.external_html.before_content,
173     after_content = layout.external_html.after_content,
174     sidebar   = *sidebar,
175     krate     = layout.krate,
176     )
177 }
178
179 pub fn redirect(dst: &mut io::Write, url: &str) -> io::Result<()> {
180     // <script> triggers a redirect before refresh, so this is fine.
181     write!(dst,
182 r##"<!DOCTYPE html>
183 <html lang="en">
184 <head>
185     <meta http-equiv="refresh" content="0;URL={url}">
186 </head>
187 <body>
188     <p>Redirecting to <a href="{url}">{url}</a>...</p>
189     <script>location.replace("{url}" + location.search + location.hash);</script>
190 </body>
191 </html>"##,
192     url = url,
193     )
194 }