rust/src/librustdoc/html/layout.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
2.9 KiB
Rust
Raw Normal View History

2018-01-23 00:45:34 +01:00
use std::path::PathBuf;
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};
use crate::html::render::{ensure_trailing_slash, StylePath};
2019-01-31 15:42:45 +01:00
use askama::Template;
use super::static_files::{StaticFiles, STATIC_FILES};
2022-01-03 21:16:05 +01:00
#[derive(Clone)]
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,
/// The given user css file which allow to customize the generated
/// documentation theme.
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
pub(crate) scrape_examples_extension: bool,
}
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,
}
impl<'a> Page<'a> {
pub(crate) fn get_static_root_path(&self) -> &str {
self.static_root_path.unwrap_or(self.root_path)
}
}
2022-01-03 21:16:05 +01:00
#[derive(Template)]
#[template(path = "page.html")]
struct PageLayout<'a> {
static_root_path: &'a str,
page: &'a Page<'a>,
layout: &'a Layout,
files: &'static StaticFiles,
themes: Vec<String>,
sidebar: String,
content: String,
krate_with_trailing_slash: String,
pub(crate) rustdoc_version: &'a str,
}
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<'_>,
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 mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
themes.sort();
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
let sidebar = Buffer::html().to_display(sidebar);
PageLayout {
static_root_path,
page,
layout,
files: &STATIC_FILES,
themes,
sidebar,
content,
krate_with_trailing_slash,
rustdoc_version,
}
.render()
.unwrap()
}
pub(crate) fn redirect(url: &str) -> String {
// <script> triggers a redirect before refresh, so this is fine.
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,
)
}