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