-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.py
276 lines (232 loc) · 8.37 KB
/
test.py
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014, Thorsten Behrens
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import odpdown
import mistune
import codecs
from lpod import ODF_MANIFEST
from lpod.document import odf_new_document
from lpod.draw_page import odf_draw_page
from nose.tools import with_setup, raises
testdoc = None
odf_renderer = None
mkdown = None
def setup():
global testdoc, odf_renderer, mkdown
testdoc = odf_new_document('presentation')
odf_renderer = odpdown.ODFRenderer(testdoc,u'Nimbus Mono L')
mkdown = mistune.Markdown(renderer=odf_renderer)
@with_setup(setup)
def test_heading1():
markdown = '# Heading 1'
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert isinstance(odf.get()[0], odf_draw_page)
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 1
assert odf.get()[0].get_elements(
'descendant::text:span')[0].get_text() == 'Heading 1'
@with_setup(setup)
def test_heading2():
markdown = '## Heading 2'
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert isinstance(odf.get()[0], odf_draw_page)
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 1
assert odf.get()[0].get_elements(
'descendant::text:span')[0].get_text() == 'Heading 2'
@raises(RuntimeError)
@with_setup(setup)
def test_heading3():
# headings of level 3 or higher not supported currently
markdown = '### Heading 3'
odf = mkdown.render(markdown)
@with_setup(setup)
def test_simple_page():
markdown = '''
## Heading
This is a sample paragraph.
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
assert (odf.get()[0].get_elements('descendant::text:span')[0].get_text() ==
'Heading')
assert (odf.get()[0].get_elements('descendant::text:span')[2].get_text() ==
'This is a sample paragraph.')
@with_setup(setup)
def test_items_page():
markdown = '''
## Heading
* this is item one
* this is item two
* and a subitem
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
assert (odf.get()[0].get_elements('descendant::text:span')[0].get_text() ==
'Heading')
items = odf.get()[0].get_elements('descendant::text:list-item')
assert len(items) == 3
assert items[0].get_elements(
'descendant::text:span')[0].get_text() == 'this is item one'
assert items[1].get_elements(
'descendant::text:span')[0].get_text() == 'this is item two'
subitems = items[1].get_elements('descendant::text:list-item')
assert len(subitems) == 1
assert subitems[0].get_elements(
'descendant::text:span')[0].get_text() == 'and a subitem'
@with_setup(setup)
def test_empty_list_items_page():
markdown = '''
## Heading
* a
*
* c
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
items = odf.get()[0].get_elements('descendant::text:list-item')
assert len(items) == 3
assert items[0].get_elements(
'descendant::text:span')[0].get_text() == 'a'
assert len(items[1].get_elements(
'descendant::text:span')) == 0
assert items[2].get_elements(
'descendant::text:span')[0].get_text() == 'c'
@with_setup(setup)
def test_xml_entity_escaping():
markdown = '''
## Heading
There "is" <some> & 'the' other to escape
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
# mistune splits text at '<', so we get two spans here
assert (odf.get()[0].get_elements('descendant::text:span')[2].get_text() ==
'There "is" ')
assert (odf.get()[0].get_elements('descendant::text:span')[3].get_text() ==
'<some>')
assert (odf.get()[0].get_elements('descendant::text:span')[4].get_text() ==
' & \'the\' other to escape')
@with_setup(setup)
def test_nested_emphasis():
markdown = '''
## Heading
***triple emphasis***
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
assert (odf.get()[0].get_elements('descendant::text:span')[2].get_attribute(
'text:style-name') == 'md2odp-TextDoubleEmphasisStyle')
assert (odf.get()[0].get_elements('descendant::text:span')[3].get_attribute(
'text:style-name') == 'md2odp-TextEmphasisStyle')
assert (odf.get()[0].get_elements('descendant::text:span')[4].get_text() ==
'triple emphasis')
@with_setup(setup)
def test_code_block():
markdown = '''
## Heading
~~~ c++
void main()
{
return -1;
}
~~~
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
assert (odf.get()[0].get_elements('descendant::text:span')[0].get_text() ==
'Heading')
spaces = odf.get()[0].get_elements('descendant::text:s')
assert len(spaces) == 1
assert spaces[0].get_attribute('text:c') == '4'
@with_setup(setup)
def test_complex():
# read more complex doc from disk. simply don't crash...
markdown = codecs.open('cramtest/test.md', 'r', encoding='utf-8').read()
odf = mkdown.render(markdown)
pass
@with_setup(setup)
def test_svg1():
markdown = '''
## Heading
![This is alt text](cramtest/test.svg)
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
assert odf.get()[0].get_elements('descendant::draw:frame')[1].get_attribute(
'svg:width') == '22cm'
assert odf.get()[0].get_elements('descendant::draw:frame')[1].get_attribute(
'svg:height') == '9cm'
assert len(odf.get()[0].get_elements('descendant::draw:image')) == 1
@with_setup(setup)
def test_weird_uris():
markdown = '''
## Heading
![This is alt text](cramtest/test.svg;_-^!$%&=+#test)
'''.strip()
mkdown.render(markdown)
assert u'.svg' in [x[-4:] for x in testdoc.get_part(
ODF_MANIFEST).get_paths()]
@with_setup(setup)
def test_single_space_bug():
markdown = '''
## Heading
~~~ bash
if [ $? -eq 0 ]; then
fi
~~~
'''.strip()
odf = mkdown.render(markdown)
assert odf.get()[0].get_elements('descendant::text:span')[7].get_text() == ' -eq '
@with_setup(setup)
def test_multiline_bold():
markdown = '''
## Heading
**bold text
next line bold**
'''.strip()
odf = mkdown.render(markdown)
assert len(odf.get()) == 1
assert len(odf.get()[0].get_elements('descendant::draw:frame')) == 2
assert (odf.get()[0].get_elements('descendant::text:span')[2].get_attribute(
'text:style-name') == 'md2odp-TextDoubleEmphasisStyle')
assert (odf.get()[0].get_elements('descendant::text:span')[3].get_text() ==
'bold text\nnext line bold')