Skip to content

Commit 93a16cb

Browse files
committed
Migrate rustdoc from Tera to Askama
See rust-lang#84419.
1 parent 89b9f7b commit 93a16cb

File tree

12 files changed

+130
-135
lines changed

12 files changed

+130
-135
lines changed

Cargo.lock

+59-27
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,47 @@ version = "0.7.0"
103103
source = "registry+https://github.com/rust-lang/crates.io-index"
104104
checksum = "5a2f58b0bb10c380af2b26e57212856b8c9a59e0925b4c20f4a174a49734eaf7"
105105

106+
[[package]]
107+
name = "askama"
108+
version = "0.11.0"
109+
source = "registry+https://github.com/rust-lang/crates.io-index"
110+
checksum = "4d8f355701c672c2ba3d718acbd213f740beea577cc4eae66accdffe15be1882"
111+
dependencies = [
112+
"askama_derive",
113+
"askama_escape",
114+
"askama_shared",
115+
]
116+
117+
[[package]]
118+
name = "askama_derive"
119+
version = "0.11.0"
120+
source = "registry+https://github.com/rust-lang/crates.io-index"
121+
checksum = "84704cab5b7ae0fd3a9f78ee5eb7b27f3749df445f04623db6633459ae283267"
122+
dependencies = [
123+
"askama_shared",
124+
"proc-macro2",
125+
"syn",
126+
]
127+
128+
[[package]]
129+
name = "askama_escape"
130+
version = "0.10.2"
131+
source = "registry+https://github.com/rust-lang/crates.io-index"
132+
checksum = "9a1bb320f97e6edf9f756bf015900038e43c7700e059688e5724a928c8f3b8d5"
133+
134+
[[package]]
135+
name = "askama_shared"
136+
version = "0.12.0"
137+
source = "registry+https://github.com/rust-lang/crates.io-index"
138+
checksum = "dae03eebba55a2697a376e58b573a29fe36893157173ac8df312ad85f3c0e012"
139+
dependencies = [
140+
"askama_escape",
141+
"nom",
142+
"proc-macro2",
143+
"quote",
144+
"syn",
145+
]
146+
106147
[[package]]
107148
name = "atty"
108149
version = "0.2.14"
@@ -1509,17 +1550,6 @@ dependencies = [
15091550
"regex",
15101551
]
15111552

1512-
[[package]]
1513-
name = "globwalk"
1514-
version = "0.8.1"
1515-
source = "registry+https://github.com/rust-lang/crates.io-index"
1516-
checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc"
1517-
dependencies = [
1518-
"bitflags",
1519-
"ignore",
1520-
"walkdir",
1521-
]
1522-
15231553
[[package]]
15241554
name = "gsgdt"
15251555
version = "0.1.2"
@@ -2245,6 +2275,12 @@ dependencies = [
22452275
"macro-utils",
22462276
]
22472277

2278+
[[package]]
2279+
name = "minimal-lexical"
2280+
version = "0.2.1"
2281+
source = "registry+https://github.com/rust-lang/crates.io-index"
2282+
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
2283+
22482284
[[package]]
22492285
name = "miniz_oxide"
22502286
version = "0.4.0"
@@ -2304,6 +2340,17 @@ version = "1.0.4"
23042340
source = "registry+https://github.com/rust-lang/crates.io-index"
23052341
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
23062342

2343+
[[package]]
2344+
name = "nom"
2345+
version = "7.1.0"
2346+
source = "registry+https://github.com/rust-lang/crates.io-index"
2347+
checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109"
2348+
dependencies = [
2349+
"memchr",
2350+
"minimal-lexical",
2351+
"version_check",
2352+
]
2353+
23072354
[[package]]
23082355
name = "ntapi"
23092356
version = "0.3.6"
@@ -4631,6 +4678,7 @@ name = "rustdoc"
46314678
version = "0.0.0"
46324679
dependencies = [
46334680
"arrayvec",
4681+
"askama",
46344682
"expect-test",
46354683
"itertools 0.9.0",
46364684
"minifier",
@@ -4642,7 +4690,6 @@ dependencies = [
46424690
"serde_json",
46434691
"smallvec",
46444692
"tempfile",
4645-
"tera",
46464693
"tracing",
46474694
"tracing-subscriber",
46484695
"tracing-tree",
@@ -5187,21 +5234,6 @@ dependencies = [
51875234
"utf-8",
51885235
]
51895236

5190-
[[package]]
5191-
name = "tera"
5192-
version = "1.10.0"
5193-
source = "registry+https://github.com/rust-lang/crates.io-index"
5194-
checksum = "81060acb882480c8793782eb96bc86f5c83d2fc7175ad46c375c6956ef7afa62"
5195-
dependencies = [
5196-
"globwalk",
5197-
"lazy_static",
5198-
"pest",
5199-
"pest_derive",
5200-
"regex",
5201-
"serde",
5202-
"serde_json",
5203-
]
5204-
52055237
[[package]]
52065238
name = "term"
52075239
version = "0.6.1"

src/librustdoc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ path = "lib.rs"
88

99
[dependencies]
1010
arrayvec = { version = "0.7", default-features = false }
11+
askama = { version = "0.11", default-features = false }
1112
pulldown-cmark = { version = "0.9", default-features = false }
1213
minifier = "0.0.41"
1314
rayon = "1.3.1"
@@ -20,7 +21,6 @@ regex = "1"
2021
rustdoc-json-types = { path = "../rustdoc-json-types" }
2122
tracing = "0.1"
2223
tracing-tree = "0.2.0"
23-
tera = { version = "1.10.0", default-features = false }
2424

2525
[dependencies.tracing-subscriber]
2626
version = "0.3.3"

src/librustdoc/html/layout.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::externalfiles::ExternalHtml;
77
use crate::html::format::{Buffer, Print};
88
use crate::html::render::{ensure_trailing_slash, StylePath};
99

10+
use askama::Template;
1011
use serde::Serialize;
1112

1213
#[derive(Clone, Serialize)]
@@ -45,7 +46,8 @@ impl<'a> Page<'a> {
4546
}
4647
}
4748

48-
#[derive(Serialize)]
49+
#[derive(Serialize, Template)]
50+
#[template(path = "page.html")]
4951
struct PageLayout<'a> {
5052
static_root_path: &'a str,
5153
page: &'a Page<'a>,
@@ -58,7 +60,6 @@ struct PageLayout<'a> {
5860
}
5961

6062
crate fn render<T: Print, S: Print>(
61-
templates: &tera::Tera,
6263
layout: &Layout,
6364
page: &Page<'_>,
6465
sidebar: S,
@@ -76,7 +77,7 @@ crate fn render<T: Print, S: Print>(
7677
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
7778
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7879
let sidebar = Buffer::html().to_display(sidebar);
79-
let teractx = tera::Context::from_serialize(PageLayout {
80+
PageLayout {
8081
static_root_path,
8182
page,
8283
layout,
@@ -85,9 +86,9 @@ crate fn render<T: Print, S: Print>(
8586
content,
8687
krate_with_trailing_slash,
8788
rustdoc_version,
88-
})
89-
.unwrap();
90-
templates.render("page.html", &teractx).unwrap()
89+
}
90+
.render()
91+
.unwrap()
9192
}
9293

9394
crate fn redirect(url: &str) -> String {

src/librustdoc/html/render/context.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_span::symbol::sym;
1515

1616
use super::print_item::{full_path, item_path, print_item};
1717
use super::search_index::build_index;
18-
use super::templates;
1918
use super::write_shared::write_shared;
2019
use super::{
2120
collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath,
@@ -118,8 +117,6 @@ crate struct SharedContext<'tcx> {
118117
/// the crate.
119118
redirections: Option<RefCell<FxHashMap<String, String>>>,
120119

121-
pub(crate) templates: tera::Tera,
122-
123120
/// Correspondance map used to link types used in the source code pages to allow to click on
124121
/// links to jump to the type's definition.
125122
crate span_correspondance_map: FxHashMap<rustc_span::Span, LinkFromSrc>,
@@ -218,11 +215,10 @@ impl<'tcx> Context<'tcx> {
218215

219216
if !self.render_redirect_pages {
220217
layout::render(
221-
&self.shared.templates,
222218
&self.shared.layout,
223219
&page,
224220
|buf: &mut _| print_sidebar(self, it, buf),
225-
|buf: &mut _| print_item(self, &self.shared.templates, it, buf, &page),
221+
|buf: &mut _| print_item(self, it, buf, &page),
226222
&self.shared.style_files,
227223
)
228224
} else {
@@ -426,7 +422,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
426422
};
427423
let mut issue_tracker_base_url = None;
428424
let mut include_sources = true;
429-
let templates = templates::load()?;
430425

431426
// Crawl the crate attributes looking for attributes which control how we're
432427
// going to emit HTML
@@ -481,7 +476,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
481476
errors: receiver,
482477
redirections: if generate_redirect_map { Some(Default::default()) } else { None },
483478
show_type_layout,
484-
templates,
485479
span_correspondance_map: matches,
486480
cache,
487481
call_locations,
@@ -577,7 +571,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
577571
};
578572
let all = self.shared.all.replace(AllTypes::new());
579573
let v = layout::render(
580-
&self.shared.templates,
581574
&self.shared.layout,
582575
&page,
583576
sidebar,
@@ -599,7 +592,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
599592
.map(StylePath::basename)
600593
.collect::<Result<_, Error>>()?;
601594
let v = layout::render(
602-
&self.shared.templates,
603595
&self.shared.layout,
604596
&page,
605597
sidebar,

src/librustdoc/html/render/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ mod tests;
3131
mod context;
3232
mod print_item;
3333
mod span_map;
34-
mod templates;
3534
mod write_shared;
3635

3736
crate use self::context::*;

src/librustdoc/html/render/print_item.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::html::highlight;
3232
use crate::html::layout::Page;
3333
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
3434

35+
use askama::Template;
3536
use serde::Serialize;
3637

3738
const ITEM_TABLE_OPEN: &str = "<div class=\"item-table\">";
@@ -46,7 +47,8 @@ struct PathComponent<'a> {
4647
name: &'a str,
4748
}
4849

49-
#[derive(Serialize)]
50+
#[derive(Serialize, Template)]
51+
#[template(path = "print_item.html")]
5052
struct ItemVars<'a> {
5153
page: &'a Page<'a>,
5254
static_root_path: &'a str,
@@ -58,13 +60,7 @@ struct ItemVars<'a> {
5860
src_href: Option<&'a str>,
5961
}
6062

61-
pub(super) fn print_item(
62-
cx: &Context<'_>,
63-
templates: &tera::Tera,
64-
item: &clean::Item,
65-
buf: &mut Buffer,
66-
page: &Page<'_>,
67-
) {
63+
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) {
6864
debug_assert!(!item.is_stripped());
6965
let typ = match *item.kind {
7066
clean::ModuleItem(_) => {
@@ -143,8 +139,7 @@ pub(super) fn print_item(
143139
src_href: src_href.as_deref(),
144140
};
145141

146-
let teractx = tera::Context::from_serialize(item_vars).unwrap();
147-
let heading = templates.render("print_item.html", &teractx).unwrap();
142+
let heading = item_vars.render().unwrap();
148143
buf.write_str(&heading);
149144

150145
match *item.kind {

src/librustdoc/html/render/templates.rs

-20
This file was deleted.

src/librustdoc/html/render/write_shared.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,7 @@ pub(super) fn write_shared(
494494
})
495495
.collect::<String>()
496496
);
497-
let v = layout::render(
498-
&cx.shared.templates,
499-
&cx.shared.layout,
500-
&page,
501-
"",
502-
content,
503-
&cx.shared.style_files,
504-
);
497+
let v = layout::render(&cx.shared.layout, &page, "", content, &cx.shared.style_files);
505498
cx.shared.fs.write(dst, v)?;
506499
}
507500
}

src/librustdoc/html/sources.rs

-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ impl SourceCollector<'_, '_> {
203203
static_extra_scripts: &[&format!("source-script{}", self.cx.shared.resource_suffix)],
204204
};
205205
let v = layout::render(
206-
&self.cx.shared.templates,
207206
&self.cx.shared.layout,
208207
&page,
209208
"",

0 commit comments

Comments
 (0)