Skip to content

Commit

Permalink
multi-file render handling
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Nov 11, 2023
1 parent 559fb54 commit a954993
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
45 changes: 31 additions & 14 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 169,13 @@ pub fn generate(
let mut files = HashMap::new();

// TODO(bartlomieju): remove
let doc_nodes = doc_nodes_by_url
let all_doc_nodes = doc_nodes_by_url
.values()
.flatten()
.cloned()
.collect::<Vec<_>>();

let all_symbols = NamespacedSymbols::new(&doc_nodes);
let all_symbols = NamespacedSymbols::new(&all_doc_nodes);

// Index page
{
Expand Down Expand Up @@ -214,21 214,30 @@ pub fn generate(
}

let partitions_by_kind =
symbols::namespace::partition_nodes_by_kind(&doc_nodes, true);
symbols::namespace::partition_nodes_by_kind(&all_doc_nodes, true);

// All symbols (list of all symbols in all files)
{
let all_symbols_render =
render_all_symbols(&ctx, &partitions_by_kind, all_symbols.clone())?;
render_all_symbols(&ctx, &partitions_by_kind, all_symbols)?;
files.insert("all_symbols.html".to_string(), all_symbols_render);
}

let sidepanel_ctx = sidepanel_render_ctx(&ctx, &partitions_by_kind);

// Pages for all discovered symbols
{
let generated_pages =
generate_pages(&ctx, &sidepanel_ctx, &doc_nodes, all_symbols)?;
let mut generated_pages = Vec::with_capacity(all_doc_nodes.len());

for (specifier, doc_nodes) in doc_nodes_by_url {
generated_pages.extend(generate_pages_for_file(
&ctx,
&sidepanel_ctx,
ctx.url_to_short_path(specifier),
&doc_nodes,
)?);
}

for (file_name, content) in generated_pages {
files.insert(file_name, content);
}
Expand All @@ -248,6 257,7 @@ pub fn generate(
fn generate_pages_inner(
ctx: &GenerateCtx,
sidepanel_ctx: &SidepanelRenderCtx,
file: &str,
name_partitions: IndexMap<String, Vec<DocNode>>,
namespace_paths: Vec<String>,
all_symbols: NamespacedSymbols,
Expand All @@ -257,9 267,9 @@ fn generate_pages_inner(

for (name, doc_nodes) in name_partitions.iter() {
let file_name = if namespace_paths.is_empty() {
format!("{}.html", name)
format!("{file}/~/{}.html", name)
} else {
format!("{}.{name}.html", namespace_paths.join("."))
format!("{file}/~/{}.{name}.html", namespace_paths.join("."))
};

let page = render_page(
Expand Down Expand Up @@ -291,6 301,7 @@ fn generate_pages_inner(
let generated = generate_pages_inner(
ctx,
sidepanel_ctx,
&file,
namespace_name_partitions,
namespace_paths,
all_symbols.clone(),
Expand All @@ -302,15 313,23 @@ fn generate_pages_inner(
Ok(generated_pages)
}

fn generate_pages(
fn generate_pages_for_file(
ctx: &GenerateCtx,
sidepanel_ctx: &SidepanelRenderCtx,
file: String,
doc_nodes: &[DocNode],
all_symbols: NamespacedSymbols,
) -> Result<Vec<(String, String)>, anyhow::Error> {
let name_partitions = partition_nodes_by_name(doc_nodes);
let all_symbols = NamespacedSymbols::new(doc_nodes);

generate_pages_inner(ctx, sidepanel_ctx, name_partitions, vec![], all_symbols)
generate_pages_inner(
ctx,
sidepanel_ctx,
&file,
name_partitions,
vec![],
all_symbols,
)
}

// TODO(bartlomieju): move a separate module?
Expand Down Expand Up @@ -378,8 397,6 @@ fn render_index(
.map(|url| ctx.url_to_short_path(url))
.collect::<Vec<_>>();

// TODO(@crowlKats): if files is empty, dont render "Modules" section in sidepanel

let kind_partitions = partitions
.into_iter()
.map(|(name, nodes)| {
Expand Down Expand Up @@ -476,7 493,7 @@ fn render_all_symbols(
ctx.global_symbol_href_resolver.clone(),
);
let namespace_ctx =
symbols::namespace::get_namespace_render_ctx(&render_ctx, partitions);
symbols::namespace::get_namespace_render_ctx(&render_ctx, partitions, true);

// TODO(bartlomieju): dedup with `render_page`
let html_head_ctx = HtmlHeadCtx {
Expand Down
26 changes: 20 additions & 6 deletions src/html/symbols/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 20,25 @@ pub struct NamespaceSectionRenderCtx {
#[derive(Serialize)]
pub struct NamespaceSectionNodeCtx {
pub doc_node_kind_ctx: DocNodeKindCtx,
// TODO: add short file path
pub name: String,
pub docs: String,
}

pub(crate) fn get_namespace_render_ctx(
ctx: &RenderContext,
partitions: &IndexMap<DocNodeKind, Vec<DocNode>>,
include_file_paths: bool,
) -> NamespaceRenderCtx {
let mut sections = Vec::with_capacity(partitions.len());

for (kind, doc_nodes) in partitions {
let ns_section_ctx = get_namespace_section_render_ctx(ctx, kind, doc_nodes);
let ns_section_ctx = get_namespace_section_render_ctx(
ctx,
kind,
doc_nodes,
include_file_paths,
);
sections.push(ns_section_ctx)
}

Expand All @@ -45,15 52,13 @@ pub(crate) fn render_namespace(
let namespace_def = doc_node.namespace_def.as_ref().unwrap();

let partitions = partition_nodes_by_kind(&namespace_def.elements, false);
let namespace_ctx = get_namespace_render_ctx(ctx, &partitions);
let namespace_ctx = get_namespace_render_ctx(ctx, &partitions, false);

let content_parts = namespace_ctx
namespace_ctx
.sections
.into_iter()
.map(|section| ctx.render("namespace_section.html", &section))
.collect::<Vec<_>>();

content_parts.join("")
.collect::<String>()
}

pub fn partition_nodes_by_kind(
Expand Down Expand Up @@ -152,6 157,7 @@ fn get_namespace_section_render_ctx(
ctx: &RenderContext,
kind: &DocNodeKind,
doc_nodes: &[DocNode],
include_file_path: bool,
) -> NamespaceSectionRenderCtx {
let kind_ctx = super::super::util::DocNodeKindCtx::from(*kind);

Expand All @@ -166,6 172,14 @@ fn get_namespace_section_render_ctx(
if !ns_parts.is_empty() {
name = format!("{}.{}", ns_parts.join("."), doc_node.name);
}
if include_file_path {
dbg!(
&doc_node.name,
&doc_node.location,
&doc_node.import_def.as_ref().unwrap().src
);
// TODO
}

NamespaceSectionNodeCtx {
doc_node_kind_ctx: doc_node.kind.into(),
Expand Down
2 changes: 1 addition & 1 deletion src/html/templates/index_sidepanel.html
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
<div id="sidepanel">
<h2><a href="{base_url}">{package_name}</a></h2>

<a href="">All symbols</a>
<a href="./all_symbols.html">All symbols</a>

{{ if files }}
<h3>Modules</h3>
Expand Down

0 comments on commit a954993

Please sign in to comment.