Skip to content

Commit

Permalink
Auto merge of #127931 - GuillaumeGomez:switch-to-rinja, r=notriddle
Browse files Browse the repository at this point in the history
Replace askama with rinja

Another askama maintainer and myself forked it and named the fork rinja. The whole difference is explained in this [blog post](https://blog.guillaume-gomez.fr/articles/2024-07-16+docs.rs+switching+jinja+template+framework+from+tera+to+rinja) (not publicly released yet, waiting for docs.rs to deploy the new version using it before). But in short, rinja got a lot of improvements and compiles faster, so I think it's definitely worth it to use it in rustdoc as well.

r? `@notriddle`
  • Loading branch information
bors committed Jul 23, 2024
2 parents d53dc75 + 8d40b9e commit 84c257e
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 63 deletions.
91 changes: 49 additions & 42 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -4799,14 +4806,14 @@ name = "rustdoc"
version = "0.0.0"
dependencies = [
"arrayvec",
"askama",
"base64",
"expect-test",
"indexmap",
"itertools",
"minifier",
"pulldown-cmark 0.9.6",
"regex",
"rinja",
"rustdoc-json-types",
"serde",
"serde_json",
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -309,7 +309,7 @@ fn toggle_close(mut w: impl fmt::Write) {
w.write_str("</details>").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>>);
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/sidebar.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/type_layout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use askama::Template;
use rinja::Template;

use rustc_data_structures::captures::Captures;
use rustc_hir::def_id::DefId;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 9 additions & 11 deletions src/librustdoc/html/templates/STYLE.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,19 +18,19 @@ 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
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
File renamed without changes.

0 comments on commit 84c257e

Please sign in to comment.