]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/layout.rs
Auto merge of #101410 - dingxiangfei2009:fix-let-else-scoping, r=jackh726
[rust.git] / src / librustdoc / html / layout.rs
1 use std::path::PathBuf;
2
3 use rustc_data_structures::fx::FxHashMap;
4
5 use crate::error::Error;
6 use crate::externalfiles::ExternalHtml;
7 use crate::html::format::{Buffer, Print};
8 use crate::html::render::{ensure_trailing_slash, StylePath};
9
10 use askama::Template;
11
12 #[derive(Clone)]
13 pub(crate) struct Layout {
14     pub(crate) logo: String,
15     pub(crate) favicon: String,
16     pub(crate) external_html: ExternalHtml,
17     pub(crate) default_settings: FxHashMap<String, String>,
18     pub(crate) krate: String,
19     /// The given user css file which allow to customize the generated
20     /// documentation theme.
21     pub(crate) css_file_extension: Option<PathBuf>,
22     /// If true, then scrape-examples.js will be included in the output HTML file
23     pub(crate) scrape_examples_extension: bool,
24 }
25
26 pub(crate) struct Page<'a> {
27     pub(crate) title: &'a str,
28     pub(crate) css_class: &'a str,
29     pub(crate) root_path: &'a str,
30     pub(crate) static_root_path: Option<&'a str>,
31     pub(crate) description: &'a str,
32     pub(crate) keywords: &'a str,
33     pub(crate) resource_suffix: &'a str,
34 }
35
36 impl<'a> Page<'a> {
37     pub(crate) fn get_static_root_path(&self) -> &str {
38         self.static_root_path.unwrap_or(self.root_path)
39     }
40 }
41
42 #[derive(Template)]
43 #[template(path = "page.html")]
44 struct PageLayout<'a> {
45     static_root_path: &'a str,
46     page: &'a Page<'a>,
47     layout: &'a Layout,
48     themes: Vec<String>,
49     sidebar: String,
50     content: String,
51     krate_with_trailing_slash: String,
52     pub(crate) rustdoc_version: &'a str,
53 }
54
55 pub(crate) fn render<T: Print, S: Print>(
56     layout: &Layout,
57     page: &Page<'_>,
58     sidebar: S,
59     t: T,
60     style_files: &[StylePath],
61 ) -> String {
62     let static_root_path = page.get_static_root_path();
63     let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
64     let mut themes: Vec<String> = style_files
65         .iter()
66         .map(StylePath::basename)
67         .collect::<Result<_, Error>>()
68         .unwrap_or_default();
69     themes.sort();
70     let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
71     let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
72     let sidebar = Buffer::html().to_display(sidebar);
73     PageLayout {
74         static_root_path,
75         page,
76         layout,
77         themes,
78         sidebar,
79         content,
80         krate_with_trailing_slash,
81         rustdoc_version,
82     }
83     .render()
84     .unwrap()
85 }
86
87 pub(crate) fn redirect(url: &str) -> String {
88     // <script> triggers a redirect before refresh, so this is fine.
89     format!(
90         r##"<!DOCTYPE html>
91 <html lang="en">
92 <head>
93     <meta http-equiv="refresh" content="0;URL={url}">
94     <title>Redirection</title>
95 </head>
96 <body>
97     <p>Redirecting to <a href="{url}">{url}</a>...</p>
98     <script>location.replace("{url}" + location.search + location.hash);</script>
99 </body>
100 </html>"##,
101         url = url,
102     )
103 }