Ejemplo n.º 1
0
    def _generate_module_doc_lines(
        self,
        module_record: ModuleRecord,
        md_document: MDDocument,
    ) -> None:
        for record in module_record.iter_records():
            if isinstance(record, AttributeRecord):
                continue

            header_level = 2
            if record.is_method:
                header_level = 3

            md_document.append_title(record.title, level=header_level)

            source_path = module_record.source_path
            source_line_number = record.line_number
            source_link = md_document.render_doc_link(
                title=FIND_IN_SOURCE_LABEL,
                target_path=source_path,
                anchor="L{}".format(source_line_number),
            )
            if self._source_code_url:
                relative_path_str = self._root_path_finder.relative(
                    source_path).as_posix()
                source_link = md_document.render_link(
                    title=FIND_IN_SOURCE_LABEL,
                    link="{}{}#L{}".format(self._source_code_url,
                                           relative_path_str,
                                           source_line_number),
                )

            md_document.append(source_link)

            signature = record.render(allow_multiline=True)
            md_document.append("```python\n{}\n```".format(signature))

            self._render_docstring(module_record=module_record,
                                   record=record,
                                   md_document=md_document)
Ejemplo n.º 2
0
 def test_generate_toc_section(self):
     md_doc = MDDocument(Path("/root/test.md"))
     md_doc.append_title("header", level=1)
     md_doc.append_title("header2", level=2)
     md_doc.append("line")
     md_doc.append("```\n\n## no header\n\n```")
     md_doc.append_title("header3", level=3)
     md_doc.append_title("header4", level=4)
     md_doc.append("#no header")
     md_doc.append("# no header\nasdd")
     self.assertEqual(
         md_doc.generate_toc_section(),
         "- [header](#header)\n    - [header2](#header2)\n        - [header3](#header3)",
     )
Ejemplo n.º 3
0
 def test_append_title(self):
     md_doc = MDDocument(Path("/test.md"))
     md_doc.append_title("title", level=1)
     md_doc.append_title("_subtitle_", level=3)
     self.assertEqual(md_doc.sections, ["# title", "### \\_subtitle\\_"])
Ejemplo n.º 4
0
    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())