-
Notifications
You must be signed in to change notification settings - Fork 1
/
document_helper.py
133 lines (101 loc) · 5.18 KB
/
document_helper.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
from string import punctuation
import enchant
from PySide6 import QtGui
class DocumentHelper():
def __init__(self, document: QtGui.QTextDocument, lang_code: str) -> None:
self.document = document
self.lang_code = lang_code
def break_document_into_fragments(self) -> list[list]:
paragraphs: list[list] = []
block = self.document.begin()
while block.isValid():
line_fragments = []
# # Iterate over fragments
# # TODO: QTextBlock doesn’t offer Qt’s internal iterator facilities for some reason, use Python iterators
# try:
# frag_it = next(block.begin())
# except (StopIteration):
# # End of paragraph detected
# paragraphs.append(lines)
# lines = []
# else:
# if frag_it == block.end():
# # End of paragraph detected
# paragraphs.append(lines)
# lines = []
# for i in frag_it:
# line_fragments.append(i.fragment())
# # print(i.fragment().text())
# # cursor.insertText()
# next(i)
# if line_fragments:
# lines.append(line_fragments)
frag_it = next(block.begin())
while True:
lines = []
for i in frag_it:
line_fragments.append(i.fragment())
# print(i.fragment().text())
next(i)
try:
next(frag_it)
except (StopIteration):
# End of paragraph detected
paragraphs.append(line_fragments)
break
block = block.next()
# if lines:
# paragraphs.append(lines)
return paragraphs
def remove_hyphens(self) -> QtGui.QTextDocument:
dictionary = enchant.Dict(self.lang_code + '_' + self.lang_code.upper())
paragraphs = self.break_document_into_fragments()
new_document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(new_document)
format = QtGui.QTextCharFormat()
first_word = ''
for p, paragraph in enumerate(paragraphs):
for l, line in enumerate(paragraph):
for f, fragment in enumerate(line):
text: str = fragment.text()
format: QtGui.QTextCharFormat = fragment.charFormat()
cursor.setCharFormat(format)
if first_word:
(first_word, a, b) = text.partition(' ')
text = b
first_word = ''
if f == len(line) - 1:
if text:
if text[-1] == '-':
if l < len(paragraph) - 1:
(pre_a, pre_b, last_word) = text[:-1].rpartition(' ')
(first_word, post_a, post_b) = paragraph[l + 1][0].text().partition(' ')
if pre_a.strip():
cursor.insertText(pre_a.strip() + ' ')
if last_word and first_word:
if dictionary.check((last_word + first_word).strip(punctuation + '»«›‹„“”')) and first_word[0].isalpha():
# text = text[:-1] + first_word
format.setUnderlineStyle(QtGui.QTextCharFormat.UnderlineStyle.DotLine)
cursor.setCharFormat(format)
cursor.insertText((last_word + first_word).strip())
else:
# text = text + first_word
format.setUnderlineColor(QtGui.QColor(255, 0, 0, 255))
format.setUnderlineStyle(QtGui.QTextCharFormat.UnderlineStyle.DotLine)
cursor.setCharFormat(format)
cursor.insertText((last_word + '-' + first_word).strip())
format.setUnderlineStyle(QtGui.QTextCharFormat.UnderlineStyle.NoUnderline)
format.clearBackground()
cursor.setCharFormat(format)
cursor.insertText(' ')
continue
if text.strip():
cursor.insertText(text.strip())
format.clearBackground()
cursor.setCharFormat(format)
cursor.insertText(' ')
if l == len(paragraph) - 1:
cursor.deletePreviousChar()
if p < len(paragraphs) - 1:
cursor.insertText('\n\n')
return new_document