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