Skip to content

Commit c534ec5

Browse files
committed
simplify modulespecifier, shortpath and doc_nodes_by_url usages
1 parent e367780 commit c534ec5

17 files changed

+350
-639
lines changed

src/html/jsdoc.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::render_context::RenderContext;
22
use super::util::*;
33
use crate::html::usage::UsagesCtx;
4+
use crate::html::FileMode;
45
use crate::html::ShortPath;
56
use crate::js_doc::JsDoc;
67
use crate::js_doc::JsDocTag;
@@ -553,12 +554,8 @@ pub struct ModuleDocCtx {
553554
impl ModuleDocCtx {
554555
pub const TEMPLATE: &'static str = "module_doc";
555556

556-
pub fn new(
557-
render_ctx: &RenderContext,
558-
short_path: &ShortPath,
559-
doc_nodes_by_url: &super::ContextDocNodesByShortPath,
560-
) -> Self {
561-
let module_doc_nodes = doc_nodes_by_url.get(short_path).unwrap();
557+
pub fn new(render_ctx: &RenderContext, short_path: &ShortPath) -> Self {
558+
let module_doc_nodes = render_ctx.ctx.doc_nodes.get(short_path).unwrap();
562559

563560
let mut sections = Vec::with_capacity(7);
564561

@@ -600,7 +597,7 @@ impl ModuleDocCtx {
600597
(None, None, None)
601598
};
602599

603-
if short_path.is_main {
600+
if short_path.is_main && render_ctx.ctx.file_mode == FileMode::SingleDts {
604601
let partitions_by_kind =
605602
super::partition::partition_nodes_by_kind(module_doc_nodes, true);
606603

src/html/mod.rs

Lines changed: 98 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub use util::qualify_drilldown_name;
3939
pub use util::DocNodeKindCtx;
4040
pub use util::HrefResolver;
4141
pub use util::NamespacedGlobalSymbols;
42+
pub use util::SectionHeaderCtx;
4243
pub use util::UrlResolveKind;
4344

4445
pub const STYLESHEET: &str = include_str!("./templates/styles.gen.css");
@@ -87,11 +88,11 @@ pub struct GenerateOptions {
8788
pub composable_output: bool,
8889
}
8990

91+
#[non_exhaustive]
9092
pub struct GenerateCtx<'ctx> {
9193
pub package_name: Option<String>,
9294
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>>,
9596
pub hbs: Handlebars<'ctx>,
9697
pub highlight_adapter: comrak_adapters::HighlightAdapter,
9798
#[cfg(feature = "ammonia")]
@@ -104,14 +105,21 @@ pub struct GenerateCtx<'ctx> {
104105
}
105106

106107
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,
109112
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
112115
.into_iter()
113116
.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+
));
115123

116124
let nodes = nodes
117125
.into_iter()
@@ -126,7 +134,22 @@ impl<'ctx> GenerateCtx<'ctx> {
126134

127135
(short_path, nodes)
128136
})
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+
})
130153
}
131154

132155
pub fn render<T: serde::Serialize>(
@@ -138,9 +161,6 @@ impl<'ctx> GenerateCtx<'ctx> {
138161
}
139162
}
140163

141-
pub type ContextDocNodesByShortPath =
142-
IndexMap<Rc<ShortPath>, Vec<DocNodeWithContext>>;
143-
144164
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
145165
pub struct ShortPath {
146166
pub path: String,
@@ -149,16 +169,17 @@ pub struct ShortPath {
149169
}
150170

151171
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
156179
.is_some_and(|main_entrypoint| main_entrypoint == &specifier);
157180

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))
162183
{
163184
return ShortPath {
164185
path: rewrite.to_owned(),
@@ -175,7 +196,7 @@ impl ShortPath {
175196
};
176197
};
177198

178-
let Some(common_ancestor) = &ctx.common_ancestor else {
199+
let Some(common_ancestor) = common_ancestor else {
179200
return ShortPath {
180201
path: url_file_path.to_string_lossy().to_string(),
181202
specifier,
@@ -471,30 +492,17 @@ pub fn generate(
471492
FileMode::Normal
472493
};
473494

495+
let composable_output = options.composable_output;
496+
474497
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)?;
490500
let mut files = HashMap::new();
491501

492-
let doc_nodes_by_short_path =
493-
ctx.doc_nodes_by_url_add_context(doc_nodes_by_url);
494-
495502
// Index page
496503
{
497-
let main_entrypoint = doc_nodes_by_short_path
504+
let main_entrypoint = ctx
505+
.doc_nodes
498506
.iter()
499507
.find(|(short_path, _)| short_path.is_main);
500508

@@ -508,11 +516,10 @@ pub fn generate(
508516
let index = pages::IndexCtx::new(
509517
&ctx,
510518
main_entrypoint.map(|(short_path, _)| short_path.clone()),
511-
&doc_nodes_by_short_path,
512519
partitions_for_entrypoint_nodes,
513520
);
514521

515-
if options.composable_output {
522+
if composable_output {
516523
files.insert(
517524
"./sidepanel.html".to_string(),
518525
ctx.render(
@@ -549,7 +556,8 @@ pub fn generate(
549556

550557
// All symbols (list of all symbols in all files)
551558
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
553561
.values()
554562
.flatten()
555563
.cloned()
@@ -558,13 +566,9 @@ pub fn generate(
558566
let partitions_by_kind =
559567
partition::partition_nodes_by_entrypoint(&all_doc_nodes, true);
560568

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);
566570

567-
if options.composable_output {
571+
if composable_output {
568572
files.insert(
569573
"./all_symbols/content.html".to_string(),
570574
ctx.render(SymbolContentCtx::TEMPLATE, &all_symbols.content),
@@ -585,9 +589,8 @@ pub fn generate(
585589

586590
// Pages for all discovered symbols
587591
{
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);
591594

592595
let symbol_pages = generate_symbol_pages_for_module(
593596
&ctx,
@@ -605,7 +608,7 @@ pub fn generate(
605608
} => {
606609
let root = ctx.href_resolver.resolve_path(
607610
UrlResolveKind::Symbol {
608-
file: &short_path,
611+
file: short_path,
609612
symbol: &symbol_group_ctx.name,
610613
},
611614
UrlResolveKind::Root,
@@ -618,7 +621,7 @@ pub fn generate(
618621
Some(short_path),
619622
);
620623

621-
if options.composable_output {
624+
if composable_output {
622625
let dir_name =
623626
format!("{}/~/{}", short_path.path, symbol_group_ctx.name);
624627

@@ -660,7 +663,7 @@ pub fn generate(
660663
} => {
661664
let redirect = serde_json::json!({ "path": href });
662665

663-
if options.composable_output {
666+
if composable_output {
664667
let file_name = format!(
665668
"{}/~/{}/redirect.json",
666669
short_path.path, current_symbol
@@ -677,60 +680,63 @@ pub fn generate(
677680
}
678681
}));
679682

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,
700688
);
701689

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+
}
704713

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+
}
710719

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);
713721
}
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+
);
716727
}
717-
} else {
718-
files.insert(
719-
format!("{}/~/index.html", short_path.path),
720-
ctx.render(pages::IndexCtx::TEMPLATE, &index),
721-
);
722728
}
723729
}
724730
}
725731

726732
files.insert(STYLESHEET_FILENAME.into(), STYLESHEET.into());
727733
files.insert(
728734
SEARCH_INDEX_FILENAME.into(),
729-
search::get_search_index_file(&ctx, &doc_nodes_by_short_path)?,
735+
search::get_search_index_file(&ctx)?,
730736
);
731737
files.insert(SCRIPT_FILENAME.into(), SCRIPT_JS.into());
732738

733-
if !options.composable_output {
739+
if !composable_output {
734740
files.insert(PAGE_STYLESHEET_FILENAME.into(), PAGE_STYLESHEET.into());
735741
files.insert(FUSE_FILENAME.into(), FUSE_JS.into());
736742
files.insert(SEARCH_FILENAME.into(), SEARCH_JS.into());

0 commit comments

Comments
 (0)