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