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