diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 904a65b4931..011d3cfcf72 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -1,6 +1,5 @@ use std::cell::RefCell; use std::collections::BTreeMap; -use std::error::Error as StdError; use std::io; use std::path::{Path, PathBuf}; use std::rc::Rc; @@ -16,6 +15,7 @@ use super::cache::{build_index, ExternalLocation}; use super::print_item::{full_path, item_path, print_item}; +use super::templates; use super::write_shared::write_shared; use super::{ collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath, @@ -33,7 +33,6 @@ use crate::html::escape::Escape; use crate::html::format::Buffer; use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap}; -use crate::html::static_files::{PAGE, PRINT_ITEM}; use crate::html::{layout, sources}; /// Major driving force in all rustdoc rendering. This contains information @@ -416,16 +415,7 @@ fn init( }; let mut issue_tracker_base_url = None; let mut include_sources = true; - - let mut templates = tera::Tera::default(); - templates.add_raw_template("page.html", PAGE).map_err(|e| Error { - file: "page.html".into(), - error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()), - })?; - templates.add_raw_template("print_item.html", PRINT_ITEM).map_err(|e| Error { - file: "print_item.html".into(), - error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()), - })?; + let templates = templates::load()?; // Crawl the crate attributes looking for attributes which control how we're // going to emit HTML diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 11682afdf89..e4c0ab6f10f 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -31,6 +31,7 @@ mod context; mod print_item; mod span_map; +mod templates; mod write_shared; crate use context::*; diff --git a/src/librustdoc/html/render/templates.rs b/src/librustdoc/html/render/templates.rs new file mode 100644 index 00000000000..d1f18239447 --- /dev/null +++ b/src/librustdoc/html/render/templates.rs @@ -0,0 +1,20 @@ +use std::error::Error as StdError; + +use crate::error::Error; + +pub(crate) fn load() -> Result { + let mut templates = tera::Tera::default(); + + macro_rules! include_template { + ($file:literal, $fullpath:literal) => { + templates.add_raw_template($file, include_str!($fullpath)).map_err(|e| Error { + file: $file.into(), + error: format!("{}: {}", e, e.source().map(|e| e.to_string()).unwrap_or_default()), + })? + }; + } + + include_template!("page.html", "../templates/page.html"); + include_template!("print_item.html", "../templates/print_item.html"); + Ok(templates) +} diff --git a/src/librustdoc/html/static_files.rs b/src/librustdoc/html/static_files.rs index a498bd2d7f9..ccc25e6cc49 100644 --- a/src/librustdoc/html/static_files.rs +++ b/src/librustdoc/html/static_files.rs @@ -70,9 +70,6 @@ crate static RUST_FAVICON_PNG_16: &[u8] = include_bytes!("static/images/favicon-16x16.png"); crate static RUST_FAVICON_PNG_32: &[u8] = include_bytes!("static/images/favicon-32x32.png"); -crate static PAGE: &str = include_str!("templates/page.html"); -crate static PRINT_ITEM: &str = include_str!("templates/print_item.html"); - /// The built-in themes given to every documentation site. crate mod themes { /// The "light" theme, selected by default when no setting is available. Used as the basis for