From 8d40b9ea6c253b58fcc28af5eaaf55d44b6ee165 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 18 Jul 2024 20:56:36 +0200 Subject: [PATCH] Replace askama with rinja --- Cargo.lock | 91 ++++++++++++---------- src/librustdoc/Cargo.toml | 2 +- src/librustdoc/html/layout.rs | 2 +- src/librustdoc/html/render/context.rs | 2 +- src/librustdoc/html/render/mod.rs | 2 +- src/librustdoc/html/render/print_item.rs | 6 +- src/librustdoc/html/render/sidebar.rs | 2 +- src/librustdoc/html/render/type_layout.rs | 2 +- src/librustdoc/html/sources.rs | 2 +- src/librustdoc/html/templates/STYLE.md | 20 +++-- src/librustdoc/{askama.toml => rinja.toml} | 0 11 files changed, 68 insertions(+), 63 deletions(-) rename src/librustdoc/{askama.toml => rinja.toml} (100%) diff --git a/Cargo.lock b/Cargo.lock index 64a7cec030af5..146070cf16713 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", + "getrandom", "once_cell", "version_check", "zerocopy", @@ -243,47 +244,6 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" -[[package]] -name = "askama" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b79091df18a97caea757e28cd2d5fda49c6cd4bd01ddffd7ff01ace0c0ad2c28" -dependencies = [ - "askama_derive", - "askama_escape", -] - -[[package]] -name = "askama_derive" -version = "0.12.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19fe8d6cb13c4714962c072ea496f3392015f0989b1a2847bb4b2d9effd71d83" -dependencies = [ - "askama_parser", - "basic-toml", - "mime", - "mime_guess", - "proc-macro2", - "quote", - "serde", - "syn 2.0.67", -] - -[[package]] -name = "askama_escape" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" - -[[package]] -name = "askama_parser" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acb1161c6b64d1c3d83108213c2a2533a342ac225aabd0bda218278c2ddb00c0" -dependencies = [ - "nom", -] - [[package]] name = "autocfg" version = "1.3.0" @@ -2661,6 +2621,18 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "once_map" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa7085055bbe9c8edbd982048dbcf8181794d4a81cb04a11931673e63cc18dc6" +dependencies = [ + "ahash", + "hashbrown", + "parking_lot", + "stable_deref_trait", +] + [[package]] name = "opener" version = "0.6.1" @@ -3313,6 +3285,41 @@ dependencies = [ "walkdir", ] +[[package]] +name = "rinja" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d47a46d7729e891c8accf260e9daa02ae6d570aa2a94fb1fb27eb5364a2323" +dependencies = [ + "rinja_derive", +] + +[[package]] +name = "rinja_derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44dae9afe59d58ed8d988d67d1945f3638125d2fd2104058399382e11bd3ea2a" +dependencies = [ + "basic-toml", + "mime", + "mime_guess", + "once_map", + "proc-macro2", + "quote", + "rinja_parser", + "serde", + "syn 2.0.67", +] + +[[package]] +name = "rinja_parser" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1771c78cd5d3b1646ef8d8f2ed100db936e8b291d3cc06e92a339ff346858c" +dependencies = [ + "nom", +] + [[package]] name = "rls" version = "2.0.0" @@ -4799,7 +4806,6 @@ name = "rustdoc" version = "0.0.0" dependencies = [ "arrayvec", - "askama", "base64", "expect-test", "indexmap", @@ -4807,6 +4813,7 @@ dependencies = [ "minifier", "pulldown-cmark 0.9.6", "regex", + "rinja", "rustdoc-json-types", "serde", "serde_json", diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index 51fb126cb3407..fe531f0ff5985 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -8,7 +8,7 @@ path = "lib.rs" [dependencies] arrayvec = { version = "0.7", default-features = false } -askama = { version = "0.12", default-features = false, features = ["config"] } +rinja = { version = "0.2", default-features = false, features = ["config"] } base64 = "0.21.7" itertools = "0.12" indexmap = "2" diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index d4b4db0f3fda6..22576b76e41e0 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -6,7 +6,7 @@ use crate::externalfiles::ExternalHtml; use crate::html::format::{Buffer, Print}; use crate::html::render::{ensure_trailing_slash, StylePath}; -use askama::Template; +use rinja::Template; use super::static_files::{StaticFiles, STATIC_FILES}; diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index db1119eca1d3d..7718413c9560f 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -36,7 +36,7 @@ use crate::html::url_parts_builder::UrlPartsBuilder; use crate::html::{layout, sources, static_files}; use crate::scrape_examples::AllCallLocations; use crate::try_err; -use askama::Template; +use rinja::Template; /// Major driving force in all rustdoc rendering. This contains information /// about where in the tree-like hierarchy rendering is occurring and controls diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 877a00e206d11..5b9ef67109cf2 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -46,7 +46,7 @@ use std::path::PathBuf; use std::rc::Rc; use std::str; -use askama::Template; +use rinja::Template; use rustc_attr::{ConstStability, DeprecatedSince, Deprecation, StabilityLevel, StableSince}; use rustc_data_structures::captures::Captures; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 9256330ac7c46..a04313b4b7903 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -36,10 +36,10 @@ use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine}; use crate::html::render::{document_full, document_item_info}; use crate::html::url_parts_builder::UrlPartsBuilder; -use askama::Template; use itertools::Itertools; +use rinja::Template; -/// Generates an Askama template struct for rendering items with common methods. +/// Generates a Rinja template struct for rendering items with common methods. /// /// Usage: /// ```ignore (illustrative) @@ -309,7 +309,7 @@ fn toggle_close(mut w: impl fmt::Write) { w.write_str("").unwrap(); } -trait ItemTemplate<'a, 'cx: 'a>: askama::Template + fmt::Display { +trait ItemTemplate<'a, 'cx: 'a>: rinja::Template + fmt::Display { fn item_and_mut_cx(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>); } diff --git a/src/librustdoc/html/render/sidebar.rs b/src/librustdoc/html/render/sidebar.rs index 3d28937eb99e1..e5bc2ace2038a 100644 --- a/src/librustdoc/html/render/sidebar.rs +++ b/src/librustdoc/html/render/sidebar.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, rc::Rc}; -use askama::Template; +use rinja::Template; use rustc_data_structures::fx::FxHashSet; use rustc_hir::{def::CtorKind, def_id::DefIdSet}; use rustc_middle::ty::{self, TyCtxt}; diff --git a/src/librustdoc/html/render/type_layout.rs b/src/librustdoc/html/render/type_layout.rs index a27e327f2353a..35a38d5375f2f 100644 --- a/src/librustdoc/html/render/type_layout.rs +++ b/src/librustdoc/html/render/type_layout.rs @@ -1,4 +1,4 @@ -use askama::Template; +use rinja::Template; use rustc_data_structures::captures::Captures; use rustc_hir::def_id::DefId; diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index d4b1da71b40a0..7b4d1fa530521 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -8,7 +8,7 @@ use crate::html::layout; use crate::html::render::Context; use crate::visit::DocVisitor; -use askama::Template; +use rinja::Template; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir::def_id::LOCAL_CRATE; use rustc_middle::ty::TyCtxt; diff --git a/src/librustdoc/html/templates/STYLE.md b/src/librustdoc/html/templates/STYLE.md index 38aac2a60e94d..32bacb11475cf 100644 --- a/src/librustdoc/html/templates/STYLE.md +++ b/src/librustdoc/html/templates/STYLE.md @@ -1,15 +1,13 @@ # Style for Templates -This directory has templates in the [Tera templating language][teradoc], which is very -similar to [Jinja2][jinjadoc] and [Django][djangodoc] templates, and also to [Askama][askamadoc]. +This directory has templates in the [Rinja templating language][rinjadoc], which is very +similar to [Jinja2][jinjadoc]. -[teradoc]: https://tera.netlify.app/docs/#templates [jinjadoc]: https://jinja.palletsprojects.com/en/3.1.x/templates/ -[djangodoc]: https://docs.djangoproject.com/en/4.1/topics/templates/ -[askamadoc]: https://docs.rs/askama/latest/askama/ +[rinjadoc]: https://docs.rs/rinja/latest/rinja/ We want our rendered output to have as little unnecessary whitespace as -possible, so that pages load quickly. To achieve that we use Tera's +possible, so that pages load quickly. To achieve that we use Rinja's [whitespace control] features. By default, whitespace characters are removed around jinja tags (`{% %}` for example). At the end of most lines, we put an empty comment tag: `{# #}`. This causes all whitespace between the end of the @@ -20,7 +18,7 @@ remove following whitespace but not preceding. We also use the whitespace control characters in most instances of tags with control flow, for example `{% if foo %}`. -[whitespace control]: https://tera.netlify.app/docs/#whitespace-control +[whitespace control]: https://rinja.readthedocs.io/en/stable/configuration.html#whitespace-control We want our templates to be readable, so we use indentation and newlines liberally. We indent by four spaces after opening an HTML tag _or_ a Jinja @@ -28,11 +26,11 @@ tag. In most cases an HTML tag should be followed by a newline, but if the tag has simple contents and fits with its close tag on a single line, the contents don't necessarily need a new line. -Askama templates support quite sophisticated control flow. To keep our templates +Rinja templates support quite sophisticated control flow. To keep our templates simple and understandable, we use only a subset: `if` and `for`. In particular -we avoid [assignments in the template logic][assignments] and [Askama +we avoid [assignments in the template logic][assignments] and [Rinja macros][macros]. This also may make things easier if we switch to a different Jinja-style template system in the future. -[assignments]: https://djc.github.io/askama/template_syntax.html#assignments -[macros]: https://djc.github.io/askama/template_syntax.html#macros +[assignments]: https://rinja.readthedocs.io/en/stable/template_syntax.html#assignments +[macros]: https://rinja.readthedocs.io/en/stable/template_syntax.html#macros diff --git a/src/librustdoc/askama.toml b/src/librustdoc/rinja.toml similarity index 100% rename from src/librustdoc/askama.toml rename to src/librustdoc/rinja.toml