def test_find_toc_by_id(self): """ Test finding the relevant TOC item by the tag ID. """ index = search_index.SearchIndex() md = dedent(""" # Heading 1 ## Heading 2 ### Heading 3 """) toc = get_toc(get_markdown_toc(md)) toc_item = index._find_toc_by_id(toc, "heading-1") self.assertEqual(toc_item.url, "#heading-1") self.assertEqual(toc_item.title, "Heading 1") toc_item2 = index._find_toc_by_id(toc, "heading-2") self.assertEqual(toc_item2.url, "#heading-2") self.assertEqual(toc_item2.title, "Heading 2") toc_item3 = index._find_toc_by_id(toc, "heading-3") self.assertEqual(toc_item3.url, "#heading-3") self.assertEqual(toc_item3.title, "Heading 3")
def render(self, config, files): """ Convert the Markdown source file to HTML as per the config. """ parts = to_html( source=self.source, src_path=self.file.src_path, extension=self.file.get_extension(), ) self.content = parts self.toc = get_toc([]) # TODO
def test_indented_toc(self): md = dedent(""" # Heading 1 ## Heading 2 ### Heading 3 """) expected = dedent(""" Heading 1 - #heading-1 Heading 2 - #heading-2 Heading 3 - #heading-3 """) toc = get_toc(get_markdown_toc(md)) self.assertEqual(str(toc).strip(), expected) self.assertEqual(len(toc), 1)
def test_entityref(self): md = dedent(""" # Heading & 1 ## Heading > 2 ### Heading < 3 """) expected = dedent(""" Heading & 1 - #heading-1 Heading > 2 - #heading-2 Heading < 3 - #heading-3 """) toc = get_toc(get_markdown_toc(md)) self.assertEqual(str(toc).strip(), expected) self.assertEqual(len(toc), 1)
def test_level(self): md = dedent(""" # Heading 1 ## Heading 1.1 ### Heading 1.1.1 ### Heading 1.1.2 ## Heading 1.2 """) toc = get_toc(get_markdown_toc(md)) def get_level_sequence(items): for item in items: yield item.level yield from get_level_sequence(item.children) self.assertEqual(tuple(get_level_sequence(toc)), (1, 2, 3, 3, 2))
def test_mixed_html(self): md = dedent(""" # Heading 1 ## Heading 2 # Heading 3 ### Heading 4 ### <a>Heading 5</a> """) expected = dedent(""" Heading 1 - #heading-1 Heading 2 - #heading-2 Heading 3 - #heading-3 Heading 4 - #heading-4 Heading 5 - #heading-5 """) toc = get_toc(get_markdown_toc(md)) self.assertEqual(str(toc).strip(), expected) self.assertEqual(len(toc), 2)
def test_charref(self): md = '# @Header' expected = '@Header - #header' toc = get_toc(get_markdown_toc(md)) self.assertEqual(str(toc).strip(), expected) self.assertEqual(len(toc), 1)
def test_create_search_index(self): html_content = """ <h1 id="heading-1">Heading 1</h1> <p>Content 1</p> <h2 id="heading-2">Heading 2</h1> <p>Content 2</p> <h3 id="heading-3">Heading 3</h1> <p>Content 3</p> """ cfg = load_config() pages = [ Page( 'Home', File( 'index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'], ), cfg, ), Page( 'About', File( 'about.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'], ), cfg, ), ] md = dedent(""" # Heading 1 ## Heading 2 ### Heading 3 """) toc = get_toc(get_markdown_toc(md)) full_content = ''.join("""Heading{0}Content{0}""".format(i) for i in range(1, 4)) for page in pages: # Fake page.read_source() and page.render() page.source = md page.toc = toc page.content = html_content index = search_index.SearchIndex() index.add_entry_from_context(page) self.assertEqual(len(index._entries), 4) loc = page.url self.assertEqual(index._entries[0]['title'], page.title) self.assertEqual( strip_whitespace(index._entries[0]['text'], ), full_content, ) self.assertEqual(index._entries[0]['location'], loc) self.assertEqual(index._entries[1]['title'], "Heading 1") self.assertEqual(index._entries[1]['text'], "Content 1") self.assertEqual( index._entries[1]['location'], "{}#heading-1".format(loc), ) self.assertEqual(index._entries[2]['title'], "Heading 2") self.assertEqual( strip_whitespace(index._entries[2]['text'], ), "Content2", ) self.assertEqual( index._entries[2]['location'], "{}#heading-2".format(loc), ) self.assertEqual(index._entries[3]['title'], "Heading 3") self.assertEqual( strip_whitespace(index._entries[3]['text'], ), "Content3", ) self.assertEqual( index._entries[3]['location'], "{}#heading-3".format(loc), )