-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
extensions/scarb-doc/src/docs_generation/markdown/context.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::docs_generation::markdown::traits::WithPath; | ||
use crate::docs_generation::markdown::SUMMARY_FILENAME; | ||
use crate::types::Crate; | ||
use cairo_lang_defs::ids::{ImplItemId, LookupItemId, TraitItemId}; | ||
use cairo_lang_doc::documentable_item::DocumentableItemId; | ||
use cairo_lang_doc::parser::CommentLinkToken; | ||
use std::collections::HashMap; | ||
|
||
pub type IncludedItems<'a> = HashMap<DocumentableItemId, &'a dyn WithPath>; | ||
|
||
pub struct MarkdownGenerationContext<'a> { | ||
included_items: IncludedItems<'a>, | ||
} | ||
|
||
impl<'a> MarkdownGenerationContext<'a> { | ||
pub fn from_crate(crate_: &'a Crate) -> Self { | ||
let included_items = crate_.root_module.get_all_item_ids(); | ||
Self { | ||
included_items: included_items | ||
.into_iter() | ||
.map(|(id, item)| { | ||
let item: &dyn WithPath = item; | ||
(id, item) | ||
}) | ||
.collect(), | ||
} | ||
} | ||
|
||
pub fn resolve_markdown_file_path_from_link(&self, link: &CommentLinkToken) -> String { | ||
match link.resolved_item { | ||
Some(resolved_item_id) => match self.included_items.get(&resolved_item_id) { | ||
Some(resolved_item) => match resolved_item_id { | ||
DocumentableItemId::Member(_) | ||
| DocumentableItemId::Variant(_) | ||
| DocumentableItemId::LookupItem(LookupItemId::TraitItem(TraitItemId::Type( | ||
_, | ||
))) | ||
| DocumentableItemId::LookupItem(LookupItemId::TraitItem( | ||
TraitItemId::Function(_), | ||
)) | ||
| DocumentableItemId::LookupItem(LookupItemId::TraitItem( | ||
TraitItemId::Constant(_), | ||
)) | ||
| DocumentableItemId::LookupItem(LookupItemId::ImplItem(ImplItemId::Type(_))) | ||
| DocumentableItemId::LookupItem(LookupItemId::ImplItem( | ||
ImplItemId::Function(_), | ||
)) | ||
| DocumentableItemId::LookupItem(LookupItemId::ImplItem( | ||
ImplItemId::Constant(_), | ||
)) => { | ||
match resolved_item.parent_full_path() { | ||
Some(parent_path) => { | ||
format!( | ||
"{}#{}", | ||
path_to_file_link(&parent_path), | ||
resolved_item.name().to_lowercase() | ||
) | ||
} | ||
// Only root_module / crate doesn't have the parent. | ||
_ => SUMMARY_FILENAME.to_string(), | ||
} | ||
} | ||
_ => path_to_file_link(&resolved_item.full_path()), | ||
}, | ||
None => link.to_string(), | ||
}, | ||
None => link.to_string(), | ||
} | ||
} | ||
} | ||
|
||
fn path_to_file_link(path: &str) -> String { | ||
format!("./{}.md", path.replace("::", "-")) | ||
} |
Oops, something went wrong.