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