def test_context_manager(self): with NamedTemporaryFile(mode="w+") as temp_f: with MDDocument(Path(temp_f.name)) as md_doc: md_doc.title = "test" self.assertEqual(temp_f.read(), "# test\n") with self.assertRaises(ValueError): with MDDocument(Path(temp_f.name)): raise ValueError("test")
def test_init(self): with NamedTemporaryFile(mode="w+") as temp_f: temp_f.write("\n".join([ "# my title", "", "subtitle", "", "", "", "subtitle2", "", "- [TOC](#toc)", "- [TOC2](#toc2)", "", "## my title 2", "", "some content", "new line", ])) temp_f.flush() md_doc = MDDocument(Path(temp_f.name)) md_doc.read() self.assertEqual(md_doc.path, Path(temp_f.name)) self.assertEqual(md_doc.title, "my title") self.assertEqual(md_doc.subtitle, "subtitle\n\nsubtitle2") self.assertEqual(md_doc.toc_section, "- [TOC](#toc)\n- [TOC2](#toc2)") self.assertEqual(md_doc.sections[0], "## my title 2") self.assertEqual(md_doc.sections[1], "some content\nnew line") md_doc.subtitle = "my subtitle" self.assertEqual(md_doc.subtitle, "my subtitle") with NamedTemporaryFile(mode="w+") as temp_f: temp_f.write("\n".join([ "# my title", "", "- [TOC](#toc)", "- [TOC2](#toc2)", "", "", "some content", ])) temp_f.flush() md_doc = MDDocument(Path(temp_f.name)) md_doc.read() self.assertEqual(md_doc.subtitle, "some content")
def generate_doc(self, source_path): # type: (Path) -> None """ Generate one module doc at once. Arguments: source_path -- Path to source file. Raises: GeneratorError -- If `source_path` not found in current repo. """ for module_record in self._module_records: if module_record.source_path != source_path: continue output_path = self._loader.get_output_path( module_record.source_path) md_document = MDDocument(output_path) self._generate_doc(module_record, md_document) md_document.write() return raise GeneratorError("Record not found for {}".format( source_path.name))
def test_append(self): md_doc = MDDocument(Path("/test.md")) md_doc.append("subtitle") md_doc.append("test") md_doc.append("") self.assertEqual(md_doc.subtitle, "subtitle") self.assertEqual(md_doc.sections[0], "test")
def test_add_toc_if_not_exists(self): with NamedTemporaryFile(mode="w+") as temp_f: temp_f.write("\n".join([ "# header", "", "- [TOC](#toc)", "- [TOC2](#toc2)", "", "## header2", "", "### header3", "", ])) temp_f.flush() md_doc = MDDocument(Path(temp_f.name)) md_doc.read() # md_doc.append_title('header', level=1) # md_doc.append_title('header2', level=2) self.assertEqual(md_doc.toc_section, "- [TOC](#toc)\n- [TOC2](#toc2)") md_doc.add_toc_if_not_exists() self.assertEqual(md_doc.toc_section, "- [TOC](#toc)\n- [TOC2](#toc2)") md_doc.toc_section = "" md_doc.add_toc_if_not_exists() self.assertEqual( md_doc.toc_section, "- [header](#header)\n - [header2](#header2)\n - [header3](#header3)", )
def test_render_doc_link(self): md_doc = MDDocument(Path("/root/test.md")) self.assertEqual(md_doc.render_doc_link("title", anchor="tag"), "[title](#tag)") self.assertEqual( md_doc.render_doc_link("title", anchor="tag", target_path=Path("/root/test.md")), "[title](#tag)", ) self.assertEqual( md_doc.render_doc_link("title", anchor="tag", target_path=Path("/root/test2.md")), "[title](test2.md#tag)", ) self.assertEqual( md_doc.render_doc_link("title", target_path=Path("/root/test.md")), "[title]()", ) self.assertEqual( md_doc.render_doc_link("title", target_path=Path("/root/test2.md")), "[title](test2.md)", ) self.assertEqual(md_doc.render_doc_link("title"), "[title]()")
def _prepare_index(self): # type: () -> None self.md_index = MDDocument(self._output_path / self.INDEX_NAME) self.md_modules = MDDocument(self._output_path / self.INDEX_NAME) # copy `README.md` content from root dir if it exists #readme_path = self._root_path / "README.md" #if readme_path.exists(): # self.md_index.read(readme_path) if not self.md_index.title: self.md_index.title = "{} {}".format(self._project_name, self.INDEX_TITLE) # copy `MODULES.md` content from root dir if it exists #modules_path = self._root_path / "MODULES.md" #if modules_path.exists(): # self.md_modules.read(modules_path) if not self.md_modules.title: self.md_modules.title = "{} {}".format(self._project_name, self.MODULES_TITLE)
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)", )
def generate_docs(self): # type: () -> None """ Generate all doc files at once. """ self._logger.debug("Generating docs for {} to {}".format( self._project_name, self._root_path_finder.relative(self._output_path))) for module_record in self._module_records: output_path = self._loader.get_output_path( module_record.source_path) md_document = MDDocument(output_path) self._generate_doc(module_record, md_document) md_document.write()
def test_render_md_doc_link(self, render_doc_link_mock): md_doc_mock = MagicMock() md_doc_mock.title = "Other title" md_doc_mock.path = "other/test.md" render_doc_link_mock.return_value = "result" md_doc = MDDocument(Path("/root/test.md")) self.assertEqual(md_doc.render_md_doc_link(md_doc_mock), "result") render_doc_link_mock.assert_called_with(anchor="other-title", target_path="other/test.md", title="Other title") self.assertEqual(md_doc.render_md_doc_link(md_doc_mock, title="Test"), "result") render_doc_link_mock.assert_called_with(anchor="other-title", target_path="other/test.md", title="Test")
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\\_"])