plissken-core::render::module_renderer
Rust
Module page rendering for Python and Rust documentation
This module provides rendering functionality for converting PythonModule
and RustModule structures into Markdown documentation files.
Structs
Section titled “Structs”struct RenderedPage
Section titled “struct RenderedPage”pub
Derives: Debug, Clone
Rendered output for a documentation file
Fields
Section titled “Fields”| Name | Type | Description |
|---|---|---|
path |
PathBuf |
Relative path for output (e.g., “my_module.md” or “rust/my_crate.md”) |
content |
String |
The rendered Markdown content |
struct ModuleRenderer<'a>
Section titled “struct ModuleRenderer<'a>”pub
Module page renderer that converts DocModel modules into Markdown files.
Methods
Section titled “Methods”pub
fn new(renderer: &'a Renderer) -> SelfCreate a new module renderer
Source
pub fn new(renderer: &'a Renderer) -> Self { Self::with_cross_refs(renderer, Vec::new()) }with_cross_refs
Section titled “with_cross_refs”pub
fn with_cross_refs( renderer: &'a Renderer, cross_refs: Vec<CrossRef>,) -> SelfCreate a module renderer with cross-references for bi-directional linking
Source
pub fn with_cross_refs(renderer: &'a Renderer, cross_refs: Vec<CrossRef>) -> Self { let starlight = renderer.is_starlight(); let style = if starlight { LinkStyle::Route } else { LinkStyle::MarkdownFile }; Self { renderer, linker: CrossRefLinker::new(cross_refs).with_style(style), starlight, module_order: HashMap::new(), rust_index: RustDocIndex::default(), } }with_module_order
Section titled “with_module_order”pub
fn with_module_order( mut self, python_modules: &[PythonModule], rust_modules: &[RustModule],) -> SelfSet the sidebar order of the module pages from all modules of the project: Python modules first, then Rust modules, each sorted by path (the order of the navigation). Used in Starlight front matter.
Source
pub fn with_module_order( mut self, python_modules: &[PythonModule], rust_modules: &[RustModule], ) -> Self { let python = python_nav_entries(python_modules) .into_iter() .map(|e| format!("py:{}", e.path)); let rust = rust_nav_entries(rust_modules) .into_iter() .map(|e| format!("rs:{}", e.path)); self.module_order = python .chain(rust) .enumerate() .map(|(i, key)| (key, i + 1)) .collect(); self }with_rust_index
Section titled “with_rust_index”pub
fn with_rust_index(mut self, rust_modules: &[RustModule]) -> SelfIndex the Rust items of the project, so that the intra-doc links of Rust doc comments ([`Config`], [x](crate::config::Config)) point to their pages. Without it, such a link shows as its text.
Source
pub fn with_rust_index(mut self, rust_modules: &[RustModule]) -> Self { let layout = PageLayout::new(); let index = RustDocIndex::new(rust_modules, |m| self.page_file(layout.rust_module_page(m))); self.rust_index = index; self }render_starlight_index
Section titled “render_starlight_index”pub
fn render_starlight_index( &self, python_modules: &[PythonModule], rust_modules: &[RustModule],) -> RenderedPageThe index page of a Starlight API reference: each module, linked to its page, with the summary of its doc comment.
Source
pub fn render_starlight_index( &self, python_modules: &[PythonModule], rust_modules: &[RustModule], ) -> RenderedPage { let index = STARLIGHT_INDEX_FILE; let mut content = front_matter( "API reference", "The API reference, written by plissken from the source.", 0, ); let link = |page: PathBuf, label: &str, summary: Option<String>| { let href = route_href(index, &self.page_file(page), None); match summary .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()) { Some(s) => format!("- [`{}`]({}): {}", label, href, s), None => format!("- [`{}`]({})", label, href), } }; let layout = PageLayout::new(); if !python_modules.is_empty() { content.push_str("## Python\n\n"); let mut modules: Vec<&PythonModule> = python_modules.iter().collect(); modules.sort_by(|a, b| a.path.cmp(&b.path)); for m in modules { let summary = m.docstring.as_deref().and_then(|d| { if m.source_type == SourceType::PyO3Binding { parse_rust_doc(d).summary } else { parse_docstring(d).summary } }); content.push_str(&link(layout.python_module_page(&m.path), &m.path, summary)); content.push('\n'); } content.push('\n'); } if !rust_modules.is_empty() { content.push_str("## Rust\n\n"); let mut modules: Vec<&RustModule> = rust_modules.iter().collect(); modules.sort_by(|a, b| a.path.cmp(&b.path)); for m in modules { let summary = m .doc_comment .as_deref() .and_then(|d| parse_rust_doc(d).summary); let summary = summary.map(|s| self.rust_doc_links(&s, index, &m.path)); content.push_str(&link(layout.rust_module_page(&m.path), &m.path, summary)); content.push('\n'); } } RenderedPage { path: PathBuf::from(index), content: self.finish_page(content), } }render_python_module
Section titled “render_python_module”pub
fn render_python_module( &self, module: &PythonModule,) -> Result<Vec<RenderedPage>, tera::Error>Render a Python module to a single Markdown file with all content inline.
This creates one file per module with classes, methods, and functions all rendered inline rather than as separate pages.
Source
pub fn render_python_module( &self, module: &PythonModule, ) -> Result<Vec<RenderedPage>, tera::Error> { // Separate classes and functions let mut classes = Vec::new(); let mut functions = Vec::new(); let mut variables = Vec::new();
for item in &module.items { match item { PythonItem::Class(c) => classes.push(c), PythonItem::Function(f) => functions.push(f), PythonItem::Variable(v) => variables.push(v), } }
// Render everything into a single page let page = self.render_python_module_inline(module, &classes, &functions, &variables)?;
Ok(vec![page]) }render_rust_module
Section titled “render_rust_module”pub
fn render_rust_module( &self, module: &RustModule,) -> Result<Vec<RenderedPage>, tera::Error>Render a Rust module to a single Markdown file with all content inline.
This creates one file per module with structs, enums, and functions all rendered inline rather than as separate pages.
Source
pub fn render_rust_module( &self, module: &RustModule, ) -> Result<Vec<RenderedPage>, tera::Error> { // Categorize items let mut structs = Vec::new(); let mut enums = Vec::new(); let mut functions = Vec::new(); let mut consts = Vec::new(); let mut traits = Vec::new(); let mut type_aliases = Vec::new(); let mut impls: HashMap<String, Vec<&RustImpl>> = HashMap::new();
for item in &module.items { match item { RustItem::Struct(s) => structs.push(s), RustItem::Enum(e) => enums.push(e), RustItem::Function(f) => functions.push(f), RustItem::Const(c) => consts.push(c), RustItem::Trait(t) => traits.push(t), RustItem::TypeAlias(t) => type_aliases.push(t), RustItem::Impl(i) => { // `Foo<T>` has the methods of struct `Foo` let name = i.target.split('<').next().unwrap_or(&i.target).trim(); impls.entry(name.to_string()).or_default().push(i); } } }
// Render everything into a single page let items = RustModuleItems { structs: &structs, enums: &enums, functions: &functions, consts: &consts, traits: &traits, type_aliases: &type_aliases, impls: &impls, }; let page = self.render_rust_module_inline(module, &items)?;
Ok(vec![page]) }render_python_modules
Section titled “render_python_modules”pub
fn render_python_modules( &self, modules: &[PythonModule],) -> Result<Vec<RenderedPage>, tera::Error>Render all Python modules from a list
Source
pub fn render_python_modules( &self, modules: &[PythonModule], ) -> Result<Vec<RenderedPage>, tera::Error> { let mut all_pages = Vec::new(); for module in modules { let pages = self.render_python_module(module)?; all_pages.extend(pages); } Ok(all_pages) }render_rust_modules
Section titled “render_rust_modules”pub
fn render_rust_modules( &self, modules: &[RustModule],) -> Result<Vec<RenderedPage>, tera::Error>Render all Rust modules from a list
Source
pub fn render_rust_modules( &self, modules: &[RustModule], ) -> Result<Vec<RenderedPage>, tera::Error> { let mut all_pages = Vec::new(); for module in modules { let pages = self.render_rust_module(module)?; all_pages.extend(pages); } Ok(all_pages) }generate_nav
Section titled “generate_nav”pub
fn generate_nav( &self, adapter: &dyn super::ssg::SSGAdapter, python_modules: &[PythonModule], rust_modules: &[RustModule],) -> StringGenerate navigation using an SSG adapter.
This method provides a unified interface for generating navigation
regardless of the target SSG. Use super::ssg::get_ssg_adapter to
get the appropriate adapter for your template.
Examples:
use plissken_core::render::ssg::get_ssg_adapter;
let adapter = get_ssg_adapter(Some("mkdocs-material"));let nav = module_renderer.generate_nav(&adapter, &python_modules, &rust_modules);Source
pub fn generate_nav( &self, adapter: &dyn super::ssg::SSGAdapter, python_modules: &[PythonModule], rust_modules: &[RustModule], ) -> String { adapter.generate_nav(python_modules, rust_modules) }generate_config
Section titled “generate_config”pub
fn generate_config( &self, adapter: &dyn super::ssg::SSGAdapter, title: &str, authors: &[String],) -> Option<String>Generate SSG config file using an adapter.
Returns None for SSGs that don’t need generated config (e.g., MkDocs).
Source
pub fn generate_config( &self, adapter: &dyn super::ssg::SSGAdapter, title: &str, authors: &[String], ) -> Option<String> { adapter.generate_config(title, authors) }generate_custom_css
Section titled “generate_custom_css”pub
fn generate_custom_css( &self, adapter: &dyn super::ssg::SSGAdapter,) -> Option<String>Generate custom CSS using an adapter.
Returns None for SSGs that don’t need custom CSS.
Source
pub fn generate_custom_css(&self, adapter: &dyn super::ssg::SSGAdapter) -> Option<String> { adapter.generate_custom_css() }generate_nav_yaml
Section titled “generate_nav_yaml”pub
fn generate_nav_yaml( &self, python_modules: &[PythonModule], rust_modules: &[RustModule],) -> StringGenerate navigation YAML for mkdocs.yml
Creates a nav structure with inline format (one file per module):
nav: - Python: - pysnake: pysnake.md - Rust: - rustscale: rust/rustscale.md - rustscale::config: rust/rustscale/config.mdSource
pub fn generate_nav_yaml( &self, python_modules: &[PythonModule], rust_modules: &[RustModule], ) -> String { let mut yaml = String::new();
// Add recommended toc setting yaml.push_str("# Recommended: add to markdown_extensions in mkdocs.yml\n"); yaml.push_str("# to hide method-level entries from the table of contents:\n"); yaml.push_str("#\n"); yaml.push_str("# markdown_extensions:\n"); yaml.push_str("# - toc:\n"); yaml.push_str("# toc_depth: 2\n"); yaml.push('\n');
yaml.push_str("nav:\n");
// Python section if !python_modules.is_empty() { yaml.push_str(" - Python:\n"); for entry in python_nav_entries(python_modules) { yaml.push_str(&format!( " - {}: {}\n", entry.path, entry.file_path.display() )); } }
// Rust section if !rust_modules.is_empty() { yaml.push_str(" - Rust:\n"); for entry in rust_nav_entries(rust_modules) { yaml.push_str(&format!( " - {}: {}\n", entry.path, entry.file_path.display() )); } }
yaml }generate_mdbook_summary
Section titled “generate_mdbook_summary”pub
fn generate_mdbook_summary( &self, python_modules: &[PythonModule], rust_modules: &[RustModule],) -> StringGenerate SUMMARY.md for mdBook navigation
Creates a hierarchical navigation with inline format (one file per module):
# Summary
# Python
- [pysnake](pysnake.md) - [pysnake.handlers](pysnake/handlers.md)
# Rust
- [rustscale](rust/rustscale.md) - [rustscale::config](rust/rustscale/config.md)Source
pub fn generate_mdbook_summary( &self, python_modules: &[PythonModule], rust_modules: &[RustModule], ) -> String { let mut summary = String::new(); summary.push_str("# Summary\n\n");
// Python section if !python_modules.is_empty() { summary.push_str("# Python\n\n"); for entry in python_nav_entries(python_modules) { let indent = " ".repeat(entry.depth); summary.push_str(&format!( "{}- [{}]({})\n", indent, entry.path, entry.file_path.display() )); } }
// Rust section if !rust_modules.is_empty() { summary.push_str("\n# Rust\n\n"); for entry in rust_nav_entries(rust_modules) { let indent = " ".repeat(entry.depth); summary.push_str(&format!( "{}- [{}]({})\n", indent, entry.path, entry.file_path.display() )); } }
summary }generate_mdbook_config
Section titled “generate_mdbook_config”pub
fn generate_mdbook_config( &self, title: &str, authors: &[String],) -> StringGenerate book.toml configuration for mdBook
Includes fold configuration for collapsible sidebar sections.
Source
pub fn generate_mdbook_config(&self, title: &str, authors: &[String]) -> String { let authors_toml = if authors.is_empty() { String::from("[]") } else { format!( "[{}]", authors .iter() .map(|a| format!("\"{}\"", a)) .collect::<Vec<_>>() .join(", ") ) };
format!( r#"[book]title = "{}"authors = {}language = "en"src = "src"
[build]build-dir = "book"
[output.html]default-theme = "rust"preferred-dark-theme = "coal"additional-css = ["theme/custom.css"]
[output.html.fold]enable = truelevel = 1"#, title, authors_toml ) }generate_mdbook_css
Section titled “generate_mdbook_css”pub
fn generate_mdbook_css(&self) -> StringGenerate custom CSS for mdBook
Hides chapter numbering for reference documentation sections (python/, rust/) while preserving numbering for other documentation sections.
Source
pub fn generate_mdbook_css(&self) -> String { r#"/* Hide chapter numbering for reference documentation sections only. Scoped to python/ and rust/ paths to avoid affecting other documentation. Uses *= (contains) since mdbook prepends path_to_root to hrefs. */.chapter-item a[href*="python/"] strong[aria-hidden="true"],.chapter-item a[href*="rust/"] strong[aria-hidden="true"] { display: none !important;}"# .to_string() }