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