Skip to content

Commit 586a9ce

Browse files
committed
Move template initialization into its own file.
1 parent 7a93800 commit 586a9ce

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

Diff for: src/librustdoc/html/render/context.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
3-
use std::error::Error as StdError;
43
use std::io;
54
use std::path::{Path, PathBuf};
65
use std::rc::Rc;
@@ -16,6 +15,7 @@ use rustc_span::symbol::sym;
1615

1716
use super::cache::{build_index, ExternalLocation};
1817
use super::print_item::{full_path, item_path, print_item};
18+
use super::templates;
1919
use super::write_shared::write_shared;
2020
use super::{
2121
collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath,
@@ -33,7 +33,6 @@ use crate::formats::FormatRenderer;
3333
use crate::html::escape::Escape;
3434
use crate::html::format::Buffer;
3535
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
36-
use crate::html::static_files::{PAGE, PRINT_ITEM};
3736
use crate::html::{layout, sources};
3837

3938
/// Major driving force in all rustdoc rendering. This contains information
@@ -416,16 +415,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
416415
};
417416
let mut issue_tracker_base_url = None;
418417
let mut include_sources = true;
419-
420-
let mut templates = tera::Tera::default();
421-
templates.add_raw_template("page.html", PAGE).map_err(|e| Error {
422-
file: "page.html".into(),
423-
error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()),
424-
})?;
425-
templates.add_raw_template("print_item.html", PRINT_ITEM).map_err(|e| Error {
426-
file: "print_item.html".into(),
427-
error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()),
428-
})?;
418+
let templates = templates::load()?;
429419

430420
// Crawl the crate attributes looking for attributes which control how we're
431421
// going to emit HTML

Diff for: src/librustdoc/html/render/mod.rs

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

3637
crate use context::*;

Diff for: src/librustdoc/html/render/templates.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::error::Error as StdError;
2+
3+
use crate::error::Error;
4+
5+
pub(crate) fn load() -> Result<tera::Tera, Error> {
6+
let mut templates = tera::Tera::default();
7+
8+
macro_rules! include_template {
9+
($file:literal, $fullpath:literal) => {
10+
templates.add_raw_template($file, include_str!($fullpath)).map_err(|e| Error {
11+
file: $file.into(),
12+
error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()),
13+
})?
14+
};
15+
}
16+
17+
include_template!("page.html", "../templates/page.html");
18+
include_template!("print_item.html", "../templates/print_item.html");
19+
Ok(templates)
20+
}

Diff for: src/librustdoc/html/static_files.rs

-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ crate static RUST_FAVICON_SVG: &[u8] = include_bytes!("static/images/favicon.svg
7070
crate static RUST_FAVICON_PNG_16: &[u8] = include_bytes!("static/images/favicon-16x16.png");
7171
crate static RUST_FAVICON_PNG_32: &[u8] = include_bytes!("static/images/favicon-32x32.png");
7272

73-
crate static PAGE: &str = include_str!("templates/page.html");
74-
crate static PRINT_ITEM: &str = include_str!("templates/print_item.html");
75-
7673
/// The built-in themes given to every documentation site.
7774
crate mod themes {
7875
/// The "light" theme, selected by default when no setting is available. Used as the basis for

0 commit comments

Comments
 (0)