Skip to content

Commit

Permalink
add context for global symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Nov 10, 2023
1 parent d2c16e3 commit 559fb54
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 183,7 @@ fn generate_docs_directory(
package_name: name,
main_entrypoint,
global_symbols: Default::default(),
global_symbol_href_resolver: std::rc::Rc::new(|_| String::new()),
global_symbol_href_resolver: std::rc::Rc::new(|_, _| String::new()),
};
let html = deno_doc::html::generate(options.clone(), doc_nodes_by_url)?;

Expand Down
10 changes: 6 additions & 4 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,8 @@ mod util;
use symbol::SymbolGroupCtx;
use symbols::namespace::NamespaceRenderCtx;

pub use self::util::GlobalSymbolHrefResolver;
pub use self::util::NamespacedGlobalSymbols;
use self::util::NamespacedSymbols;

const STYLESHEET: &str = include_str!("./templates/styles.css");
Expand All @@ -44,16 46,16 @@ pub struct GenerateOptions {
/// If only a single file is specified during generation, this will always
/// default to that file.
pub main_entrypoint: Option<ModuleSpecifier>,
pub global_symbols: NamespacedSymbols,
pub global_symbol_href_resolver: util::GlobalSymbolHrefResolver,
pub global_symbols: NamespacedGlobalSymbols,
pub global_symbol_href_resolver: GlobalSymbolHrefResolver,
}

struct GenerateCtx<'ctx> {
package_name: String,
common_ancestor: Option<PathBuf>,
tt: Rc<TinyTemplate<'ctx>>,
global_symbols: NamespacedSymbols,
global_symbol_href_resolver: util::GlobalSymbolHrefResolver,
global_symbols: NamespacedGlobalSymbols,
global_symbol_href_resolver: GlobalSymbolHrefResolver,
}

impl<'ctx> GenerateCtx<'ctx> {
Expand Down
30 changes: 23 additions & 7 deletions src/html/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
use crate::DocNodeKind;
use serde::Serialize;
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use tinytemplate::TinyTemplate;

Expand All @@ -23,7 23,7 @@ pub fn title_to_id(title: &str) -> String {
/// ["Deno", "read"]
/// ["Deno", "errors"]
/// ["Deno", "errors", "HttpError"]
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct NamespacedSymbols(Rc<HashSet<Vec<String>>>);

impl NamespacedSymbols {
Expand Down Expand Up @@ -65,7 65,20 @@ impl NamespacedSymbols {
}
}

pub type GlobalSymbolHrefResolver = Rc<dyn Fn(&[String]) -> String>;
#[derive(Clone, Default)]
pub struct NamespacedGlobalSymbols(Rc<HashMap<Vec<String>, String>>);

impl NamespacedGlobalSymbols {
pub fn new(symbols: HashMap<Vec<String>, String>) -> Self {
Self(Rc::new(symbols))
}

fn get(&self, path: &[String]) -> Option<&String> {
self.0.get(path)
}
}

pub type GlobalSymbolHrefResolver = Rc<dyn Fn(&[String], &String) -> String>;

#[derive(Clone)]
pub struct RenderContext<'ctx> {
Expand All @@ -74,15 87,15 @@ pub struct RenderContext<'ctx> {
current_type_params: HashSet<String>,
/// A vector of parts of the current namespace, eg. `vec!["Deno", "errors"]`.
namespace_parts: Vec<String>,
global_symbols: NamespacedSymbols,
global_symbols: NamespacedGlobalSymbols,
global_symbol_href_resolver: GlobalSymbolHrefResolver,
}

impl<'ctx> RenderContext<'ctx> {
pub fn new(
tt: Rc<TinyTemplate<'ctx>>,
all_symbols: NamespacedSymbols,
global_symbols: NamespacedSymbols,
global_symbols: NamespacedGlobalSymbols,
global_symbol_href_resolver: GlobalSymbolHrefResolver,
) -> Self {
Self {
Expand Down Expand Up @@ -154,8 167,11 @@ impl<'ctx> RenderContext<'ctx> {

// TODO(crowlKats): handle currentImports

if self.global_symbols.contains(&target_symbol_parts) {
return Some((self.global_symbol_href_resolver)(&target_symbol_parts));
if let Some(context) = self.global_symbols.get(&target_symbol_parts) {
return Some((self.global_symbol_href_resolver)(
&target_symbol_parts,
context,
));
}

None
Expand Down

0 comments on commit 559fb54

Please sign in to comment.