Skip to content

Commit

Permalink
Move included items to a context
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 committed Nov 22, 2024
1 parent fc47a58 commit c8aedb4
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 136 deletions.
37 changes: 20 additions & 17 deletions extensions/scarb-doc/src/docs_generation/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use camino::Utf8Path;
use context::MarkdownGenerationContext;
use itertools::chain;
use std::fs;

Expand All @@ -11,12 +12,13 @@ use crate::docs_generation::markdown::traits::{
use crate::docs_generation::{collect_all_top_level_items, TopLevelItems};
use crate::errors::{IODirectoryCreationError, IOWriteError};
use crate::types::{
Constant, Enum, ExternFunction, ExternType, FreeFunction, Impl, ImplAlias, IncludedItems,
Module, Struct, Trait, TypeAlias,
Constant, Enum, ExternFunction, ExternType, FreeFunction, Impl, ImplAlias, Module, Struct,
Trait, TypeAlias,
};
use crate::PackageInformation;

mod book_toml;
mod context;
mod summary;
mod traits;

Expand All @@ -26,17 +28,17 @@ const BOOK_TOML_FILENAME: &str = "book.toml";
pub const SUMMARY_FILENAME: &str = "SUMMARY.md";

type Filename = String;
type GeneratedFile = (Filename, String);

pub struct MarkdownContent {
book_toml: String,
summary: String,
doc_files: Vec<(Filename, String)>,
doc_files: Vec<GeneratedFile>,
}

impl MarkdownContent {
pub fn from_crate(package_information: &PackageInformation) -> Result<Self> {
let top_level_items = collect_all_top_level_items(&package_information.crate_);
let included_items = &package_information.crate_.included_items;

let summary_file_content = generate_summary_file_content(&top_level_items)?;
let TopLevelItems {
Expand All @@ -53,18 +55,19 @@ impl MarkdownContent {
extern_functions,
} = top_level_items;

let context = MarkdownGenerationContext::from_crate(&package_information.crate_);
let docs_for_top_level_items = chain!(
generate_top_level_docs_contents(&modules, included_items)?,
generate_top_level_docs_contents(&constants, included_items)?,
generate_top_level_docs_contents(&free_functions, included_items)?,
generate_top_level_docs_contents(&structs, included_items)?,
generate_top_level_docs_contents(&enums, included_items)?,
generate_top_level_docs_contents(&type_aliases, included_items)?,
generate_top_level_docs_contents(&impl_aliases, included_items)?,
generate_top_level_docs_contents(&traits, included_items)?,
generate_top_level_docs_contents(&impls, included_items)?,
generate_top_level_docs_contents(&extern_types, included_items)?,
generate_top_level_docs_contents(&extern_functions, included_items)?,
generate_top_level_docs_contents(&modules, &context)?,
generate_top_level_docs_contents(&constants, &context)?,
generate_top_level_docs_contents(&free_functions, &context)?,
generate_top_level_docs_contents(&structs, &context)?,
generate_top_level_docs_contents(&enums, &context)?,
generate_top_level_docs_contents(&type_aliases, &context)?,
generate_top_level_docs_contents(&impl_aliases, &context)?,
generate_top_level_docs_contents(&traits, &context)?,
generate_top_level_docs_contents(&impls, &context)?,
generate_top_level_docs_contents(&extern_types, &context)?,
generate_top_level_docs_contents(&extern_functions, &context)?,
)
.collect::<Vec<(String, String)>>();

Expand Down Expand Up @@ -150,13 +153,13 @@ impl MarkdownContent {

fn generate_top_level_docs_contents(
items: &[&impl TopLevelMarkdownDocItem],
included_items: &IncludedItems,
context: &MarkdownGenerationContext,
) -> Result<Vec<(Filename, String)>> {
items
.iter()
.map(|item| {
let filename = item.filename();
item.generate_markdown(included_items, BASE_HEADER_LEVEL)
item.generate_markdown(context, BASE_HEADER_LEVEL)
.map(|markdown| (filename, markdown))
})
.collect()
Expand Down
74 changes: 74 additions & 0 deletions extensions/scarb-doc/src/docs_generation/markdown/context.rs
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("::", "-"))
}
Loading

0 comments on commit c8aedb4

Please sign in to comment.