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