reportlab/platypus/paraparser.py
author rgbecker
Fri, 14 Apr 2000 12:17:05 +0000
changeset 129 623baa767856
parent 128 a0676f013314
child 132 8a9576d734b9
permissions -rw-r--r--
Splitting layout.py
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     1
import string
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
     2
import re
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
     3
from types import TupleType
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     4
import sys
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     5
import os
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     6
import copy
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     7
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     8
try:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     9
	from xml.parsers import xmllib
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    10
	_xmllib_newStyle = 1
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    11
except ImportError:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    12
	import xmllib
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    13
	_xmllib_newStyle = 0
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    14
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    15
from reportlab.lib.colors import stringToColor, white, black, red, Color
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    16
from reportlab.lib.fonts import tt2ps, ps2tt
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    17
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
    18
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    19
_re_para = re.compile('^\\s*<\\s*para(\\s+|>)')
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    20
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    21
sizeDelta = 2		# amount to reduce font size by for super and sub script
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    22
subFraction = 0.5	# fraction of font size that a sub script should be lowered
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    23
superFraction = 0.5	# fraction of font size that a super script should be raised
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    24
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
    25
def _num(s):
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    26
	if s[0] in ['+','-']:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    27
		try:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    28
			return ('relative',int(s))
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    29
		except ValueError:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    30
			return ('relative',float(s))
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    31
	else:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    32
		try:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    33
			return int(s)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    34
		except ValueError:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    35
			return float(s)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    36
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    37
def _align(s):
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    38
	s = string.lower(s)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    39
	if s=='left': return TA_LEFT
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    40
	elif s=='right': return TA_RIGHT
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    41
	elif s=='justify': return TA_JUSTIFY
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    42
	elif s in ('centre','center'): return TA_CENTER
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    43
	else: raise ValueError
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    44
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    45
_paraAttrMap = {'font': ('fontName', None),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    46
				'fontsize': ('fontSize', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    47
				'leading': ('leading', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    48
				'lindent': ('leftIndent', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    49
				'rindent': ('rightIndent', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    50
				'findent': ('firstLineIndent', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    51
				'align': ('alignment', _align),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    52
				'spaceb': ('spaceBefore', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    53
				'spacea': ('spaceAfter', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    54
				'bfont': ('bulletFontName', None),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    55
				'bfontsize': ('bulletFontIndent',_num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    56
				'bindent': ('bulletFontIndent',_num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    57
				'color':('textColor',stringToColor),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    58
				'fg': ('textColor',stringToColor)}
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    59
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    60
#things which are valid font attributes
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    61
_fontAttrMap = {'size': ('fontSize', _num),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    62
				'name': ('fontName', None),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    63
				'fg': 	('textColor', stringToColor),
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    64
				'color':('textColor', stringToColor)}
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    65
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    66
def _addAttributeNames(m):
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    67
	K = m.keys()
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    68
	for k in K:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    69
		n = string.lower(m[k][0])
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    70
		if not m.has_key(n):
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    71
			m[n] = m[k]
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    72
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    73
_addAttributeNames(_paraAttrMap)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    74
_addAttributeNames(_fontAttrMap)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    75
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    76
def _applyAttributes(obj, attr):
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    77
	for k, v in attr.items():
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    78
		if type(v) is TupleType and v[0]=='relative':
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    79
			v = v[1]+getattr(obj,k,0)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    80
		setattr(obj,k,v)
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
    81
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    82
#characters not supported: epsi, Gammad, gammad, kappav, rhov, Upsi, upsi
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    83
greeks = {
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    84
	'alpha':'a',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    85
	'beta':'b',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    86
	'chi':'c',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    87
	'Delta':'D',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    88
	'delta':'d',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    89
	'epsiv':'e',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    90
	'eta':'h',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    91
	'Gamma':'G',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    92
	'gamma':'g',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    93
	'iota':'i',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    94
	'kappa':'k',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    95
	'Lambda':'L',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    96
	'lambda':'l',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    97
	'mu':'m',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    98
	'nu':'n',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    99
	'Omega':'W',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   100
	'omega':'w',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   101
	'omicron':'x',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   102
	'Phi':'F',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   103
	'phi':'f',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   104
	'phiv':'j',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   105
	'Pi':'P',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   106
	'pi':'p',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   107
	'piv':'v',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   108
	'Psi':'Y',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   109
	'psi':'y',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   110
	'rho':'r',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   111
	'Sigma':'S',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   112
	'sigma':'s',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   113
	'sigmav':'V',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   114
	'tau':'t',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   115
	'Theta':'Q',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   116
	'theta':'q',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   117
	'thetav':'j',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   118
	'Xi':'X',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   119
	'xi':'x',
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   120
	'zeta':'z'
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   121
}
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   122
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   123
#------------------------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   124
class ParaFrag:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   125
	"""class ParaFrag contains the intermediate representation of string
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   126
	segments as they are being parsed by the XMLParser.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   127
	"""
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   128
	def __init__(self,**attr):
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   129
		for k,v in attr.items():
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   130
			setattr(self,k,v)
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   131
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   132
	def clone(self,**attr):
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   133
		n = apply(ParaFrag,(),self.__dict__)
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   134
		if attr != {}: apply(ParaFrag.__init__,(n,),attr)
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   135
		return n
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   136
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   137
#------------------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   138
# The ParaFormatter will be able to format the following xml
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   139
# tags:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   140
#	   < b > < /b > - bold
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   141
#	   < i > < /i > - italics
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   142
#	   < u > < /u > - underline
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   143
#	   < super > < /super > - superscript
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   144
#	   < sub > < /sub > - subscript
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   145
#	   <font name=fontfamily/fontname color=colorname size=float>
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   146
#
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   147
#		The whole may be surrounded by <para> </para> tags
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   148
#
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   149
# It will also be able to handle any MathML specified Greek characters.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   150
#------------------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   151
class ParaParser(xmllib.XMLParser):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   152
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   153
	#----------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   154
	# First we will define all of the xml tag handler functions.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   155
	#
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   156
	# start_<tag>(attributes)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   157
	# end_<tag>()
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   158
	#
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   159
	# While parsing the xml ParaFormatter will call these
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   160
	# functions to handle the string formatting tags.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   161
	# At the start of each tag the corresponding field will
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   162
	# be set to 1 and at the end tag the corresponding field will
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   163
	# be set to 0.	Then when handle_data is called the options
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   164
	# for that data will be aparent by the current settings.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   165
	#----------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   166
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   167
	#### bold
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   168
	def start_b( self, attributes ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   169
		self._push(bold=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   170
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   171
	def end_b( self ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   172
		self._pop(bold=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   173
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   174
	#### italics
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   175
	def start_i( self, attributes ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   176
		self._push(italic=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   177
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   178
	def end_i( self ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   179
		self._pop(italic=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   180
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   181
	#### underline
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   182
	def start_u( self, attributes ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   183
		self._push(underline=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   184
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   185
	def end_u( self ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   186
		self._pop(underline=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   187
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   188
	#### super script
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   189
	def start_super( self, attributes ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   190
		self._push(super=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   191
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   192
	def end_super( self ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   193
		self._pop(super=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   194
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   195
	#### sub script
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   196
	def start_sub( self, attributes ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   197
		self._push(sub=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   198
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   199
	def end_sub( self ):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   200
		self._pop(sub=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   201
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   202
	#### greek script
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   203
	if _xmllib_newStyle:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   204
		def handle_entityref(self,name):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   205
			if greeks.has_key(name):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   206
				self._push(greek=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   207
				self.handle_data(greeks[name])
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   208
				self._pop(greek=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   209
			else:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   210
				xmllib.XMLParser.handle_entityref(self,name)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   211
	else:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   212
		def start_greekLetter(self, attributes,letter):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   213
			self._push(greek=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   214
			self.handle_data(letter)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   215
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   216
	def start_greek(self, attributes):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   217
		self._push(greek=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   218
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   219
	def end_greek(self):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   220
		self._pop(greek=1)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   221
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   222
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   223
	def start_font(self,attr):
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   224
		A = self.getAttributes(attr,_fontAttrMap)
113
2c20ecb9015e Fixed start_font
rgbecker
parents: 112
diff changeset
   225
		apply(self._push,(),A)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   226
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   227
	def end_font(self):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   228
		self._pop()
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   229
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   230
	def start_para(self,attr):
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   231
		style = self._style
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   232
		if attr!={}:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   233
			style = copy.deepcopy(style)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   234
			_applyAttributes(style,self.getAttributes(attr,_paraAttrMap))
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   235
			self._style = style
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   236
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   237
		# initialize semantic values
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   238
		frag = ParaFrag()
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   239
		frag.sub = 0
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   240
		frag.super = 0
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   241
		frag.rise = 0
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   242
		frag.fontName, frag.bold, frag.italic = ps2tt(style.fontName)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   243
		frag.fontSize = style.fontSize
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   244
		frag.underline = 0
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   245
		frag.textColor = style.textColor
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   246
		frag.greek = 0
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   247
		self._stack = [frag]
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   248
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   249
	def end_para(self):
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   250
		self._pop()
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   251
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   252
	def _push(self,**attr):
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   253
		frag = copy.copy(self._stack[-1])
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   254
		_applyAttributes(frag,attr)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   255
		self._stack.append(frag)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   256
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   257
	def _pop(self,**kw):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   258
		frag = self._stack[-1]
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   259
		del self._stack[-1]
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   260
		for k, v in kw.items():
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   261
			assert getattr(frag,k)==v
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   262
		return frag
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   263
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   264
	def getAttributes(self,attr,attrMap):
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   265
		A = {}
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   266
		for k, v in attr.items():
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   267
			k = string.lower(k)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   268
			if k in attrMap.keys():
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   269
				j = attrMap[k]
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   270
				func = j[1]
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   271
				try:
123
daa339ac3104 Fixes to attribute handling and test script
rgbecker
parents: 119
diff changeset
   272
					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
   273
				except:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   274
					self.syntax_error('%s: invalid value %s'%(k,v))
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   275
			else:
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   276
				self.syntax_error('invalid attribute name %s'%k)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   277
		return A
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   278
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   279
	#----------------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   280
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   281
	def __init__(self,verbose=0):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   282
		if _xmllib_newStyle:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   283
			xmllib.XMLParser.__init__(self,verbose=verbose)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   284
		else:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   285
			xmllib.XMLParser.__init__(self)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   286
			# set up handlers for various tags
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   287
			self.elements = {	'b': (self.start_b, self.end_b),
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   288
							'u': (self.start_u, self.end_u),
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   289
							'i': (self.start_i, self.end_i),
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   290
							'super': (self.start_super, self.end_super),
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   291
							'sub': (self.start_sub, self.end_sub),
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   292
							'font': (self.start_font, self.end_font),
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   293
							'greek': (self.start_greek, self.end_greek)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   294
							}
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   295
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   296
			# automatically add handlers for all of the greek characters
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   297
			for item in greeks.keys():
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   298
				self.elements[item] = (lambda attr,self=self,letter=greeks[item]:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   299
					self.start_greekLetter(attr,letter), self.end_greek)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   300
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   301
			# set up dictionary for greek characters, this is a class variable
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   302
			self.entitydefs = copy.copy(self.entitydefs)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   303
			for item in greeks.keys():
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   304
				self.entitydefs[item] = '<%s/>' % item
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   305
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   306
	def _reset(self, style):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   307
		'''reset the parser'''
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   308
		xmllib.XMLParser.reset(self)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   309
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   310
		# initialize list of string segments to empty
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   311
		self.errors = []
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   312
		self.fragList = []
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   313
		self._style = style
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   314
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   315
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   316
	def syntax_error(self,message):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   317
		if message[:11]=="attribute `" and message[-18:]=="' value not quoted": return
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   318
		self.errors.append(message)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   319
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   320
	#----------------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   321
	def handle_data(self,data):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   322
		"Creates an intermediate representation of string segments."
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   323
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   324
		frag = copy.copy(self._stack[-1])
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   325
		#save our data
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   326
		frag.text = data
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   327
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   328
		# if sub and super are both one they will cancel each other out
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   329
		if frag.sub == 1 and frag.super == 1:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   330
			frag.sub = 0
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   331
			frag.super = 0
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   332
112
1d4892961fdb Added rise attribute
rgbecker
parents: 102
diff changeset
   333
		if frag.sub:
1d4892961fdb Added rise attribute
rgbecker
parents: 102
diff changeset
   334
			frag.rise = -frag.fontSize*subFraction
1d4892961fdb Added rise attribute
rgbecker
parents: 102
diff changeset
   335
			frag.fontSize = max(frag.fontSize-sizeDelta,3)
115
49b2fae9ea60 Fixes to superscript stuff
rgbecker
parents: 113
diff changeset
   336
		elif frag.super:
112
1d4892961fdb Added rise attribute
rgbecker
parents: 102
diff changeset
   337
			frag.rise = frag.fontSize*superFraction
115
49b2fae9ea60 Fixes to superscript stuff
rgbecker
parents: 113
diff changeset
   338
			frag.fontSize = max(frag.fontSize-sizeDelta,3)
112
1d4892961fdb Added rise attribute
rgbecker
parents: 102
diff changeset
   339
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   340
		if frag.greek: frag.fontName = 'symbol'
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   341
		# bold, italic, and underline
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   342
		frag.fontName = tt2ps(frag.fontName,frag.bold,frag.italic)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   343
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   344
		self.fragList.append(frag)
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   345
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   346
	#----------------------------------------------------------------
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   347
	def parse(self, text, style):
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   348
		"""Given a formatted string will return a list of
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   349
		ParaFrag objects with their calculated widths.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   350
		If errors occur None will be returned and the
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   351
		self.errors holds a list of the error messages.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   352
		"""
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   353
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   354
		# the xmlparser requires that all text be surrounded by xml
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   355
		# tags, therefore we must throw some unused flags around the
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   356
		# given string
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   357
		self._reset(style)	# reinitialise the parser
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   358
		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
   359
			text = "<para>"+text+"</para>"
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   360
		self.feed(text)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   361
		self.close()	# force parsing to complete
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   362
		style = self._style
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   363
		del self._style
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   364
		if len(self.errors)==0:
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   365
			fragList = self.fragList
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   366
			self.fragList = []
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   367
			return style, fragList
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   368
		else:
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   369
			return style, None
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   370
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   371
if __name__=='__main__':
129
623baa767856 Splitting layout.py
rgbecker
parents: 128
diff changeset
   372
	from reportlab.platypus.paragraph import cleanBlockQuotedText
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   373
	_parser=ParaParser()
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   374
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   375
	style=ParaFrag()
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   376
	style.fontName='Times-Roman'
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   377
	style.fontSize = 12
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   378
	style.textColor = black
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   379
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   380
	text='''
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   381
	<b><i><greek>a</greek>D</i></b>&beta;
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   382
	<font name="helvetica" size="15" color=green>
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   383
	Tell me, O muse, of that ingenious hero who travelled far and wide
113
2c20ecb9015e Fixed start_font
rgbecker
parents: 112
diff changeset
   384
	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
   385
	and many were the nations with whose manners and customs he was acquainted;
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   386
	moreover he suffered much by sea while trying to save his own life
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   387
	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
   388
	his men, for they perished through their own sheer folly in eating
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   389
	the cattle of the Sun-god Hyperion; so the god prevented them from
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   390
	ever reaching home. Tell me, too, about all these things, O daughter
115
49b2fae9ea60 Fixes to superscript stuff
rgbecker
parents: 113
diff changeset
   391
	of Jove, from whatsoever source you<super>1</super> may know them.
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   392
	'''
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   393
	text = cleanBlockQuotedText(text)
123
daa339ac3104 Fixes to attribute handling and test script
rgbecker
parents: 119
diff changeset
   394
	l,rv = _parser.parse(text,style)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   395
	if rv is None:
123
daa339ac3104 Fixes to attribute handling and test script
rgbecker
parents: 119
diff changeset
   396
		for l in _parser.errors:
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   397
			print l
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   398
	else:
128
a0676f013314 Splitting layout.py
rgbecker
parents: 123
diff changeset
   399
		print 'ParaStyle', l.fontName,l.fontSize,l.textColor
a0676f013314 Splitting layout.py
rgbecker
parents: 123
diff changeset
   400
		for l in rv:
a0676f013314 Splitting layout.py
rgbecker
parents: 123
diff changeset
   401
			print l.fontName,l.fontSize,l.textColor,l.bold, l.rise, l.text[:25]