1- #! [ allow ( missing_docs ) ]
1+ //! Support for theme files.
22
33use anyhow:: Result ;
4+ use mdbook_core:: config:: HtmlConfig ;
5+ use mdbook_core:: utils:: fs;
46use std:: path:: { Path , PathBuf } ;
57use 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]
3940pub 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
6263impl 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
165200impl Default for Theme {
0 commit comments