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