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_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")