@@ -161,63 +161,8 @@ def getControl(self):
161161 def build (self ):
162162 doc = document .Document ()
163163
164- #run = []
165- #propStack = [{}]
166- #block = [None]
167-
168- #prevListLevel = None
169- #listLevel = None
170- #listStack = [doc]
171-
172164 ctx = BuildContext (doc )
173165
174- def flush ():
175- if ctx .block is None :
176- ctx .block = document .Paragraph ()
177-
178- ctx .block .content .append (
179- document .Text (ctx .propStack [- 1 ].copy (),
180- [u"" .join (ctx .run )]))
181-
182- ctx .run [:] = []
183-
184-
185- def cleanParagraph ():
186- """
187- Compress text runs, remove whitespace at start and end, skip empty blocks, etc
188- """
189-
190- runs = ctx .block .content
191-
192- if not runs :
193- ctx .block = None
194- return
195-
196- joinedRuns = []
197- hasContent = False
198-
199- for run in runs :
200-
201- if run .content [0 ].strip ():
202- hasContent = True
203- else :
204- continue
205-
206- # Join runs only if their properties match
207- if joinedRuns and (run .properties == joinedRuns [- 1 ].properties ):
208- joinedRuns [- 1 ].content [0 ] += run .content [0 ]
209- else :
210- joinedRuns .append (run )
211-
212- if hasContent :
213- # Strip beginning of paragraph
214- joinedRuns [0 ].content [0 ] = joinedRuns [0 ].content [0 ].lstrip ()
215- # And then strip the end
216- joinedRuns [- 1 ].content [0 ] = joinedRuns [- 1 ].content [0 ].rstrip ()
217- ctx .block .content = joinedRuns
218- else :
219- ctx .block = None
220-
221166
222167 for bit in self .group .flatten ():
223168
@@ -228,14 +173,14 @@ def cleanParagraph():
228173 ctx .propStack .append (ctx .propStack [- 1 ].copy ())
229174
230175 elif bit is Pop :
231- flush ()
176+ ctx . flush ()
232177 ctx .propStack .pop ()
233178
234179 elif isinstance (bit , Para ):
235180
236- flush ()
181+ ctx . flush ()
237182 if ctx .block .content :
238- cleanParagraph ()
183+ ctx . cleanParagraph ()
239184 if ctx .block is not None :
240185 ctx .listStack [- 1 ].append (ctx .block )
241186
@@ -253,11 +198,11 @@ def cleanParagraph():
253198 ctx .block = None
254199
255200 elif bit is Reset :
256- flush ()
201+ ctx . flush ()
257202 ctx .propStack [- 1 ].clear ()
258203
259204 elif isinstance (bit , ReadableMarker ):
260- flush ()
205+ ctx . flush ()
261206 if bit .val :
262207 # RTF needs underline markers for hyperlinks,
263208 # but nothing else does. If we're in a hyperlink,
@@ -271,9 +216,9 @@ def cleanParagraph():
271216 del propStack [- 1 ][bit .name ]
272217
273218 if ctx .block is not None :
274- flush ()
219+ ctx . flush ()
275220 if ctx .block .content :
276- cleanParagraph ()
221+ ctx . cleanParagraph ()
277222 if ctx .block is not None :
278223 ctx .listStack [- 1 ].append (ctx .block )
279224
@@ -282,6 +227,7 @@ def cleanParagraph():
282227
283228
284229class BuildContext (object ):
230+
285231 def __init__ (self , doc ):
286232 self .run = []
287233 self .propStack = [{}]
@@ -291,6 +237,56 @@ def __init__(self, doc):
291237 self .listStack = [doc ]
292238
293239
240+ def flush (self ):
241+ if self .block is None :
242+ self .block = document .Paragraph ()
243+
244+ self .block .content .append (
245+ document .Text (self .propStack [- 1 ].copy (),
246+ [u"" .join (self .run )]))
247+
248+ self .run [:] = []
249+
250+
251+
252+ def cleanParagraph (self ):
253+ """
254+ Compress text runs, remove whitespace at start and end,
255+ skip empty blocks, etc
256+ """
257+
258+ runs = self .block .content
259+
260+ if not runs :
261+ self .block = None
262+ return
263+
264+ joinedRuns = []
265+ hasContent = False
266+
267+ for run in runs :
268+
269+ if run .content [0 ].strip ():
270+ hasContent = True
271+ else :
272+ continue
273+
274+ # Join runs only if their properties match
275+ if joinedRuns and (run .properties == joinedRuns [- 1 ].properties ):
276+ joinedRuns [- 1 ].content [0 ] += run .content [0 ]
277+ else :
278+ joinedRuns .append (run )
279+
280+ if hasContent :
281+ # Strip beginning of paragraph
282+ joinedRuns [0 ].content [0 ] = joinedRuns [0 ].content [0 ].lstrip ()
283+ # And then strip the end
284+ joinedRuns [- 1 ].content [0 ] = joinedRuns [- 1 ].content [0 ].rstrip ()
285+ self .block .content = joinedRuns
286+ else :
287+ self .block = None
288+
289+
294290
295291
296292class Group (object ):
0 commit comments