author | andy_robinson |
Mon, 12 Jun 2000 11:27:17 +0000 | |
changeset 266 | 081154da1a78 |
parent 253 | cfcf8d555a2c |
child 267 | 52a348f6c4c3 |
permissions | -rw-r--r-- |
162 | 1 |
############################################################################### |
2 |
# |
|
3 |
# ReportLab Public License Version 1.0 |
|
4 |
# |
|
5 |
# Except for the change of names the spirit and intention of this |
|
6 |
# license is the same as that of Python |
|
7 |
# |
|
8 |
# (C) Copyright ReportLab Inc. 1998-2000. |
|
9 |
# |
|
10 |
# |
|
11 |
# All Rights Reserved |
|
12 |
# |
|
13 |
# Permission to use, copy, modify, and distribute this software and its |
|
14 |
# documentation for any purpose and without fee is hereby granted, provided |
|
15 |
# that the above copyright notice appear in all copies and that both that |
|
16 |
# copyright notice and this permission notice appear in supporting |
|
17 |
# documentation, and that the name of ReportLab not be used |
|
18 |
# in advertising or publicity pertaining to distribution of the software |
|
19 |
# without specific, written prior permission. |
|
20 |
# |
|
21 |
# |
|
22 |
# Disclaimer |
|
23 |
# |
|
24 |
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
|
25 |
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, |
|
26 |
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT |
|
27 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
|
28 |
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
|
29 |
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
30 |
# PERFORMANCE OF THIS SOFTWARE. |
|
31 |
# |
|
32 |
############################################################################### |
|
33 |
# $Log: paraparser.py,v $ |
|
266 | 34 |
# Revision 1.22 2000/06/12 11:27:17 andy_robinson |
35 |
# Added Sequencer and associated XML tags |
|
36 |
# |
|
253 | 37 |
# Revision 1.21 2000/06/01 15:23:06 rgbecker |
38 |
# Platypus re-organisation |
|
266 | 39 |
# |
250 | 40 |
# Revision 1.20 2000/05/31 10:12:20 rgbecker |
41 |
# <bullet> xml tag added |
|
253 | 42 |
# |
248 | 43 |
# Revision 1.19 2000/05/26 09:49:23 rgbecker |
44 |
# Color fixes; thanks to J Alet |
|
250 | 45 |
# |
238 | 46 |
# Revision 1.18 2000/05/20 15:36:42 andy_robinson |
47 |
# Removed 1.5.2-style getattr call |
|
248 | 48 |
# |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
211
diff
changeset
|
49 |
# Revision 1.17 2000/05/16 14:28:55 rgbecker |
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
211
diff
changeset
|
50 |
# Fixes/Changes to get testplatypus to work with new framework |
238 | 51 |
# |
211 | 52 |
# Revision 1.16 2000/05/15 12:15:29 rgbecker |
53 |
# CDATA handler added |
|
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
211
diff
changeset
|
54 |
# |
209 | 55 |
# Revision 1.15 2000/05/13 16:04:06 rgbecker |
56 |
# made size alias of fontsize for <para> |
|
211 | 57 |
# |
192 | 58 |
# Revision 1.14 2000/05/11 14:05:17 rgbecker |
59 |
# Use reportlab.lib.xmllib |
|
209 | 60 |
# |
162 | 61 |
# Revision 1.13 2000/04/25 13:07:57 rgbecker |
62 |
# Added license |
|
192 | 63 |
# |
266 | 64 |
__version__=''' $Id: paraparser.py,v 1.22 2000/06/12 11:27:17 andy_robinson Exp $ ''' |
96 | 65 |
import string |
119 | 66 |
import re |
67 |
from types import TupleType |
|
96 | 68 |
import sys |
69 |
import os |
|
70 |
import copy |
|
71 |
||
192 | 72 |
#try: |
73 |
# from xml.parsers import xmllib |
|
74 |
# _xmllib_newStyle = 1 |
|
209 | 75 |
try: |
76 |
from reportlab.lib import xmllib |
|
77 |
_xmllib_newStyle = 1 |
|
78 |
except ImportError: |
|
79 |
import xmllib |
|
80 |
_xmllib_newStyle = 0 |
|
81 |
||
96 | 82 |
|
248 | 83 |
from reportlab.lib.colors import toColor, white, black, red, Color |
96 | 84 |
from reportlab.lib.fonts import tt2ps, ps2tt |
119 | 85 |
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY |
266 | 86 |
from reportlab.lib.sequencer import Sequencer |
119 | 87 |
_re_para = re.compile('^\\s*<\\s*para(\\s+|>)') |
96 | 88 |
|
89 |
sizeDelta = 2 # amount to reduce font size by for super and sub script |
|
90 |
subFraction = 0.5 # fraction of font size that a sub script should be lowered |
|
91 |
superFraction = 0.5 # fraction of font size that a super script should be raised |
|
92 |
||
102 | 93 |
def _num(s): |
119 | 94 |
if s[0] in ['+','-']: |
95 |
try: |
|
96 |
return ('relative',int(s)) |
|
97 |
except ValueError: |
|
98 |
return ('relative',float(s)) |
|
99 |
else: |
|
100 |
try: |
|
101 |
return int(s) |
|
102 |
except ValueError: |
|
103 |
return float(s) |
|
104 |
||
105 |
def _align(s): |
|
106 |
s = string.lower(s) |
|
107 |
if s=='left': return TA_LEFT |
|
108 |
elif s=='right': return TA_RIGHT |
|
109 |
elif s=='justify': return TA_JUSTIFY |
|
110 |
elif s in ('centre','center'): return TA_CENTER |
|
111 |
else: raise ValueError |
|
112 |
||
113 |
_paraAttrMap = {'font': ('fontName', None), |
|
250 | 114 |
'face': ('fontName', None), |
119 | 115 |
'fontsize': ('fontSize', _num), |
209 | 116 |
'size': ('fontSize', _num), |
119 | 117 |
'leading': ('leading', _num), |
118 |
'lindent': ('leftIndent', _num), |
|
119 |
'rindent': ('rightIndent', _num), |
|
120 |
'findent': ('firstLineIndent', _num), |
|
121 |
'align': ('alignment', _align), |
|
122 |
'spaceb': ('spaceBefore', _num), |
|
123 |
'spacea': ('spaceAfter', _num), |
|
124 |
'bfont': ('bulletFontName', None), |
|
250 | 125 |
'bfontsize': ('bulletFontSize',_num), |
126 |
'bindent': ('bulletIndent',_num), |
|
248 | 127 |
'bcolor': ('bulletColor',toColor), |
128 |
'color':('textColor',toColor), |
|
129 |
'fg': ('textColor',toColor)} |
|
119 | 130 |
|
250 | 131 |
_bulletAttrMap = { |
132 |
'font': ('bulletFontName', None), |
|
133 |
'face': ('bulletFontName', None), |
|
134 |
'size': ('bulletFontSize',_num), |
|
135 |
'fontsize': ('bulletFontSize',_num), |
|
136 |
'indent': ('bulletIndent',_num), |
|
137 |
'color': ('bulletColor',toColor), |
|
138 |
'fg': ('bulletColor',toColor)} |
|
139 |
||
119 | 140 |
#things which are valid font attributes |
141 |
_fontAttrMap = {'size': ('fontSize', _num), |
|
250 | 142 |
'face': ('fontName', None), |
119 | 143 |
'name': ('fontName', None), |
248 | 144 |
'fg': ('textColor', toColor), |
145 |
'color':('textColor', toColor)} |
|
119 | 146 |
|
266 | 147 |
|
119 | 148 |
def _addAttributeNames(m): |
149 |
K = m.keys() |
|
150 |
for k in K: |
|
151 |
n = string.lower(m[k][0]) |
|
152 |
if not m.has_key(n): |
|
153 |
m[n] = m[k] |
|
154 |
||
155 |
_addAttributeNames(_paraAttrMap) |
|
156 |
_addAttributeNames(_fontAttrMap) |
|
250 | 157 |
_addAttributeNames(_bulletAttrMap) |
119 | 158 |
|
159 |
def _applyAttributes(obj, attr): |
|
160 |
for k, v in attr.items(): |
|
161 |
if type(v) is TupleType and v[0]=='relative': |
|
238 | 162 |
#AR 20/5/2000 - remove 1.5.2-ism |
163 |
#v = v[1]+getattr(obj,k,0) |
|
164 |
if hasattr(obj, k): |
|
165 |
v = v[1]+getattr(obj,k) |
|
166 |
else: |
|
167 |
v = v[1] |
|
119 | 168 |
setattr(obj,k,v) |
102 | 169 |
|
96 | 170 |
#characters not supported: epsi, Gammad, gammad, kappav, rhov, Upsi, upsi |
171 |
greeks = { |
|
172 |
'alpha':'a', |
|
173 |
'beta':'b', |
|
174 |
'chi':'c', |
|
175 |
'Delta':'D', |
|
176 |
'delta':'d', |
|
177 |
'epsiv':'e', |
|
178 |
'eta':'h', |
|
179 |
'Gamma':'G', |
|
180 |
'gamma':'g', |
|
181 |
'iota':'i', |
|
182 |
'kappa':'k', |
|
183 |
'Lambda':'L', |
|
184 |
'lambda':'l', |
|
185 |
'mu':'m', |
|
186 |
'nu':'n', |
|
187 |
'Omega':'W', |
|
188 |
'omega':'w', |
|
189 |
'omicron':'x', |
|
190 |
'Phi':'F', |
|
191 |
'phi':'f', |
|
192 |
'phiv':'j', |
|
193 |
'Pi':'P', |
|
194 |
'pi':'p', |
|
195 |
'piv':'v', |
|
196 |
'Psi':'Y', |
|
197 |
'psi':'y', |
|
198 |
'rho':'r', |
|
199 |
'Sigma':'S', |
|
200 |
'sigma':'s', |
|
201 |
'sigmav':'V', |
|
202 |
'tau':'t', |
|
203 |
'Theta':'Q', |
|
204 |
'theta':'q', |
|
205 |
'thetav':'j', |
|
206 |
'Xi':'X', |
|
207 |
'xi':'x', |
|
208 |
'zeta':'z' |
|
209 |
} |
|
210 |
||
211 |
#------------------------------------------------------------------------ |
|
212 |
class ParaFrag: |
|
213 |
"""class ParaFrag contains the intermediate representation of string |
|
214 |
segments as they are being parsed by the XMLParser. |
|
215 |
""" |
|
102 | 216 |
def __init__(self,**attr): |
217 |
for k,v in attr.items(): |
|
218 |
setattr(self,k,v) |
|
219 |
||
220 |
def clone(self,**attr): |
|
221 |
n = apply(ParaFrag,(),self.__dict__) |
|
222 |
if attr != {}: apply(ParaFrag.__init__,(n,),attr) |
|
223 |
return n |
|
96 | 224 |
|
225 |
#------------------------------------------------------------------ |
|
226 |
# The ParaFormatter will be able to format the following xml |
|
227 |
# tags: |
|
228 |
# < b > < /b > - bold |
|
229 |
# < i > < /i > - italics |
|
230 |
# < u > < /u > - underline |
|
231 |
# < super > < /super > - superscript |
|
232 |
# < sub > < /sub > - subscript |
|
233 |
# <font name=fontfamily/fontname color=colorname size=float> |
|
266 | 234 |
# < bullet > </bullet> - bullet text (at head of para only) |
119 | 235 |
# The whole may be surrounded by <para> </para> tags |
236 |
# |
|
96 | 237 |
# It will also be able to handle any MathML specified Greek characters. |
238 |
#------------------------------------------------------------------ |
|
239 |
class ParaParser(xmllib.XMLParser): |
|
240 |
||
241 |
#---------------------------------------------------------- |
|
242 |
# First we will define all of the xml tag handler functions. |
|
243 |
# |
|
244 |
# start_<tag>(attributes) |
|
245 |
# end_<tag>() |
|
246 |
# |
|
247 |
# While parsing the xml ParaFormatter will call these |
|
248 |
# functions to handle the string formatting tags. |
|
249 |
# At the start of each tag the corresponding field will |
|
250 |
# be set to 1 and at the end tag the corresponding field will |
|
251 |
# be set to 0. Then when handle_data is called the options |
|
252 |
# for that data will be aparent by the current settings. |
|
253 |
#---------------------------------------------------------- |
|
254 |
||
255 |
#### bold |
|
256 |
def start_b( self, attributes ): |
|
257 |
self._push(bold=1) |
|
258 |
||
259 |
def end_b( self ): |
|
260 |
self._pop(bold=1) |
|
261 |
||
262 |
#### italics |
|
263 |
def start_i( self, attributes ): |
|
264 |
self._push(italic=1) |
|
265 |
||
266 |
def end_i( self ): |
|
267 |
self._pop(italic=1) |
|
268 |
||
269 |
#### underline |
|
270 |
def start_u( self, attributes ): |
|
271 |
self._push(underline=1) |
|
272 |
||
273 |
def end_u( self ): |
|
274 |
self._pop(underline=1) |
|
275 |
||
276 |
#### super script |
|
277 |
def start_super( self, attributes ): |
|
278 |
self._push(super=1) |
|
279 |
||
280 |
def end_super( self ): |
|
281 |
self._pop(super=1) |
|
282 |
||
283 |
#### sub script |
|
284 |
def start_sub( self, attributes ): |
|
285 |
self._push(sub=1) |
|
286 |
||
287 |
def end_sub( self ): |
|
288 |
self._pop(sub=1) |
|
289 |
||
290 |
#### greek script |
|
291 |
if _xmllib_newStyle: |
|
292 |
def handle_entityref(self,name): |
|
293 |
if greeks.has_key(name): |
|
294 |
self._push(greek=1) |
|
295 |
self.handle_data(greeks[name]) |
|
296 |
self._pop(greek=1) |
|
297 |
else: |
|
298 |
xmllib.XMLParser.handle_entityref(self,name) |
|
134 | 299 |
|
300 |
def syntax_error(self,lineno,message): |
|
301 |
self._syntax_error(message) |
|
302 |
||
96 | 303 |
else: |
304 |
def start_greekLetter(self, attributes,letter): |
|
305 |
self._push(greek=1) |
|
306 |
self.handle_data(letter) |
|
307 |
||
134 | 308 |
def syntax_error(self,message): |
309 |
self._syntax_error(message) |
|
310 |
||
311 |
def _syntax_error(self,message): |
|
312 |
if message[:10]=="attribute " and message[-17:]==" value not quoted": return |
|
313 |
self.errors.append(message) |
|
314 |
||
96 | 315 |
def start_greek(self, attributes): |
316 |
self._push(greek=1) |
|
317 |
||
318 |
def end_greek(self): |
|
319 |
self._pop(greek=1) |
|
320 |
||
119 | 321 |
|
96 | 322 |
def start_font(self,attr): |
119 | 323 |
A = self.getAttributes(attr,_fontAttrMap) |
113 | 324 |
apply(self._push,(),A) |
96 | 325 |
|
326 |
def end_font(self): |
|
327 |
self._pop() |
|
328 |
||
250 | 329 |
def _initial_frag(self,attr,attrMap,bullet=0): |
119 | 330 |
style = self._style |
331 |
if attr!={}: |
|
332 |
style = copy.deepcopy(style) |
|
250 | 333 |
_applyAttributes(style,self.getAttributes(attr,attrMap)) |
119 | 334 |
self._style = style |
335 |
||
336 |
# initialize semantic values |
|
337 |
frag = ParaFrag() |
|
338 |
frag.sub = 0 |
|
339 |
frag.super = 0 |
|
340 |
frag.rise = 0 |
|
341 |
frag.underline = 0 |
|
342 |
frag.greek = 0 |
|
250 | 343 |
if bullet: |
344 |
frag.fontName, frag.bold, frag.italic = ps2tt(style.bulletFontName) |
|
345 |
frag.fontSize = style.bulletFontSize |
|
346 |
frag.textColor = hasattr(style,'bulletColor') and style.bulletColor or style.textColor |
|
347 |
else: |
|
348 |
frag.fontName, frag.bold, frag.italic = ps2tt(style.fontName) |
|
349 |
frag.fontSize = style.fontSize |
|
350 |
frag.textColor = style.textColor |
|
351 |
return frag |
|
352 |
||
353 |
def start_para(self,attr): |
|
354 |
self._stack = [self._initial_frag(attr,_paraAttrMap)] |
|
119 | 355 |
|
356 |
def end_para(self): |
|
357 |
self._pop() |
|
358 |
||
250 | 359 |
def start_bullet(self,attr): |
360 |
if hasattr(self,'bFragList'): |
|
361 |
self._syntax_error('only one <bullet> tag allowed') |
|
362 |
self.bFragList = [] |
|
363 |
frag = self._initial_frag(attr,_bulletAttrMap,1) |
|
364 |
frag.isBullet = 1 |
|
365 |
self._stack.append(frag) |
|
366 |
||
367 |
def end_bullet(self): |
|
368 |
self._pop() |
|
369 |
||
266 | 370 |
|
371 |
||
372 |
#--------------------------------------------------------------- |
|
373 |
def setSequencer(self, seq): |
|
374 |
self._seq = seq |
|
375 |
||
376 |
def getSequencer(self): |
|
377 |
if self._seq is None: |
|
378 |
self._seq = Sequencer() |
|
379 |
return self._seq |
|
380 |
||
381 |
def start_seqdefault(self, attr): |
|
382 |
try: |
|
383 |
default = attr['id'] |
|
384 |
except KeyError: |
|
385 |
default = None |
|
386 |
self.getSequencer().setDefaultCounter(default) |
|
387 |
||
388 |
def end_seqdefault(self): |
|
389 |
pass |
|
390 |
||
391 |
def start_seqreset(self, attr): |
|
392 |
try: |
|
393 |
id = attr['id'] |
|
394 |
except KeyError: |
|
395 |
id = None |
|
396 |
try: |
|
397 |
base = math.atoi(attr['base']) |
|
398 |
except: |
|
399 |
base=1 |
|
400 |
self.getSequencer().reset(id, base) |
|
401 |
||
402 |
def end_seqreset(self): |
|
403 |
pass |
|
404 |
||
405 |
def start_seq(self, attr): |
|
406 |
#if it has a template, use that; otherwise try for id; |
|
407 |
#otherwise take default sequence |
|
408 |
if attr.has_key('template'): |
|
409 |
templ = attr['template'] |
|
410 |
self.handle_data(templ % self.getSequencer()) |
|
411 |
return |
|
412 |
elif attr.has_key('id'): |
|
413 |
id = attr['id'] |
|
414 |
else: |
|
415 |
id = None |
|
416 |
output = self.getSequencer().nextf(id) |
|
417 |
self.handle_data(output) |
|
418 |
||
419 |
def end_seq(self): |
|
420 |
pass |
|
421 |
||
422 |
#--------------------------------------------------------------- |
|
119 | 423 |
def _push(self,**attr): |
96 | 424 |
frag = copy.copy(self._stack[-1]) |
119 | 425 |
_applyAttributes(frag,attr) |
96 | 426 |
self._stack.append(frag) |
427 |
||
428 |
def _pop(self,**kw): |
|
429 |
frag = self._stack[-1] |
|
430 |
del self._stack[-1] |
|
431 |
for k, v in kw.items(): |
|
432 |
assert getattr(frag,k)==v |
|
433 |
return frag |
|
434 |
||
119 | 435 |
def getAttributes(self,attr,attrMap): |
436 |
A = {} |
|
437 |
for k, v in attr.items(): |
|
438 |
k = string.lower(k) |
|
439 |
if k in attrMap.keys(): |
|
440 |
j = attrMap[k] |
|
441 |
func = j[1] |
|
442 |
try: |
|
123 | 443 |
A[j[0]] = (func is None) and v or apply(func,(v,)) |
119 | 444 |
except: |
134 | 445 |
self._syntax_error('%s: invalid value %s'%(k,v)) |
119 | 446 |
else: |
134 | 447 |
self._syntax_error('invalid attribute name %s'%k) |
119 | 448 |
return A |
449 |
||
96 | 450 |
#---------------------------------------------------------------- |
451 |
||
452 |
def __init__(self,verbose=0): |
|
266 | 453 |
# the sequencing stuff presumes access to a sequencer. |
454 |
# this may be set with setSequencer(); if a <seq> tag |
|
455 |
# is encountered and it has not been set, a default |
|
456 |
# sequencer will be provided. |
|
457 |
self._seq = None |
|
458 |
||
96 | 459 |
if _xmllib_newStyle: |
460 |
xmllib.XMLParser.__init__(self,verbose=verbose) |
|
461 |
else: |
|
462 |
xmllib.XMLParser.__init__(self) |
|
463 |
# set up handlers for various tags |
|
464 |
self.elements = { 'b': (self.start_b, self.end_b), |
|
465 |
'u': (self.start_u, self.end_u), |
|
466 |
'i': (self.start_i, self.end_i), |
|
467 |
'super': (self.start_super, self.end_super), |
|
468 |
'sub': (self.start_sub, self.end_sub), |
|
469 |
'font': (self.start_font, self.end_font), |
|
132 | 470 |
'greek': (self.start_greek, self.end_greek), |
471 |
'para': (self.start_para, self.end_para) |
|
96 | 472 |
} |
473 |
||
132 | 474 |
|
96 | 475 |
# automatically add handlers for all of the greek characters |
476 |
for item in greeks.keys(): |
|
477 |
self.elements[item] = (lambda attr,self=self,letter=greeks[item]: |
|
478 |
self.start_greekLetter(attr,letter), self.end_greek) |
|
479 |
||
480 |
# set up dictionary for greek characters, this is a class variable |
|
192 | 481 |
self.entitydefs = self.entitydefs.copy() |
96 | 482 |
for item in greeks.keys(): |
483 |
self.entitydefs[item] = '<%s/>' % item |
|
484 |
||
266 | 485 |
|
486 |
||
250 | 487 |
def _iReset(self): |
488 |
self.fragList = [] |
|
489 |
if hasattr(self, 'bFragList'): delattr(self,'bFragList') |
|
490 |
||
96 | 491 |
def _reset(self, style): |
492 |
'''reset the parser''' |
|
493 |
xmllib.XMLParser.reset(self) |
|
494 |
||
495 |
# initialize list of string segments to empty |
|
496 |
self.errors = [] |
|
119 | 497 |
self._style = style |
250 | 498 |
self._iReset() |
96 | 499 |
|
500 |
#---------------------------------------------------------------- |
|
501 |
def handle_data(self,data): |
|
502 |
"Creates an intermediate representation of string segments." |
|
503 |
||
504 |
frag = copy.copy(self._stack[-1]) |
|
102 | 505 |
#save our data |
506 |
frag.text = data |
|
96 | 507 |
|
508 |
# if sub and super are both one they will cancel each other out |
|
509 |
if frag.sub == 1 and frag.super == 1: |
|
510 |
frag.sub = 0 |
|
511 |
frag.super = 0 |
|
512 |
||
112 | 513 |
if frag.sub: |
514 |
frag.rise = -frag.fontSize*subFraction |
|
515 |
frag.fontSize = max(frag.fontSize-sizeDelta,3) |
|
115 | 516 |
elif frag.super: |
112 | 517 |
frag.rise = frag.fontSize*superFraction |
115 | 518 |
frag.fontSize = max(frag.fontSize-sizeDelta,3) |
112 | 519 |
|
102 | 520 |
if frag.greek: frag.fontName = 'symbol' |
96 | 521 |
# bold, italic, and underline |
102 | 522 |
frag.fontName = tt2ps(frag.fontName,frag.bold,frag.italic) |
96 | 523 |
|
250 | 524 |
if hasattr(frag,'isBullet'): |
525 |
delattr(frag,'isBullet') |
|
526 |
self.bFragList.append(frag) |
|
527 |
else: |
|
528 |
self.fragList.append(frag) |
|
96 | 529 |
|
211 | 530 |
def handle_cdata(self,data): |
531 |
self.handle_data(data) |
|
532 |
||
96 | 533 |
#---------------------------------------------------------------- |
102 | 534 |
def parse(self, text, style): |
96 | 535 |
"""Given a formatted string will return a list of |
536 |
ParaFrag objects with their calculated widths. |
|
537 |
If errors occur None will be returned and the |
|
538 |
self.errors holds a list of the error messages. |
|
539 |
""" |
|
540 |
||
541 |
# the xmlparser requires that all text be surrounded by xml |
|
542 |
# tags, therefore we must throw some unused flags around the |
|
543 |
# given string |
|
544 |
self._reset(style) # reinitialise the parser |
|
119 | 545 |
if not(len(text)>=6 and text[0]=='<' and _re_para.match(text)): |
546 |
text = "<para>"+text+"</para>" |
|
547 |
self.feed(text) |
|
96 | 548 |
self.close() # force parsing to complete |
119 | 549 |
style = self._style |
550 |
del self._style |
|
96 | 551 |
if len(self.errors)==0: |
552 |
fragList = self.fragList |
|
250 | 553 |
bFragList = hasattr(self,'bFragList') and self.bFragList or None |
554 |
self._iReset() |
|
96 | 555 |
else: |
250 | 556 |
fragList = bFragList = None |
557 |
return style, fragList, bFragList |
|
96 | 558 |
|
559 |
if __name__=='__main__': |
|
253 | 560 |
from reportlab.platypus import cleanBlockQuotedText |
96 | 561 |
_parser=ParaParser() |
133 | 562 |
def check_text(text,p=_parser): |
563 |
print '##########' |
|
564 |
text = cleanBlockQuotedText(text) |
|
250 | 565 |
l,rv,bv = p.parse(text,style) |
133 | 566 |
if rv is None: |
567 |
for l in _parser.errors: |
|
568 |
print l |
|
569 |
else: |
|
570 |
print 'ParaStyle', l.fontName,l.fontSize,l.textColor |
|
571 |
for l in rv: |
|
572 |
print l.fontName,l.fontSize,l.textColor,l.bold, l.rise, l.text[:25] |
|
96 | 573 |
|
574 |
style=ParaFrag() |
|
575 |
style.fontName='Times-Roman' |
|
576 |
style.fontSize = 12 |
|
577 |
style.textColor = black |
|
250 | 578 |
style.bulletFontName = black |
579 |
style.bulletFontName='Times-Roman' |
|
580 |
style.bulletFontSize=12 |
|
96 | 581 |
|
582 |
text=''' |
|
583 |
<b><i><greek>a</greek>D</i></b>β |
|
584 |
<font name="helvetica" size="15" color=green> |
|
585 |
Tell me, O muse, of that ingenious hero who travelled far and wide |
|
113 | 586 |
after</font> he had sacked the famous town of Troy. Many cities did he visit, |
96 | 587 |
and many were the nations with whose manners and customs he was acquainted; |
588 |
moreover he suffered much by sea while trying to save his own life |
|
589 |
and bring his men safely home; but do what he might he could not save |
|
590 |
his men, for they perished through their own sheer folly in eating |
|
591 |
the cattle of the Sun-god Hyperion; so the god prevented them from |
|
592 |
ever reaching home. Tell me, too, about all these things, O daughter |
|
115 | 593 |
of Jove, from whatsoever source you<super>1</super> may know them. |
96 | 594 |
''' |
133 | 595 |
check_text(text) |
596 |
check_text('<para> </para>') |
|
209 | 597 |
check_text('<para font="times-bold" size=24 leading=28.8 spaceAfter=72>ReportLab -- Reporting for the Internet Age</para>') |
133 | 598 |
check_text(''' |
599 |
<font color=red>τ</font>Tell me, O muse, of that ingenious hero who travelled far and wide |
|
600 |
after he had sacked the famous town of Troy. Many cities did he visit, |
|
601 |
and many were the nations with whose manners and customs he was acquainted; |
|
602 |
moreover he suffered much by sea while trying to save his own life |
|
603 |
and bring his men safely home; but do what he might he could not save |
|
604 |
his men, for they perished through their own sheer folly in eating |
|
605 |
the cattle of the Sun-god Hyperion; so the god prevented them from |
|
606 |
ever reaching home. Tell me, too, about all these things, O daughter |
|
607 |
of Jove, from whatsoever source you may know them.''') |
|
608 |
check_text(''' |
|
609 |
Telemachus took this speech as of good omen and rose at once, for |
|
610 |
he was bursting with what he had to say. He stood in the middle of |
|
611 |
the assembly and the good herald Pisenor brought him his staff. Then, |
|
612 |
turning to Aegyptius, "Sir," said he, "it is I, as you will shortly |
|
613 |
learn, who have convened you, for it is I who am the most aggrieved. |
|
614 |
I have not got wind of any host approaching about which I would warn |
|
615 |
you, nor is there any matter of public moment on which I would speak. |
|
616 |
My grieveance is purely personal, and turns on two great misfortunes |
|
617 |
which have fallen upon my house. The first of these is the loss of |
|
618 |
my excellent father, who was chief among all you here present, and |
|
619 |
was like a father to every one of you; the second is much more serious, |
|
620 |
and ere long will be the utter ruin of my estate. The sons of all |
|
621 |
the chief men among you are pestering my mother to marry them against |
|
622 |
her will. They are afraid to go to her father Icarius, asking him |
|
623 |
to choose the one he likes best, and to provide marriage gifts for |
|
624 |
his daughter, but day by day they keep hanging about my father's house, |
|
625 |
sacrificing our oxen, sheep, and fat goats for their banquets, and |
|
626 |
never giving so much as a thought to the quantity of wine they drink. |
|
627 |
No estate can stand such recklessness; we have now no Ulysses to ward |
|
628 |
off harm from our doors, and I cannot hold my own against them. I |
|
629 |
shall never all my days be as good a man as he was, still I would |
|
630 |
indeed defend myself if I had power to do so, for I cannot stand such |
|
631 |
treatment any longer; my house is being disgraced and ruined. Have |
|
632 |
respect, therefore, to your own consciences and to public opinion. |
|
633 |
Fear, too, the wrath of heaven, lest the gods should be displeased |
|
634 |
and turn upon you. I pray you by Jove and Themis, who is the beginning |
|
635 |
and the end of councils, [do not] hold back, my friends, and leave |
|
636 |
me singlehanded- unless it be that my brave father Ulysses did some |
|
637 |
wrong to the Achaeans which you would now avenge on me, by aiding |
|
638 |
and abetting these suitors. Moreover, if I am to be eaten out of house |
|
639 |
and home at all, I had rather you did the eating yourselves, for I |
|
640 |
could then take action against you to some purpose, and serve you |
|
641 |
with notices from house to house till I got paid in full, whereas |
|
642 |
now I have no remedy."''') |
|
643 |
||
644 |
check_text(''' |
|
645 |
But as the sun was rising from the fair sea into the firmament of |
|
646 |
heaven to shed light on mortals and immortals, they reached Pylos |
|
647 |
the city of Neleus. Now the people of Pylos were gathered on the sea |
|
648 |
shore to offer sacrifice of black bulls to Neptune lord of the Earthquake. |
|
649 |
There were nine guilds with five hundred men in each, and there were |
|
650 |
nine bulls to each guild. As they were eating the inward meats and |
|
651 |
burning the thigh bones [on the embers] in the name of Neptune, Telemachus |
|
652 |
and his crew arrived, furled their sails, brought their ship to anchor, |
|
653 |
and went ashore. ''') |
|
654 |
check_text(''' |
|
655 |
So the neighbours and kinsmen of Menelaus were feasting and making |
|
656 |
merry in his house. There was a bard also to sing to them and play |
|
657 |
his lyre, while two tumblers went about performing in the midst of |
|
658 |
them when the man struck up with his tune.]''') |
|
659 |
check_text(''' |
|
660 |
"When we had passed the [Wandering] rocks, with Scylla and terrible |
|
661 |
Charybdis, we reached the noble island of the sun-god, where were |
|
662 |
the goodly cattle and sheep belonging to the sun Hyperion. While still |
|
663 |
at sea in my ship I could bear the cattle lowing as they came home |
|
664 |
to the yards, and the sheep bleating. Then I remembered what the blind |
|
665 |
Theban prophet Teiresias had told me, and how carefully Aeaean Circe |
|
666 |
had warned me to shun the island of the blessed sun-god. So being |
|
667 |
much troubled I said to the men, 'My men, I know you are hard pressed, |
|
668 |
but listen while I tell you the prophecy that Teiresias made me, and |
|
669 |
how carefully Aeaean Circe warned me to shun the island of the blessed |
|
670 |
sun-god, for it was here, she said, that our worst danger would lie. |
|
671 |
Head the ship, therefore, away from the island.''') |
|
192 | 672 |
check_text('''< > & " '''') |
211 | 673 |
check_text('''<![CDATA[<>&'"]]>''') |
250 | 674 |
check_text('''<bullet face=courier size=14 color=green>+</bullet> |
675 |
There was a bard also to sing to them and play |
|
676 |
his lyre, while two tumblers went about performing in the midst of |
|
677 |
them when the man struck up with his tune.]''') |