]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
Added a `rustdoc` shortcut for collapse/expand all
[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     pub playground_url: String,
23 }
24
25 pub struct Page<'a> {
26     pub title: &'a str,
27     pub ty: &'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)
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}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">
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 {ty}">{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>&larrb;</dt>
101                     <dd>Move up in search results</dd>
102                     <dt>&rarrb;</dt>
103                     <dd>Move down in search results</dd>
104                     <dt>&#9166;</dt>
105                     <dd>Go to active search result</dd>
106                     <dt>+</dt>
107                     <dd>Collapse/expand all sections</dd>
108                 </dl>
109             </div>
110
111             <div class="infos">
112                 <h2>Search Tricks</h2>
113
114                 <p>
115                     Prefix searches with a type followed by a colon (e.g.
116                     <code>fn:</code>) to restrict the search to a given type.
117                 </p>
118
119                 <p>
120                     Accepted types are: <code>fn</code>, <code>mod</code>,
121                     <code>struct</code>, <code>enum</code>,
122                     <code>trait</code>, <code>type</code>, <code>macro</code>,
123                     and <code>const</code>.
124                 </p>
125
126                 <p>
127                     Search functions by type signature (e.g.
128                     <code>vec -> usize</code> or <code>* -> vec</code>)
129                 </p>
130             </div>
131         </div>
132     </aside>
133
134     {after_content}
135
136     <script>
137         window.rootPath = "{root_path}";
138         window.currentCrate = "{krate}";
139         window.playgroundUrl = "{play_url}";
140     </script>
141     <script src="{root_path}jquery.js"></script>
142     <script src="{root_path}main.js"></script>
143     {play_js}
144     <script defer src="{root_path}search-index.js"></script>
145 </body>
146 </html>"##,
147     css_extension = if css_file_extension {
148         format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{root_path}theme.css\">",
149                 root_path = page.root_path)
150     } else {
151         "".to_owned()
152     },
153     content   = *t,
154     root_path = page.root_path,
155     ty        = page.ty,
156     logo      = if layout.logo.is_empty() {
157         "".to_string()
158     } else {
159         format!("<a href='{}{}/index.html'>\
160                  <img src='{}' alt='logo' width='100'></a>",
161                 page.root_path, layout.krate,
162                 layout.logo)
163     },
164     title     = page.title,
165     description = page.description,
166     keywords = page.keywords,
167     favicon   = if layout.favicon.is_empty() {
168         "".to_string()
169     } else {
170         format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
171     },
172     in_header = layout.external_html.in_header,
173     before_content = layout.external_html.before_content,
174     after_content = layout.external_html.after_content,
175     sidebar   = *sidebar,
176     krate     = layout.krate,
177     play_url  = layout.playground_url,
178     play_js   = if layout.playground_url.is_empty() {
179         "".to_string()
180     } else {
181         format!(r#"<script src="{}playpen.js"></script>"#, page.root_path)
182     },
183     )
184 }
185
186 pub fn redirect(dst: &mut io::Write, url: &str) -> io::Result<()> {
187     // <script> triggers a redirect before refresh, so this is fine.
188     write!(dst,
189 r##"<!DOCTYPE html>
190 <html lang="en">
191 <head>
192     <meta http-equiv="refresh" content="0;URL={url}">
193 </head>
194 <body>
195     <p>Redirecting to <a href="{url}">{url}</a>...</p>
196     <script>location.replace("{url}" + location.search + location.hash);</script>
197 </body>
198 </html>"##,
199     url = url,
200     )
201 }