Custom renderes #203
-
What would be the best approach to provide a custom markdown renderer and use Parsing modules with |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hey @ariG23498 , There is an interface for renderer plugins, and Pydoc-Markdown ships with a few default renderers: For example, if you want to use MkDocs but want to change how the Markdown files themselves are rendered, you can change the properties of the like so: renderer:
type: mkdocs
markdown:
render_toc: true
html_headers: true
code_headers: true If instead you are looking to produce a bunch of Markdown files in a specific format but also in a custom folder structure, there is not currently a native plugin that does just that. But you can take a look at the existing plugin's code to get started, and I'm also here to answer questions. Cheers, |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick reply 👍 My queries are twofold:
I did go through the |
Beta Was this translation helpful? Give feedback.
-
It would be really useful to have the option of specifying a filepath to a custom renderer (modelled on the existing Renderers). |
Beta Was this translation helpful? Give feedback.
-
Oh my, how has this slipped through the cracks until now. Sorry delaying the response almost up to a year 😿 Custom renderer@ariG23498 To your first question
We're in Pydoc-Markdown 4.x now, but I think the same was also possible in Pydoc-Markdown 3x back in 2019. The steps needed to make this work are
A small example# myrenderer.py
import dataclasses
import docspec
import typing as t
from pathlib import Path
from pydoc_markdown.interfaces import Context, Renderer
from pydoc_markdown.contrib.renderers.markdown import MarkdownRenderer
@dataclasses.dataclass
class MyRenderer(Renderer):
output_dir: str
markdown: MarkdownRenderer = dataclasses.field(default_factory=MarkdownRenderer)
def init(self, context: Context) -> None:
self.markdown.init(context)
def render(self, modules: t.List[docspec.Module]) -> None:
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
for module in modules:
path = Path(self.output_dir) / f'{module.name}.md'
path.write_text(self.markdown.render_to_string([module])) # pydoc-markdown.yaml
loaders:
- type: python
search_path: [ src ]
renderer:
type: myrenderer.MyRenderer
output_dir: generated Now when you run
@songololo I hope this answers your questions as well 🙂 Markdown template tweaksTo your second question,
The particular renderers for Docusaurus, MkDocs and Hugo all use the Example renderer:
type: hugo
markdown:
source_linker:
type: github
repo: NiklasRosenstein/pydoc-markdown
add_module_prefix: yes
classdef_with_decorators: false However, your particular question for not rendering the If you need more flexibility than this, Pydoc-Markdown 4.1.0 introduced an experimental Jinja2 renderer. However do note that as of right now, the Jinja2 renderer API and configuration is not stable. |
Beta Was this translation helpful? Give feedback.
Oh my, how has this slipped through the cracks until now. Sorry delaying the response almost up to a year 😿
Custom renderer
@ariG23498 To your first question
We're in Pydoc-Markdown 4.x now, but I think the same was also possible in Pydoc-Markdown 3x back in 2019. The steps needed to make this work are
PYTHONPATH
)