]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
Add alt tags for logos
[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::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     -> 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
53     {favicon}
54     {in_header}
55 </head>
56 <body class="rustdoc">
57     <!--[if lte IE 8]>
58     <div class="warning">
59         This old browser is unsupported and will most likely display funky
60         things.
61     </div>
62     <![endif]-->
63
64     {before_content}
65
66     <nav class="sidebar">
67         {logo}
68         {sidebar}
69     </nav>
70
71     <nav class="sub">
72         <form class="search-form js-only">
73             <div class="search-container">
74                 <input class="search-input" name="search"
75                        autocomplete="off"
76                        placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
77                        type="search">
78             </div>
79         </form>
80     </nav>
81
82     <section id='main' class="content {ty}">{content}</section>
83     <section id='search' class="content hidden"></section>
84
85     <section class="footer"></section>
86
87     <aside id="help" class="hidden">
88         <div>
89             <h1 class="hidden">Help</h1>
90
91             <div class="shortcuts">
92                 <h2>Keyboard Shortcuts</h2>
93
94                 <dl>
95                     <dt>?</dt>
96                     <dd>Show this help dialog</dd>
97                     <dt>S</dt>
98                     <dd>Focus the search field</dd>
99                     <dt>&larrb;</dt>
100                     <dd>Move up in search results</dd>
101                     <dt>&rarrb;</dt>
102                     <dd>Move down in search results</dd>
103                     <dt>&#9166;</dt>
104                     <dd>Go to active search result</dd>
105                 </dl>
106             </div>
107
108             <div class="infos">
109                 <h2>Search Tricks</h2>
110
111                 <p>
112                     Prefix searches with a type followed by a colon (e.g.
113                     <code>fn:</code>) to restrict the search to a given type.
114                 </p>
115
116                 <p>
117                     Accepted types are: <code>fn</code>, <code>mod</code>,
118                     <code>struct</code>, <code>enum</code>,
119                     <code>trait</code>, <code>type</code>, <code>macro</code>,
120                     and <code>const</code>.
121                 </p>
122
123                 <p>
124                     Search functions by type signature (e.g.
125                     <code>vec -> usize</code>)
126                 </p>
127             </div>
128         </div>
129     </aside>
130
131     {after_content}
132
133     <script>
134         window.rootPath = "{root_path}";
135         window.currentCrate = "{krate}";
136         window.playgroundUrl = "{play_url}";
137     </script>
138     <script src="{root_path}jquery.js"></script>
139     <script src="{root_path}main.js"></script>
140     {play_js}
141     <script defer src="{root_path}search-index.js"></script>
142 </body>
143 </html>"##,
144     content   = *t,
145     root_path = page.root_path,
146     ty        = page.ty,
147     logo      = if layout.logo.is_empty() {
148         "".to_string()
149     } else {
150         format!("<a href='{}{}/index.html'>\
151                  <img src='{}' alt='logo' width='100'></a>",
152                 page.root_path, layout.krate,
153                 layout.logo)
154     },
155     title     = page.title,
156     description = page.description,
157     keywords = page.keywords,
158     favicon   = if layout.favicon.is_empty() {
159         "".to_string()
160     } else {
161         format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
162     },
163     in_header = layout.external_html.in_header,
164     before_content = layout.external_html.before_content,
165     after_content = layout.external_html.after_content,
166     sidebar   = *sidebar,
167     krate     = layout.krate,
168     play_url  = layout.playground_url,
169     play_js   = if layout.playground_url.is_empty() {
170         "".to_string()
171     } else {
172         format!(r#"<script src="{}playpen.js"></script>"#, page.root_path)
173     },
174     )
175 }
176
177 pub fn redirect(dst: &mut io::Write, url: &str) -> io::Result<()> {
178     // <script> triggers a redirect before refresh, so this is fine.
179     write!(dst,
180 r##"<!DOCTYPE html>
181 <html lang="en">
182 <head>
183     <meta http-equiv="refresh" content="0;URL={url}">
184 </head>
185 <body>
186     <p>Redirecting to <a href="{url}">{url}</a>...</p>
187     <script>location.replace("{url}" + location.search + location.hash);</script>
188 </body>
189 </html>"##,
190     url = url,
191     )
192 }