Skip to content

Commit

Permalink
some code cleanup in Pydoc preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRosenstein committed Mar 14, 2022
1 parent 42fa47f commit 02713da
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/pydoc_markdown/novella/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ def renderer(self, renderer=None, closure=None):
else:
return self._renderer

def _load_api_suite(self) -> ApiSuite:
if self._suite is None:
modules = list(self._loader.load())
for processor in self._processors:
processor.process(modules, self)
self._suite = ApiSuite(modules)
return self._suite

def _replace_pylink_tag(self, tag: Tag) -> str | None:
return f'{{@link pydoc:{tag.args.strip()}}}'

# MarkdownPreprocessor

def setup(self) -> None:
Expand All @@ -121,22 +132,18 @@ def process_files(self, files: MarkdownFiles) -> None:
context = Context(str(Path.cwd()))
self._loader.init(context)
self._renderer.init(context)

if self._suite is None:
modules = list(self._loader.load())
for processor in self._processors:
processor.process(modules, self)
self._suite = ApiSuite(modules)
suite = self._load_api_suite()

for file in files:
tags = [t for t in parse_block_tags(file.content) if t.name == 'pydoc']
file.content = replace_tags(file.content, tags, lambda t: self._replace_pydoc_tag(self._suite, file, t))
file.content = replace_tags(file.content, tags, lambda t: self._replace_pydoc_tag(file, t))
tags = [t for t in parse_inline_tags(file.content) if t.name == 'pylink']
file.content = replace_tags(file.content, tags, lambda t: self._replace_pylink_tag(t))

def _replace_pydoc_tag(self, suite: ApiSuite, file: MarkdownFile, tag: Tag) -> str | None:
def _replace_pydoc_tag(self, file: MarkdownFile, tag: Tag) -> str | None:
assert self._suite is not None
fqn = tag.args.strip()
objects = suite.resolve_fqn(fqn)
objects = self._suite.resolve_fqn(fqn)
if len(objects) > 1:
logger.warning(' found multiple matches for Python FQN <fg=cyan>%s</fg>', fqn)
elif not objects:
Expand All @@ -147,6 +154,3 @@ def _replace_pydoc_tag(self, suite: ApiSuite, file: MarkdownFile, tag: Tag) -> s
fp = io.StringIO()
self._renderer.render_object(fp, objects[0], tag.options)
return self.action.repeat(file.path, file.output_path, fp.getvalue())

def _replace_pylink_tag(self, tag: Tag) -> str | None:
return f'{{@link pydoc:{tag.args.strip()}}}'

0 comments on commit 02713da

Please sign in to comment.