File: test_include.py

package info (click to toggle)
python-markdown-include 0.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: python: 242; makefile: 3
file content (201 lines) | stat: -rw-r--r-- 5,454 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from markdown_include.include import IncludePreprocessor, MarkdownInclude

import markdown
import pathlib
from textwrap import dedent

import pytest


RESOURCE_DIR = pathlib.Path(__file__).parent.absolute() / "resources"


@pytest.fixture(scope="module")
def markdown_include():
    return MarkdownInclude(configs={"base_path": RESOURCE_DIR})


@pytest.fixture(scope="module")
def markdown_include_inherit_heading_depth():
    return MarkdownInclude(
        configs={"base_path": RESOURCE_DIR, "inheritHeadingDepth": True}
    )


def test_single_include(markdown_include):
    source = "{!simple.md!}"
    html = markdown.markdown(source, extensions=[markdown_include])

    assert html == "<p>This is a simple template</p>"


def test_double_include(markdown_include):
    source = "{!simple.md!} and {!simple_2.md!}"
    html = markdown.markdown(source, extensions=[markdown_include])

    assert (
        html == "<p>This is a simple template and This is another simple template</p>"
    )


def test_headers(markdown_include):
    source = (
        "Source file\n"
        "# Heading Level 1 of main file\n"
        "{!header.md!}\n"
        "## Heading Level 2 of main file\n"
        "{!header.md!}"
    )

    html = markdown.markdown(source, extensions=[markdown_include])

    assert html == dedent(
        """\
        <p>Source file</p>
        <h1>Heading Level 1 of main file</h1>
        <h1>This heading will be one level deeper from the previous heading</h1>
        <p>More included file content.
        End of included content.</p>
        <h2>Heading Level 2 of main file</h2>
        <h1>This heading will be one level deeper from the previous heading</h1>
        <p>More included file content.
        End of included content.</p>"""
    )


def test_embedded_template(markdown_include):
    source = "{!template_inside.md!}"
    html = markdown.markdown(source, extensions=[markdown_include])

    assert (
        html
        == "<p>This is a simple template</p>\n<p>This is a template with a template.</p>"
    )


def test_single_include_inherit_heading_depth(markdown_include_inherit_heading_depth):
    source = "{!simple.md!}"
    html = markdown.markdown(
        source, extensions=[markdown_include_inherit_heading_depth]
    )

    assert html == "<p>This is a simple template</p>"


def test_double_include_inherit_heading_depth(markdown_include_inherit_heading_depth):
    source = "{!simple.md!} and {!simple_2.md!}"
    html = markdown.markdown(
        source, extensions=[markdown_include_inherit_heading_depth]
    )

    assert (
        html == "<p>This is a simple template and This is another simple template</p>"
    )


def test_headers_inherit_heading_depth(markdown_include_inherit_heading_depth):
    source = (
        "Source file\n"
        "# Heading Level 1 of main file\n"
        "{!header.md!}\n"
        "## Heading Level 2 of main file\n"
        "{!header.md!}"
    )

    html = markdown.markdown(
        source, extensions=[markdown_include_inherit_heading_depth]
    )

    assert html == dedent(
        """\
        <p>Source file</p>
        <h1>Heading Level 1 of main file</h1>
        <h2>This heading will be one level deeper from the previous heading</h2>
        <p>More included file content.
        End of included content.</p>
        <h2>Heading Level 2 of main file</h2>
        <h3>This heading will be one level deeper from the previous heading</h3>
        <p>More included file content.
        End of included content.</p>"""
    )


def test_processor_lines():
    processor = IncludePreprocessor(
        None,
        {
            "base_path": RESOURCE_DIR,
            "encoding": "utf-8",
            "inheritHeadingDepth": False,
            "headingOffset": 0,
            "throwException": False,
        },
    )

    source = [
        "Source file",
        "# Heading Level 1 of main file",
        "{!header.md!}",
        "## Heading Level 2 of main file",
        "{!header.md!}",
    ]
    result_lines = processor.run(source)

    assert len(result_lines) == 9


def test_include_lines(markdown_include):
    source = "{!longer.md!lines=1 3}"
    html = markdown.markdown(source, extensions=[markdown_include])

    assert html == dedent(
        """\
        <p>This is line 1
        This is line 3</p>"""
    )


def test_include_line_range(markdown_include):
    source = "{!longer.md!lines=3-5}"
    html = markdown.markdown(source, extensions=[markdown_include])

    assert html == dedent(
        """\
        <p>This is line 3
        This is line 4
        This is line 5</p>"""
    )


def test_include_lines_and_line_range(markdown_include):
    source = "{!longer.md!lines=1 3-5 8}"
    html = markdown.markdown(source, extensions=[markdown_include])

    assert html == dedent(
        """\
        <p>This is line 1
        This is line 3
        This is line 4
        This is line 5
        This is line 8</p>"""
    )


def test_include_lines_out_of_order(markdown_include):
    source = "{!longer.md!lines=3 1}"
    html = markdown.markdown(source, extensions=[markdown_include])

    assert html == dedent(
        """\
        <p>This is line 3
        This is line 1</p>"""
    )


def test_nested_table(markdown_include_inherit_heading_depth):
    source = "{!table_inner.md!}"
    html = markdown.markdown(
        source, extensions=[markdown_include_inherit_heading_depth, "tables"]
    )

    assert "<table>" in html