@@ -39,6 +39,7 @@ pub use util::qualify_drilldown_name;
39
39
pub use util:: DocNodeKindCtx ;
40
40
pub use util:: HrefResolver ;
41
41
pub use util:: NamespacedGlobalSymbols ;
42
+ pub use util:: SectionHeaderCtx ;
42
43
pub use util:: UrlResolveKind ;
43
44
44
45
pub const STYLESHEET : & str = include_str ! ( "./templates/styles.gen.css" ) ;
@@ -87,11 +88,11 @@ pub struct GenerateOptions {
87
88
pub composable_output : bool ,
88
89
}
89
90
91
+ #[ non_exhaustive]
90
92
pub struct GenerateCtx < ' ctx > {
91
93
pub package_name : Option < String > ,
92
94
pub common_ancestor : Option < PathBuf > ,
93
- pub main_entrypoint : Option < ModuleSpecifier > ,
94
- pub specifiers : Vec < ModuleSpecifier > ,
95
+ pub doc_nodes : IndexMap < Rc < ShortPath > , Vec < DocNodeWithContext > > ,
95
96
pub hbs : Handlebars < ' ctx > ,
96
97
pub highlight_adapter : comrak_adapters:: HighlightAdapter ,
97
98
#[ cfg( feature = "ammonia" ) ]
@@ -104,14 +105,21 @@ pub struct GenerateCtx<'ctx> {
104
105
}
105
106
106
107
impl < ' ctx > GenerateCtx < ' ctx > {
107
- pub fn doc_nodes_by_url_add_context (
108
- & self ,
108
+ pub fn new (
109
+ options : GenerateOptions ,
110
+ common_ancestor : Option < PathBuf > ,
111
+ file_mode : FileMode ,
109
112
doc_nodes_by_url : IndexMap < ModuleSpecifier , Vec < DocNode > > ,
110
- ) -> ContextDocNodesByShortPath {
111
- doc_nodes_by_url
113
+ ) -> Result < Self , anyhow :: Error > {
114
+ let doc_nodes = doc_nodes_by_url
112
115
. into_iter ( )
113
116
. map ( |( specifier, nodes) | {
114
- let short_path = Rc :: new ( ShortPath :: new ( self , specifier) ) ;
117
+ let short_path = Rc :: new ( ShortPath :: new (
118
+ specifier,
119
+ options. main_entrypoint . as_ref ( ) ,
120
+ options. rewrite_map . as_ref ( ) ,
121
+ common_ancestor. as_ref ( ) ,
122
+ ) ) ;
115
123
116
124
let nodes = nodes
117
125
. into_iter ( )
@@ -126,7 +134,22 @@ impl<'ctx> GenerateCtx<'ctx> {
126
134
127
135
( short_path, nodes)
128
136
} )
129
- . collect :: < IndexMap < _ , _ > > ( )
137
+ . collect :: < IndexMap < _ , _ > > ( ) ;
138
+
139
+ Ok ( Self {
140
+ package_name : options. package_name ,
141
+ common_ancestor,
142
+ doc_nodes,
143
+ hbs : setup_hbs ( ) ?,
144
+ highlight_adapter : setup_highlighter ( false ) ,
145
+ #[ cfg( feature = "ammonia" ) ]
146
+ url_rewriter : None ,
147
+ href_resolver : options. href_resolver ,
148
+ usage_composer : options. usage_composer ,
149
+ rewrite_map : options. rewrite_map ,
150
+ sidebar_hide_all_symbols : file_mode == FileMode :: SingleDts ,
151
+ file_mode,
152
+ } )
130
153
}
131
154
132
155
pub fn render < T : serde:: Serialize > (
@@ -138,9 +161,6 @@ impl<'ctx> GenerateCtx<'ctx> {
138
161
}
139
162
}
140
163
141
- pub type ContextDocNodesByShortPath =
142
- IndexMap < Rc < ShortPath > , Vec < DocNodeWithContext > > ;
143
-
144
164
#[ derive( Clone , Debug , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
145
165
pub struct ShortPath {
146
166
pub path : String ,
@@ -149,16 +169,17 @@ pub struct ShortPath {
149
169
}
150
170
151
171
impl ShortPath {
152
- pub fn new ( ctx : & GenerateCtx , specifier : ModuleSpecifier ) -> Self {
153
- let is_main = ctx
154
- . main_entrypoint
155
- . as_ref ( )
172
+ pub fn new (
173
+ specifier : ModuleSpecifier ,
174
+ main_entrypoint : Option < & ModuleSpecifier > ,
175
+ rewrite_map : Option < & IndexMap < ModuleSpecifier , String > > ,
176
+ common_ancestor : Option < & PathBuf > ,
177
+ ) -> Self {
178
+ let is_main = main_entrypoint
156
179
. is_some_and ( |main_entrypoint| main_entrypoint == & specifier) ;
157
180
158
- if let Some ( rewrite) = ctx
159
- . rewrite_map
160
- . as_ref ( )
161
- . and_then ( |rewrite_map| rewrite_map. get ( & specifier) )
181
+ if let Some ( rewrite) =
182
+ rewrite_map. and_then ( |rewrite_map| rewrite_map. get ( & specifier) )
162
183
{
163
184
return ShortPath {
164
185
path : rewrite. to_owned ( ) ,
@@ -175,7 +196,7 @@ impl ShortPath {
175
196
} ;
176
197
} ;
177
198
178
- let Some ( common_ancestor) = & ctx . common_ancestor else {
199
+ let Some ( common_ancestor) = common_ancestor else {
179
200
return ShortPath {
180
201
path : url_file_path. to_string_lossy ( ) . to_string ( ) ,
181
202
specifier,
@@ -471,30 +492,17 @@ pub fn generate(
471
492
FileMode :: Normal
472
493
} ;
473
494
495
+ let composable_output = options. composable_output ;
496
+
474
497
let common_ancestor = find_common_ancestor ( doc_nodes_by_url. keys ( ) , true ) ;
475
- let ctx = GenerateCtx {
476
- package_name : options. package_name ,
477
- common_ancestor,
478
- main_entrypoint : options. main_entrypoint ,
479
- specifiers : doc_nodes_by_url. keys ( ) . cloned ( ) . collect ( ) ,
480
- hbs : setup_hbs ( ) ?,
481
- highlight_adapter : setup_highlighter ( false ) ,
482
- #[ cfg( feature = "ammonia" ) ]
483
- url_rewriter : None ,
484
- href_resolver : options. href_resolver ,
485
- usage_composer : options. usage_composer ,
486
- rewrite_map : options. rewrite_map ,
487
- sidebar_hide_all_symbols : file_mode == FileMode :: SingleDts ,
488
- file_mode,
489
- } ;
498
+ let ctx =
499
+ GenerateCtx :: new ( options, common_ancestor, file_mode, doc_nodes_by_url) ?;
490
500
let mut files = HashMap :: new ( ) ;
491
501
492
- let doc_nodes_by_short_path =
493
- ctx. doc_nodes_by_url_add_context ( doc_nodes_by_url) ;
494
-
495
502
// Index page
496
503
{
497
- let main_entrypoint = doc_nodes_by_short_path
504
+ let main_entrypoint = ctx
505
+ . doc_nodes
498
506
. iter ( )
499
507
. find ( |( short_path, _) | short_path. is_main ) ;
500
508
@@ -508,11 +516,10 @@ pub fn generate(
508
516
let index = pages:: IndexCtx :: new (
509
517
& ctx,
510
518
main_entrypoint. map ( |( short_path, _) | short_path. clone ( ) ) ,
511
- & doc_nodes_by_short_path,
512
519
partitions_for_entrypoint_nodes,
513
520
) ;
514
521
515
- if options . composable_output {
522
+ if composable_output {
516
523
files. insert (
517
524
"./sidepanel.html" . to_string ( ) ,
518
525
ctx. render (
@@ -549,7 +556,8 @@ pub fn generate(
549
556
550
557
// All symbols (list of all symbols in all files)
551
558
if ctx. file_mode != FileMode :: SingleDts {
552
- let all_doc_nodes = doc_nodes_by_short_path
559
+ let all_doc_nodes = ctx
560
+ . doc_nodes
553
561
. values ( )
554
562
. flatten ( )
555
563
. cloned ( )
@@ -558,13 +566,9 @@ pub fn generate(
558
566
let partitions_by_kind =
559
567
partition:: partition_nodes_by_entrypoint ( & all_doc_nodes, true ) ;
560
568
561
- let all_symbols = pages:: AllSymbolsCtx :: new (
562
- & ctx,
563
- partitions_by_kind,
564
- & doc_nodes_by_short_path,
565
- ) ;
569
+ let all_symbols = pages:: AllSymbolsCtx :: new ( & ctx, partitions_by_kind) ;
566
570
567
- if options . composable_output {
571
+ if composable_output {
568
572
files. insert (
569
573
"./all_symbols/content.html" . to_string ( ) ,
570
574
ctx. render ( SymbolContentCtx :: TEMPLATE , & all_symbols. content ) ,
@@ -585,9 +589,8 @@ pub fn generate(
585
589
586
590
// Pages for all discovered symbols
587
591
{
588
- for ( short_path, doc_nodes) in & doc_nodes_by_short_path {
589
- let partitions_for_nodes =
590
- partition:: get_partitions_for_file ( & ctx, doc_nodes) ;
592
+ for ( short_path, doc_nodes) in & ctx. doc_nodes {
593
+ let partitions_for_nodes = get_partitions_for_file ( & ctx, doc_nodes) ;
591
594
592
595
let symbol_pages = generate_symbol_pages_for_module (
593
596
& ctx,
@@ -605,7 +608,7 @@ pub fn generate(
605
608
} => {
606
609
let root = ctx. href_resolver . resolve_path (
607
610
UrlResolveKind :: Symbol {
608
- file : & short_path,
611
+ file : short_path,
609
612
symbol : & symbol_group_ctx. name ,
610
613
} ,
611
614
UrlResolveKind :: Root ,
@@ -618,7 +621,7 @@ pub fn generate(
618
621
Some ( short_path) ,
619
622
) ;
620
623
621
- if options . composable_output {
624
+ if composable_output {
622
625
let dir_name =
623
626
format ! ( "{}/~/{}" , short_path. path, symbol_group_ctx. name) ;
624
627
@@ -660,7 +663,7 @@ pub fn generate(
660
663
} => {
661
664
let redirect = serde_json:: json!( { "path" : href } ) ;
662
665
663
- if options . composable_output {
666
+ if composable_output {
664
667
let file_name = format ! (
665
668
"{}/~/{}/redirect.json" ,
666
669
short_path. path, current_symbol
@@ -677,60 +680,63 @@ pub fn generate(
677
680
}
678
681
} ) ) ;
679
682
680
- let index = pages:: IndexCtx :: new (
681
- & ctx,
682
- Some ( short_path. clone ( ) ) ,
683
- & doc_nodes_by_short_path,
684
- partitions_for_nodes,
685
- ) ;
686
-
687
- if options. composable_output {
688
- let dir = format ! ( "{}/~" , short_path. path) ;
689
- files. insert (
690
- format ! ( "{dir}/sidepanel.html" ) ,
691
- ctx. render (
692
- sidepanels:: IndexSidepanelCtx :: TEMPLATE ,
693
- & index. sidepanel_ctx ,
694
- ) ,
695
- ) ;
696
-
697
- files. insert (
698
- format ! ( "{dir}/breadcrumbs.html" ) ,
699
- ctx. render ( util:: BreadcrumbsCtx :: TEMPLATE , & index. breadcrumbs_ctx ) ,
683
+ if !short_path. is_main {
684
+ let index = pages:: IndexCtx :: new (
685
+ & ctx,
686
+ Some ( short_path. clone ( ) ) ,
687
+ partitions_for_nodes,
700
688
) ;
701
689
702
- if index. module_doc . is_some ( ) || index. all_symbols . is_some ( ) {
703
- let mut out = String :: new ( ) ;
690
+ if composable_output {
691
+ let dir = format ! ( "{}/~" , short_path. path) ;
692
+ files. insert (
693
+ format ! ( "{dir}/sidepanel.html" ) ,
694
+ ctx. render (
695
+ sidepanels:: IndexSidepanelCtx :: TEMPLATE ,
696
+ & index. sidepanel_ctx ,
697
+ ) ,
698
+ ) ;
699
+
700
+ files. insert (
701
+ format ! ( "{dir}/breadcrumbs.html" ) ,
702
+ ctx. render ( util:: BreadcrumbsCtx :: TEMPLATE , & index. breadcrumbs_ctx ) ,
703
+ ) ;
704
+
705
+ if index. module_doc . is_some ( ) || index. all_symbols . is_some ( ) {
706
+ let mut out = String :: new ( ) ;
707
+
708
+ if let Some ( module_doc) = index. module_doc {
709
+ out. push_str (
710
+ & ctx. render ( jsdoc:: ModuleDocCtx :: TEMPLATE , & module_doc) ,
711
+ ) ;
712
+ }
704
713
705
- if let Some ( module_doc ) = index. module_doc {
706
- out. push_str (
707
- & ctx. render ( jsdoc :: ModuleDocCtx :: TEMPLATE , & module_doc ) ,
708
- ) ;
709
- }
714
+ if let Some ( all_symbols ) = index. all_symbols {
715
+ out. push_str (
716
+ & ctx. render ( SymbolContentCtx :: TEMPLATE , & all_symbols ) ,
717
+ ) ;
718
+ }
710
719
711
- if let Some ( all_symbols) = index. all_symbols {
712
- out. push_str ( & ctx. render ( SymbolContentCtx :: TEMPLATE , & all_symbols) ) ;
720
+ files. insert ( format ! ( "{dir}/content.html" ) , out) ;
713
721
}
714
-
715
- files. insert ( format ! ( "{dir}/content.html" ) , out) ;
722
+ } else {
723
+ files. insert (
724
+ format ! ( "{}/~/index.html" , short_path. path) ,
725
+ ctx. render ( pages:: IndexCtx :: TEMPLATE , & index) ,
726
+ ) ;
716
727
}
717
- } else {
718
- files. insert (
719
- format ! ( "{}/~/index.html" , short_path. path) ,
720
- ctx. render ( pages:: IndexCtx :: TEMPLATE , & index) ,
721
- ) ;
722
728
}
723
729
}
724
730
}
725
731
726
732
files. insert ( STYLESHEET_FILENAME . into ( ) , STYLESHEET . into ( ) ) ;
727
733
files. insert (
728
734
SEARCH_INDEX_FILENAME . into ( ) ,
729
- search:: get_search_index_file ( & ctx, & doc_nodes_by_short_path ) ?,
735
+ search:: get_search_index_file ( & ctx) ?,
730
736
) ;
731
737
files. insert ( SCRIPT_FILENAME . into ( ) , SCRIPT_JS . into ( ) ) ;
732
738
733
- if !options . composable_output {
739
+ if !composable_output {
734
740
files. insert ( PAGE_STYLESHEET_FILENAME . into ( ) , PAGE_STYLESHEET . into ( ) ) ;
735
741
files. insert ( FUSE_FILENAME . into ( ) , FUSE_JS . into ( ) ) ;
736
742
files. insert ( SEARCH_FILENAME . into ( ) , SEARCH_JS . into ( ) ) ;
0 commit comments