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