@@ -188,8 +188,8 @@ crate struct SharedContext {
188
188
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
189
189
/// should be ordered alphabetically or in order of appearance (in the source code).
190
190
pub sort_modules_alphabetically : bool ,
191
- /// Additional themes to be added to the generated docs.
192
- pub themes : Vec < PathBuf > ,
191
+ /// Additional CSS files to be added to the generated docs.
192
+ pub style_files : Vec < StylePath > ,
193
193
/// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
194
194
/// "light-v2.css").
195
195
pub resource_suffix : String ,
@@ -418,6 +418,14 @@ impl Serialize for TypeWithKind {
418
418
}
419
419
}
420
420
421
+ #[ derive( Debug , Clone ) ]
422
+ pub struct StylePath {
423
+ /// The path to the theme
424
+ pub path : PathBuf ,
425
+ /// What the `disabled` attribute should be set to in the HTML tag
426
+ pub disabled : bool ,
427
+ }
428
+
421
429
thread_local ! ( static CACHE_KEY : RefCell <Arc <Cache >> = Default :: default ( ) ) ;
422
430
thread_local ! ( pub static CURRENT_DEPTH : Cell <usize > = Cell :: new( 0 ) ) ;
423
431
@@ -461,7 +469,7 @@ pub fn run(
461
469
id_map,
462
470
playground_url,
463
471
sort_modules_alphabetically,
464
- themes,
472
+ themes : style_files ,
465
473
extension_css,
466
474
extern_html_root_urls,
467
475
resource_suffix,
@@ -531,7 +539,7 @@ pub fn run(
531
539
layout,
532
540
created_dirs : Default :: default ( ) ,
533
541
sort_modules_alphabetically,
534
- themes ,
542
+ style_files ,
535
543
resource_suffix,
536
544
static_root_path,
537
545
fs : DocFS :: new ( & errors) ,
@@ -540,6 +548,19 @@ pub fn run(
540
548
playground,
541
549
} ;
542
550
551
+ // Add the default themes to the `Vec` of stylepaths
552
+ //
553
+ // Note that these must be added before `sources::render` is called
554
+ // so that the resulting source pages are styled
555
+ //
556
+ // `light.css` is not disabled because it is the stylesheet that stays loaded
557
+ // by the browser as the theme stylesheet. The theme system (hackily) works by
558
+ // changing the href to this stylesheet. All other themes are disabled to
559
+ // prevent rule conflicts
560
+ scx. style_files . push ( StylePath { path : PathBuf :: from ( "light.css" ) , disabled : false } ) ;
561
+ scx. style_files . push ( StylePath { path : PathBuf :: from ( "dark.css" ) , disabled : true } ) ;
562
+ scx. style_files . push ( StylePath { path : PathBuf :: from ( "ayu.css" ) , disabled : true } ) ;
563
+
543
564
let dst = output;
544
565
scx. ensure_dir ( & dst) ?;
545
566
krate = sources:: render ( & dst, & mut scx, krate) ?;
@@ -616,11 +637,40 @@ fn write_shared(
616
637
// then we'll run over the "official" styles.
617
638
let mut themes: FxHashSet < String > = FxHashSet :: default ( ) ;
618
639
619
- for entry in & cx. shared . themes {
620
- let content = try_err ! ( fs:: read( & entry) , & entry) ;
621
- let theme = try_none ! ( try_none!( entry. file_stem( ) , & entry) . to_str( ) , & entry) ;
622
- let extension = try_none ! ( try_none!( entry. extension( ) , & entry) . to_str( ) , & entry) ;
623
- cx. shared . fs . write ( cx. path ( & format ! ( "{}.{}" , theme, extension) ) , content. as_slice ( ) ) ?;
640
+ for entry in & cx. shared . style_files {
641
+ let theme = try_none ! ( try_none!( entry. path. file_stem( ) , & entry. path) . to_str( ) , & entry. path) ;
642
+ let extension =
643
+ try_none ! ( try_none!( entry. path. extension( ) , & entry. path) . to_str( ) , & entry. path) ;
644
+
645
+ // Handle the official themes
646
+ match theme {
647
+ "light" => write_minify (
648
+ & cx. shared . fs ,
649
+ cx. path ( "light.css" ) ,
650
+ static_files:: themes:: LIGHT ,
651
+ options. enable_minification ,
652
+ ) ?,
653
+ "dark" => write_minify (
654
+ & cx. shared . fs ,
655
+ cx. path ( "dark.css" ) ,
656
+ static_files:: themes:: DARK ,
657
+ options. enable_minification ,
658
+ ) ?,
659
+ "ayu" => write_minify (
660
+ & cx. shared . fs ,
661
+ cx. path ( "ayu.css" ) ,
662
+ static_files:: themes:: AYU ,
663
+ options. enable_minification ,
664
+ ) ?,
665
+ _ => {
666
+ // Handle added third-party themes
667
+ let content = try_err ! ( fs:: read( & entry. path) , & entry. path) ;
668
+ cx. shared
669
+ . fs
670
+ . write ( cx. path ( & format ! ( "{}.{}" , theme, extension) ) , content. as_slice ( ) ) ?;
671
+ }
672
+ } ;
673
+
624
674
themes. insert ( theme. to_owned ( ) ) ;
625
675
}
626
676
@@ -634,20 +684,6 @@ fn write_shared(
634
684
write ( cx. path ( "brush.svg" ) , static_files:: BRUSH_SVG ) ?;
635
685
write ( cx. path ( "wheel.svg" ) , static_files:: WHEEL_SVG ) ?;
636
686
write ( cx. path ( "down-arrow.svg" ) , static_files:: DOWN_ARROW_SVG ) ?;
637
- write_minify (
638
- & cx. shared . fs ,
639
- cx. path ( "light.css" ) ,
640
- static_files:: themes:: LIGHT ,
641
- options. enable_minification ,
642
- ) ?;
643
- themes. insert ( "light" . to_owned ( ) ) ;
644
- write_minify (
645
- & cx. shared . fs ,
646
- cx. path ( "dark.css" ) ,
647
- static_files:: themes:: DARK ,
648
- options. enable_minification ,
649
- ) ?;
650
- themes. insert ( "dark" . to_owned ( ) ) ;
651
687
652
688
let mut themes: Vec < & String > = themes. iter ( ) . collect ( ) ;
653
689
themes. sort ( ) ;
@@ -958,7 +994,7 @@ themePicker.onblur = handleThemeButtonsBlur;
958
994
} )
959
995
. collect:: <String >( )
960
996
) ;
961
- let v = layout:: render ( & cx. shared . layout , & page, "" , content, & cx. shared . themes ) ;
997
+ let v = layout:: render ( & cx. shared . layout , & page, "" , content, & cx. shared . style_files ) ;
962
998
cx. shared . fs . write ( & dst, v. as_bytes ( ) ) ?;
963
999
}
964
1000
}
@@ -1376,7 +1412,7 @@ impl Context {
1376
1412
& page,
1377
1413
sidebar,
1378
1414
|buf : & mut Buffer | all. print ( buf) ,
1379
- & self . shared . themes ,
1415
+ & self . shared . style_files ,
1380
1416
) ;
1381
1417
self . shared . fs . write ( & final_file, v. as_bytes ( ) ) ?;
1382
1418
@@ -1385,9 +1421,9 @@ impl Context {
1385
1421
page. description = "Settings of Rustdoc" ;
1386
1422
page. root_path = "./" ;
1387
1423
1388
- let mut themes = self . shared . themes . clone ( ) ;
1424
+ let mut style_files = self . shared . style_files . clone ( ) ;
1389
1425
let sidebar = "<p class='location'>Settings</p><div class='sidebar-elems'></div>" ;
1390
- themes . push ( PathBuf :: from ( "settings.css" ) ) ;
1426
+ style_files . push ( StylePath { path : PathBuf :: from ( "settings.css" ) , disabled : false } ) ;
1391
1427
let v = layout:: render (
1392
1428
& self . shared . layout ,
1393
1429
& page,
@@ -1396,7 +1432,7 @@ impl Context {
1396
1432
self . shared . static_root_path . as_deref ( ) . unwrap_or ( "./" ) ,
1397
1433
& self . shared . resource_suffix ,
1398
1434
) ,
1399
- & themes ,
1435
+ & style_files ,
1400
1436
) ;
1401
1437
self . shared . fs . write ( & settings_file, v. as_bytes ( ) ) ?;
1402
1438
@@ -1458,7 +1494,7 @@ impl Context {
1458
1494
& page,
1459
1495
|buf : & mut _ | print_sidebar ( self , it, buf) ,
1460
1496
|buf : & mut _ | print_item ( self , it, buf) ,
1461
- & self . shared . themes ,
1497
+ & self . shared . style_files ,
1462
1498
)
1463
1499
} else {
1464
1500
let mut url = self . root_path ( ) ;
0 commit comments