]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
Refactor away `inferred_obligations` from the trait selector
[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 use std::path::PathBuf;
14
15 use externalfiles::ExternalHtml;
16
17 #[derive(Clone)]
18 pub struct Layout {
19     pub logo: String,
20     pub favicon: String,
21     pub external_html: ExternalHtml,
22     pub krate: String,
23 }
24
25 pub struct Page<'a> {
26     pub title: &'a str,
27     pub css_class: &'a str,
28     pub root_path: &'a str,
29     pub description: &'a str,
30     pub keywords: &'a str,
31 }
32
33 pub fn render<T: fmt::Display, S: fmt::Display>(
34     dst: &mut io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
35     css_file_extension: bool, themes: &[PathBuf])
36     -> io::Result<()>
37 {
38     write!(dst,
39 r##"<!DOCTYPE html>
40 <html lang="en">
41 <head>
42     <meta charset="utf-8">
43     <meta name="viewport" content="width=device-width, initial-scale=1.0">
44     <meta name="generator" content="rustdoc">
45     <meta name="description" content="{description}">
46     <meta name="keywords" content="{keywords}">
47
48     <title>{title}</title>
49
50     <link rel="stylesheet" type="text/css" href="{root_path}normalize.css">
51     <link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css" id="mainThemeStyle">
52     {themes}
53     <link rel="stylesheet" type="text/css" href="{root_path}dark.css">
54     <link rel="stylesheet" type="text/css" href="{root_path}main.css" id="themeStyle">
55     <script src="{root_path}storage.js"></script>
56     {css_extension}
57
58     {favicon}
59     {in_header}
60 </head>
61 <body class="rustdoc {css_class}">
62     <!--[if lte IE 8]>
63     <div class="warning">
64         This old browser is unsupported and will most likely display funky
65         things.
66     </div>
67     <![endif]-->
68
69     {before_content}
70
71     <nav class="sidebar">
72         <div class="sidebar-menu">&#9776;</div>
73         {logo}
74         {sidebar}
75     </nav>
76
77     <div class="theme-picker">
78         <button id="theme-picker" aria-label="Pick another theme!">
79             <img src="{root_path}brush.svg" width="18" alt="Pick another theme!">
80         </button>
81         <div id="theme-choices"></div>
82     </div>
83     <script src="{root_path}theme.js"></script>
84     <nav class="sub">
85         <form class="search-form js-only">
86             <div class="search-container">
87                 <input class="search-input" name="search"
88                        autocomplete="off"
89                        placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
90                        type="search">
91             </div>
92         </form>
93     </nav>
94
95     <section id='main' class="content">{content}</section>
96     <section id='search' class="content hidden"></section>
97
98     <section class="footer"></section>
99
100     <aside id="help" class="hidden">
101         <div>
102             <h1 class="hidden">Help</h1>
103
104             <div class="shortcuts">
105                 <h2>Keyboard Shortcuts</h2>
106
107                 <dl>
108                     <dt><kbd>?</kbd></dt>
109                     <dd>Show this help dialog</dd>
110                     <dt><kbd>S</kbd></dt>
111                     <dd>Focus the search field</dd>
112                     <dt><kbd>↑</kbd></dt>
113                     <dd>Move up in search results</dd>
114                     <dt><kbd>↓</kbd></dt>
115                     <dd>Move down in search results</dd>
116                     <dt><kbd>↹</kbd></dt>
117                     <dd>Switch tab</dd>
118                     <dt><kbd>&#9166;</kbd></dt>
119                     <dd>Go to active search result</dd>
120                     <dt><kbd>+</kbd></dt>
121                     <dd>Expand all sections</dd>
122                     <dt><kbd>-</kbd></dt>
123                     <dd>Collapse all sections</dd>
124                 </dl>
125             </div>
126
127             <div class="infos">
128                 <h2>Search Tricks</h2>
129
130                 <p>
131                     Prefix searches with a type followed by a colon (e.g.
132                     <code>fn:</code>) to restrict the search to a given type.
133                 </p>
134
135                 <p>
136                     Accepted types are: <code>fn</code>, <code>mod</code>,
137                     <code>struct</code>, <code>enum</code>,
138                     <code>trait</code>, <code>type</code>, <code>macro</code>,
139                     and <code>const</code>.
140                 </p>
141
142                 <p>
143                     Search functions by type signature (e.g.
144                     <code>vec -> usize</code> or <code>* -> vec</code>)
145                 </p>
146             </div>
147         </div>
148     </aside>
149
150     {after_content}
151
152     <script>
153         window.rootPath = "{root_path}";
154         window.currentCrate = "{krate}";
155     </script>
156     <script src="{root_path}main.js"></script>
157     <script defer src="{root_path}search-index.js"></script>
158 </body>
159 </html>"##,
160     css_extension = if css_file_extension {
161         format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme.css\">",
162                 root_path = page.root_path)
163     } else {
164         "".to_owned()
165     },
166     content   = *t,
167     root_path = page.root_path,
168     css_class = page.css_class,
169     logo      = if layout.logo.is_empty() {
170         "".to_string()
171     } else {
172         format!("<a href='{}{}/index.html'>\
173                  <img src='{}' alt='logo' width='100'></a>",
174                 page.root_path, layout.krate,
175                 layout.logo)
176     },
177     title     = page.title,
178     description = page.description,
179     keywords = page.keywords,
180     favicon   = if layout.favicon.is_empty() {
181         "".to_string()
182     } else {
183         format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
184     },
185     in_header = layout.external_html.in_header,
186     before_content = layout.external_html.before_content,
187     after_content = layout.external_html.after_content,
188     sidebar   = *sidebar,
189     krate     = layout.krate,
190     themes = themes.iter()
191                    .filter_map(|t| t.file_stem())
192                    .filter_map(|t| t.to_str())
193                    .map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}">"#,
194                                     page.root_path, t))
195                    .collect::<String>(),
196     )
197 }
198
199 pub fn redirect(dst: &mut io::Write, url: &str) -> io::Result<()> {
200     // <script> triggers a redirect before refresh, so this is fine.
201     write!(dst,
202 r##"<!DOCTYPE html>
203 <html lang="en">
204 <head>
205     <meta http-equiv="refresh" content="0;URL={url}">
206 </head>
207 <body>
208     <p>Redirecting to <a href="{url}">{url}</a>...</p>
209     <script>location.replace("{url}" + location.search + location.hash);</script>
210 </body>
211 </html>"##,
212     url = url,
213     )
214 }