2018-01-23 00:45:34 +01:00
|
|
|
use std::path::PathBuf;
|
2013-09-18 22:18:38 -07:00
|
|
|
|
2020-12-31 23:25:30 -05:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
|
2019-02-23 16:40:07 +09:00
|
|
|
use crate::externalfiles::ExternalHtml;
|
2019-10-10 12:09:01 +02:00
|
|
|
use crate::html::escape::Escape;
|
2019-08-31 09:07:29 -04:00
|
|
|
use crate::html::format::{Buffer, Print};
|
2020-07-12 14:37:22 -04:00
|
|
|
use crate::html::render::{ensure_trailing_slash, StylePath};
|
2019-01-31 15:42:45 +01:00
|
|
|
|
2021-06-08 17:19:03 -07:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize)]
|
2020-11-14 17:59:58 -05:00
|
|
|
crate struct Layout {
|
|
|
|
crate logo: String,
|
|
|
|
crate favicon: String,
|
|
|
|
crate external_html: ExternalHtml,
|
2020-12-31 23:25:30 -05:00
|
|
|
crate default_settings: FxHashMap<String, String>,
|
2020-11-14 17:59:58 -05:00
|
|
|
crate krate: String,
|
2019-08-30 10:51:13 -04:00
|
|
|
/// The given user css file which allow to customize the generated
|
|
|
|
/// documentation theme.
|
2020-11-14 17:59:58 -05:00
|
|
|
crate css_file_extension: Option<PathBuf>,
|
2019-08-30 10:51:13 -04:00
|
|
|
/// If false, the `select` element to have search filtering by crates on rendered docs
|
|
|
|
/// won't be generated.
|
2020-11-14 17:59:58 -05:00
|
|
|
crate generate_search_filter: bool,
|
2021-10-01 13:05:35 -07:00
|
|
|
/// If true, then scrape-examples.js will be included in the output HTML file
|
|
|
|
crate scrape_examples_extension: bool,
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2021-06-08 17:19:03 -07:00
|
|
|
#[derive(Serialize)]
|
2020-11-14 17:59:58 -05:00
|
|
|
crate struct Page<'a> {
|
|
|
|
crate title: &'a str,
|
|
|
|
crate css_class: &'a str,
|
|
|
|
crate root_path: &'a str,
|
|
|
|
crate static_root_path: Option<&'a str>,
|
|
|
|
crate description: &'a str,
|
|
|
|
crate keywords: &'a str,
|
|
|
|
crate resource_suffix: &'a str,
|
|
|
|
crate extra_scripts: &'a [&'a str],
|
|
|
|
crate static_extra_scripts: &'a [&'a str],
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:41:24 +02:00
|
|
|
impl<'a> Page<'a> {
|
|
|
|
crate fn get_static_root_path(&self) -> &str {
|
|
|
|
self.static_root_path.unwrap_or(self.root_path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:19:03 -07:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct PageLayout<'a> {
|
|
|
|
static_root_path: &'a str,
|
|
|
|
page: &'a Page<'a>,
|
|
|
|
layout: &'a Layout,
|
|
|
|
style_files: String,
|
|
|
|
sidebar: String,
|
|
|
|
content: String,
|
|
|
|
krate_with_trailing_slash: String,
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:59:58 -05:00
|
|
|
crate fn render<T: Print, S: Print>(
|
2021-06-08 17:19:03 -07:00
|
|
|
templates: &tera::Tera,
|
2018-12-20 13:28:55 +01:00
|
|
|
layout: &Layout,
|
2019-02-23 16:40:07 +09:00
|
|
|
page: &Page<'_>,
|
2019-08-31 11:22:51 -04:00
|
|
|
sidebar: S,
|
2019-08-31 12:18:25 -04:00
|
|
|
t: T,
|
2020-07-12 14:37:22 -04:00
|
|
|
style_files: &[StylePath],
|
2019-08-30 10:35:14 -04:00
|
|
|
) -> String {
|
2021-05-09 20:41:24 +02:00
|
|
|
let static_root_path = page.get_static_root_path();
|
2021-06-08 17:19:03 -07:00
|
|
|
let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
|
|
|
|
let style_files = style_files
|
|
|
|
.iter()
|
2021-10-01 17:12:39 +02:00
|
|
|
.filter_map(|t| t.path.file_stem().map(|stem| (stem, t.disabled)))
|
|
|
|
.filter_map(|t| t.0.to_str().map(|path| (path, t.1)))
|
2021-06-08 17:19:03 -07:00
|
|
|
.map(|t| {
|
2019-12-22 17:42:04 -05:00
|
|
|
format!(
|
2020-07-12 14:37:22 -04:00
|
|
|
r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#,
|
|
|
|
Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)),
|
|
|
|
if t.1 { "disabled" } else { "" },
|
|
|
|
if t.0 == "light" { "id=\"themeStyle\"" } else { "" }
|
2021-06-08 17:19:03 -07:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<String>();
|
|
|
|
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
|
|
|
|
let sidebar = Buffer::html().to_display(sidebar);
|
|
|
|
let teractx = tera::Context::from_serialize(PageLayout {
|
|
|
|
static_root_path,
|
|
|
|
page,
|
|
|
|
layout,
|
|
|
|
style_files,
|
|
|
|
sidebar,
|
|
|
|
content,
|
|
|
|
krate_with_trailing_slash,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
templates.render("page.html", &teractx).unwrap()
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:59:58 -05:00
|
|
|
crate fn redirect(url: &str) -> String {
|
2014-12-04 12:48:16 +09:00
|
|
|
// <script> triggers a redirect before refresh, so this is fine.
|
2019-08-30 10:35:14 -04:00
|
|
|
format!(
|
2019-12-22 17:42:04 -05:00
|
|
|
r##"<!DOCTYPE html>
|
2014-05-29 13:50:47 -07:00
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="refresh" content="0;URL={url}">
|
2021-04-23 17:47:50 +02:00
|
|
|
<title>Redirection</title>
|
2014-05-29 13:50:47 -07:00
|
|
|
</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>"##,
|
2019-12-22 17:42:04 -05:00
|
|
|
url = url,
|
2014-05-29 13:50:47 -07:00
|
|
|
)
|
|
|
|
}
|