From 02713da85cb77ab777fec122ab42e15fa6624ef4 Mon Sep 17 00:00:00 2001 From: Niklas Rosenstein Date: Mon, 14 Mar 2022 19:08:21 +0100 Subject: [PATCH] some code cleanup in Pydoc preprocessor --- src/pydoc_markdown/novella/preprocessor.py | 28 ++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/pydoc_markdown/novella/preprocessor.py b/src/pydoc_markdown/novella/preprocessor.py index 0fa1ea06..b83ccb2b 100644 --- a/src/pydoc_markdown/novella/preprocessor.py +++ b/src/pydoc_markdown/novella/preprocessor.py @@ -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: @@ -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 %s', fqn) elif not objects: @@ -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()}}}'