]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/html/layout.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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::prelude::*;
13 use std::io;
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 pub playground_url: String,
24 }
25
26 pub struct Page<'a> {
27 pub title: &'a str,
28 pub ty: &'a str,
29 pub root_path: &'a str,
30 pub description: &'a str,
31 pub keywords: &'a str,
32 }
33
34 pub fn render<T: fmt::Display, S: fmt::Display>(
35 dst: &mut io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
36 css_file_extension: bool)
37 -> io::Result<()>
38 {
39 write!(dst,
40 r##"<!DOCTYPE html>
41 <html lang="en">
42 <head>
43 <meta charset="utf-8">
44 <meta name="viewport" content="width=device-width, initial-scale=1.0">
45 <meta name="generator" content="rustdoc">
46 <meta name="description" content="{description}">
47 <meta name="keywords" content="{keywords}">
48
49 <title>{title}</title>
50
51 <link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css">
52 <link rel="stylesheet" type="text/css" href="{root_path}main.css">
53 {css_extension}
54
55 {favicon}
56 {in_header}
57 </head>
58 <body class="rustdoc">
59 <!--[if lte IE 8]>
60 <div class="warning">
61 This old browser is unsupported and will most likely display funky
62 things.
63 </div>
64 <![endif]-->
65
66 {before_content}
67
68 <nav class="sidebar">
69 {logo}
70 {sidebar}
71 </nav>
72
73 <nav class="sub">
74 <form class="search-form js-only">
75 <div class="search-container">
76 <input class="search-input" name="search"
77 autocomplete="off"
78 placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
79 type="search">
80 </div>
81 </form>
82 </nav>
83
84 <section id='main' class="content {ty}">{content}</section>
85 <section id='search' class="content hidden"></section>
86
87 <section class="footer"></section>
88
89 <aside id="help" class="hidden">
90 <div>
91 <h1 class="hidden">Help</h1>
92
93 <div class="shortcuts">
94 <h2>Keyboard Shortcuts</h2>
95
96 <dl>
97 <dt>?</dt>
98 <dd>Show this help dialog</dd>
99 <dt>S</dt>
100 <dd>Focus the search field</dd>
101 <dt>&larrb;</dt>
102 <dd>Move up in search results</dd>
103 <dt>&rarrb;</dt>
104 <dd>Move down in search results</dd>
105 <dt>&#9166;</dt>
106 <dd>Go to active search result</dd>
107 </dl>
108 </div>
109
110 <div class="infos">
111 <h2>Search Tricks</h2>
112
113 <p>
114 Prefix searches with a type followed by a colon (e.g.
115 <code>fn:</code>) to restrict the search to a given type.
116 </p>
117
118 <p>
119 Accepted types are: <code>fn</code>, <code>mod</code>,
120 <code>struct</code>, <code>enum</code>,
121 <code>trait</code>, <code>type</code>, <code>macro</code>,
122 and <code>const</code>.
123 </p>
124
125 <p>
126 Search functions by type signature (e.g.
127 <code>vec -> usize</code> or <code>* -> vec</code>)
128 </p>
129 </div>
130 </div>
131 </aside>
132
133 {after_content}
134
135 <script>
136 window.rootPath = "{root_path}";
137 window.currentCrate = "{krate}";
138 window.playgroundUrl = "{play_url}";
139 </script>
140 <script src="{root_path}jquery.js"></script>
141 <script src="{root_path}main.js"></script>
142 {play_js}
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 ty = page.ty,
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 play_url = layout.playground_url,
177 play_js = if layout.playground_url.is_empty() {
178 "".to_string()
179 } else {
180 format!(r#"<script src="{}playpen.js"></script>"#, page.root_path)
181 },
182 )
183 }
184
185 pub fn redirect(dst: &mut io::Write, url: &str) -> io::Result<()> {
186 // <script> triggers a redirect before refresh, so this is fine.
187 write!(dst,
188 r##"<!DOCTYPE html>
189 <html lang="en">
190 <head>
191 <meta http-equiv="refresh" content="0;URL={url}">
192 </head>
193 <body>
194 <p>Redirecting to <a href="{url}">{url}</a>...</p>
195 <script>location.replace("{url}" + location.search + location.hash);</script>
196 </body>
197 </html>"##,
198 url = url,
199 )
200 }