Skip to content

Commit e916815

Browse files
committed
Auto merge of #92526 - djc:rustdoc-askama, r=jsha
Migrate rustdoc from Tera to Askama See #84419. Should probably get a benchmarking run to verify if it has the intended effect on rustdoc performance. cc `@jsha` `@jyn514.`
2 parents 124555a + ef96d57 commit e916815

File tree

13 files changed

+127
-148
lines changed

13 files changed

+127
-148
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/config.rs

-5
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,6 @@ crate struct RenderOptions {
257257
/// If present, playground URL to use in the "Run" button added to code samples generated from
258258
/// standalone Markdown files. If not present, `playground_url` is used.
259259
crate markdown_playground_url: Option<String>,
260-
/// If false, the `select` element to have search filtering by crates on rendered docs
261-
/// won't be generated.
262-
crate generate_search_filter: bool,
263260
/// Document items that have lower than `pub` visibility.
264261
crate document_private: bool,
265262
/// Document items that have `doc(hidden)`.
@@ -638,7 +635,6 @@ impl Options {
638635
let crate_version = matches.opt_str("crate-version");
639636
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
640637
let static_root_path = matches.opt_str("static-root-path");
641-
let generate_search_filter = !matches.opt_present("disable-per-crate-search");
642638
let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
643639
let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
644640
let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
@@ -724,7 +720,6 @@ impl Options {
724720
markdown_no_toc,
725721
markdown_css,
726722
markdown_playground_url,
727-
generate_search_filter,
728723
document_private,
729724
document_hidden,
730725
generate_redirect_map,

src/librustdoc/html/layout.rs

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

10-
use serde::Serialize;
10+
use askama::Template;
1111

12-
#[derive(Clone, Serialize)]
12+
#[derive(Clone)]
1313
crate struct Layout {
1414
crate logo: String,
1515
crate favicon: String,
@@ -19,14 +19,10 @@ crate struct Layout {
1919
/// The given user css file which allow to customize the generated
2020
/// documentation theme.
2121
crate css_file_extension: Option<PathBuf>,
22-
/// If false, the `select` element to have search filtering by crates on rendered docs
23-
/// won't be generated.
24-
crate generate_search_filter: bool,
2522
/// If true, then scrape-examples.js will be included in the output HTML file
2623
crate scrape_examples_extension: bool,
2724
}
2825

29-
#[derive(Serialize)]
3026
crate struct Page<'a> {
3127
crate title: &'a str,
3228
crate css_class: &'a str,
@@ -45,7 +41,8 @@ impl<'a> Page<'a> {
4541
}
4642
}
4743

48-
#[derive(Serialize)]
44+
#[derive(Template)]
45+
#[template(path = "page.html")]
4946
struct PageLayout<'a> {
5047
static_root_path: &'a str,
5148
page: &'a Page<'a>,
@@ -58,7 +55,6 @@ struct PageLayout<'a> {
5855
}
5956

6057
crate fn render<T: Print, S: Print>(
61-
templates: &tera::Tera,
6258
layout: &Layout,
6359
page: &Page<'_>,
6460
sidebar: S,
@@ -76,7 +72,7 @@ crate fn render<T: Print, S: Print>(
7672
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
7773
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7874
let sidebar = Buffer::html().to_display(sidebar);
79-
let teractx = tera::Context::from_serialize(PageLayout {
75+
PageLayout {
8076
static_root_path,
8177
page,
8278
layout,
@@ -85,9 +81,9 @@ crate fn render<T: Print, S: Print>(
8581
content,
8682
krate_with_trailing_slash,
8783
rustdoc_version,
88-
})
89-
.unwrap();
90-
templates.render("page.html", &teractx).unwrap()
84+
}
85+
.render()
86+
.unwrap()
9187
}
9288

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

src/librustdoc/html/render/context.rs

+1-11
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 {
@@ -391,7 +387,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
391387
extension_css,
392388
resource_suffix,
393389
static_root_path,
394-
generate_search_filter,
395390
unstable_features,
396391
generate_redirect_map,
397392
show_type_layout,
@@ -421,12 +416,10 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
421416
default_settings,
422417
krate: krate.name(tcx).to_string(),
423418
css_file_extension: extension_css,
424-
generate_search_filter,
425419
scrape_examples_extension: !call_locations.is_empty(),
426420
};
427421
let mut issue_tracker_base_url = None;
428422
let mut include_sources = true;
429-
let templates = templates::load()?;
430423

431424
// Crawl the crate attributes looking for attributes which control how we're
432425
// going to emit HTML
@@ -481,7 +474,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
481474
errors: receiver,
482475
redirections: if generate_redirect_map { Some(Default::default()) } else { None },
483476
show_type_layout,
484-
templates,
485477
span_correspondance_map: matches,
486478
cache,
487479
call_locations,
@@ -577,7 +569,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
577569
};
578570
let all = self.shared.all.replace(AllTypes::new());
579571
let v = layout::render(
580-
&self.shared.templates,
581572
&self.shared.layout,
582573
&page,
583574
sidebar,
@@ -599,7 +590,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
599590
.map(StylePath::basename)
600591
.collect::<Result<_, Error>>()?;
601592
let v = layout::render(
602-
&self.shared.templates,
603593
&self.shared.layout,
604594
&page,
605595
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-12
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ use crate::html::highlight;
3232
use crate::html::layout::Page;
3333
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
3434

35-
use serde::Serialize;
35+
use askama::Template;
3636

3737
const ITEM_TABLE_OPEN: &str = "<div class=\"item-table\">";
3838
const ITEM_TABLE_CLOSE: &str = "</div>";
3939
const ITEM_TABLE_ROW_OPEN: &str = "<div class=\"item-row\">";
4040
const ITEM_TABLE_ROW_CLOSE: &str = "</div>";
4141

4242
// A component in a `use` path, like `string` in std::string::ToString
43-
#[derive(Serialize)]
4443
struct PathComponent<'a> {
4544
path: String,
4645
name: &'a str,
4746
}
4847

49-
#[derive(Serialize)]
48+
#[derive(Template)]
49+
#[template(path = "print_item.html")]
5050
struct ItemVars<'a> {
5151
page: &'a Page<'a>,
5252
static_root_path: &'a str,
@@ -58,13 +58,7 @@ struct ItemVars<'a> {
5858
src_href: Option<&'a str>,
5959
}
6060

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-
) {
61+
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) {
6862
debug_assert!(!item.is_stripped());
6963
let typ = match *item.kind {
7064
clean::ModuleItem(_) => {
@@ -143,8 +137,7 @@ pub(super) fn print_item(
143137
src_href: src_href.as_deref(),
144138
};
145139

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

150143
match *item.kind {

src/librustdoc/html/render/templates.rs

-20
This file was deleted.

0 commit comments

Comments
 (0)