Move template initialization into its own file.

This commit is contained in:
Jacob Hoffman-Andrews 2021-10-08 23:25:54 -07:00
parent 7a938005e1
commit 586a9cea75
4 changed files with 23 additions and 15 deletions

View File

@ -1,6 +1,5 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::error::Error as StdError;
use std::io; use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::rc::Rc; use std::rc::Rc;
@ -16,6 +15,7 @@
use super::cache::{build_index, ExternalLocation}; use super::cache::{build_index, ExternalLocation};
use super::print_item::{full_path, item_path, print_item}; use super::print_item::{full_path, item_path, print_item};
use super::templates;
use super::write_shared::write_shared; use super::write_shared::write_shared;
use super::{ use super::{
collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath, collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath,
@ -33,7 +33,6 @@
use crate::html::escape::Escape; use crate::html::escape::Escape;
use crate::html::format::Buffer; use crate::html::format::Buffer;
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap}; use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
use crate::html::static_files::{PAGE, PRINT_ITEM};
use crate::html::{layout, sources}; use crate::html::{layout, sources};
/// Major driving force in all rustdoc rendering. This contains information /// 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 issue_tracker_base_url = None;
let mut include_sources = true; let mut include_sources = true;
let templates = templates::load()?;
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()),
})?;
// Crawl the crate attributes looking for attributes which control how we're // Crawl the crate attributes looking for attributes which control how we're
// going to emit HTML // going to emit HTML

View File

@ -31,6 +31,7 @@
mod context; mod context;
mod print_item; mod print_item;
mod span_map; mod span_map;
mod templates;
mod write_shared; mod write_shared;
crate use context::*; crate use context::*;

View File

@ -0,0 +1,20 @@
use std::error::Error as StdError;
use crate::error::Error;
pub(crate) fn load() -> Result<tera::Tera, Error> {
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)
}

View File

@ -70,9 +70,6 @@
crate static RUST_FAVICON_PNG_16: &[u8] = include_bytes!("static/images/favicon-16x16.png"); 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 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. /// The built-in themes given to every documentation site.
crate mod themes { crate mod themes {
/// The "light" theme, selected by default when no setting is available. Used as the basis for /// The "light" theme, selected by default when no setting is available. Used as the basis for