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