]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
(doc) Properly doc hotkeys in generated docs.
[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 #[deriving(Clone)]
15 pub struct Layout {
16     pub logo: String,
17     pub favicon: String,
18     pub krate: String,
19     pub playground_url: String,
20 }
21
22 pub struct Page<'a> {
23     pub title: &'a str,
24     pub ty: &'a str,
25     pub root_path: &'a str,
26 }
27
28 pub fn render<T: fmt::Show, S: fmt::Show>(
29     dst: &mut io::Writer, layout: &Layout, page: &Page, sidebar: &S, t: &T)
30     -> io::IoResult<()>
31 {
32     write!(dst,
33 r##"<!DOCTYPE html>
34 <html lang="en">
35 <head>
36     <meta charset="utf-8">
37     <meta name="viewport" content="width=device-width, initial-scale=1.0">
38     <meta name="description" content="The {krate} library documentation.">
39
40     <title>{title}</title>
41
42     <link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:400,600'
43           rel='stylesheet' type='text/css'>
44     <link rel="stylesheet" type="text/css" href="{root_path}main.css">
45
46     {favicon}
47 </head>
48 <body>
49     <!--[if lte IE 8]>
50     <div class="warning">
51         This old browser is unsupported and will most likely display funky
52         things.
53     </div>
54     <![endif]-->
55
56     <section class="sidebar">
57         {logo}
58         {sidebar}
59     </section>
60
61     <nav class="sub">
62         <form class="search-form js-only">
63             <div class="search-container">
64                 <input class="search-input" name="search"
65                        autocomplete="off"
66                        placeholder="Click or press 'S' to search, '?' for more options..."
67                        type="search">
68             </div>
69         </form>
70     </nav>
71
72     <section id='main' class="content {ty}">{content}</section>
73     <section id='search' class="content hidden"></section>
74
75     <section class="footer"></section>
76
77     <div id="help" class="hidden">
78         <div class="shortcuts">
79             <h1>Keyboard shortcuts</h1>
80             <dl>
81                 <dt>?</dt>
82                 <dd>Show this help dialog</dd>
83                 <dt>S</dt>
84                 <dd>Focus the search field</dd>
85                 <dt>&larrb;</dt>
86                 <dd>Move up in search results</dd>
87                 <dt>&rarrb;</dt>
88                 <dd>Move down in search results</dd>
89                 <dt>&#9166;</dt>
90                 <dd>Go to active search result</dd>
91             </dl>
92         </div>
93         <div class="infos">
94             <h1>Search tricks</h1>
95             <p>
96                 Prefix searches with a type followed by a colon (e.g.
97                 <code>fn:</code>) to restrict the search to a given type.
98             </p>
99             <p>
100                 Accepted types are: <code>fn</code>, <code>mod</code>,
101                 <code>struct</code> (or <code>str</code>), <code>enum</code>,
102                 <code>trait</code>, <code>typedef</code> (or
103                 <code>tdef</code>).
104             </p>
105         </div>
106     </div>
107
108     <script>
109         window.rootPath = "{root_path}";
110         window.currentCrate = "{krate}";
111         window.playgroundUrl = "{play_url}";
112     </script>
113     <script src="{root_path}jquery.js"></script>
114     <script src="{root_path}main.js"></script>
115     {play_js}
116     <script async src="{root_path}search-index.js"></script>
117 </body>
118 </html>"##,
119     content   = *t,
120     root_path = page.root_path,
121     ty        = page.ty,
122     logo      = if layout.logo.len() == 0 {
123         "".to_string()
124     } else {
125         format!("<a href='{}{}/index.html'>\
126                  <img src='{}' alt='' width='100'></a>",
127                 page.root_path, layout.krate,
128                 layout.logo)
129     },
130     title     = page.title,
131     favicon   = if layout.favicon.len() == 0 {
132         "".to_string()
133     } else {
134         format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
135     },
136     sidebar   = *sidebar,
137     krate     = layout.krate,
138     play_url  = layout.playground_url,
139     play_js   = if layout.playground_url.len() == 0 {
140         "".to_string()
141     } else {
142         format!(r#"<script src="{}playpen.js"></script>"#, page.root_path)
143     },
144     )
145 }
146
147 pub fn redirect(dst: &mut io::Writer, url: &str) -> io::IoResult<()> {
148     write!(dst,
149 r##"<!DOCTYPE html>
150 <html lang="en">
151 <head>
152     <meta http-equiv="refresh" content="0;URL={url}">
153 </head>
154 <body>
155 </body>
156 </html>"##,
157     url = url,
158     )
159 }