1
|
|
|
from unittest import TestCase |
2
|
|
|
from panflute import ( |
3
|
|
|
Doc, |
4
|
|
|
convert_text, |
5
|
|
|
) |
6
|
|
|
|
7
|
|
|
import pandoc_latex_color |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class ColorTestCase(TestCase): |
11
|
|
|
@classmethod |
12
|
|
|
def conversion(cls, markdown) -> Doc: |
13
|
|
|
""" |
14
|
|
|
Convert a markdown text to a panflute instance. |
15
|
|
|
|
16
|
|
|
Parameters |
17
|
|
|
---------- |
18
|
|
|
markdown |
19
|
|
|
Text |
20
|
|
|
|
21
|
|
|
Returns |
22
|
|
|
------- |
23
|
|
|
Doc |
24
|
|
|
A panflute document |
25
|
|
|
""" |
26
|
|
|
doc = convert_text(markdown, standalone=True) |
27
|
|
|
doc.format = "latex" |
28
|
|
|
pandoc_latex_color.main(doc) |
29
|
|
|
return doc |
30
|
|
|
|
31
|
|
|
def verify_conversion(self, markdown, expected, output_format="latex") -> None: |
32
|
|
|
""" |
33
|
|
|
Verify the conversion |
34
|
|
|
|
35
|
|
|
Parameters |
36
|
|
|
---------- |
37
|
|
|
markdown |
38
|
|
|
Input text |
39
|
|
|
expected |
40
|
|
|
Expected output |
41
|
|
|
output_format |
42
|
|
|
Desired conversion |
43
|
|
|
""" |
44
|
|
|
doc = self.conversion(markdown) |
45
|
|
|
text = convert_text( |
46
|
|
|
doc, |
47
|
|
|
input_format="panflute", |
48
|
|
|
output_format=output_format, |
49
|
|
|
extra_args=["--wrap=none"], |
50
|
|
|
) |
51
|
|
|
print(text.strip()) |
52
|
|
|
print(expected.strip()) |
53
|
|
|
self.assertEqual(text.strip(), expected.strip()) |
54
|
|
|
|
55
|
|
|
def test_span_classes(self): |
56
|
|
|
self.verify_conversion( |
57
|
|
|
""" |
58
|
|
|
--- |
59
|
|
|
pandoc-latex-color: |
60
|
|
|
- classes: [class1, class2] |
61
|
|
|
color: red |
62
|
|
|
--- |
63
|
|
|
|
64
|
|
|
[This is a span]{.class1 .class2} |
65
|
|
|
""", |
66
|
|
|
""" |
67
|
|
|
{\\color{red}This is a span} |
68
|
|
|
""", |
69
|
|
|
) |
70
|
|
|
self.verify_conversion( |
71
|
|
|
""" |
72
|
|
|
--- |
73
|
|
|
pandoc-latex-color: |
74
|
|
|
- classes: [class1, class2] |
75
|
|
|
color: red |
76
|
|
|
bgcolor: blue |
77
|
|
|
--- |
78
|
|
|
|
79
|
|
|
[**This is a span**]{.class1 .class2} |
80
|
|
|
""", |
81
|
|
|
""" |
82
|
|
|
{\\color{red}\\sethlcolor{blue}\\hl{\\textbf{This is a span}}} |
83
|
|
|
""", |
84
|
|
|
) |
85
|
|
|
|
86
|
|
|
def test_span_attributes(self): |
87
|
|
|
self.verify_conversion( |
88
|
|
|
""" |
89
|
|
|
[This is a span]{latex-color="red"} |
90
|
|
|
""", |
91
|
|
|
""" |
92
|
|
|
{\\color{red}This is a span} |
93
|
|
|
""", |
94
|
|
|
) |
95
|
|
|
self.verify_conversion( |
96
|
|
|
""" |
97
|
|
|
[This is a span]{latex-color="red" latex-bgcolor="blue"} |
98
|
|
|
""", |
99
|
|
|
""" |
100
|
|
|
{\\color{red}\\sethlcolor{blue}\\hl{This is a span}} |
101
|
|
|
""", |
102
|
|
|
) |
103
|
|
|
|
104
|
|
|
def test_bad_color(self): |
105
|
|
|
self.verify_conversion( |
106
|
|
|
""" |
107
|
|
|
[This is a span]{latex-color="badcolor"} |
108
|
|
|
""", |
109
|
|
|
""" |
110
|
|
|
{\\color{black}This is a span} |
111
|
|
|
""", |
112
|
|
|
) |
113
|
|
|
|
114
|
|
|
def test_missing_color(self): |
115
|
|
|
self.verify_conversion( |
116
|
|
|
""" |
117
|
|
|
--- |
118
|
|
|
pandoc-latex-color: |
119
|
|
|
- classes: [class1, class2] |
120
|
|
|
--- |
121
|
|
|
|
122
|
|
|
[This is a span]{.class1 .class2} |
123
|
|
|
""", |
124
|
|
|
""" |
125
|
|
|
{\\color{black}This is a span} |
126
|
|
|
""", |
127
|
|
|
) |
128
|
|
|
|