1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::path::PathBuf;
use rustc_data_structures::fx::FxHashMap;
use crate::externalfiles::ExternalHtml;
use crate::html::escape::Escape;
use crate::html::format::{Buffer, Print};
use crate::html::render::{ensure_trailing_slash, StylePath};
use serde::Serialize;
#[derive(Clone, Serialize)]
crate struct Layout {
crate logo: String,
crate favicon: String,
crate external_html: ExternalHtml,
crate default_settings: FxHashMap<String, String>,
crate krate: String,
crate css_file_extension: Option<PathBuf>,
crate generate_search_filter: bool,
}
#[derive(Serialize)]
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],
}
impl<'a> Page<'a> {
crate fn get_static_root_path(&self) -> &str {
self.static_root_path.unwrap_or(self.root_path)
}
}
#[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,
}
crate fn render<T: Print, S: Print>(
templates: &tera::Tera,
layout: &Layout,
page: &Page<'_>,
sidebar: S,
t: T,
style_files: &[StylePath],
) -> String {
let static_root_path = page.get_static_root_path();
let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
let style_files = style_files
.iter()
.filter_map(|t| {
if let Some(stem) = t.path.file_stem() { Some((stem, t.disabled)) } else { None }
})
.filter_map(|t| if let Some(path) = t.0.to_str() { Some((path, t.1)) } else { None })
.map(|t| {
format!(
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 { "" }
)
})
.collect::<String>();
let content = Buffer::html().to_display(t);
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()
}
crate fn redirect(url: &str) -> String {
format!(
r##"<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL={url}">
<title>Redirection</title>
</head>
<body>
<p>Redirecting to <a href="{url}">{url}</a>...</p>
<script>location.replace("{url}" + location.search + location.hash);</script>
</body>
</html>"##,
url = url,
)
}