File tree Expand file tree Collapse file tree 3 files changed +42
-13
lines changed Expand file tree Collapse file tree 3 files changed +42
-13
lines changed Original file line number Diff line number Diff line change @@ -110,10 +110,7 @@ impl BookBuilder {
110110 debug ! ( "Copying theme" ) ;
111111
112112 let html_config = self . config . html_config ( ) . unwrap_or_default ( ) ;
113- let themedir = html_config
114- . theme
115- . unwrap_or_else ( || self . config . book . src . join ( "theme" ) ) ;
116- let themedir = self . root . join ( themedir) ;
113+ let themedir = html_config. theme_dir ( & self . root ) ;
117114
118115 if !themedir. exists ( ) {
119116 debug ! (
@@ -127,7 +124,9 @@ impl BookBuilder {
127124 index. write_all ( theme:: INDEX ) ?;
128125
129126 let cssdir = themedir. join ( "css" ) ;
130- fs:: create_dir ( & cssdir) ?;
127+ if !cssdir. exists ( ) {
128+ fs:: create_dir ( & cssdir) ?;
129+ }
131130
132131 let mut general_css = File :: create ( cssdir. join ( "general.css" ) ) ?;
133132 general_css. write_all ( theme:: GENERAL_CSS ) ?;
Original file line number Diff line number Diff line change @@ -28,15 +28,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
2828
2929 // If flag `--theme` is present, copy theme to src
3030 if args. is_present ( "theme" ) {
31- config. set ( "output.html.theme" , "src/theme" ) ?;
31+ let theme_dir = book_dir. join ( "theme" ) ;
32+ println ! ( ) ;
33+ println ! ( "Copying the default theme to {}" , theme_dir. display( ) ) ;
3234 // Skip this if `--force` is present
33- if !args. is_present ( "force" ) {
34- // Print warning
35- println ! ( ) ;
36- println ! (
37- "Copying the default theme to {}" ,
38- builder. config( ) . book. src. display( )
39- ) ;
35+ if !args. is_present ( "force" ) && theme_dir. exists ( ) {
4036 println ! ( "This could potentially overwrite files already present in that directory." ) ;
4137 print ! ( "\n Are you sure you want to continue? (y/n) " ) ;
4238
Original file line number Diff line number Diff line change @@ -108,3 +108,37 @@ fn book_toml_isnt_required() {
108108
109109 md. build ( ) . unwrap ( ) ;
110110}
111+
112+ #[ test]
113+ fn copy_theme ( ) {
114+ let temp = TempFileBuilder :: new ( ) . prefix ( "mdbook" ) . tempdir ( ) . unwrap ( ) ;
115+ MDBook :: init ( temp. path ( ) ) . copy_theme ( true ) . build ( ) . unwrap ( ) ;
116+ let expected = vec ! [
117+ "book.js" ,
118+ "css/chrome.css" ,
119+ "css/general.css" ,
120+ "css/print.css" ,
121+ "css/variables.css" ,
122+ "favicon.png" ,
123+ "favicon.svg" ,
124+ "highlight.css" ,
125+ "highlight.js" ,
126+ "index.hbs" ,
127+ ] ;
128+ let theme_dir = temp. path ( ) . join ( "theme" ) ;
129+ let mut actual: Vec < _ > = walkdir:: WalkDir :: new ( & theme_dir)
130+ . into_iter ( )
131+ . filter_map ( |e| e. ok ( ) )
132+ . filter ( |e| !e. file_type ( ) . is_dir ( ) )
133+ . map ( |e| {
134+ e. path ( )
135+ . strip_prefix ( & theme_dir)
136+ . unwrap ( )
137+ . to_str ( )
138+ . unwrap ( )
139+ . replace ( '\\' , "/" )
140+ } )
141+ . collect ( ) ;
142+ actual. sort ( ) ;
143+ assert_eq ! ( actual, expected) ;
144+ }
You can’t perform that action at this time.
0 commit comments