2013-09-18 22:18:38 -07:00
|
|
|
|
use std::fmt;
|
2018-01-23 00:45:34 +01:00
|
|
|
|
use std::path::PathBuf;
|
2013-09-18 22:18:38 -07:00
|
|
|
|
|
2019-02-23 16:40:07 +09:00
|
|
|
|
use crate::externalfiles::ExternalHtml;
|
|
|
|
|
use crate::html::render::SlashChecker;
|
2019-08-30 10:24:42 -04:00
|
|
|
|
use crate::html::format::Buffer;
|
2019-01-31 15:42:45 +01:00
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
|
#[derive(Clone)]
|
2013-09-18 22:18:38 -07:00
|
|
|
|
pub struct Layout {
|
2014-05-22 16:57:53 -07:00
|
|
|
|
pub logo: String,
|
|
|
|
|
pub favicon: String,
|
2014-06-28 03:35:25 -07:00
|
|
|
|
pub external_html: ExternalHtml,
|
2014-05-22 16:57:53 -07:00
|
|
|
|
pub krate: String,
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
|
pub struct Page<'a> {
|
2014-03-28 10:27:24 -07:00
|
|
|
|
pub title: &'a str,
|
2016-08-03 13:14:59 +12:00
|
|
|
|
pub css_class: &'a str,
|
2014-03-28 10:27:24 -07:00
|
|
|
|
pub root_path: &'a str,
|
2018-12-20 10:18:45 -06:00
|
|
|
|
pub static_root_path: Option<&'a str>,
|
2014-08-04 14:30:06 -07:00
|
|
|
|
pub description: &'a str,
|
2016-03-13 15:36:01 +01:00
|
|
|
|
pub keywords: &'a str,
|
2018-02-24 19:14:36 +01:00
|
|
|
|
pub resource_suffix: &'a str,
|
2018-12-20 10:57:03 -06:00
|
|
|
|
pub extra_scripts: &'a [&'a str],
|
|
|
|
|
pub static_extra_scripts: &'a [&'a str],
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
|
pub fn render<T: fmt::Display, S: fmt::Display>(
|
2019-08-30 10:24:42 -04:00
|
|
|
|
dst: &mut Buffer,
|
2018-12-20 13:28:55 +01:00
|
|
|
|
layout: &Layout,
|
2019-02-23 16:40:07 +09:00
|
|
|
|
page: &Page<'_>,
|
2018-12-20 13:28:55 +01:00
|
|
|
|
sidebar: &S,
|
|
|
|
|
t: &T,
|
|
|
|
|
css_file_extension: bool,
|
|
|
|
|
themes: &[PathBuf],
|
|
|
|
|
generate_search_filter: bool,
|
2019-08-30 10:24:42 -04:00
|
|
|
|
) {
|
2018-12-20 10:18:45 -06:00
|
|
|
|
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
|
2013-10-14 21:32:12 +02:00
|
|
|
|
write!(dst,
|
2018-04-14 17:13:46 +02:00
|
|
|
|
"<!DOCTYPE html>\
|
|
|
|
|
<html lang=\"en\">\
|
|
|
|
|
<head>\
|
|
|
|
|
<meta charset=\"utf-8\">\
|
|
|
|
|
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
|
|
|
|
|
<meta name=\"generator\" content=\"rustdoc\">\
|
|
|
|
|
<meta name=\"description\" content=\"{description}\">\
|
|
|
|
|
<meta name=\"keywords\" content=\"{keywords}\">\
|
|
|
|
|
<title>{title}</title>\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}normalize{suffix}.css\">\
|
|
|
|
|
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}rustdoc{suffix}.css\" \
|
2018-04-14 17:13:46 +02:00
|
|
|
|
id=\"mainThemeStyle\">\
|
|
|
|
|
{themes}\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}dark{suffix}.css\">\
|
|
|
|
|
<link rel=\"stylesheet\" type=\"text/css\" href=\"{static_root_path}light{suffix}.css\" \
|
2018-04-14 17:13:46 +02:00
|
|
|
|
id=\"themeStyle\">\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
<script src=\"{static_root_path}storage{suffix}.js\"></script>\
|
|
|
|
|
<noscript><link rel=\"stylesheet\" href=\"{static_root_path}noscript{suffix}.css\"></noscript>\
|
2018-04-14 17:13:46 +02:00
|
|
|
|
{css_extension}\
|
|
|
|
|
{favicon}\
|
|
|
|
|
{in_header}\
|
2018-11-29 01:29:49 +01:00
|
|
|
|
<style type=\"text/css\">\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
#crate-search{{background-image:url(\"{static_root_path}down-arrow{suffix}.svg\");}}\
|
2018-11-29 01:29:49 +01:00
|
|
|
|
</style>\
|
2018-04-14 17:13:46 +02:00
|
|
|
|
</head>\
|
|
|
|
|
<body class=\"rustdoc {css_class}\">\
|
|
|
|
|
<!--[if lte IE 8]>\
|
|
|
|
|
<div class=\"warning\">\
|
|
|
|
|
This old browser is unsupported and will most likely display funky \
|
|
|
|
|
things.\
|
|
|
|
|
</div>\
|
|
|
|
|
<![endif]-->\
|
|
|
|
|
{before_content}\
|
|
|
|
|
<nav class=\"sidebar\">\
|
|
|
|
|
<div class=\"sidebar-menu\">☰</div>\
|
|
|
|
|
{logo}\
|
|
|
|
|
{sidebar}\
|
|
|
|
|
</nav>\
|
|
|
|
|
<div class=\"theme-picker\">\
|
|
|
|
|
<button id=\"theme-picker\" aria-label=\"Pick another theme!\">\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
<img src=\"{static_root_path}brush{suffix}.svg\" \
|
|
|
|
|
width=\"18\" \
|
|
|
|
|
alt=\"Pick another theme!\">\
|
2018-04-14 17:13:46 +02:00
|
|
|
|
</button>\
|
|
|
|
|
<div id=\"theme-choices\"></div>\
|
|
|
|
|
</div>\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
<script src=\"{static_root_path}theme{suffix}.js\"></script>\
|
2018-04-14 17:13:46 +02:00
|
|
|
|
<nav class=\"sub\">\
|
|
|
|
|
<form class=\"search-form js-only\">\
|
|
|
|
|
<div class=\"search-container\">\
|
2018-12-20 13:28:55 +01:00
|
|
|
|
<div>{filter_crates}\
|
2018-10-01 00:47:54 +02:00
|
|
|
|
<input class=\"search-input\" name=\"search\" \
|
|
|
|
|
autocomplete=\"off\" \
|
|
|
|
|
spellcheck=\"false\" \
|
|
|
|
|
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
|
|
|
|
|
type=\"search\">\
|
|
|
|
|
</div>\
|
2018-04-13 22:54:09 +02:00
|
|
|
|
<a id=\"settings-menu\" href=\"{root_path}settings.html\">\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
<img src=\"{static_root_path}wheel{suffix}.svg\" \
|
|
|
|
|
width=\"18\" \
|
|
|
|
|
alt=\"Change settings\">\
|
2018-04-13 22:54:09 +02:00
|
|
|
|
</a>\
|
2018-04-14 17:13:46 +02:00
|
|
|
|
</div>\
|
|
|
|
|
</form>\
|
|
|
|
|
</nav>\
|
|
|
|
|
<section id=\"main\" class=\"content\">{content}</section>\
|
|
|
|
|
<section id=\"search\" class=\"content hidden\"></section>\
|
|
|
|
|
<section class=\"footer\"></section>\
|
|
|
|
|
<aside id=\"help\" class=\"hidden\">\
|
|
|
|
|
<div>\
|
|
|
|
|
<h1 class=\"hidden\">Help</h1>\
|
|
|
|
|
<div class=\"shortcuts\">\
|
|
|
|
|
<h2>Keyboard Shortcuts</h2>\
|
|
|
|
|
<dl>\
|
|
|
|
|
<dt><kbd>?</kbd></dt>\
|
|
|
|
|
<dd>Show this help dialog</dd>\
|
|
|
|
|
<dt><kbd>S</kbd></dt>\
|
|
|
|
|
<dd>Focus the search field</dd>\
|
|
|
|
|
<dt><kbd>↑</kbd></dt>\
|
|
|
|
|
<dd>Move up in search results</dd>\
|
|
|
|
|
<dt><kbd>↓</kbd></dt>\
|
|
|
|
|
<dd>Move down in search results</dd>\
|
|
|
|
|
<dt><kbd>↹</kbd></dt>\
|
|
|
|
|
<dd>Switch tab</dd>\
|
|
|
|
|
<dt><kbd>⏎</kbd></dt>\
|
|
|
|
|
<dd>Go to active search result</dd>\
|
|
|
|
|
<dt><kbd>+</kbd></dt>\
|
|
|
|
|
<dd>Expand all sections</dd>\
|
|
|
|
|
<dt><kbd>-</kbd></dt>\
|
|
|
|
|
<dd>Collapse all sections</dd>\
|
|
|
|
|
</dl>\
|
|
|
|
|
</div>\
|
|
|
|
|
<div class=\"infos\">\
|
|
|
|
|
<h2>Search Tricks</h2>\
|
|
|
|
|
<p>\
|
2018-11-27 02:59:49 +00:00
|
|
|
|
Prefix searches with a type followed by a colon (e.g., \
|
2018-04-14 17:13:46 +02:00
|
|
|
|
<code>fn:</code>) to restrict the search to a given type.\
|
|
|
|
|
</p>\
|
|
|
|
|
<p>\
|
|
|
|
|
Accepted types are: <code>fn</code>, <code>mod</code>, \
|
|
|
|
|
<code>struct</code>, <code>enum</code>, \
|
|
|
|
|
<code>trait</code>, <code>type</code>, <code>macro</code>, \
|
|
|
|
|
and <code>const</code>.\
|
|
|
|
|
</p>\
|
|
|
|
|
<p>\
|
2018-11-27 02:59:49 +00:00
|
|
|
|
Search functions by type signature (e.g., \
|
2018-04-14 17:13:46 +02:00
|
|
|
|
<code>vec -> usize</code> or <code>* -> vec</code>)\
|
|
|
|
|
</p>\
|
2018-04-14 17:20:36 +02:00
|
|
|
|
<p>\
|
2018-11-27 02:59:49 +00:00
|
|
|
|
Search multiple things at once by splitting your query with comma (e.g., \
|
2018-04-14 17:20:36 +02:00
|
|
|
|
<code>str,u8</code> or <code>String,struct:Vec,test</code>)\
|
|
|
|
|
</p>\
|
2018-04-14 17:13:46 +02:00
|
|
|
|
</div>\
|
|
|
|
|
</div>\
|
|
|
|
|
</aside>\
|
|
|
|
|
{after_content}\
|
|
|
|
|
<script>\
|
|
|
|
|
window.rootPath = \"{root_path}\";\
|
|
|
|
|
window.currentCrate = \"{krate}\";\
|
|
|
|
|
</script>\
|
2019-04-07 18:09:28 +02:00
|
|
|
|
<script src=\"{root_path}aliases{suffix}.js\"></script>\
|
2018-12-20 10:18:45 -06:00
|
|
|
|
<script src=\"{static_root_path}main{suffix}.js\"></script>\
|
2018-12-20 10:57:03 -06:00
|
|
|
|
{static_extra_scripts}\
|
2018-11-06 01:40:12 +01:00
|
|
|
|
{extra_scripts}\
|
2019-04-07 18:09:28 +02:00
|
|
|
|
<script defer src=\"{root_path}search-index{suffix}.js\"></script>\
|
2018-04-14 17:13:46 +02:00
|
|
|
|
</body>\
|
|
|
|
|
</html>",
|
2016-03-13 15:36:01 +01:00
|
|
|
|
css_extension = if css_file_extension {
|
2018-12-20 10:18:45 -06:00
|
|
|
|
format!("<link rel=\"stylesheet\" \
|
|
|
|
|
type=\"text/css\" \
|
|
|
|
|
href=\"{static_root_path}theme{suffix}.css\">",
|
|
|
|
|
static_root_path = static_root_path,
|
2018-02-24 19:14:36 +01:00
|
|
|
|
suffix=page.resource_suffix)
|
2016-03-13 15:36:01 +01:00
|
|
|
|
} else {
|
2018-08-23 10:14:52 +02:00
|
|
|
|
String::new()
|
2016-03-13 15:36:01 +01:00
|
|
|
|
},
|
2013-09-18 22:18:38 -07:00
|
|
|
|
content = *t,
|
2018-12-20 10:18:45 -06:00
|
|
|
|
static_root_path = static_root_path,
|
2013-09-18 22:18:38 -07:00
|
|
|
|
root_path = page.root_path,
|
2016-08-03 13:14:59 +12:00
|
|
|
|
css_class = page.css_class,
|
2019-01-31 15:42:45 +01:00
|
|
|
|
logo = {
|
|
|
|
|
let p = format!("{}{}", page.root_path, layout.krate);
|
|
|
|
|
let p = SlashChecker(&p);
|
|
|
|
|
if layout.logo.is_empty() {
|
|
|
|
|
format!("<a href='{path}index.html'>\
|
2019-04-27 15:09:57 +02:00
|
|
|
|
<div class='logo-container'>\
|
2019-04-29 11:45:06 +02:00
|
|
|
|
<img src='{static_root_path}rust-logo{suffix}.png' alt='logo'></div></a>",
|
2019-01-31 15:42:45 +01:00
|
|
|
|
path=p,
|
|
|
|
|
static_root_path=static_root_path,
|
|
|
|
|
suffix=page.resource_suffix)
|
|
|
|
|
} else {
|
|
|
|
|
format!("<a href='{}index.html'>\
|
2019-04-29 11:45:06 +02:00
|
|
|
|
<div class='logo-container'><img src='{}' alt='logo'></div></a>",
|
2019-01-31 15:42:45 +01:00
|
|
|
|
p,
|
|
|
|
|
layout.logo)
|
|
|
|
|
}
|
2014-05-28 09:24:28 -07:00
|
|
|
|
},
|
2013-09-18 22:18:38 -07:00
|
|
|
|
title = page.title,
|
2014-08-04 13:53:33 -07:00
|
|
|
|
description = page.description,
|
2014-08-04 14:30:06 -07:00
|
|
|
|
keywords = page.keywords,
|
2015-03-24 16:53:34 -07:00
|
|
|
|
favicon = if layout.favicon.is_empty() {
|
2019-01-12 22:27:09 +01:00
|
|
|
|
format!(r#"<link rel="shortcut icon" href="{static_root_path}favicon{suffix}.ico">"#,
|
|
|
|
|
static_root_path=static_root_path,
|
|
|
|
|
suffix=page.resource_suffix)
|
2014-05-28 09:24:28 -07:00
|
|
|
|
} else {
|
|
|
|
|
format!(r#"<link rel="shortcut icon" href="{}">"#, layout.favicon)
|
|
|
|
|
},
|
2014-06-28 03:35:25 -07:00
|
|
|
|
in_header = layout.external_html.in_header,
|
|
|
|
|
before_content = layout.external_html.before_content,
|
|
|
|
|
after_content = layout.external_html.after_content,
|
2013-09-18 22:18:38 -07:00
|
|
|
|
sidebar = *sidebar,
|
2014-02-05 22:15:24 +01:00
|
|
|
|
krate = layout.krate,
|
2018-01-23 00:45:34 +01:00
|
|
|
|
themes = themes.iter()
|
|
|
|
|
.filter_map(|t| t.file_stem())
|
|
|
|
|
.filter_map(|t| t.to_str())
|
2018-04-13 22:54:09 +02:00
|
|
|
|
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}{}.css">"#,
|
2018-12-20 10:18:45 -06:00
|
|
|
|
static_root_path,
|
2018-04-13 22:54:09 +02:00
|
|
|
|
t,
|
|
|
|
|
page.resource_suffix))
|
2018-01-23 00:45:34 +01:00
|
|
|
|
.collect::<String>(),
|
2018-02-24 19:14:36 +01:00
|
|
|
|
suffix=page.resource_suffix,
|
2018-12-20 10:57:03 -06:00
|
|
|
|
static_extra_scripts=page.static_extra_scripts.iter().map(|e| {
|
|
|
|
|
format!("<script src=\"{static_root_path}{extra_script}.js\"></script>",
|
|
|
|
|
static_root_path=static_root_path,
|
|
|
|
|
extra_script=e)
|
|
|
|
|
}).collect::<String>(),
|
|
|
|
|
extra_scripts=page.extra_scripts.iter().map(|e| {
|
2018-12-03 22:37:34 +01:00
|
|
|
|
format!("<script src=\"{root_path}{extra_script}.js\"></script>",
|
2018-11-06 01:40:12 +01:00
|
|
|
|
root_path=page.root_path,
|
2018-12-03 22:37:34 +01:00
|
|
|
|
extra_script=e)
|
2018-11-06 01:40:12 +01:00
|
|
|
|
}).collect::<String>(),
|
2018-12-20 13:28:55 +01:00
|
|
|
|
filter_crates=if generate_search_filter {
|
|
|
|
|
"<select id=\"crate-search\">\
|
|
|
|
|
<option value=\"All crates\">All crates</option>\
|
|
|
|
|
</select>"
|
|
|
|
|
} else {
|
|
|
|
|
""
|
|
|
|
|
},
|
2014-01-30 11:30:21 -08:00
|
|
|
|
)
|
2013-09-18 22:18:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 10:24:42 -04:00
|
|
|
|
pub fn redirect(dst: &mut Buffer, url: &str) {
|
2014-12-04 12:48:16 +09:00
|
|
|
|
// <script> triggers a redirect before refresh, so this is fine.
|
2014-05-29 13:50:47 -07:00
|
|
|
|
write!(dst,
|
|
|
|
|
r##"<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta http-equiv="refresh" content="0;URL={url}">
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2014-12-04 12:48:16 +09:00
|
|
|
|
<p>Redirecting to <a href="{url}">{url}</a>...</p>
|
|
|
|
|
<script>location.replace("{url}" + location.search + location.hash);</script>
|
2014-05-29 13:50:47 -07:00
|
|
|
|
</body>
|
|
|
|
|
</html>"##,
|
|
|
|
|
url = url,
|
|
|
|
|
)
|
|
|
|
|
}
|