1 use std::path::PathBuf;
3 use rustc_data_structures::fx::FxHashMap;
5 use crate::externalfiles::ExternalHtml;
6 use crate::html::escape::Escape;
7 use crate::html::format::{Buffer, Print};
8 use crate::html::render::{ensure_trailing_slash, StylePath};
12 #[derive(Clone, Serialize)]
15 crate favicon: String,
16 crate external_html: ExternalHtml,
17 crate default_settings: FxHashMap<String, String>,
19 /// The given user css file which allow to customize the generated
20 /// documentation theme.
21 crate css_file_extension: Option<PathBuf>,
22 /// If false, the `select` element to have search filtering by crates on rendered docs
23 /// won't be generated.
24 crate generate_search_filter: bool,
25 /// If true, then scrape-examples.js will be included in the output HTML file
26 crate scrape_examples_extension: bool,
30 crate struct Page<'a> {
32 crate css_class: &'a str,
33 crate root_path: &'a str,
34 crate static_root_path: Option<&'a str>,
35 crate description: &'a str,
36 crate keywords: &'a str,
37 crate resource_suffix: &'a str,
38 crate extra_scripts: &'a [&'a str],
39 crate static_extra_scripts: &'a [&'a str],
43 crate fn get_static_root_path(&self) -> &str {
44 self.static_root_path.unwrap_or(self.root_path)
49 struct PageLayout<'a> {
50 static_root_path: &'a str,
56 krate_with_trailing_slash: String,
59 crate fn render<T: Print, S: Print>(
60 templates: &tera::Tera,
65 style_files: &[StylePath],
67 let static_root_path = page.get_static_root_path();
68 let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
69 let style_files = style_files
71 .filter_map(|t| t.path.file_stem().map(|stem| (stem, t.disabled)))
72 .filter_map(|t| t.0.to_str().map(|path| (path, t.1)))
75 r#"<link rel="stylesheet" type="text/css" href="{}.css" {} {}>"#,
76 Escape(&format!("{}{}{}", static_root_path, t.0, page.resource_suffix)),
77 if t.1 { "disabled" } else { "" },
78 if t.0 == "light" { "id=\"themeStyle\"" } else { "" }
82 let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
83 let sidebar = Buffer::html().to_display(sidebar);
84 let teractx = tera::Context::from_serialize(PageLayout {
91 krate_with_trailing_slash,
94 templates.render("page.html", &teractx).unwrap()
97 crate fn redirect(url: &str) -> String {
98 // <script> triggers a redirect before refresh, so this is fine.
103 <meta http-equiv="refresh" content="0;URL={url}">
104 <title>Redirection</title>
107 <p>Redirecting to <a href="{url}">{url}</a>...</p>
108 <script>location.replace("{url}" + location.search + location.hash);</script>