1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
import os
from mistune import create_markdown
from mistune.directives import (
RSTDirective,
FencedDirective,
Admonition,
TableOfContents,
Include,
Image,
Figure,
)
from tests import BaseTestCase
from tests.fixtures import ROOT
def load_directive_test(filename, directive, cls):
class TestDirective(BaseTestCase):
@staticmethod
def parse(text):
md = create_markdown(
escape=False,
plugins=[cls([directive])],
)
html = md(text)
return html
TestDirective.load_fixtures(filename + ".txt")
globals()["TestDirective_" + filename] = TestDirective
load_directive_test("rst_admonition", Admonition(), RSTDirective)
load_directive_test("rst_toc", TableOfContents(), RSTDirective)
load_directive_test("fenced_admonition", Admonition(), FencedDirective)
load_directive_test("fenced_toc", TableOfContents(), FencedDirective)
load_directive_test("fenced_image", Image(), FencedDirective)
load_directive_test("fenced_figure", Figure(), FencedDirective)
class CustomizeTableOfContents(TableOfContents):
def generate_heading_id(self, token, i):
return "t-" + str(i + 1)
class TestCustomizeToc(BaseTestCase):
def test_rst_toc(self):
md = create_markdown(
escape=False,
plugins=[
RSTDirective([CustomizeTableOfContents()]),
],
)
html = md("# h1\n\n.. toc::\n")
self.assertIn('<h1 id="t-1">h1</h1>', html)
self.assertIn('<a href="#t-1">h1</a>', html)
def test_fenced_toc(self):
md = create_markdown(
escape=False,
plugins=[
FencedDirective([CustomizeTableOfContents()]),
],
)
html = md("# h1\n\n```{toc}\n```\n")
self.assertIn('<h1 id="t-1">h1</h1>', html)
self.assertIn('<a href="#t-1">h1</a>', html)
def test_colon_fenced_toc(self):
md = create_markdown(
escape=False,
plugins=[
FencedDirective([CustomizeTableOfContents()], ":"),
],
)
html = md("# h1\n\n:::{toc}\n:::\n")
self.assertIn('<h1 id="t-1">h1</h1>', html)
self.assertIn('<a href="#t-1">h1</a>', html)
class TestDirectiveInclude(BaseTestCase):
md = create_markdown(escape=False, plugins=[RSTDirective([Include()])]) # type: ignore[list-item]
def test_html_include(self):
html = self.md.read(os.path.join(ROOT, "include/text.md"))[0]
self.assertIn("Could not include self", html)
self.assertIn("Could not find file", html)
self.assertIn("<div>include html</div>", html)
self.assertIn("<blockquote>", html)
self.assertIn("# Table of Contents", html)
def test_include_missing_source(self):
s = ".. include:: foo.txt"
html = self.md(s)
self.assertIn("Missing source file", html)
|