diff --git a/Cargo.lock b/Cargo.lock index 50a5d78731f..ef9f91fdb43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -103,6 +103,47 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a2f58b0bb10c380af2b26e57212856b8c9a59e0925b4c20f4a174a49734eaf7" +[[package]] +name = "askama" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d8f355701c672c2ba3d718acbd213f740beea577cc4eae66accdffe15be1882" +dependencies = [ + "askama_derive", + "askama_escape", + "askama_shared", +] + +[[package]] +name = "askama_derive" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84704cab5b7ae0fd3a9f78ee5eb7b27f3749df445f04623db6633459ae283267" +dependencies = [ + "askama_shared", + "proc-macro2", + "syn", +] + +[[package]] +name = "askama_escape" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a1bb320f97e6edf9f756bf015900038e43c7700e059688e5724a928c8f3b8d5" + +[[package]] +name = "askama_shared" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae03eebba55a2697a376e58b573a29fe36893157173ac8df312ad85f3c0e012" +dependencies = [ + "askama_escape", + "nom", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atty" version = "0.2.14" @@ -1509,17 +1550,6 @@ dependencies = [ "regex", ] -[[package]] -name = "globwalk" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" -dependencies = [ - "bitflags", - "ignore", - "walkdir", -] - [[package]] name = "gsgdt" version = "0.1.2" @@ -2245,6 +2275,12 @@ dependencies = [ "macro-utils", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.4.0" @@ -2304,6 +2340,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nom" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" +dependencies = [ + "memchr", + "minimal-lexical", + "version_check", +] + [[package]] name = "ntapi" version = "0.3.6" @@ -4631,6 +4678,7 @@ name = "rustdoc" version = "0.0.0" dependencies = [ "arrayvec", + "askama", "expect-test", "itertools 0.9.0", "minifier", @@ -4642,7 +4690,6 @@ dependencies = [ "serde_json", "smallvec", "tempfile", - "tera", "tracing", "tracing-subscriber", "tracing-tree", @@ -5187,21 +5234,6 @@ dependencies = [ "utf-8", ] -[[package]] -name = "tera" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81060acb882480c8793782eb96bc86f5c83d2fc7175ad46c375c6956ef7afa62" -dependencies = [ - "globwalk", - "lazy_static", - "pest", - "pest_derive", - "regex", - "serde", - "serde_json", -] - [[package]] name = "term" version = "0.6.1" diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index a3cb982f277..5025342c1d6 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -8,6 +8,7 @@ path = "lib.rs" [dependencies] arrayvec = { version = "0.7", default-features = false } +askama = { version = "0.11", default-features = false } pulldown-cmark = { version = "0.9", default-features = false } minifier = "0.0.41" rayon = "1.3.1" @@ -20,7 +21,6 @@ regex = "1" rustdoc-json-types = { path = "../rustdoc-json-types" } tracing = "0.1" tracing-tree = "0.2.0" -tera = { version = "1.10.0", default-features = false } [dependencies.tracing-subscriber] version = "0.3.3" diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 3d3fa3aaeaa..1eba43d26b2 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -7,6 +7,7 @@ use crate::externalfiles::ExternalHtml; use crate::html::format::{Buffer, Print}; use crate::html::render::{ensure_trailing_slash, StylePath}; +use askama::Template; use serde::Serialize; #[derive(Clone, Serialize)] @@ -45,7 +46,8 @@ impl<'a> Page<'a> { } } -#[derive(Serialize)] +#[derive(Serialize, Template)] +#[template(path = "page.html")] struct PageLayout<'a> { static_root_path: &'a str, page: &'a Page<'a>, @@ -58,7 +60,6 @@ struct PageLayout<'a> { } crate fn render( - templates: &tera::Tera, layout: &Layout, page: &Page<'_>, sidebar: S, @@ -76,7 +77,7 @@ crate fn render( 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); - let teractx = tera::Context::from_serialize(PageLayout { + PageLayout { static_root_path, page, layout, @@ -85,9 +86,9 @@ crate fn render( content, krate_with_trailing_slash, rustdoc_version, - }) - .unwrap(); - templates.render("page.html", &teractx).unwrap() + } + .render() + .unwrap() } crate fn redirect(url: &str) -> String { diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 2ae203e0d2f..9d7be783e85 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -15,7 +15,6 @@ use rustc_span::symbol::sym; use super::print_item::{full_path, item_path, print_item}; use super::search_index::build_index; -use super::templates; use super::write_shared::write_shared; use super::{ collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath, @@ -118,8 +117,6 @@ crate struct SharedContext<'tcx> { /// the crate. redirections: Option>>, - pub(crate) templates: tera::Tera, - /// Correspondance map used to link types used in the source code pages to allow to click on /// links to jump to the type's definition. crate span_correspondance_map: FxHashMap, @@ -218,11 +215,10 @@ impl<'tcx> Context<'tcx> { if !self.render_redirect_pages { layout::render( - &self.shared.templates, &self.shared.layout, &page, |buf: &mut _| print_sidebar(self, it, buf), - |buf: &mut _| print_item(self, &self.shared.templates, it, buf, &page), + |buf: &mut _| print_item(self, it, buf, &page), &self.shared.style_files, ) } else { @@ -426,7 +422,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { }; let mut issue_tracker_base_url = None; let mut include_sources = true; - let templates = templates::load()?; // Crawl the crate attributes looking for attributes which control how we're // going to emit HTML @@ -481,7 +476,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { errors: receiver, redirections: if generate_redirect_map { Some(Default::default()) } else { None }, show_type_layout, - templates, span_correspondance_map: matches, cache, call_locations, @@ -577,7 +571,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { }; let all = self.shared.all.replace(AllTypes::new()); let v = layout::render( - &self.shared.templates, &self.shared.layout, &page, sidebar, @@ -599,7 +592,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { .map(StylePath::basename) .collect::>()?; let v = layout::render( - &self.shared.templates, &self.shared.layout, &page, sidebar, diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 2038e338186..fba8835e868 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -31,7 +31,6 @@ mod tests; mod context; mod print_item; mod span_map; -mod templates; mod write_shared; crate use self::context::*; diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 9f2830ba542..29e03f7f65b 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -32,6 +32,7 @@ use crate::html::highlight; use crate::html::layout::Page; use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine}; +use askama::Template; use serde::Serialize; const ITEM_TABLE_OPEN: &str = "
"; @@ -46,7 +47,8 @@ struct PathComponent<'a> { name: &'a str, } -#[derive(Serialize)] +#[derive(Serialize, Template)] +#[template(path = "print_item.html")] struct ItemVars<'a> { page: &'a Page<'a>, static_root_path: &'a str, @@ -58,13 +60,7 @@ struct ItemVars<'a> { src_href: Option<&'a str>, } -pub(super) fn print_item( - cx: &Context<'_>, - templates: &tera::Tera, - item: &clean::Item, - buf: &mut Buffer, - page: &Page<'_>, -) { +pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) { debug_assert!(!item.is_stripped()); let typ = match *item.kind { clean::ModuleItem(_) => { @@ -143,8 +139,7 @@ pub(super) fn print_item( src_href: src_href.as_deref(), }; - let teractx = tera::Context::from_serialize(item_vars).unwrap(); - let heading = templates.render("print_item.html", &teractx).unwrap(); + let heading = item_vars.render().unwrap(); buf.write_str(&heading); match *item.kind { diff --git a/src/librustdoc/html/render/templates.rs b/src/librustdoc/html/render/templates.rs deleted file mode 100644 index d1f18239447..00000000000 --- a/src/librustdoc/html/render/templates.rs +++ /dev/null @@ -1,20 +0,0 @@ -use std::error::Error as StdError; - -use crate::error::Error; - -pub(crate) fn load() -> Result { - let mut templates = tera::Tera::default(); - - macro_rules! include_template { - ($file:literal, $fullpath:literal) => { - templates.add_raw_template($file, include_str!($fullpath)).map_err(|e| Error { - file: $file.into(), - error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()), - })? - }; - } - - include_template!("page.html", "../templates/page.html"); - include_template!("print_item.html", "../templates/print_item.html"); - Ok(templates) -} diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 563f4ae7385..2e763dbd8fe 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -494,14 +494,7 @@ pub(super) fn write_shared( }) .collect::() ); - let v = layout::render( - &cx.shared.templates, - &cx.shared.layout, - &page, - "", - content, - &cx.shared.style_files, - ); + let v = layout::render(&cx.shared.layout, &page, "", content, &cx.shared.style_files); cx.shared.fs.write(dst, v)?; } } diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index 04c2b7a0c9a..bae04f2095a 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -203,7 +203,6 @@ impl SourceCollector<'_, '_> { static_extra_scripts: &[&format!("source-script{}", self.cx.shared.resource_suffix)], }; let v = layout::render( - &self.cx.shared.templates, &self.cx.shared.layout, &page, "", diff --git a/src/librustdoc/html/templates/STYLE.md b/src/librustdoc/templates/STYLE.md similarity index 100% rename from src/librustdoc/html/templates/STYLE.md rename to src/librustdoc/templates/STYLE.md diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/templates/page.html similarity index 60% rename from src/librustdoc/html/templates/page.html rename to src/librustdoc/templates/page.html index 5cade1b1a4c..e26d9856221 100644 --- a/src/librustdoc/html/templates/page.html +++ b/src/librustdoc/templates/page.html @@ -7,20 +7,20 @@ {#- -#} {#- -#} {{page.title}} {#- -#} - {#- -#} - {#- -#} - {#- -#} - {#- -#} - {#- -#} - {#- -#} + {#- -#} + {#- -#} + {#- -#} + {#- -#} + {#- -#} + {#- -#} {#- -#} + href="{{static_root_path|safe}}normalize{{page.resource_suffix}}.css"> {#- -#} {#- -#} {%- for theme in themes -%} {%- endfor -%} {#- -#} - {#- -#} - {#- -#} - {#- -#} + {#- -#} + {#- -#} + {#- -#} {%- for script in page.static_extra_scripts -%} - {#- -#} + {#- -#} {% endfor %} {%- if layout.scrape_examples_extension -%} - {#- -#} + {#- -#} {%- endif -%} {%- for script in page.extra_scripts -%} - {#- -#} + {#- -#} {% endfor %} {#- -#} - {%- if layout.css_file_extension -%} - {#- -#} - {%- endif -%} - {%- if layout.favicon -%} + {%- match layout.css_file_extension -%} + {%- when Some with (ext) -%} + {#- -#} + {%- else -%} + {%- endmatch -%} + {%- if !layout.favicon.is_empty() -%} {#- -#} {%- else -%} {#- -#} + href="{{static_root_path|safe}}favicon-16x16{{page.resource_suffix}}.png"> {#- -#} {#- -#} + href="{{static_root_path|safe}}favicon-32x32{{page.resource_suffix}}.png"> {#- -#} {#- -#} + href="{{static_root_path|safe}}favicon{{page.resource_suffix}}.svg"> {#- -#} {%- endif -%} - {{- layout.external_html.in_header | safe -}} + {{- layout.external_html.in_header|safe -}} {#- -#} {#- -#} {#- -#} - {{- layout.external_html.before_content | safe -}} + {{- layout.external_html.before_content|safe -}} {#- -#}
{#- -#}
{#- -#}
{#- -#} - {#- -#} - {%- if layout.logo -%} - logo + {#- -#} + {%- if !layout.logo.is_empty() %} + logo {#- -#} {%- else -%} - + {#- -#} {%- endif -%} {#- -#}
{#- -#} {#- -#} - {#- -#} + {#- -#} Change settings {#- -#} + src="{{static_root_path|safe}}wheel{{page.resource_suffix}}.svg"> {#- -#} {#- -#}
{#- -#} {#- -#} {#- -#}
{#- -#} -
{{- content | safe -}}
{#- -#} +
{{- content|safe -}}
{#- -#} {#- -#} {#- -#} {#- -#} - {{- layout.external_html.after_content | safe -}} + {{- layout.external_html.after_content|safe -}}
{#- -#} diff --git a/src/librustdoc/html/templates/print_item.html b/src/librustdoc/templates/print_item.html similarity index 63% rename from src/librustdoc/html/templates/print_item.html rename to src/librustdoc/templates/print_item.html index 09cd8513a64..c98c6db424a 100644 --- a/src/librustdoc/html/templates/print_item.html +++ b/src/librustdoc/templates/print_item.html @@ -4,23 +4,25 @@ {{-typ-}} {#- The breadcrumbs of the item path, like std::string -#} {%- for component in path_components -%} - {{component.name}}:: + {{component.name}}:: {%- endfor -%} {{name}} {#- -#} {#- -#} {#- -#} {#- -#} {#- -#} - {% if stability_since_raw %} - {{- stability_since_raw | safe -}} · + {% if !stability_since_raw.is_empty() %} + {{- stability_since_raw|safe -}} · {% endif %} - {%- if src_href %} - source · - {% endif -%} + {%- match src_href -%} + {%- when Some with (href) -%} + source + {%- else -%} · + {%- endmatch -%} {#- -#} [] {#- -#} {#- -#}