-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.py
More file actions
398 lines (290 loc) · 10.6 KB
/
document.py
File metadata and controls
398 lines (290 loc) · 10.6 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# -*- coding: UTF-8 -*-
import os
import codecs
import re
from diff_match_patch import diff_match_patch
from fragments import FragmentIdentifier
###############################################################################
class Document:
def __init__ (self):
self._analyticIndex = None
self._properties = None
self._content = None
self._formatting = []
self._metadata = []
self._sections = None
def initWithFile (self, documentPath):
file = codecs.open(os.path.join(documentPath, 'content.txt'), 'r', 'utf-8')
self._content = []
for line in file:
self._content.append(line.rstrip('\n').replace('\\r', '\r'))
file.close()
self._formatting = self.readFragmentFile(os.path.join(documentPath, 'formatting.txt')) or []
self._metadata = self.readFragmentFile(os.path.join(documentPath, 'metadata.txt')) or []
self._properties = self.readPropertyFile(os.path.join(documentPath, 'properties.txt')) or {}
return self
def initWithDocumentInfo (self, content, formatting, metadata, properties=None):
self._content = content
if formatting:
self._formatting = sorted(formatting)
if metadata:
self._metadata = sorted(metadata)
self._properties = properties
return self
def readFragmentFile (self, filename):
if os.path.isfile(filename):
fragments = []
file = codecs.open(filename, 'r', 'utf-8')
for line in file:
normalizedLine = line.replace('\\n', '\n')
fragments.append(FragmentIdentifier.fragmentWithLine(normalizedLine, self.content()))
file.close()
result = sorted(fragments)
else:
result = None
return result
def readPropertyFile (self, filename):
if os.path.isfile(filename):
pattern = re.compile(r"((?P<spaces>\s+)|(?P<key>[^\s\:][^\:]*)\:)\s*(?P<value>[^\n]+)?", re.DOTALL)
properties = {}
file = codecs.open(filename, 'r', 'utf-8')
key = None
for line in file:
match = pattern.match(line)
if match.group('key'):
if key:
properties[key] = value
key = match.group('key')
value = match.group('value')
else:
if value:
value += '\n' + match.group('value')
else:
value = match.group('value')
pass
if key and value:
properties[key] = value
file.close()
result = properties
else:
result = None
return result
def content (self):
return self._content
def text (self):
return Document.contentToText(self.content())
def tableOfContent (self):
def fragmentTocLevel (fragment):
if 'style' in fragment.attributes and fragment.attributes['style'].startswith('Heading '):
result = fragment.attributes['style'][len('Heading '):]
else:
result = None
return result
rootElement = TocEntry(0, None, 0, None)
currentElement = rootElement
count = 0
for tag in self.tags():
tagLevel = fragmentTocLevel(tag)
if tagLevel:
count += 1
currentElement = currentElement.addItem(count, tag, tagLevel)
return rootElement
def formatting (self):
return self._formatting
def metadata (self):
return self._metadata
def tags (self):
return sorted(self.formatting() + self.metadata())
def writeTo (self, destinationFolder):
if not os.path.exists(destinationFolder):
os.makedirs(destinationFolder)
contentFile = open(os.path.join(destinationFolder, 'content.txt'), 'w')
for paragraph in self.content():
contentFile.write(paragraph.replace('\r', '\\r').encode('utf-8') + '\n')
contentFile.close()
if self._formatting:
formattingFile = open(os.path.join(destinationFolder, 'formatting.txt'),'w')
for fragment in self._formatting:
formattingFile.write((unicode(fragment) + '\n').encode('utf-8'))
formattingFile.close()
if self._metadata:
metadataFile = open(os.path.join(destinationFolder, 'metadata.txt'),'w')
for fragment in self._metadata:
metadataFile.write((unicode(fragment) + '\n').encode('utf-8'))
metadataFile.close()
def mergeWithDocument (self, newVersion):
diffs = Document.computeDiffs(self.text(), newVersion.text())
translationMap = Document.computeTranslationMap(diffs)
reframedMetadata = []
for normalizedMetadata in Document.normalizeFragments(self.content(), self.metadata()):
updatedFragment = normalizedMetadata.updateWithTranslationMap(translationMap)
if updatedFragment:
reframedMetadata.append(updatedFragment)
updatedMetadata = Document.rebaseFragments(newVersion.content(), reframedMetadata)
return Document().initWithDocumentInfo(newVersion.content(), newVersion.formatting(), updatedMetadata)
#==========================================================================
def properties (self):
if not self._properties:
self._properties = {}
return self._properties
def analyticIndex (self):
if not self._analyticIndex:
# TODO: load actual analytic index here
self._analyticIndex = []
return self._analyticIndex
#--------------------------------------------------------------------------
def sections (self, aSplitFunction):
def computeSections (aSplitFunction):
def shouldSplitSectionsAtLine (aSplitFunction, lineNumber):
result = False
# fragmentsAtLine = filter(lambda fragment:fragment.shouldSelectionStartAtLine(lineNumber), self.tags())
# if len(fragmentsAtLine) > 0:
# result = True
# else:
# result = False
for fragment in filter(lambda fragment:fragment.startingPoint() == (lineNumber, 1), self.tags()):
# if 'structure' in fragment.attributes and fragment.attributes['structure'] in ['possible-page-break', 'page-break']:
# result = True
# if 'style' in fragment.attributes and fragment.attributes['style'].startswith('Heading '):
# result = True
if aSplitFunction(fragment):
result = True
return result
result = []
counter = 1
fromIndex = 1
toIndex = 1
gotSomeActualContent = False
totalNumberOfLines = len(self.content())
while toIndex <= totalNumberOfLines:
if shouldSplitSectionsAtLine(aSplitFunction, toIndex) and gotSomeActualContent:
result.append(DocumentSection(self, "section_" + str(counter).zfill(2), (fromIndex, toIndex - 1)))
gotSomeActualContent = False
counter += 1
fromIndex = toIndex
else:
gotSomeActualContent = True
toIndex += 1
result.append(DocumentSection(self, "section_" + str(counter).zfill(2), (fromIndex, toIndex - 1)))
return result
# if not self._sections:
# self._sections = computeSections()
#
# return self._sections
# splitFunction = aSplitFunction if aSplitFunction else DocumentSection.splitSectionOnPageBreak
return computeSections(aSplitFunction)
#==========================================================================
@staticmethod
def normalizeFragments (content, fragments):
if fragments:
result = []
lineAccumulatedCount = Document.lineAccumulatedCount(content)
for fragment in fragments:
result.append(fragment.normalize(lineAccumulatedCount[int(fragment.startingPoint()[0]) - 1]))
else:
result = None
return result
@staticmethod
def rebaseFragments (content, fragments):
if fragments:
result = []
lineAccumulatedCount = Document.lineAccumulatedCount(content)
for fragment in fragments:
result.append(fragment.rebase(lineAccumulatedCount))
else:
result = None
return result
@staticmethod
def lineAccumulatedCount (content):
currentCounter = 0
result = []
for line in content:
result.append((currentCounter, len(line)))
currentCounter = currentCounter + len(line) + 1
return result
@staticmethod
def computeTranslationMap (diffs):
result = []
blockStartIndex = 1
blockEndIndex = 1
currentOffset = 0
for diff in diffs:
if diff[0] == diff_match_patch.DIFF_EQUAL:
blockEndIndex = blockStartIndex + len(diff[1])
elif diff[0] == diff_match_patch.DIFF_INSERT:
if blockStartIndex < blockEndIndex:
result.append((blockStartIndex, blockEndIndex, currentOffset))
blockStartIndex = blockEndIndex
result.append((blockStartIndex, blockEndIndex, currentOffset, diff[1]))
currentOffset += len(diff[1])
elif diff[0] == diff_match_patch.DIFF_DELETE:
if blockStartIndex < blockEndIndex:
result.append((blockStartIndex, blockEndIndex, currentOffset))
blockStartIndex = blockEndIndex + len(diff[1])
blockEndIndex = blockStartIndex
currentOffset -= len(diff[1])
else:
raise DiffUnhandledCase(diff[0])
if blockStartIndex < blockEndIndex:
result.append((blockStartIndex, blockEndIndex, currentOffset))
return result
@staticmethod
def computeDiffs (original, newVersion):
diffEngine = diff_match_patch()
result = diffEngine.diff_main(original, newVersion)
diffEngine.diff_cleanupSemantic(result)
return result
@staticmethod
def contentToText (content):
return '\n'.join(content) + '\n'
@staticmethod
def cleanupTextAndTags (text, tags):
pass
###############################################################################
class DocumentSection:
def __init__ (self, document, name, contentRange):
self._document = document
self._name = name
self._contentRange = contentRange
def document (self):
return self._document
def name (self):
return self._name
def contentRange (self):
return self._contentRange
def content (self):
return self.document().content()[self.contentRange()[0] - 1:self.contentRange()[1]]
def isFragmentRelevant (self, fragment):
return (self.contentRange()[0], 1) <= fragment.startingPoint() < (self.contentRange()[1] + 1, 1)
def tags (self):
result = []
offset = self.contentRange()[0] - 1
for tag in self.document().tags():
# if (self.contentRange()[0], 1) <= tag.startingPoint() < (self.contentRange()[1] + 1, 1):
if self.isFragmentRelevant(tag):
result.append(tag.shiftFragmentLine(offset, self.contentRange()[1] - self.contentRange()[0] + 1))
return result
@staticmethod
def splitSectionOnPageBreak (fragment):
return 'structure' in fragment.attributes and fragment.attributes['structure'] in ['possible-page-break', 'page-break']
@staticmethod
def splitSectionOnHeaders (fragment):
return 'style' in fragment.attributes and fragment.attributes['style'].startswith('Heading ')
###############################################################################
class TocEntry:
def __init__ (self, count, fragment, level, parent):
self.count = count
self.fragment = fragment
self.level = level
self.parent = parent
self.children = []
def addItem (self, count, fragment, level):
if self.level < level:
result = TocEntry(count, fragment, level, self)
self.children.append(result)
else:
result = self.parent.addItem(count, fragment, level)
return result
###############################################################################
class DiffUnhandledCase(Exception):
pass