Exemplo n.º 1
0
    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")
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 def test_entityref(self):
     md = dedent("""
     # Heading & 1
     ## Heading > 2
     ### Heading < 3
     """)
     expected = dedent("""
     Heading &amp; 1 - #heading-1
         Heading &gt; 2 - #heading-2
             Heading &lt; 3 - #heading-3
     """)
     toc = get_toc(get_markdown_toc(md))
     self.assertEqual(str(toc).strip(), expected)
     self.assertEqual(len(toc), 1)
Exemplo n.º 4
0
    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))
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 def test_page(title, filename, config):
     test_page = Page(
         title,
         File(filename, config['docs_dir'], config['site_dir'],
              config['use_directory_urls']), config)
     test_page.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>"""
     test_page.markdown = dedent("""
         # Heading 1
         ## Heading 2
         ### Heading 3""")
     test_page.toc = get_toc(get_markdown_toc(test_page.markdown))
     return test_page
Exemplo n.º 7
0
    def render(self, config, files):
        """
        Convert the Markdown source file to HTML as per the config.
        """

        extensions = [_RelativePathExtension(self.file, files)
                      ] + config['markdown_extensions']

        md = markdown.Markdown(extensions=extensions,
                               extension_configs=config['mdx_configs'] or {})
        try:
            doc = pandoc.Document(defaults=config["pandoc"])
            doc.markdown = self.markdown
            self.content = doc.html
        except Exception as e:
            logging.warn(
                f"Pandoc conversion failed with the following message: {e}")
            self.content = md.convert(self.markdown)
        self.toc = get_toc(getattr(md, 'toc_tokens', []))
Exemplo n.º 8
0
 def test_charref(self):
     md = '# &#64;Header'
     expected = '&#64;Header - #header'
     toc = get_toc(get_markdown_toc(md))
     self.assertEqual(str(toc).strip(), expected)
     self.assertEqual(len(toc), 1)
Exemplo n.º 9
0
    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>
        """

        base_cfg = load_config()
        pages = [
            Page(
                'Home',
                File('index.md', base_cfg['docs_dir'], base_cfg['site_dir'],
                     base_cfg['use_directory_urls']), base_cfg),
            Page(
                'About',
                File('about.md', base_cfg['docs_dir'], base_cfg['site_dir'],
                     base_cfg['use_directory_urls']), base_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))

        plugin = search.SearchPlugin()
        errors, warnings = plugin.load_config({})

        for page in pages:
            # Fake page.read_source() and page.render()
            page.markdown = md
            page.toc = toc
            page.content = html_content

            index = search_index.SearchIndex(**plugin.config)
            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))