Finish move of theme to mdbook-html

This updates everything for the move of theme to mdbook-html. There
will be followup commits that will be doing more cleanup here.
This commit is contained in:
Eric Huss 2025-07-21 18:32:36 -07:00
parent 3087686559
commit 753780f653
13 changed files with 77 additions and 56 deletions

6
Cargo.lock generated
View file

@ -1271,6 +1271,7 @@ dependencies = [
"ignore",
"log",
"mdbook-core",
"mdbook-html",
"mdbook-markdown",
"mdbook-preprocessor",
"mdbook-renderer",
@ -1315,6 +1316,11 @@ dependencies = [
[[package]]
name = "mdbook-html"
version = "0.5.0-alpha.1"
dependencies = [
"anyhow",
"log",
"tempfile",
]
[[package]]
name = "mdbook-markdown"

View file

@ -65,6 +65,7 @@ handlebars = "6.0"
hex = "0.4.3"
log.workspace = true
mdbook-core.workspace = true
mdbook-html.workspace = true
mdbook-markdown.workspace = true
mdbook-preprocessor.workspace = true
mdbook-renderer.workspace = true
@ -109,7 +110,7 @@ walkdir = "2.3.3"
default = ["watch", "serve", "search"]
watch = ["dep:notify", "dep:notify-debouncer-mini", "dep:ignore", "dep:pathdiff", "dep:walkdir"]
serve = ["dep:futures-util", "dep:tokio", "dep:axum", "dep:tower-http"]
search = ["dep:elasticlunr-rs", "dep:ammonia"]
search = ["dep:elasticlunr-rs", "dep:ammonia", "mdbook-html/search"]
[[bin]]
doc = false

View file

@ -8,6 +8,14 @@ repository.workspace = true
rust-version.workspace = true
[dependencies]
anyhow.workspace = true
log.workspace = true
[dev-dependencies]
tempfile.workspace = true
[lints]
workspace = true
[features]
search = []

View file

@ -1 +1,3 @@
//! mdBook HTML renderer.
pub mod theme;

View file

@ -1,61 +1,61 @@
pub static CSS: &[u8] = include_bytes!("fonts.css");
pub static CSS: &[u8] = include_bytes!("../../front-end/fonts/fonts.css");
// An array of (file_name, file_contents) pairs
pub static LICENSES: [(&str, &[u8]); 2] = [
(
"fonts/OPEN-SANS-LICENSE.txt",
include_bytes!("OPEN-SANS-LICENSE.txt"),
include_bytes!("../../front-end/fonts/OPEN-SANS-LICENSE.txt"),
),
(
"fonts/SOURCE-CODE-PRO-LICENSE.txt",
include_bytes!("SOURCE-CODE-PRO-LICENSE.txt"),
include_bytes!("../../front-end/fonts/SOURCE-CODE-PRO-LICENSE.txt"),
),
];
// An array of (file_name, file_contents) pairs
pub static OPEN_SANS: [(&str, &[u8]); 10] = [
(
"fonts/open-sans-v17-all-charsets-300.woff2",
include_bytes!("open-sans-v17-all-charsets-300.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-300.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-300italic.woff2",
include_bytes!("open-sans-v17-all-charsets-300italic.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-300italic.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-regular.woff2",
include_bytes!("open-sans-v17-all-charsets-regular.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-regular.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-italic.woff2",
include_bytes!("open-sans-v17-all-charsets-italic.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-italic.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-600.woff2",
include_bytes!("open-sans-v17-all-charsets-600.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-600.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-600italic.woff2",
include_bytes!("open-sans-v17-all-charsets-600italic.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-600italic.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-700.woff2",
include_bytes!("open-sans-v17-all-charsets-700.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-700.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-700italic.woff2",
include_bytes!("open-sans-v17-all-charsets-700italic.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-700italic.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-800.woff2",
include_bytes!("open-sans-v17-all-charsets-800.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-800.woff2"),
),
(
"fonts/open-sans-v17-all-charsets-800italic.woff2",
include_bytes!("open-sans-v17-all-charsets-800italic.woff2"),
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-800italic.woff2"),
),
];
// A (file_name, file_contents) pair
pub static SOURCE_CODE_PRO: (&str, &[u8]) = (
"fonts/source-code-pro-v11-all-charsets-500.woff2",
include_bytes!("source-code-pro-v11-all-charsets-500.woff2"),
include_bytes!("../../front-end/fonts/source-code-pro-v11-all-charsets-500.woff2"),
);

View file

@ -11,31 +11,36 @@ pub mod playground_editor;
#[cfg(feature = "search")]
pub mod searcher;
pub static INDEX: &[u8] = include_bytes!("templates/index.hbs");
pub static HEAD: &[u8] = include_bytes!("templates/head.hbs");
pub static REDIRECT: &[u8] = include_bytes!("templates/redirect.hbs");
pub static HEADER: &[u8] = include_bytes!("templates/header.hbs");
pub static TOC_JS: &[u8] = include_bytes!("templates/toc.js.hbs");
pub static TOC_HTML: &[u8] = include_bytes!("templates/toc.html.hbs");
pub static CHROME_CSS: &[u8] = include_bytes!("css/chrome.css");
pub static GENERAL_CSS: &[u8] = include_bytes!("css/general.css");
pub static PRINT_CSS: &[u8] = include_bytes!("css/print.css");
pub static VARIABLES_CSS: &[u8] = include_bytes!("css/variables.css");
pub static FAVICON_PNG: &[u8] = include_bytes!("images/favicon.png");
pub static FAVICON_SVG: &[u8] = include_bytes!("images/favicon.svg");
pub static JS: &[u8] = include_bytes!("js/book.js");
pub static HIGHLIGHT_JS: &[u8] = include_bytes!("js/highlight.js");
pub static TOMORROW_NIGHT_CSS: &[u8] = include_bytes!("css/tomorrow-night.css");
pub static HIGHLIGHT_CSS: &[u8] = include_bytes!("css/highlight.css");
pub static AYU_HIGHLIGHT_CSS: &[u8] = include_bytes!("css/ayu-highlight.css");
pub static CLIPBOARD_JS: &[u8] = include_bytes!("js/clipboard.min.js");
pub static FONT_AWESOME: &[u8] = include_bytes!("css/font-awesome.min.css");
pub static FONT_AWESOME_EOT: &[u8] = include_bytes!("fonts/fontawesome-webfont.eot");
pub static FONT_AWESOME_SVG: &[u8] = include_bytes!("fonts/fontawesome-webfont.svg");
pub static FONT_AWESOME_TTF: &[u8] = include_bytes!("fonts/fontawesome-webfont.ttf");
pub static FONT_AWESOME_WOFF: &[u8] = include_bytes!("fonts/fontawesome-webfont.woff");
pub static FONT_AWESOME_WOFF2: &[u8] = include_bytes!("fonts/fontawesome-webfont.woff2");
pub static FONT_AWESOME_OTF: &[u8] = include_bytes!("fonts/FontAwesome.otf");
pub static INDEX: &[u8] = include_bytes!("../../front-end/templates/index.hbs");
pub static HEAD: &[u8] = include_bytes!("../../front-end/templates/head.hbs");
pub static REDIRECT: &[u8] = include_bytes!("../../front-end/templates/redirect.hbs");
pub static HEADER: &[u8] = include_bytes!("../../front-end/templates/header.hbs");
pub static TOC_JS: &[u8] = include_bytes!("../../front-end/templates/toc.js.hbs");
pub static TOC_HTML: &[u8] = include_bytes!("../../front-end/templates/toc.html.hbs");
pub static CHROME_CSS: &[u8] = include_bytes!("../../front-end/css/chrome.css");
pub static GENERAL_CSS: &[u8] = include_bytes!("../../front-end/css/general.css");
pub static PRINT_CSS: &[u8] = include_bytes!("../../front-end/css/print.css");
pub static VARIABLES_CSS: &[u8] = include_bytes!("../../front-end/css/variables.css");
pub static FAVICON_PNG: &[u8] = include_bytes!("../../front-end/images/favicon.png");
pub static FAVICON_SVG: &[u8] = include_bytes!("../../front-end/images/favicon.svg");
pub static JS: &[u8] = include_bytes!("../../front-end/js/book.js");
pub static HIGHLIGHT_JS: &[u8] = include_bytes!("../../front-end/js/highlight.js");
pub static TOMORROW_NIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/tomorrow-night.css");
pub static HIGHLIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/highlight.css");
pub static AYU_HIGHLIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/ayu-highlight.css");
pub static CLIPBOARD_JS: &[u8] = include_bytes!("../../front-end/js/clipboard.min.js");
pub static FONT_AWESOME: &[u8] = include_bytes!("../../front-end/css/font-awesome.min.css");
pub static FONT_AWESOME_EOT: &[u8] =
include_bytes!("../../front-end/fonts/fontawesome-webfont.eot");
pub static FONT_AWESOME_SVG: &[u8] =
include_bytes!("../../front-end/fonts/fontawesome-webfont.svg");
pub static FONT_AWESOME_TTF: &[u8] =
include_bytes!("../../front-end/fonts/fontawesome-webfont.ttf");
pub static FONT_AWESOME_WOFF: &[u8] =
include_bytes!("../../front-end/fonts/fontawesome-webfont.woff");
pub static FONT_AWESOME_WOFF2: &[u8] =
include_bytes!("../../front-end/fonts/fontawesome-webfont.woff2");
pub static FONT_AWESOME_OTF: &[u8] = include_bytes!("../../front-end/fonts/FontAwesome.otf");
/// The `Theme` struct should be used instead of the static variables because
/// the `new()` method will look if the user has a theme directory in their

View file

@ -1,7 +1,8 @@
//! Theme dependencies for the playground editor.
pub static JS: &[u8] = include_bytes!("editor.js");
pub static ACE_JS: &[u8] = include_bytes!("ace.js");
pub static MODE_RUST_JS: &[u8] = include_bytes!("mode-rust.js");
pub static THEME_DAWN_JS: &[u8] = include_bytes!("theme-dawn.js");
pub static THEME_TOMORROW_NIGHT_JS: &[u8] = include_bytes!("theme-tomorrow_night.js");
pub static JS: &[u8] = include_bytes!("../../front-end/playground_editor/editor.js");
pub static ACE_JS: &[u8] = include_bytes!("../../front-end/playground_editor/ace.js");
pub static MODE_RUST_JS: &[u8] = include_bytes!("../../front-end/playground_editor/mode-rust.js");
pub static THEME_DAWN_JS: &[u8] = include_bytes!("../../front-end/playground_editor/theme-dawn.js");
pub static THEME_TOMORROW_NIGHT_JS: &[u8] =
include_bytes!("../../front-end/playground_editor/theme-tomorrow_night.js");

View file

@ -1,6 +1,6 @@
//! Theme dependencies for in-browser search. Not included in mdbook when
//! the "search" cargo feature is disabled.
pub static JS: &[u8] = include_bytes!("searcher.js");
pub static MARK_JS: &[u8] = include_bytes!("mark.min.js");
pub static ELASTICLUNR_JS: &[u8] = include_bytes!("elasticlunr.min.js");
pub static JS: &[u8] = include_bytes!("../../front-end/searcher/searcher.js");
pub static MARK_JS: &[u8] = include_bytes!("../../front-end/searcher/mark.min.js");
pub static ELASTICLUNR_JS: &[u8] = include_bytes!("../../front-end/searcher/elasticlunr.min.js");

View file

@ -3,11 +3,11 @@ use std::io::Write;
use std::path::PathBuf;
use super::MDBook;
use crate::theme;
use anyhow::{Context, Result};
use log::{debug, error, info, trace};
use mdbook_core::config::Config;
use mdbook_core::utils::fs::write_file;
use mdbook_html::theme;
/// A helper for setting up a new book and its directory structure.
#[derive(Debug, Clone, PartialEq)]

View file

@ -83,8 +83,6 @@
pub mod book;
pub mod preprocess;
pub mod renderer;
#[path = "front-end/mod.rs"]
pub mod theme;
pub use crate::book::BookItem;
pub use crate::book::MDBook;

View file

@ -2,7 +2,6 @@ use crate::book::{Book, BookItem};
use crate::renderer::html_handlebars::StaticFiles;
use crate::renderer::html_handlebars::helpers;
use crate::renderer::{RenderContext, Renderer};
use crate::theme::{self, Theme};
use std::borrow::Cow;
use std::collections::BTreeMap;
@ -17,6 +16,7 @@ use log::{debug, info, trace, warn};
use mdbook_core::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition};
use mdbook_core::utils;
use mdbook_core::utils::fs::get_404_output_file;
use mdbook_html::theme::Theme;
use mdbook_markdown::{render_markdown, render_markdown_with_path};
use regex::{Captures, Regex};
@ -362,7 +362,7 @@ impl Renderer for HtmlHandlebars {
None => ctx.root.join("theme"),
};
let theme = theme::Theme::new(theme_dir);
let theme = Theme::new(theme_dir);
debug!("Register the index handlebars template");
handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?;

View file

@ -8,13 +8,13 @@ use elasticlunr::{Index, IndexBuilder};
use log::{debug, warn};
use mdbook_core::config::{Search, SearchChapterSettings};
use mdbook_core::utils;
use mdbook_html::theme::searcher;
use mdbook_markdown::new_cmark_parser;
use pulldown_cmark::*;
use serde::Serialize;
use crate::book::{Book, BookItem, Chapter};
use crate::renderer::html_handlebars::StaticFiles;
use crate::theme::searcher;
const MAX_WORD_LENGTH_TO_INDEX: usize = 80;

View file

@ -4,9 +4,9 @@ use anyhow::{Context, Result};
use log::{debug, warn};
use mdbook_core::config::HtmlConfig;
use mdbook_core::utils;
use mdbook_html::theme::{self, Theme, playground_editor};
use crate::renderer::html_handlebars::helpers::resources::ResourceHelper;
use crate::theme::{self, Theme, playground_editor};
use std::borrow::Cow;
use std::collections::HashMap;
@ -300,9 +300,9 @@ impl StaticFiles {
#[cfg(test)]
mod tests {
use super::*;
use crate::theme::Theme;
use mdbook_core::config::HtmlConfig;
use mdbook_core::utils::fs::write_file;
use mdbook_html::theme::Theme;
use tempfile::TempDir;
#[test]