Skip to content

Commit

Permalink
replace current file and current symbol for current resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Dec 6, 2023
1 parent fae5183 commit 5bf22ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 63 deletions.
39 changes: 23 additions & 16 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,25 @@ const FUSE_FILENAME: &str = "fuse.js";
const SEARCH_JS: &str = include_str!("./templates/search.js");
const SEARCH_FILENAME: &str = "search.js";

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum UrlResolveKind<'a> {
Root,
AllSymbols,
File(&'a str),
Symbol { file: &'a str, symbol: &'a str },
}

impl UrlResolveKind<'_> {
fn get_file(&self) -> Option<&str> {
match self {
UrlResolveKind::Root => None,
UrlResolveKind::AllSymbols => None,
UrlResolveKind::File(file) => Some(file),
UrlResolveKind::Symbol { file, .. } => Some(file),
}
}
}

/// Arguments are current and target
pub type UrlResolver = Rc<dyn Fn(UrlResolveKind, UrlResolveKind) -> String>;

Expand Down Expand Up @@ -642,15 +653,15 @@ fn render_index(
&file,
);

let short_path = specifier.map(|specifier| ctx.url_to_short_path(specifier));

let render_ctx = RenderContext::new(
ctx,
all_symbols,
specifier.map(|specifier| ctx.url_to_short_path(specifier)),
None,
if specifier.is_none() {
Some(UrlResolveKind::Root)
if let Some(short_path) = &short_path {
UrlResolveKind::File(short_path)
} else {
None
UrlResolveKind::Root
},
);

Expand Down Expand Up @@ -709,13 +720,8 @@ fn render_all_symbols(
partitions: &IndexMap<DocNodeKind, Vec<DocNodeWithContext>>,
all_symbols: NamespacedSymbols,
) -> Result<String, anyhow::Error> {
let render_ctx = RenderContext::new(
ctx,
all_symbols,
None,
None,
Some(UrlResolveKind::AllSymbols),
);
let render_ctx =
RenderContext::new(ctx, all_symbols, UrlResolveKind::AllSymbols);
let namespace_ctx =
namespace::get_namespace_render_ctx(&render_ctx, partitions);

Expand Down Expand Up @@ -801,9 +807,10 @@ fn render_page(
let mut render_ctx = RenderContext::new(
ctx,
all_symbols,
Some(file.to_string()),
Some(namespaced_name.clone()),
None,
UrlResolveKind::Symbol {
file,
symbol: &namespaced_name,
},
);
if !namespace_paths.is_empty() {
render_ctx = render_ctx.with_namespace(namespace_paths.to_vec())
Expand Down
5 changes: 3 additions & 2 deletions src/html/symbols/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,16 @@ fn get_namespace_section_render_ctx(
name = format!("{}.{}", ns_parts.join("."), doc_node.name);
}

let current_resolve = ctx.get_current_resolve();
NamespaceSectionNodeCtx {
doc_node_kind_ctx: doc_node.kind.into(),
origin: origin.clone(),
href: (ctx.url_resolver)(
ctx.get_current_resolve(),
current_resolve,
crate::html::UrlResolveKind::Symbol {
file: origin
.as_deref()
.or_else(|| ctx.get_current_file())
.or_else(|| current_resolve.get_file())
.unwrap(),
symbol: &name,
},
Expand Down
53 changes: 8 additions & 45 deletions src/html/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::html::UrlResolveKind;
use crate::DocNodeKind;
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::collections::HashSet;
use std::rc::Rc;
use tinytemplate::TinyTemplate;

Expand Down Expand Up @@ -91,18 +92,14 @@ pub struct RenderContext<'ctx> {
global_symbols: NamespacedGlobalSymbols,
global_symbol_href_resolver: GlobalSymbolHrefResolver,
pub url_resolver: super::UrlResolver,
current_file: Option<String>,
current_symbol: Option<String>,
current_resolve: Option<UrlResolveKind<'ctx>>,
current_resolve: UrlResolveKind<'ctx>,
}

impl<'ctx> RenderContext<'ctx> {
pub fn new(
ctx: &super::GenerateCtx<'ctx>,
all_symbols: NamespacedSymbols,
current_file: Option<String>,
current_symbol: Option<String>,
current_resolve: Option<UrlResolveKind<'ctx>>,
current_resolve: UrlResolveKind<'ctx>,
) -> Self {
Self {
tt: ctx.tt.clone(),
Expand All @@ -112,8 +109,6 @@ impl<'ctx> RenderContext<'ctx> {
global_symbols: ctx.global_symbols.clone(),
global_symbol_href_resolver: ctx.global_symbol_href_resolver.clone(),
url_resolver: ctx.url_resolver.clone(),
current_file,
current_symbol,
current_resolve,
}
}
Expand All @@ -135,29 +130,10 @@ impl<'ctx> RenderContext<'ctx> {
}
}

pub fn with_file(&self, current_file: Option<String>) -> Self {
Self {
current_file,
..self.clone()
}
}

pub fn with_symbol(&self, current_symbol: Option<String>) -> Self {
Self {
current_symbol,
..self.clone()
}
}

pub fn with_current_resolve(
&self,
current_resolve: Option<UrlResolveKind<'ctx>>,
current_resolve: UrlResolveKind<'ctx>,
) -> Self {
assert!(matches!(
current_resolve,
None | Some(UrlResolveKind::Root) | Some(UrlResolveKind::AllSymbols)
));

Self {
current_resolve,
..self.clone()
Expand All @@ -180,21 +156,8 @@ impl<'ctx> RenderContext<'ctx> {
self.namespace_parts.clone()
}

pub fn get_current_file(&self) -> Option<&str> {
self.current_file.as_deref()
}

pub fn get_current_symbol(&self) -> Option<&str> {
self.current_symbol.as_deref()
}

pub fn get_current_resolve(&self) -> UrlResolveKind {
match (self.get_current_file(), self.get_current_symbol()) {
(Some(file), Some(symbol)) => UrlResolveKind::Symbol { file, symbol },
(Some(file), None) => UrlResolveKind::File(file),
(None, None) => self.current_resolve.clone().unwrap(),
_ => unreachable!(),
}
self.current_resolve
}

pub fn lookup_symbol_href(&self, target_symbol: &str) -> Option<String> {
Expand All @@ -213,7 +176,7 @@ impl<'ctx> RenderContext<'ctx> {
return Some((self.url_resolver)(
self.get_current_resolve(),
UrlResolveKind::Symbol {
file: self.current_file.as_deref().unwrap(),
file: self.get_current_resolve().get_file().unwrap(),
symbol: &current_parts.join("."),
},
));
Expand All @@ -227,7 +190,7 @@ impl<'ctx> RenderContext<'ctx> {
return Some((self.url_resolver)(
self.get_current_resolve(),
UrlResolveKind::Symbol {
file: self.get_current_file().unwrap_or_default(), // TODO
file: self.get_current_resolve().get_file().unwrap_or_default(), // TODO
symbol: &target_symbol_parts.join("."),
},
));
Expand Down

0 comments on commit 5bf22ec

Please sign in to comment.