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