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-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
|
|
|
|
2022-01-03 18:18:46 +01:00
|
|
|
use askama::Template;
|
2021-06-08 17:19:03 -07:00
|
|
|
|
2022-10-24 01:28:55 -07:00
|
|
|
use super::static_files::{StaticFiles, STATIC_FILES};
|
|
|
|
|
2022-01-03 21:16:05 +01:00
|
|
|
#[derive(Clone)]
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) struct Layout {
|
|
|
|
pub(crate) logo: String,
|
|
|
|
pub(crate) favicon: String,
|
|
|
|
pub(crate) external_html: ExternalHtml,
|
|
|
|
pub(crate) default_settings: FxHashMap<String, String>,
|
|
|
|
pub(crate) krate: String,
|
2019-08-30 10:51:13 -04:00
|
|
|
/// The given user css file which allow to customize the generated
|
|
|
|
/// documentation theme.
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) css_file_extension: Option<PathBuf>,
|
2021-10-01 13:05:35 -07:00
|
|
|
/// If true, then scrape-examples.js will be included in the output HTML file
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) scrape_examples_extension: bool,
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) struct Page<'a> {
|
|
|
|
pub(crate) title: &'a str,
|
|
|
|
pub(crate) css_class: &'a str,
|
|
|
|
pub(crate) root_path: &'a str,
|
|
|
|
pub(crate) static_root_path: Option<&'a str>,
|
|
|
|
pub(crate) description: &'a str,
|
|
|
|
pub(crate) keywords: &'a str,
|
|
|
|
pub(crate) resource_suffix: &'a str,
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:41:24 +02:00
|
|
|
impl<'a> Page<'a> {
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn get_static_root_path(&self) -> &str {
|
2021-05-09 20:41:24 +02:00
|
|
|
self.static_root_path.unwrap_or(self.root_path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 21:16:05 +01:00
|
|
|
#[derive(Template)]
|
2022-01-03 18:18:46 +01:00
|
|
|
#[template(path = "page.html")]
|
2021-06-08 17:19:03 -07:00
|
|
|
struct PageLayout<'a> {
|
|
|
|
static_root_path: &'a str,
|
|
|
|
page: &'a Page<'a>,
|
|
|
|
layout: &'a Layout,
|
2022-10-24 01:28:55 -07:00
|
|
|
|
|
|
|
files: &'static StaticFiles,
|
|
|
|
|
2021-11-22 23:23:58 -08:00
|
|
|
themes: Vec<String>,
|
2021-06-08 17:19:03 -07:00
|
|
|
sidebar: String,
|
|
|
|
content: String,
|
|
|
|
krate_with_trailing_slash: String,
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) rustdoc_version: &'a str,
|
2021-06-08 17:19:03 -07:00
|
|
|
}
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn render<T: Print, S: Print>(
|
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();
|
2022-10-24 01:28:55 -07:00
|
|
|
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
|
2021-11-23 21:23:54 -08:00
|
|
|
themes.sort();
|
2022-10-24 01:28:55 -07:00
|
|
|
|
2021-11-23 19:22:29 -08:00
|
|
|
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
|
2021-06-08 17:19:03 -07:00
|
|
|
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
|
|
|
|
let sidebar = Buffer::html().to_display(sidebar);
|
2022-01-03 18:18:46 +01:00
|
|
|
PageLayout {
|
2021-06-08 17:19:03 -07:00
|
|
|
static_root_path,
|
|
|
|
page,
|
|
|
|
layout,
|
2022-10-24 01:28:55 -07:00
|
|
|
files: &STATIC_FILES,
|
2021-11-22 23:23:58 -08:00
|
|
|
themes,
|
2021-06-08 17:19:03 -07:00
|
|
|
sidebar,
|
|
|
|
content,
|
|
|
|
krate_with_trailing_slash,
|
2021-11-23 19:22:29 -08:00
|
|
|
rustdoc_version,
|
2022-01-03 18:18:46 +01:00
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap()
|
2013-09-18 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(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!(
|
2014-05-29 13:50:47 -07:00
|
|
|
r##"<!DOCTYPE html>
|
|
|
|
<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>"##,
|
|
|
|
url = url,
|
|
|
|
)
|
|
|
|
}
|