File: test_markdown_parse.py

package info (click to toggle)
fpdf2 2.8.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,860 kB
  • sloc: python: 39,487; sh: 133; makefile: 12
file content (184 lines) | stat: -rw-r--r-- 6,081 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
# pylint: disable=protected-access
from fpdf import FPDF
from fpdf.line_break import Fragment

PDF = FPDF()
GSTATE = PDF._get_current_graphics_state()
GSTATE_B = GSTATE.copy()
GSTATE_B["font_style"] = "B"
GSTATE_I = GSTATE.copy()
GSTATE_I["font_style"] = "I"
GSTATE_S = GSTATE.copy()
GSTATE_S["strikethrough"] = True
GSTATE_U = GSTATE.copy()
GSTATE_U["underline"] = True
GSTATE_BI = GSTATE.copy()
GSTATE_BI["font_style"] = "BI"


def test_markdown_parse_simple_ok():
    frags = tuple(
        FPDF()._parse_chars(
            "**bold**, __italics__, ~~strikethrough~~ and --underlined--", True
        )
    )
    expected = (
        Fragment("bold", GSTATE_B, k=PDF.k),
        Fragment(", ", GSTATE, k=PDF.k),
        Fragment("italics", GSTATE_I, k=PDF.k),
        Fragment(", ", GSTATE, k=PDF.k),
        Fragment("strikethrough", GSTATE_S, k=PDF.k),
        Fragment(" and ", GSTATE, k=PDF.k),
        Fragment("underlined", GSTATE_U, k=PDF.k),
    )
    assert frags == expected


def test_markdown_parse_simple_ok_escaped():
    frags = tuple(
        FPDF()._parse_chars(
            "\\**bold\\**, \\__italics\\__ and \\--underlined\\-- escaped", True
        )
    )
    expected = (
        Fragment("**bold**, __italics__ and --underlined-- escaped", GSTATE, k=PDF.k),
    )
    assert frags == expected
    frags = tuple(
        FPDF()._parse_chars(
            r"raw \**bold\**, \__italics\__ and \--underlined\-- escaped", True
        )
    )
    expected = (
        Fragment(
            "raw **bold**, __italics__ and --underlined-- escaped", GSTATE, k=PDF.k
        ),
    )
    assert frags == expected
    frags = tuple(FPDF()._parse_chars("escape *\\*between marker*\\*", True))
    expected = (Fragment("escape *\\*between marker*\\*", GSTATE, k=PDF.k),)
    assert frags == expected
    frags = tuple(FPDF()._parse_chars("escape **\\after marker**\\", True))
    expected = (
        Fragment("escape ", GSTATE, k=PDF.k),
        Fragment("\\after marker", GSTATE_B, k=PDF.k),
        Fragment("\\", GSTATE, k=PDF.k),
    )


def test_markdown_unrelated_escape():
    frags = tuple(FPDF()._parse_chars("unrelated \\ escape \\**bold\\**", True))
    expected = (Fragment("unrelated \\ escape **bold**", GSTATE, k=PDF.k),)
    assert frags == expected
    frags = tuple(
        FPDF()._parse_chars("unrelated \\\\ double escape \\**bold\\**", True)
    )
    expected = (Fragment("unrelated \\\\ double escape **bold**", GSTATE, k=PDF.k),)
    assert frags == expected


def test_markdown_parse_multiple_escape():
    frags = tuple(FPDF()._parse_chars("\\\\**bold\\\\** double escaped", True))
    expected = (
        Fragment("\\", GSTATE, k=PDF.k),
        Fragment("bold\\", GSTATE_B, k=PDF.k),
        Fragment(" double escaped", GSTATE, k=PDF.k),
    )
    assert frags == expected
    frags = tuple(FPDF()._parse_chars("\\\\\\**triple bold\\\\\\** escaped", True))
    expected = (Fragment("\\**triple bold\\** escaped", GSTATE, k=PDF.k),)
    assert frags == expected


def test_markdown_parse_overlapping():
    frags = tuple(FPDF()._parse_chars("**bold __italics__**", True))
    expected = (
        Fragment("bold ", GSTATE_B, k=PDF.k),
        Fragment("italics", GSTATE_BI, k=PDF.k),
    )
    assert frags == expected


def test_markdown_parse_overlapping_escaped():
    frags = tuple(FPDF()._parse_chars("**bold \\__italics\\__**", True))
    expected = (Fragment("bold __italics__", GSTATE_B, k=PDF.k),)
    assert frags == expected


def test_markdown_parse_crossing_markers():
    frags = tuple(FPDF()._parse_chars("**bold __and** italics__", True))
    expected = (
        Fragment("bold ", GSTATE_B, k=PDF.k),
        Fragment("and", GSTATE_BI, k=PDF.k),
        Fragment(" italics", GSTATE_I, k=PDF.k),
    )
    assert frags == expected


def test_markdown_parse_crossing_markers_escaped():
    frags = tuple(FPDF()._parse_chars("**bold __and\\** italics__", True))
    expected = (
        Fragment("bold ", GSTATE_B, k=PDF.k),
        Fragment("and** italics", GSTATE_BI, k=PDF.k),
    )
    assert frags == expected


def test_markdown_parse_unterminated():
    frags = tuple(FPDF()._parse_chars("**bold __italics__", True))
    expected = (
        Fragment("bold ", GSTATE_B, k=PDF.k),
        Fragment("italics", GSTATE_BI, k=PDF.k),
    )
    assert frags == expected


def test_markdown_parse_unterminated_escaped():
    frags = tuple(FPDF()._parse_chars("**bold\\** __italics__", True))
    expected = (
        Fragment("bold** ", GSTATE_B, k=PDF.k),
        Fragment("italics", GSTATE_BI, k=PDF.k),
    )
    assert frags == expected


def test_markdown_parse_line_of_markers():
    frags = tuple(FPDF()._parse_chars("*** woops", True))
    expected = (Fragment("*** woops", GSTATE, k=PDF.k),)
    assert frags == expected
    frags = tuple(FPDF()._parse_chars("----------", True))
    expected = (Fragment("----------", GSTATE, k=PDF.k),)
    assert frags == expected
    frags = tuple(FPDF()._parse_chars("****BOLD**", True))
    expected = (Fragment("****BOLD", GSTATE, k=PDF.k),)
    assert frags == expected
    frags = tuple(FPDF()._parse_chars("* **BOLD**", True))
    expected = (
        Fragment("* ", GSTATE, k=PDF.k),
        Fragment("BOLD", GSTATE_B, k=PDF.k),
    )
    assert frags == expected


def test_markdown_parse_line_of_markers_escaped():
    frags = tuple(FPDF()._parse_chars("\\****BOLD**", True))
    expected = (Fragment("\\****BOLD", GSTATE, k=PDF.k),)
    assert frags == expected
    frags = tuple(FPDF()._parse_chars("*\\***BOLD**", True))
    expected = (Fragment("*\\***BOLD", GSTATE, k=PDF.k),)
    assert frags == expected


def test_markdown_parse_newline_after_markdown_link():  # issue 916
    text = "[fpdf2](https://py-pdf.github.io/fpdf2/)\nGo visit it!"
    frags = tuple(FPDF()._parse_chars(text, True))
    expected = (
        Fragment(
            "fpdf2",
            {**GSTATE, "underline": True},
            k=PDF.k,
            link="https://py-pdf.github.io/fpdf2/",
        ),
        Fragment("\nGo visit it!", GSTATE, k=PDF.k),
    )
    assert frags == expected