From 93a16cb7e2ee415f9b1a46165b297aa16b2a4fa0 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 3 Jan 2022 18:18:46 +0100 Subject: [PATCH 1/3] Migrate rustdoc from Tera to Askama See #84419. --- Cargo.lock | 86 +++++++++++------ src/librustdoc/Cargo.toml | 2 +- src/librustdoc/html/layout.rs | 13 +-- src/librustdoc/html/render/context.rs | 10 +- src/librustdoc/html/render/mod.rs | 1 - src/librustdoc/html/render/print_item.rs | 15 +-- src/librustdoc/html/render/templates.rs | 20 ---- src/librustdoc/html/render/write_shared.rs | 9 +- src/librustdoc/html/sources.rs | 1 - src/librustdoc/{html => }/templates/STYLE.md | 0 src/librustdoc/{html => }/templates/page.html | 92 ++++++++++--------- .../{html => }/templates/print_item.html | 16 ++-- 12 files changed, 130 insertions(+), 135 deletions(-) delete mode 100644 src/librustdoc/html/render/templates.rs rename src/librustdoc/{html => }/templates/STYLE.md (100%) rename src/librustdoc/{html => }/templates/page.html (60%) rename src/librustdoc/{html => }/templates/print_item.html (63%) 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 -%} {#- -#} [] {#- -#} {#- -#} From b00d6668b7665fbf040e4768030571bb5aa8c6de Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 3 Jan 2022 21:16:05 +0100 Subject: [PATCH 2/3] Address feedback --- src/librustdoc/html/layout.rs | 6 ++---- src/librustdoc/html/render/print_item.rs | 4 +--- src/librustdoc/templates/page.html | 10 ++++------ 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 1eba43d26b2..38d333cc983 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -8,9 +8,8 @@ use crate::html::format::{Buffer, Print}; use crate::html::render::{ensure_trailing_slash, StylePath}; use askama::Template; -use serde::Serialize; -#[derive(Clone, Serialize)] +#[derive(Clone)] crate struct Layout { crate logo: String, crate favicon: String, @@ -27,7 +26,6 @@ crate struct Layout { crate scrape_examples_extension: bool, } -#[derive(Serialize)] crate struct Page<'a> { crate title: &'a str, crate css_class: &'a str, @@ -46,7 +44,7 @@ impl<'a> Page<'a> { } } -#[derive(Serialize, Template)] +#[derive(Template)] #[template(path = "page.html")] struct PageLayout<'a> { static_root_path: &'a str, diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 29e03f7f65b..52268987a20 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -33,7 +33,6 @@ use crate::html::layout::Page; use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine}; use askama::Template; -use serde::Serialize; const ITEM_TABLE_OPEN: &str = "
"; const ITEM_TABLE_CLOSE: &str = "
"; @@ -41,13 +40,12 @@ const ITEM_TABLE_ROW_OPEN: &str = "
"; const ITEM_TABLE_ROW_CLOSE: &str = "
"; // A component in a `use` path, like `string` in std::string::ToString -#[derive(Serialize)] struct PathComponent<'a> { path: String, name: &'a str, } -#[derive(Serialize, Template)] +#[derive(Template)] #[template(path = "print_item.html")] struct ItemVars<'a> { page: &'a Page<'a>, diff --git a/src/librustdoc/templates/page.html b/src/librustdoc/templates/page.html index e26d9856221..673260ac6d0 100644 --- a/src/librustdoc/templates/page.html +++ b/src/librustdoc/templates/page.html @@ -49,12 +49,10 @@ {#- -#} {#- -#} - {%- match layout.css_file_extension -%} - {%- when Some with (ext) -%} - {#- -#} - {%- else -%} - {%- endmatch -%} + {%- if layout.css_file_extension.is_some() -%} + {#- -#} + {%- endif -%} {%- if !layout.favicon.is_empty() -%} {#- -#} {%- else -%} From ef96d573bff12330080d22f12cad96b818ea5da7 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 9 Jan 2022 21:33:00 +0100 Subject: [PATCH 3/3] Remove configuration which became unused in 8abb4bb698c9d74507adb9cd7b54a032f3c1b595 --- src/librustdoc/config.rs | 5 ----- src/librustdoc/html/layout.rs | 3 --- src/librustdoc/html/render/context.rs | 2 -- 3 files changed, 10 deletions(-) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index d300afa3132..959f83a0211 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -257,9 +257,6 @@ crate struct RenderOptions { /// If present, playground URL to use in the "Run" button added to code samples generated from /// standalone Markdown files. If not present, `playground_url` is used. crate markdown_playground_url: Option, - /// If false, the `select` element to have search filtering by crates on rendered docs - /// won't be generated. - crate generate_search_filter: bool, /// Document items that have lower than `pub` visibility. crate document_private: bool, /// Document items that have `doc(hidden)`. @@ -638,7 +635,6 @@ impl Options { let crate_version = matches.opt_str("crate-version"); let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some(); let static_root_path = matches.opt_str("static-root-path"); - let generate_search_filter = !matches.opt_present("disable-per-crate-search"); let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from); let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from); let test_builder = matches.opt_str("test-builder").map(PathBuf::from); @@ -724,7 +720,6 @@ impl Options { markdown_no_toc, markdown_css, markdown_playground_url, - generate_search_filter, document_private, document_hidden, generate_redirect_map, diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 38d333cc983..4ca71ea8684 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -19,9 +19,6 @@ crate struct Layout { /// The given user css file which allow to customize the generated /// documentation theme. crate css_file_extension: Option, - /// If false, the `select` element to have search filtering by crates on rendered docs - /// won't be generated. - crate generate_search_filter: bool, /// If true, then scrape-examples.js will be included in the output HTML file crate scrape_examples_extension: bool, } diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 9d7be783e85..85661eefc73 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -387,7 +387,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { extension_css, resource_suffix, static_root_path, - generate_search_filter, unstable_features, generate_redirect_map, show_type_layout, @@ -417,7 +416,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { default_settings, krate: krate.name(tcx).to_string(), css_file_extension: extension_css, - generate_search_filter, scrape_examples_extension: !call_locations.is_empty(), }; let mut issue_tracker_base_url = None;