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