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
|
from epy import CharPos, TextMark, TextSpan, HTMLtoLines
def test_text_mark_validation_check():
assert TextMark(start=CharPos(row=3, col=1), end=CharPos(row=3, col=5)).is_valid()
assert TextMark(start=CharPos(row=3, col=5), end=CharPos(row=3, col=5)).is_valid()
assert not TextMark(start=CharPos(row=3, col=5), end=CharPos(row=3, col=2)).is_valid()
assert TextMark(start=CharPos(row=3, col=5), end=CharPos(row=5, col=2)).is_valid()
assert not TextMark(start=CharPos(row=8, col=5), end=CharPos(row=5, col=2)).is_valid()
assert not TextMark(start=CharPos(row=0, col=3)).is_valid()
def test_mark_to_span():
text = [
"Lorem ipsum dolor sit amet,",
"consectetur adipiscing elit.",
"Curabitur rutrum massa", # 2
"pretium, pulvinar ligula a,", # 3
"aliquam est. Proin ut lectus", # 4
"ac massa fermentum commodo.", # 5
"Duis ac urna a felis mollis",
"laoreet. Nullam finibus nibh",
"convallis, commodo nisl sit",
"amet, vestibulum mauris. Nulla",
"lacinia ultrices lacinia. Duis",
"auctor nunc non felis",
"ultricies, ut egestas tellus",
"rhoncus. Aenean ultrices",
"efficitur lacinia. Aliquam",
"eros lacus, luctus eu lacinia",
"in, eleifend nec nunc. Nam",
"condimentum malesuada",
"facilisis.",
]
assert HTMLtoLines._mark_to_spans(
text, [TextMark(start=CharPos(row=2, col=3), end=CharPos(row=2, col=19))]
) == [TextSpan(start=CharPos(row=2, col=3), n_letters=16)]
assert HTMLtoLines._mark_to_spans(
text,
[
TextMark(start=CharPos(row=2, col=3), end=CharPos(row=3, col=5)),
],
) == [
TextSpan(start=CharPos(row=2, col=3), n_letters=19),
TextSpan(start=CharPos(row=3, col=0), n_letters=5),
]
assert HTMLtoLines._mark_to_spans(
text,
[
TextMark(start=CharPos(row=2, col=3), end=CharPos(row=5, col=3)),
],
) == [
TextSpan(start=CharPos(row=2, col=3), n_letters=19),
TextSpan(start=CharPos(row=3, col=0), n_letters=27),
TextSpan(start=CharPos(row=4, col=0), n_letters=28),
TextSpan(start=CharPos(row=5, col=0), n_letters=3),
]
def test_span_adjustment():
# 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur rutrum massa.'
text = [
"Lorem ipsum dolor",
"sit amet,",
"consectetur",
"adipiscing elit.",
"Curabitur rutrum",
"massa.",
]
assert HTMLtoLines._adjust_wrapped_spans(
text, TextSpan(start=CharPos(row=0, col=2), n_letters=0)
) == [TextSpan(start=CharPos(row=0, col=2), n_letters=0)]
assert HTMLtoLines._adjust_wrapped_spans(
text, TextSpan(start=CharPos(row=0, col=2), n_letters=5)
) == [TextSpan(start=CharPos(row=0, col=2), n_letters=5)]
assert HTMLtoLines._adjust_wrapped_spans(
text, TextSpan(start=CharPos(row=0, col=15), n_letters=2)
) == [TextSpan(start=CharPos(row=0, col=15), n_letters=2)]
assert HTMLtoLines._adjust_wrapped_spans(
text, TextSpan(start=CharPos(row=0, col=14), n_letters=7)
) == [
TextSpan(start=CharPos(row=0, col=14), n_letters=3),
TextSpan(start=CharPos(row=1, col=0), n_letters=4),
]
# assert HTMLtoLines._adjust_wrapped_spans(
# text, TextSpan(start=CharPos(row=1, col=7), n_letters=20)
# ) == [TextSpan(start=CharPos(row=0, col=14), n_letters=3), TextSpan(start=CharPos(row=1, col=0), n_letters=4)]
def test_group_blocks():
block_list = [
TextSpan(start=CharPos(row=0, col=0), n_letters=4),
TextSpan(start=CharPos(row=1, col=0), n_letters=4),
TextSpan(start=CharPos(row=3, col=0), n_letters=4),
TextSpan(start=CharPos(row=3, col=0), n_letters=4),
TextSpan(start=CharPos(row=15, col=0), n_letters=4),
TextSpan(start=CharPos(row=15, col=0), n_letters=4),
]
assert HTMLtoLines._group_spans_by_row(block_list) == {
0: [TextSpan(start=CharPos(row=0, col=0), n_letters=4)],
1: [TextSpan(start=CharPos(row=1, col=0), n_letters=4)],
3: [
TextSpan(start=CharPos(row=3, col=0), n_letters=4),
TextSpan(start=CharPos(row=3, col=0), n_letters=4),
],
15: [
TextSpan(start=CharPos(row=15, col=0), n_letters=4),
TextSpan(start=CharPos(row=15, col=0), n_letters=4),
],
}
|