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