Skip to content

Commit 7fcacf3

Browse files
committed
Move theme copy to the Theme type and reduce visibility
This moves the code for copying the theme to the theme directory to the Theme type so that the code lives closer to the data definition. This also then reduces the public API surface of the Theme to give a little more flexibility for updating it in the future.
1 parent 2aa2b95 commit 7fcacf3

File tree

5 files changed

+96
-88
lines changed

5 files changed

+96
-88
lines changed

crates/mdbook-driver/src/init.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::MDBook;
44
use anyhow::{Context, Result};
55
use mdbook_core::config::Config;
66
use mdbook_core::utils::fs;
7-
use mdbook_html::theme;
7+
use mdbook_html::theme::Theme;
88
use std::path::PathBuf;
99
use tracing::{debug, error, info, trace};
1010

@@ -109,37 +109,7 @@ impl BookBuilder {
109109
debug!("Copying theme");
110110

111111
let html_config = self.config.html_config().unwrap_or_default();
112-
let themedir = html_config.theme_dir(&self.root);
113-
114-
fs::write(themedir.join("book.js"), theme::JS)?;
115-
fs::write(themedir.join("favicon.png"), theme::FAVICON_PNG)?;
116-
fs::write(themedir.join("favicon.svg"), theme::FAVICON_SVG)?;
117-
fs::write(themedir.join("highlight.css"), theme::HIGHLIGHT_CSS)?;
118-
fs::write(themedir.join("highlight.js"), theme::HIGHLIGHT_JS)?;
119-
fs::write(themedir.join("index.hbs"), theme::INDEX)?;
120-
121-
let cssdir = themedir.join("css");
122-
123-
fs::write(cssdir.join("general.css"), theme::GENERAL_CSS)?;
124-
fs::write(cssdir.join("chrome.css"), theme::CHROME_CSS)?;
125-
fs::write(cssdir.join("variables.css"), theme::VARIABLES_CSS)?;
126-
if html_config.print.enable {
127-
fs::write(cssdir.join("print.css"), theme::PRINT_CSS)?;
128-
}
129-
130-
let fonts_dir = themedir.join("fonts");
131-
fs::write(fonts_dir.join("fonts.css"), theme::fonts::CSS)?;
132-
for (file_name, contents) in theme::fonts::LICENSES {
133-
fs::write(themedir.join(file_name), contents)?;
134-
}
135-
for (file_name, contents) in theme::fonts::OPEN_SANS.iter() {
136-
fs::write(themedir.join(file_name), contents)?;
137-
}
138-
fs::write(
139-
themedir.join(theme::fonts::SOURCE_CODE_PRO.0),
140-
theme::fonts::SOURCE_CODE_PRO.1,
141-
)?;
142-
112+
Theme::copy_theme(&html_config, &self.root)?;
143113
Ok(())
144114
}
145115

crates/mdbook-html/src/theme/fonts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
pub static CSS: &[u8] = include_bytes!("../../front-end/fonts/fonts.css");
1+
pub(crate) static CSS: &[u8] = include_bytes!("../../front-end/fonts/fonts.css");
22
// An array of (file_name, file_contents) pairs
3-
pub static LICENSES: [(&str, &[u8]); 2] = [
3+
pub(crate) static LICENSES: [(&str, &[u8]); 2] = [
44
(
55
"fonts/OPEN-SANS-LICENSE.txt",
66
include_bytes!("../../front-end/fonts/OPEN-SANS-LICENSE.txt"),
@@ -11,7 +11,7 @@ pub static LICENSES: [(&str, &[u8]); 2] = [
1111
),
1212
];
1313
// An array of (file_name, file_contents) pairs
14-
pub static OPEN_SANS: [(&str, &[u8]); 10] = [
14+
pub(crate) static OPEN_SANS: [(&str, &[u8]); 10] = [
1515
(
1616
"fonts/open-sans-v17-all-charsets-300.woff2",
1717
include_bytes!("../../front-end/fonts/open-sans-v17-all-charsets-300.woff2"),
@@ -55,7 +55,7 @@ pub static OPEN_SANS: [(&str, &[u8]); 10] = [
5555
];
5656

5757
// A (file_name, file_contents) pair
58-
pub static SOURCE_CODE_PRO: (&str, &[u8]) = (
58+
pub(crate) static SOURCE_CODE_PRO: (&str, &[u8]) = (
5959
"fonts/source-code-pro-v11-all-charsets-500.woff2",
6060
include_bytes!("../../front-end/fonts/source-code-pro-v11-all-charsets-500.woff2"),
6161
);

crates/mdbook-html/src/theme/mod.rs

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
#![allow(missing_docs)]
1+
//! Support for theme files.
22
33
use anyhow::Result;
4+
use mdbook_core::config::HtmlConfig;
5+
use mdbook_core::utils::fs;
46
use std::path::{Path, PathBuf};
57
use tracing::{info, warn};
68

7-
pub mod fonts;
8-
pub mod playground_editor;
9+
pub(crate) mod fonts;
10+
pub(crate) mod playground_editor;
911
#[cfg(feature = "search")]
10-
pub mod searcher;
11-
12-
pub static INDEX: &[u8] = include_bytes!("../../front-end/templates/index.hbs");
13-
pub static HEAD: &[u8] = include_bytes!("../../front-end/templates/head.hbs");
14-
pub static REDIRECT: &[u8] = include_bytes!("../../front-end/templates/redirect.hbs");
15-
pub static HEADER: &[u8] = include_bytes!("../../front-end/templates/header.hbs");
16-
pub static TOC_JS: &[u8] = include_bytes!("../../front-end/templates/toc.js.hbs");
17-
pub static TOC_HTML: &[u8] = include_bytes!("../../front-end/templates/toc.html.hbs");
18-
pub static CHROME_CSS: &[u8] = include_bytes!("../../front-end/css/chrome.css");
19-
pub static GENERAL_CSS: &[u8] = include_bytes!("../../front-end/css/general.css");
20-
pub static PRINT_CSS: &[u8] = include_bytes!("../../front-end/css/print.css");
21-
pub static VARIABLES_CSS: &[u8] = include_bytes!("../../front-end/css/variables.css");
22-
pub static FAVICON_PNG: &[u8] = include_bytes!("../../front-end/images/favicon.png");
23-
pub static FAVICON_SVG: &[u8] = include_bytes!("../../front-end/images/favicon.svg");
24-
pub static JS: &[u8] = include_bytes!("../../front-end/js/book.js");
25-
pub static HIGHLIGHT_JS: &[u8] = include_bytes!("../../front-end/js/highlight.js");
26-
pub static TOMORROW_NIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/tomorrow-night.css");
27-
pub static HIGHLIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/highlight.css");
28-
pub static AYU_HIGHLIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/ayu-highlight.css");
29-
pub static CLIPBOARD_JS: &[u8] = include_bytes!("../../front-end/js/clipboard.min.js");
12+
pub(crate) mod searcher;
13+
14+
static INDEX: &[u8] = include_bytes!("../../front-end/templates/index.hbs");
15+
static HEAD: &[u8] = include_bytes!("../../front-end/templates/head.hbs");
16+
static REDIRECT: &[u8] = include_bytes!("../../front-end/templates/redirect.hbs");
17+
static HEADER: &[u8] = include_bytes!("../../front-end/templates/header.hbs");
18+
static TOC_JS: &[u8] = include_bytes!("../../front-end/templates/toc.js.hbs");
19+
static TOC_HTML: &[u8] = include_bytes!("../../front-end/templates/toc.html.hbs");
20+
static CHROME_CSS: &[u8] = include_bytes!("../../front-end/css/chrome.css");
21+
static GENERAL_CSS: &[u8] = include_bytes!("../../front-end/css/general.css");
22+
static PRINT_CSS: &[u8] = include_bytes!("../../front-end/css/print.css");
23+
static VARIABLES_CSS: &[u8] = include_bytes!("../../front-end/css/variables.css");
24+
static FAVICON_PNG: &[u8] = include_bytes!("../../front-end/images/favicon.png");
25+
static FAVICON_SVG: &[u8] = include_bytes!("../../front-end/images/favicon.svg");
26+
static JS: &[u8] = include_bytes!("../../front-end/js/book.js");
27+
static HIGHLIGHT_JS: &[u8] = include_bytes!("../../front-end/js/highlight.js");
28+
static TOMORROW_NIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/tomorrow-night.css");
29+
static HIGHLIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/highlight.css");
30+
static AYU_HIGHLIGHT_CSS: &[u8] = include_bytes!("../../front-end/css/ayu-highlight.css");
31+
static CLIPBOARD_JS: &[u8] = include_bytes!("../../front-end/js/clipboard.min.js");
3032

3133
/// The `Theme` struct should be used instead of the static variables because
3234
/// the `new()` method will look if the user has a theme directory in their
@@ -35,28 +37,27 @@ pub static CLIPBOARD_JS: &[u8] = include_bytes!("../../front-end/js/clipboard.mi
3537
/// You should only ever use the static variables directly if you want to
3638
/// override the user's theme with the defaults.
3739
#[derive(Debug, PartialEq)]
38-
#[non_exhaustive]
3940
pub struct Theme {
40-
pub index: Vec<u8>,
41-
pub head: Vec<u8>,
42-
pub redirect: Vec<u8>,
43-
pub header: Vec<u8>,
44-
pub toc_js: Vec<u8>,
45-
pub toc_html: Vec<u8>,
46-
pub chrome_css: Vec<u8>,
47-
pub general_css: Vec<u8>,
48-
pub print_css: Vec<u8>,
49-
pub variables_css: Vec<u8>,
50-
pub fonts_css: Option<Vec<u8>>,
51-
pub font_files: Vec<PathBuf>,
52-
pub favicon_png: Option<Vec<u8>>,
53-
pub favicon_svg: Option<Vec<u8>>,
54-
pub js: Vec<u8>,
55-
pub highlight_css: Vec<u8>,
56-
pub tomorrow_night_css: Vec<u8>,
57-
pub ayu_highlight_css: Vec<u8>,
58-
pub highlight_js: Vec<u8>,
59-
pub clipboard_js: Vec<u8>,
41+
pub(crate) index: Vec<u8>,
42+
pub(crate) head: Vec<u8>,
43+
pub(crate) redirect: Vec<u8>,
44+
pub(crate) header: Vec<u8>,
45+
pub(crate) toc_js: Vec<u8>,
46+
pub(crate) toc_html: Vec<u8>,
47+
pub(crate) chrome_css: Vec<u8>,
48+
pub(crate) general_css: Vec<u8>,
49+
pub(crate) print_css: Vec<u8>,
50+
pub(crate) variables_css: Vec<u8>,
51+
pub(crate) fonts_css: Option<Vec<u8>>,
52+
pub(crate) font_files: Vec<PathBuf>,
53+
pub(crate) favicon_png: Option<Vec<u8>>,
54+
pub(crate) favicon_svg: Option<Vec<u8>>,
55+
pub(crate) js: Vec<u8>,
56+
pub(crate) highlight_css: Vec<u8>,
57+
pub(crate) tomorrow_night_css: Vec<u8>,
58+
pub(crate) ayu_highlight_css: Vec<u8>,
59+
pub(crate) highlight_js: Vec<u8>,
60+
pub(crate) clipboard_js: Vec<u8>,
6061
}
6162

6263
impl Theme {
@@ -160,6 +161,40 @@ impl Theme {
160161

161162
theme
162163
}
164+
165+
/// Copies the default theme files to the theme directory.
166+
pub fn copy_theme(html_config: &HtmlConfig, root: &Path) -> Result<()> {
167+
let themedir = html_config.theme_dir(root);
168+
169+
fs::write(themedir.join("book.js"), JS)?;
170+
fs::write(themedir.join("favicon.png"), FAVICON_PNG)?;
171+
fs::write(themedir.join("favicon.svg"), FAVICON_SVG)?;
172+
fs::write(themedir.join("highlight.css"), HIGHLIGHT_CSS)?;
173+
fs::write(themedir.join("highlight.js"), HIGHLIGHT_JS)?;
174+
fs::write(themedir.join("index.hbs"), INDEX)?;
175+
176+
let cssdir = themedir.join("css");
177+
178+
fs::write(cssdir.join("general.css"), GENERAL_CSS)?;
179+
fs::write(cssdir.join("chrome.css"), CHROME_CSS)?;
180+
fs::write(cssdir.join("variables.css"), VARIABLES_CSS)?;
181+
if html_config.print.enable {
182+
fs::write(cssdir.join("print.css"), PRINT_CSS)?;
183+
}
184+
185+
fs::write(themedir.join("fonts").join("fonts.css"), fonts::CSS)?;
186+
for (file_name, contents) in fonts::LICENSES {
187+
fs::write(themedir.join(file_name), contents)?;
188+
}
189+
for (file_name, contents) in fonts::OPEN_SANS.iter() {
190+
fs::write(themedir.join(file_name), contents)?;
191+
}
192+
fs::write(
193+
themedir.join(fonts::SOURCE_CODE_PRO.0),
194+
fonts::SOURCE_CODE_PRO.1,
195+
)?;
196+
Ok(())
197+
}
163198
}
164199

165200
impl Default for Theme {
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Theme dependencies for the playground editor.
22
3-
pub static JS: &[u8] = include_bytes!("../../front-end/playground_editor/editor.js");
4-
pub static ACE_JS: &[u8] = include_bytes!("../../front-end/playground_editor/ace.js");
5-
pub static MODE_RUST_JS: &[u8] = include_bytes!("../../front-end/playground_editor/mode-rust.js");
6-
pub static THEME_DAWN_JS: &[u8] = include_bytes!("../../front-end/playground_editor/theme-dawn.js");
7-
pub static THEME_TOMORROW_NIGHT_JS: &[u8] =
3+
pub(crate) static JS: &[u8] = include_bytes!("../../front-end/playground_editor/editor.js");
4+
pub(crate) static ACE_JS: &[u8] = include_bytes!("../../front-end/playground_editor/ace.js");
5+
pub(crate) static MODE_RUST_JS: &[u8] =
6+
include_bytes!("../../front-end/playground_editor/mode-rust.js");
7+
pub(crate) static THEME_DAWN_JS: &[u8] =
8+
include_bytes!("../../front-end/playground_editor/theme-dawn.js");
9+
pub(crate) static THEME_TOMORROW_NIGHT_JS: &[u8] =
810
include_bytes!("../../front-end/playground_editor/theme-tomorrow_night.js");
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Theme dependencies for in-browser search. Not included in mdbook when
22
//! the "search" cargo feature is disabled.
33
4-
pub static JS: &[u8] = include_bytes!("../../front-end/searcher/searcher.js");
5-
pub static MARK_JS: &[u8] = include_bytes!("../../front-end/searcher/mark.min.js");
6-
pub static ELASTICLUNR_JS: &[u8] = include_bytes!("../../front-end/searcher/elasticlunr.min.js");
4+
pub(crate) static JS: &[u8] = include_bytes!("../../front-end/searcher/searcher.js");
5+
pub(crate) static MARK_JS: &[u8] = include_bytes!("../../front-end/searcher/mark.min.js");
6+
pub(crate) static ELASTICLUNR_JS: &[u8] =
7+
include_bytes!("../../front-end/searcher/elasticlunr.min.js");

0 commit comments

Comments
 (0)