def _build_modules_toc_lines(self, import_string: ImportString, max_depth: int, md_document: MDDocument, start_level: int) -> List[str]: lines: List[str] = [] parts = import_string.parts last_import_string_parts: List[str] = [] for module_record in self._module_records: output_path = self._loader.get_output_path( module_record.source_path) if module_record.import_string == import_string: continue if import_string and not module_record.import_string.startswith( import_string): continue import_string_parts = module_record.import_string.parts if len(import_string_parts) > len(parts) + max_depth: continue for index, import_string_part in enumerate( import_string_parts[:-1]): if index < len(parts): continue if (len(last_import_string_parts) > index and last_import_string_parts[index] == import_string_parts[index]): continue title = make_title(import_string_part) toc_line = md_document.get_toc_line(title, level=index - len(parts) + start_level) lines.append(toc_line) last_import_string_parts = import_string_parts link = md_document.render_doc_link( title=module_record.title, target_path=output_path, anchor=md_document.get_anchor(module_record.title), ) toc_line = md_document.get_toc_line( link, level=len(import_string_parts) - len(parts) - 1 + start_level) lines.append(toc_line) return lines
def _build_breadcrumbs_string( self, module_record: ModuleRecord, md_document: MDDocument, ) -> str: import_string_breadcrumbs: List[str] = [] parent_import_strings = [] import_string = module_record.import_string while not import_string.is_top_level(): import_string = import_string.parent parent_import_strings.append(import_string) parent_import_strings.reverse() for parent_import_string in parent_import_strings: parent_module_record = self._module_records.find_module_record( parent_import_string) if not parent_module_record: import_string_breadcrumbs.append("`{}`".format( make_title(parent_import_string.parts[-1]))) continue output_path = self._loader.get_output_path( parent_module_record.source_path) import_string_breadcrumbs.append( md_document.render_doc_link( parent_module_record.title, target_path=output_path, anchor=md_document.get_anchor(parent_module_record.title), )) breadcrumbs = ([ md_document.render_md_doc_link(self.md_index, title=self._project_name), md_document.render_md_doc_link(self.md_modules, title=self.MODULES_TITLE), ] + import_string_breadcrumbs + [module_record.title]) return " / ".join(breadcrumbs)
def test_get_anchor(self): self.assertEqual(MDDocument.get_anchor("s T_e-s%t"), "s-t_e-st") self.assertEqual(MDDocument.get_anchor("test"), "test")
def _render_docstring( self, module_record: ModuleRecord, record: NodeRecord, md_document: MDDocument, ) -> None: """ Get object docstring and convert it to a valid markdown using `handsdown.processors.base.BaseDocstringProcessor`. Arguments: module_record -- Parent ModuleRecord record -- Target NodeRecord md_document -- Output document. Returns: A module docstring with valid markdown. """ docstring = record.docstring docstring = self._replace_links(module_record, record, md_document, docstring) section_map = self._docstring_processor.build_sections(docstring) for attrubute in record.get_documented_attribute_strings(): section_map.add_line_indent("Attributes", "- {}".format(attrubute)) related_import_strings = record.get_related_import_strings( module_record) links = [] title = "" for import_string in related_import_strings: related_module_record = self._module_records.find_module_record( import_string) if not related_module_record: continue related_record = related_module_record.find_record(import_string) if not related_record: continue if related_record is record: continue title = related_record.title target_path = self._loader.get_output_path( related_module_record.source_path) link = md_document.render_doc_link( title, target_path=target_path, anchor=md_document.get_anchor(title)) links.append(link) links.sort() for link in links: section_map.add_line("See also", "- {}".format(link)) self._logger.debug( "Adding link `{}` to `{}` `See also` section".format( title, record.title)) for section in section_map.sections: if section.title: md_document.append_title(section.title, level=4) for block in section.blocks: md_document.append(block.render())
def _replace_links( self, module_record: ModuleRecord, record: NodeRecord, md_document: MDDocument, docstring: str, ) -> str: parent_import_string = None if not record.import_string.is_top_level(): parent_import_string = record.import_string.parent for match in self._short_link_re.findall(docstring): related_record_name = match.replace("`", "") related_import_string = None related_record = None target_path = md_document.path # find record in parent if parent_import_string: related_import_string = parent_import_string + related_record_name if related_import_string != record.import_string: related_record = module_record.find_record( related_import_string) # find record in module if not related_record: related_import_string = module_record.import_string + related_record_name related_record = module_record.find_record( related_import_string) # find record globally if not related_record: related_import_string = ImportString(related_record_name) related_module_record = self._module_records.find_module_record( related_import_string) if related_module_record: related_record = related_module_record.find_record( related_import_string) target_path = self._loader.get_output_path( related_module_record.source_path) if not related_record: continue if related_record.import_string.startswith(record.import_string): continue title = related_record.title anchor = md_document.get_anchor(related_record.title) if isinstance(related_record, AttributeRecord): parent_related_record = module_record.find_record( related_record.import_string.parent) if parent_related_record: anchor = md_document.get_anchor( parent_related_record.title) link = md_document.render_doc_link(title, anchor=anchor, target_path=target_path) docstring = docstring.replace(match, link) self._logger.debug("Adding local link '{}' to '{}'".format( title, record.title)) return docstring