reportlab/platypus/paraparser.py
author andy
Sun, 01 Oct 2006 22:53:20 +0000
changeset 2694 dd0ea6474ea0
parent 2693 3c61a57aecd1
child 2742 8edd54153201
permissions -rw-r--r--
fixes to crashing PTO trailer when empty, and numbering
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2321
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 433
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2321
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/paraparser.py
2321
3454f5b41760 Unicode and UTF8 support changes
andy
parents: 2200
diff changeset
     4
__version__=''' $Id$ '''
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     5
import string
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
     6
import re
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
     7
from types import TupleType, UnicodeType, StringType
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     8
import sys
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
     9
import os
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    10
import copy
2693
3c61a57aecd1 missing import statement
andy
parents: 2670
diff changeset
    11
import unicodedata
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 267
diff changeset
    12
import reportlab.lib.sequencer
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 514
diff changeset
    13
from reportlab.lib.abag import ABag
1160
2cb58079d830 Removed Aaron's rather thoughtless hacks
rgbecker
parents: 1156
diff changeset
    14
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
    15
from reportlab.lib import xmllib
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    16
248
c103b7a55e79 Color fixes; thanks to J Alet
rgbecker
parents: 238
diff changeset
    17
from reportlab.lib.colors import toColor, white, black, red, Color
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    18
from reportlab.lib.fonts import tt2ps, ps2tt
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    19
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    20
from reportlab.lib.units import inch,mm,cm,pica
2410
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2376
diff changeset
    21
_re_para = re.compile(r'^\s*<\s*para(?:\s+|>|/>)')
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
    22
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    23
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
    24
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
    25
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
    26
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    27
def _num(s, unit=1):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    28
    """Convert a string like '10cm' to an int or float (in points).
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    29
       The default unit is point, but optionally you can use other
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    30
       default units like mm.
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    31
    """
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    32
    if s[-2:]=='cm':
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    33
        unit=cm
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    34
        s = s[:-2]
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    35
    if s[-2:]=='in':
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    36
        unit=inch
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    37
        s = s[:-2]
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    38
    if s[-2:]=='pt':
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    39
        unit=1
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    40
        s = s[:-2]
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    41
    if s[-1:]=='i':
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    42
        unit=inch
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    43
        s = s[:-1]
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    44
    if s[-2:]=='mm':
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    45
        unit=mm
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    46
        s = s[:-2]
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    47
    if s[-4:]=='pica':
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    48
        unit=pica
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    49
        s = s[:-4]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    50
    if s[0] in ['+','-']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    51
        try:
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    52
            return ('relative',int(s)*unit)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    53
        except ValueError:
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    54
            return ('relative',float(s)*unit)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    55
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    56
        try:
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    57
            return int(s)*unit
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    58
        except ValueError:
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    59
            return float(s)*unit
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    60
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    61
def _align(s):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    62
    s = string.lower(s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    63
    if s=='left': return TA_LEFT
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    64
    elif s=='right': return TA_RIGHT
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    65
    elif s=='justify': return TA_JUSTIFY
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    66
    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
    67
    else: raise ValueError
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    68
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    69
_paraAttrMap = {'font': ('fontName', None),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    70
                'face': ('fontName', None),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    71
                'fontsize': ('fontSize', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    72
                'size': ('fontSize', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    73
                'leading': ('leading', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    74
                'lindent': ('leftIndent', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    75
                'rindent': ('rightIndent', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    76
                'findent': ('firstLineIndent', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    77
                'align': ('alignment', _align),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    78
                'spaceb': ('spaceBefore', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    79
                'spacea': ('spaceAfter', _num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    80
                'bfont': ('bulletFontName', None),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    81
                'bfontsize': ('bulletFontSize',_num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    82
                'bindent': ('bulletIndent',_num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    83
                'bcolor': ('bulletColor',toColor),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    84
                'color':('textColor',toColor),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    85
                'backcolor':('backColor',toColor),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    86
                'bgcolor':('backColor',toColor),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    87
                'bg':('backColor',toColor),
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    88
                'fg': ('textColor',toColor),
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    89
                }
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
    90
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
    91
_bulletAttrMap = {
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    92
                'font': ('bulletFontName', None),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    93
                'face': ('bulletFontName', None),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    94
                'size': ('bulletFontSize',_num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    95
                'fontsize': ('bulletFontSize',_num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    96
                'indent': ('bulletIndent',_num),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
    97
                'color': ('bulletColor',toColor),
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    98
                'fg': ('bulletColor',toColor),
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
    99
                }
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   100
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   101
#things which are valid font attributes
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   102
_fontAttrMap = {'size': ('fontSize', _num),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   103
                'face': ('fontName', None),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   104
                'name': ('fontName', None),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   105
                'fg':   ('textColor', toColor),
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   106
                'color':('textColor', toColor),
2446
6b9268ab33c3 allow solid para background
andy
parents: 2410
diff changeset
   107
                'backcolor':('backColor',toColor),
6b9268ab33c3 allow solid para background
andy
parents: 2410
diff changeset
   108
                'bgcolor':('backColor',toColor),
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   109
                }
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   110
#things which are valid font attributes
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   111
_linkAttrMap = {'size': ('fontSize', _num),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   112
                'face': ('fontName', None),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   113
                'name': ('fontName', None),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   114
                'fg':   ('textColor', toColor),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   115
                'color':('textColor', toColor),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   116
                'backcolor':('backColor',toColor),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   117
                'bgcolor':('backColor',toColor),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   118
                'dest': ('link', None),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   119
                'destination': ('link', None),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   120
                'target': ('link', None),
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2585
diff changeset
   121
                'href': ('link', None),
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   122
                }
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   123
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   124
def _addAttributeNames(m):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   125
    K = m.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   126
    for k in K:
1944
a50f8e3f93f8 laissez faire case
rgbecker
parents: 1940
diff changeset
   127
        n = m[k][0]
a50f8e3f93f8 laissez faire case
rgbecker
parents: 1940
diff changeset
   128
        if not m.has_key(n): m[n] = m[k]
a50f8e3f93f8 laissez faire case
rgbecker
parents: 1940
diff changeset
   129
        n = string.lower(n)
a50f8e3f93f8 laissez faire case
rgbecker
parents: 1940
diff changeset
   130
        if not m.has_key(n): m[n] = m[k]
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   131
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   132
_addAttributeNames(_paraAttrMap)
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   133
_addAttributeNames(_fontAttrMap)
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   134
_addAttributeNames(_bulletAttrMap)
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   135
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   136
def _applyAttributes(obj, attr):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   137
    for k, v in attr.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   138
        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
   139
            #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
   140
            #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
   141
            if hasattr(obj, k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   142
                v = v[1]+getattr(obj,k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   143
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   144
                v = v[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   145
        setattr(obj,k,v)
102
1818e7fa3738 Added clone method to ParaFrag
rgbecker
parents: 96
diff changeset
   146
1931
784fce255e2d Added in more special entities as suggested by Christoph Zwerschke
rgbecker
parents: 1736
diff changeset
   147
#Named character entities intended to be supported from the special font
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2053
diff changeset
   148
#with additions suggested by Christoph Zwerschke who also suggested the
1931
784fce255e2d Added in more special entities as suggested by Christoph Zwerschke
rgbecker
parents: 1736
diff changeset
   149
#numeric entity names that follow.
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   150
greeks = {
2585
ee08fea4505b reportlab: added pound sign
rgbecker
parents: 2584
diff changeset
   151
    'pound': '\xc2\xa3',
ee08fea4505b reportlab: added pound sign
rgbecker
parents: 2584
diff changeset
   152
    'nbsp': '\xc2\xa0',
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   153
    'alefsym': '\xe2\x84\xb5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   154
    'Alpha': '\xce\x91',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   155
    'alpha': '\xce\xb1',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   156
    'and': '\xe2\x88\xa7',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   157
    'ang': '\xe2\x88\xa0',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   158
    'asymp': '\xe2\x89\x88',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   159
    'Beta': '\xce\x92',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   160
    'beta': '\xce\xb2',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   161
    'bull': '\xe2\x80\xa2',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   162
    'cap': '\xe2\x88\xa9',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   163
    'Chi': '\xce\xa7',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   164
    'chi': '\xcf\x87',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   165
    'clubs': '\xe2\x99\xa3',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   166
    'cong': '\xe2\x89\x85',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   167
    'cup': '\xe2\x88\xaa',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   168
    'darr': '\xe2\x86\x93',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   169
    'dArr': '\xe2\x87\x93',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   170
    'delta': '\xce\xb4',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   171
    'Delta': '\xe2\x88\x86',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   172
    'diams': '\xe2\x99\xa6',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   173
    'empty': '\xe2\x88\x85',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   174
    'Epsilon': '\xce\x95',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   175
    'epsilon': '\xce\xb5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   176
    'epsiv': '\xce\xb5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   177
    'equiv': '\xe2\x89\xa1',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   178
    'Eta': '\xce\x97',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   179
    'eta': '\xce\xb7',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   180
    'euro': '\xe2\x82\xac',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   181
    'exist': '\xe2\x88\x83',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   182
    'forall': '\xe2\x88\x80',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   183
    'frasl': '\xe2\x81\x84',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   184
    'Gamma': '\xce\x93',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   185
    'gamma': '\xce\xb3',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   186
    'ge': '\xe2\x89\xa5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   187
    'harr': '\xe2\x86\x94',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   188
    'hArr': '\xe2\x87\x94',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   189
    'hearts': '\xe2\x99\xa5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   190
    'hellip': '\xe2\x80\xa6',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   191
    'image': '\xe2\x84\x91',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   192
    'infin': '\xe2\x88\x9e',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   193
    'int': '\xe2\x88\xab',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   194
    'Iota': '\xce\x99',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   195
    'iota': '\xce\xb9',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   196
    'isin': '\xe2\x88\x88',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   197
    'Kappa': '\xce\x9a',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   198
    'kappa': '\xce\xba',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   199
    'Lambda': '\xce\x9b',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   200
    'lambda': '\xce\xbb',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   201
    'lang': '\xe2\x8c\xa9',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   202
    'larr': '\xe2\x86\x90',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   203
    'lArr': '\xe2\x87\x90',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   204
    'lceil': '\xef\xa3\xae',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   205
    'le': '\xe2\x89\xa4',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   206
    'lfloor': '\xef\xa3\xb0',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   207
    'lowast': '\xe2\x88\x97',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   208
    'loz': '\xe2\x97\x8a',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   209
    'minus': '\xe2\x88\x92',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   210
    'mu': '\xc2\xb5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   211
    'Mu': '\xce\x9c',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   212
    'nabla': '\xe2\x88\x87',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   213
    'ne': '\xe2\x89\xa0',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   214
    'ni': '\xe2\x88\x8b',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   215
    'notin': '\xe2\x88\x89',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   216
    'nsub': '\xe2\x8a\x84',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   217
    'Nu': '\xce\x9d',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   218
    'nu': '\xce\xbd',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   219
    'oline': '\xef\xa3\xa5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   220
    'omega': '\xcf\x89',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   221
    'Omega': '\xe2\x84\xa6',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   222
    'Omicron': '\xce\x9f',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   223
    'omicron': '\xce\xbf',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   224
    'oplus': '\xe2\x8a\x95',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   225
    'or': '\xe2\x88\xa8',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   226
    'otimes': '\xe2\x8a\x97',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   227
    'part': '\xe2\x88\x82',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   228
    'perp': '\xe2\x8a\xa5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   229
    'Phi': '\xce\xa6',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   230
    'phi': '\xcf\x95',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   231
    'phis': '\xcf\x86',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   232
    'Pi': '\xce\xa0',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   233
    'pi': '\xcf\x80',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   234
    'piv': '\xcf\x96',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   235
    'prime': '\xe2\x80\xb2',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   236
    'prod': '\xe2\x88\x8f',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   237
    'prop': '\xe2\x88\x9d',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   238
    'Psi': '\xce\xa8',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   239
    'psi': '\xcf\x88',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   240
    'radic': '\xe2\x88\x9a',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   241
    'rang': '\xe2\x8c\xaa',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   242
    'rarr': '\xe2\x86\x92',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   243
    'rArr': '\xe2\x87\x92',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   244
    'rceil': '\xef\xa3\xb9',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   245
    'real': '\xe2\x84\x9c',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   246
    'rfloor': '\xef\xa3\xbb',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   247
    'Rho': '\xce\xa1',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   248
    'rho': '\xcf\x81',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   249
    'sdot': '\xe2\x8b\x85',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   250
    'Sigma': '\xce\xa3',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   251
    'sigma': '\xcf\x83',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   252
    'sigmaf': '\xcf\x82',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   253
    'sigmav': '\xcf\x82',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   254
    'sim': '\xe2\x88\xbc',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   255
    'spades': '\xe2\x99\xa0',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   256
    'sub': '\xe2\x8a\x82',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   257
    'sube': '\xe2\x8a\x86',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   258
    'sum': '\xe2\x88\x91',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   259
    'sup': '\xe2\x8a\x83',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   260
    'supe': '\xe2\x8a\x87',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   261
    'Tau': '\xce\xa4',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   262
    'tau': '\xcf\x84',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   263
    'there4': '\xe2\x88\xb4',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   264
    'Theta': '\xce\x98',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   265
    'theta': '\xce\xb8',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   266
    'thetasym': '\xcf\x91',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   267
    'thetav': '\xcf\x91',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   268
    'trade': '\xef\xa3\xaa',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   269
    'uarr': '\xe2\x86\x91',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   270
    'uArr': '\xe2\x87\x91',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   271
    'upsih': '\xcf\x92',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   272
    'Upsilon': '\xce\xa5',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   273
    'upsilon': '\xcf\x85',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   274
    'weierp': '\xe2\x84\x98',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   275
    'Xi': '\xce\x9e',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   276
    'xi': '\xce\xbe',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   277
    'Zeta': '\xce\x96',
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   278
    'zeta': '\xce\xb6',
1931
784fce255e2d Added in more special entities as suggested by Christoph Zwerschke
rgbecker
parents: 1736
diff changeset
   279
    }
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   280
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   281
#------------------------------------------------------------------------
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 514
diff changeset
   282
class ParaFrag(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   283
    """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
   284
    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
   285
    fontname, fontSize, rise, textColor, cbDefn
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   286
    """
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   287
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   288
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   289
_greek2Utf8=None
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   290
def _greekConvert(data):
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   291
    global _greek2Utf8
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   292
    if not _greek2Utf8:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   293
        from reportlab.pdfbase.rl_codecs import RL_Codecs
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   294
        import codecs
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   295
        dm = decoding_map = codecs.make_identity_dict(xrange(32,256))
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   296
        for k in xrange(0,32):
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   297
            dm[k] = None
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   298
        dm.update(RL_Codecs._RL_Codecs__rl_codecs_data['symbol'][0])
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   299
        _greek2Utf8 = {}
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   300
        for k,v in dm.iteritems():
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   301
            if not v:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   302
                u = '\0'
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   303
            else:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   304
                u = unichr(v).encode('utf8')
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   305
            _greek2Utf8[chr(k)] = u
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   306
    return ''.join(map(_greek2Utf8.__getitem__,data))
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   307
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   308
#------------------------------------------------------------------
267
52a348f6c4c3 noted replication of XML markup comment between paraparser.py and paragraph.py
aaron_watters
parents: 266
diff changeset
   309
# !!! NOTE !!! THIS TEXT IS NOW REPLICATED IN PARAGRAPH.PY !!!
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   310
# The ParaFormatter will be able to format the following
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   311
# tags:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   312
#       < /b > - bold
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   313
#       < /i > - italics
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   314
#       < u > < /u > - underline
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   315
#       < strike > < /strike > - strike through
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   316
#       < super > < /super > - superscript
1736
dafc17db33d2 Attempt to use sup as well as super
rgbecker
parents: 1683
diff changeset
   317
#       < sup > < /sup > - superscript
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   318
#       < sub > < /sub > - subscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   319
#       <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
   320
#       < 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
   321
#       <onDraw name=callable label="a label">
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   322
#       <link>link text</link>
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   323
#           attributes of links 
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   324
#               size/fontSize=num
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   325
#               name/face/fontName=name
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   326
#               fg/textColor/color=color
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   327
#               backcolor/backColor/bgcolor=color
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   328
#               dest/destination/target/href/link=target
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   329
#       <unichar name="unicode character name"/>
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   330
#       <unichar value="unicode code point"/>
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   331
#       <greek> - </greek>
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   332
#
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   333
#       The whole may be surrounded by <para> </para> tags
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   334
#
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   335
# It will also be able to handle any MathML specified Greek characters.
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   336
#------------------------------------------------------------------
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   337
class ParaParser(xmllib.XMLParser):
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   338
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   339
    #----------------------------------------------------------
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   340
    # 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
   341
    #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   342
    # start_<tag>(attributes)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   343
    # end_<tag>()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   344
    #
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   345
    # 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
   346
    # 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
   347
    # 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
   348
    # 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
   349
    # 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
   350
    # 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
   351
    #----------------------------------------------------------
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   352
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   353
    def __getattr__( self, attrName ):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   354
        """This way we can handle <TAG> the same way as <tag> (ignoring case)."""
2369
f3cc620c14ed paraparser.py: minor speedup
rgbecker
parents: 2368
diff changeset
   355
        if attrName!=attrName.lower() and attrName!="caseSensitive" and not self.caseSensitive and \
f3cc620c14ed paraparser.py: minor speedup
rgbecker
parents: 2368
diff changeset
   356
            (attrName.startswith("start_") or attrName.startswith("end_")):
f3cc620c14ed paraparser.py: minor speedup
rgbecker
parents: 2368
diff changeset
   357
                return getattr(self,attrName.lower())
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   358
        raise AttributeError, attrName
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   359
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   360
    #### bold
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   361
    def start_b( self, attributes ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   362
        self._push(bold=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   363
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   364
    def end_b( self ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   365
        self._pop(bold=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   366
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   367
    def start_strong( self, attributes ):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   368
        self._push(bold=1)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   369
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   370
    def end_strong( self ):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   371
        self._pop(bold=1)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   372
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   373
    #### italics
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   374
    def start_i( self, attributes ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   375
        self._push(italic=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   376
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   377
    def end_i( self ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   378
        self._pop(italic=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   379
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   380
    def start_em( self, attributes ):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   381
        self._push(italic=1)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   382
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   383
    def end_em( self ):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   384
        self._pop(italic=1)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   385
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   386
    #### underline
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   387
    def start_u( self, attributes ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   388
        self._push(underline=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   389
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   390
    def end_u( self ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   391
        self._pop(underline=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   392
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   393
    #### strike
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   394
    def start_strike( self, attributes ):
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   395
        self._push(strike=1)
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   396
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   397
    def end_strike( self ):
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   398
        self._pop(strike=1)
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   399
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   400
    #### link
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   401
    def start_link(self, attributes):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   402
        self._push(**self.getAttributes(attributes,_linkAttrMap))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   403
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   404
    def end_link(self):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   405
        frag = self._stack[-1]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   406
        del self._stack[-1]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   407
        assert frag.link!=None
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   408
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   409
    #### super script
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   410
    def start_super( self, attributes ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   411
        self._push(super=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   412
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   413
    def end_super( self ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   414
        self._pop(super=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   415
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   416
    start_sup = start_super
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   417
    end_sup = end_super
1736
dafc17db33d2 Attempt to use sup as well as super
rgbecker
parents: 1683
diff changeset
   418
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   419
    #### sub script
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   420
    def start_sub( self, attributes ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   421
        self._push(sub=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   422
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   423
    def end_sub( self ):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   424
        self._pop(sub=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   425
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   426
    #### greek script
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   427
    #### add symbol encoding
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   428
    def handle_charref(self, name):
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   429
        try:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   430
            if name[0]=='x':
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   431
                n = int(name[1:],16)
1931
784fce255e2d Added in more special entities as suggested by Christoph Zwerschke
rgbecker
parents: 1736
diff changeset
   432
            else:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   433
                n = int(name)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   434
        except ValueError:
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   435
            self.unknown_charref(name)
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   436
            return
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   437
        self.handle_data(unichr(n).encode('utf8'))
134
60e8e0aee073 Fixed syntax_error handling
rgbecker
parents: 133
diff changeset
   438
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   439
    def handle_entityref(self,name):
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   440
        if greeks.has_key(name):
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   441
            self.handle_data(greeks[name])
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   442
        else:
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   443
            xmllib.XMLParser.handle_entityref(self,name)
134
60e8e0aee073 Fixed syntax_error handling
rgbecker
parents: 133
diff changeset
   444
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   445
    def syntax_error(self,lineno,message):
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   446
        self._syntax_error(message)
134
60e8e0aee073 Fixed syntax_error handling
rgbecker
parents: 133
diff changeset
   447
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   448
    def _syntax_error(self,message):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   449
        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
   450
        self.errors.append(message)
134
60e8e0aee073 Fixed syntax_error handling
rgbecker
parents: 133
diff changeset
   451
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   452
    def start_greek(self, attr):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   453
        self._push(greek=1)
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
    def end_greek(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   456
        self._pop(greek=1)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   457
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   458
    def start_unichar(self, attr):
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   459
        if attr.has_key('name'):
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   460
            if attr.has_key('code'):
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   461
                self._syntax_error('<unichar/> invalid with both name and code attributes')
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   462
            try:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   463
                v = unicodedata.lookup(attr['name']).encode('utf8')
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   464
            except KeyError:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   465
                self._syntax_error('<unichar/> invalid name attribute\n"%s"' % name)
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   466
                v = '\0'
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   467
        elif attr.has_key('code'):
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   468
            try:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   469
                v = unichr(int(eval(attr['code']))).encode('utf8')
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   470
            except:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   471
                self._syntax_error('<unichar/> invalid code attribute %s' % attr['code'])
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   472
                v = '\0'
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   473
        else:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   474
            v = None
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   475
            if attr:
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   476
                self._syntax_error('<unichar/> invalid attribute %s' % attr.keys()[0])
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   477
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   478
        if v is not None:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   479
            self.handle_data(v)
2585
ee08fea4505b reportlab: added pound sign
rgbecker
parents: 2584
diff changeset
   480
        self._push(_selfClosingTag='unichar')
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   481
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   482
    def end_unichar(self):
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   483
        self._pop()
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   484
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   485
    def start_font(self,attr):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   486
        self._push(**self.getAttributes(attr,_fontAttrMap))
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   487
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   488
    def end_font(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   489
        self._pop()
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   490
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   491
    def start_br(self, attr):
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   492
        #just do the trick to make sure there is no content
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   493
        self._push(_selfClosingTag='br',lineBreak=True,text='')
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   494
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   495
    def end_br(self):
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   496
        frag = self._stack[-1]
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   497
        assert frag._selfClosingTag=='br' and frag.lineBreak,'Parser failure in <br/>'
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   498
        del frag._selfClosingTag
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   499
        self.handle_data('')
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   500
        self._pop()
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   501
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   502
    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
   503
        style = self._style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   504
        if attr!={}:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   505
            style = copy.deepcopy(style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   506
            _applyAttributes(style,self.getAttributes(attr,attrMap))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   507
            self._style = style
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   508
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   509
        # initialize semantic values
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   510
        frag = ParaFrag()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   511
        frag.sub = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   512
        frag.super = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   513
        frag.rise = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   514
        frag.underline = 0
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   515
        frag.strike = 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   516
        frag.greek = 0
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   517
        frag.link = None
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   518
        if bullet:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   519
            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
   520
            frag.fontSize = style.bulletFontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   521
            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
   522
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   523
            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
   524
            frag.fontSize = style.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   525
            frag.textColor = style.textColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   526
        return frag
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   527
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   528
    def start_para(self,attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   529
        self._stack = [self._initial_frag(attr,_paraAttrMap)]
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   530
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   531
    def end_para(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   532
        self._pop()
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   533
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   534
    def start_bullet(self,attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   535
        if hasattr(self,'bFragList'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   536
            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
   537
        self.bFragList = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   538
        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
   539
        frag.isBullet = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   540
        self._stack.append(frag)
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   541
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   542
    def end_bullet(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   543
        self._pop()
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   544
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   545
    #---------------------------------------------------------------
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   546
    def start_seqdefault(self, attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   547
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   548
            default = attr['id']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   549
        except KeyError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   550
            default = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   551
        self._seq.setDefaultCounter(default)
266
081154da1a78 Added Sequencer and associated XML tags
andy_robinson
parents: 253
diff changeset
   552
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   553
    def end_seqdefault(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   554
        pass
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   555
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   556
    def start_seqreset(self, attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   557
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   558
            id = attr['id']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   559
        except KeyError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   560
            id = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   561
        try:
2368
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   562
            base = int(attr['base'])
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   563
        except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   564
            base=0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   565
        self._seq.reset(id, base)
266
081154da1a78 Added Sequencer and associated XML tags
andy_robinson
parents: 253
diff changeset
   566
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   567
    def end_seqreset(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   568
        pass
744
2abd99baf95b Accepts seqdefault/seqDefault and seqreset/seqReset
andy_robinson
parents: 677
diff changeset
   569
2368
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   570
    def start_seqchain(self, attr):
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   571
        try:
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   572
            order = attr['order']
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   573
        except KeyError:
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   574
            order = ''
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   575
        order = order.split()
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   576
        seq = self._seq
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   577
        for p,c in zip(order[:-1],order[1:]):
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   578
            seq.chain(p, c)
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   579
    end_seqchain = end_seqreset
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   580
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   581
    def start_seqformat(self, attr):
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   582
        try:
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   583
            id = attr['id']
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   584
        except KeyError:
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   585
            id = None
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   586
        try:
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   587
            value = attr['value']
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   588
        except KeyError:
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   589
            value = '1'
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   590
        self._seq.setFormat(id,value)
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   591
    end_seqformat = end_seqreset
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   592
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   593
    # 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
   594
    # the above ones should be deprecated over time. 2001-03-22
2368
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   595
    start_seqDefault = start_seqdefault
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   596
    end_seqDefault = end_seqdefault
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   597
    start_seqReset = start_seqreset
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   598
    end_seqReset = end_seqreset
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   599
    start_seqChain = start_seqchain
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   600
    end_seqChain = end_seqchain
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   601
    start_seqFormat = start_seqformat
791a362e9cae added seqchain/format tags
rgbecker
parents: 2341
diff changeset
   602
    end_seqFormat = end_seqformat
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   603
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   604
    def start_seq(self, attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   605
        #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
   606
        #otherwise take default sequence
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   607
        if attr.has_key('template'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   608
            templ = attr['template']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   609
            self.handle_data(templ % self._seq)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   610
            return
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   611
        elif attr.has_key('id'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   612
            id = attr['id']
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   613
        else:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   614
            id = None
2694
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   615
        increment = attr.get('inc', None)
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   616
        if not increment:
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   617
            output = self._seq.nextf(id)
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   618
        else:
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   619
            #accepts "no" for do not increment, or an integer.
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   620
            #thus, 0 and 1 increment by the right amounts.
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   621
            if increment.lower() == 'no':
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   622
                output = self._seq.thisf(id)
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   623
            else:
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   624
                incr = int(increment)
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   625
                output = self._seq.thisf(id)
dd0ea6474ea0 fixes to crashing PTO trailer when empty, and numbering
andy
parents: 2693
diff changeset
   626
                self._seq.reset(id, self._seq._this() + incr)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   627
        self.handle_data(output)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   628
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   629
    def end_seq(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   630
        pass
266
081154da1a78 Added Sequencer and associated XML tags
andy_robinson
parents: 253
diff changeset
   631
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   632
    def start_onDraw(self,attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   633
        defn = ABag()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   634
        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
   635
        else: self._syntax_error('<onDraw> needs at least a name attribute')
506
68bd275f16e2 Added onDraw tag to paragraphs
rgbecker
parents: 494
diff changeset
   636
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   637
        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
   638
        defn.kind='onDraw'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   639
        self._push(cbDefn=defn)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   640
        self.handle_data('')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   641
        self._pop()
506
68bd275f16e2 Added onDraw tag to paragraphs
rgbecker
parents: 494
diff changeset
   642
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   643
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   644
    #---------------------------------------------------------------
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   645
    def _push(self,**attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   646
        frag = copy.copy(self._stack[-1])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   647
        _applyAttributes(frag,attr)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   648
        self._stack.append(frag)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   649
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   650
    def _pop(self,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   651
        frag = self._stack[-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   652
        del self._stack[-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   653
        for k, v in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   654
            assert getattr(frag,k)==v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   655
        return frag
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   656
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   657
    def getAttributes(self,attr,attrMap):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   658
        A = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   659
        for k, v in attr.items():
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   660
            if not self.caseSensitive:
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   661
                k = string.lower(k)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   662
            if k in attrMap.keys():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   663
                j = attrMap[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   664
                func = j[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   665
                try:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   666
                    A[j[0]] = (func is None) and v or func(v)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   667
                except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   668
                    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
   669
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   670
                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
   671
        return A
119
b4dc589c8364 <para> tag added in layout.py paraparser.py
rgbecker
parents: 115
diff changeset
   672
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   673
    #----------------------------------------------------------------
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   674
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   675
    def __init__(self,verbose=0):
1944
a50f8e3f93f8 laissez faire case
rgbecker
parents: 1940
diff changeset
   676
        self.caseSensitive = 0
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   677
        xmllib.XMLParser.__init__(self,verbose=verbose)
266
081154da1a78 Added Sequencer and associated XML tags
andy_robinson
parents: 253
diff changeset
   678
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   679
    def _iReset(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   680
        self.fragList = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   681
        if hasattr(self, 'bFragList'): delattr(self,'bFragList')
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   682
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   683
    def _reset(self, style):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   684
        '''reset the parser'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   685
        xmllib.XMLParser.reset(self)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   686
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   687
        # 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
   688
        self.errors = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   689
        self._style = style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   690
        self._iReset()
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   691
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   692
    #----------------------------------------------------------------
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   693
    def handle_data(self,data):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   694
        "Creates an intermediate representation of string segments."
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   695
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   696
        frag = copy.copy(self._stack[-1])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   697
        if hasattr(frag,'cbDefn'):
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   698
            if data!='': self._syntax_error('Only <onDraw> tag allowed')
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   699
        elif hasattr(frag,'_selfClosingTag'):
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   700
            if data!='': self._syntax_error('No content allowed in %s tag' % frag._selfClosingTag)
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   701
            return
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   702
        else:
1736
dafc17db33d2 Attempt to use sup as well as super
rgbecker
parents: 1683
diff changeset
   703
            # if sub and super are both on they will cancel each other out
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   704
            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
   705
                frag.sub = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   706
                frag.super = 0
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   707
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   708
            if frag.sub:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   709
                frag.rise = -frag.fontSize*subFraction
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   710
                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
   711
            elif frag.super:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   712
                frag.rise = frag.fontSize*superFraction
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   713
                frag.fontSize = max(frag.fontSize-sizeDelta,3)
112
1d4892961fdb Added rise attribute
rgbecker
parents: 102
diff changeset
   714
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   715
            if frag.greek:
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   716
                frag.fontName = 'symbol'
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   717
                data = _greekConvert(data)
514
3784fe357a72 Slight optimisation in handle_data for cbdefn frags
rgbecker
parents: 508
diff changeset
   718
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   719
        # bold, italic, and underline
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   720
        x = frag.fontName = tt2ps(frag.fontName,frag.bold,frag.italic)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   721
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   722
        #save our data
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   723
        frag.text = data
514
3784fe357a72 Slight optimisation in handle_data for cbdefn frags
rgbecker
parents: 508
diff changeset
   724
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   725
        if hasattr(frag,'isBullet'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   726
            delattr(frag,'isBullet')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   727
            self.bFragList.append(frag)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   728
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   729
            self.fragList.append(frag)
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   730
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   731
    def handle_cdata(self,data):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   732
        self.handle_data(data)
211
52541f1643b6 CDATA handler added
rgbecker
parents: 209
diff changeset
   733
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   734
    def _setup_for_parse(self,style):
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   735
        self._seq = reportlab.lib.sequencer.getSequencer()
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   736
        self._reset(style)  # reinitialise the parser
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   737
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   738
    def parse(self, text, style):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   739
        """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
   740
        ParaFrag objects with their calculated widths.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   741
        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
   742
        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
   743
        """
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   744
        # AR 20040612 - when we feed Unicode strings in, sgmlop
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   745
        # tries to coerce to ASCII.  Must intercept, coerce to
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   746
        # any 8-bit encoding which defines most of 256 points,
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   747
        # and revert at end.  Yuk.  Preliminary step prior to
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   748
        # removal of parser altogether.
2646
d177c247184a platypus: eliminate StringType usages and cp1252 usage
rgbecker
parents: 2644
diff changeset
   749
        enc = self._enc = 'utf8' #our legacy default
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   750
        self._UNI = type(text) is UnicodeType
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   751
        if self._UNI:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   752
            text = text.encode(enc)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   753
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   754
        self._setup_for_parse(style)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   755
        # 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
   756
        # 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
   757
        # given string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   758
        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
   759
            text = "<para>"+text+"</para>"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   760
        self.feed(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   761
        self.close()    # force parsing to complete
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   762
        return self._complete_parse()
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   763
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   764
    def _complete_parse(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   765
        del self._seq
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   766
        style = self._style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   767
        del self._style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   768
        if len(self.errors)==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   769
            fragList = self.fragList
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   770
            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
   771
            self._iReset()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   772
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   773
            fragList = bFragList = None
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   774
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   775
        if self._UNI:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   776
            #reconvert to unicode
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   777
            if fragList:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   778
                for frag in fragList:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   779
                    frag.text = unicode(frag.text, self._enc)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   780
            if bFragList:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   781
                for frag in bFragList:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2446
diff changeset
   782
                    frag.text = unicode(frag.text, self._enc)
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   783
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   784
        return style, fragList, bFragList
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   785
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   786
    def _tt_parse(self,tt):
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   787
        tag = tt[0]
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   788
        try:
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   789
            start = getattr(self,'start_'+tag)
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   790
            end = getattr(self,'end_'+tag)
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   791
        except AttributeError:
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   792
            raise ValueError('Invalid tag "%s"' % tag)
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   793
        start(tt[1] or {})
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   794
        C = tt[2]
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   795
        if C:
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   796
            M = self._tt_handlers
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   797
            for c in C:
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   798
                M[type(c) is TupleType](c)
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   799
        end()
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   800
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   801
    def tt_parse(self,tt,style):
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   802
        '''parse from tupletree form'''
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   803
        self._setup_for_parse(style)
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   804
        self._tt_handlers = self.handle_data,self._tt_parse
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   805
        self._tt_parse(tt)
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   806
        return self._complete_parse()
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2369
diff changeset
   807
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   808
if __name__=='__main__':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   809
    from reportlab.platypus import cleanBlockQuotedText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   810
    _parser=ParaParser()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   811
    def check_text(text,p=_parser):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   812
        print '##########'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   813
        text = cleanBlockQuotedText(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   814
        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
   815
        if rv is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   816
            for l in _parser.errors:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   817
                print l
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   818
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   819
            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
   820
            for l in rv:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   821
                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
   822
                if hasattr(l,'cbDefn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   823
                    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
   824
                else: print
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   825
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   826
    style=ParaFrag()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   827
    style.fontName='Times-Roman'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   828
    style.fontSize = 12
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   829
    style.textColor = black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   830
    style.bulletFontName = black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   831
    style.bulletFontName='Times-Roman'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   832
    style.bulletFontSize=12
96
2a9cca4c5cf0 Beginnings of a paragraph parser
rgbecker
parents:
diff changeset
   833
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   834
    text='''
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
   835
    <b><i><greek>a</greek>D</i></b>&beta;<unichr value="0x394"/>
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   836
    <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
   837
    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
   838
    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
   839
    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
   840
    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
   841
    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
   842
    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
   843
    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
   844
    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
   845
    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
   846
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   847
    check_text(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   848
    check_text('<para> </para>')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   849
    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
   850
    check_text('''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   851
    <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
   852
    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
   853
    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
   854
    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
   855
    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
   856
    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
   857
    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
   858
    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
   859
    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
   860
    check_text('''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   861
    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
   862
    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
   863
    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
   864
    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
   865
    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
   866
    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
   867
    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
   868
    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
   869
    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
   870
    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
   871
    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
   872
    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
   873
    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
   874
    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
   875
    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
   876
    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
   877
    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
   878
    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
   879
    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
   880
    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
   881
    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
   882
    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
   883
    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
   884
    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
   885
    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
   886
    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
   887
    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
   888
    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
   889
    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
   890
    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
   891
    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
   892
    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
   893
    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
   894
    now I have no remedy."''')
133
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   895
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   896
    check_text('''
133
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   897
But as the sun was rising from the fair sea into the firmament of
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   898
heaven to shed light on mortals and immortals, they reached Pylos
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   899
the city of Neleus. Now the people of Pylos were gathered on the sea
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   900
shore to offer sacrifice of black bulls to Neptune lord of the Earthquake.
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   901
There were nine guilds with five hundred men in each, and there were
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   902
nine bulls to each guild. As they were eating the inward meats and
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   903
burning the thigh bones [on the embers] in the name of Neptune, Telemachus
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   904
and his crew arrived, furled their sails, brought their ship to anchor,
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   905
and went ashore. ''')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   906
    check_text('''
133
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   907
So the neighbours and kinsmen of Menelaus were feasting and making
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   908
merry in his house. There was a bard also to sing to them and play
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   909
his lyre, while two tumblers went about performing in the midst of
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   910
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
   911
    check_text('''
133
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   912
"When we had passed the [Wandering] rocks, with Scylla and terrible
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   913
Charybdis, we reached the noble island of the sun-god, where were
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   914
the goodly cattle and sheep belonging to the sun Hyperion. While still
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   915
at sea in my ship I could bear the cattle lowing as they came home
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   916
to the yards, and the sheep bleating. Then I remembered what the blind
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   917
Theban prophet Teiresias had told me, and how carefully Aeaean Circe
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   918
had warned me to shun the island of the blessed sun-god. So being
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   919
much troubled I said to the men, 'My men, I know you are hard pressed,
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2594
diff changeset
   920
but listen while I <strike>tell you the prophecy that</strike> Teiresias made me, and
133
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   921
how carefully Aeaean Circe warned me to shun the island of the blessed
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   922
sun-god, for it was here, she said, that our worst danger would lie.
ba7b4eb9be6c Debugging xml changes
rgbecker
parents: 132
diff changeset
   923
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
   924
    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
   925
    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
   926
    check_text('''<![CDATA[<>&'"]]>''')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1160
diff changeset
   927
    check_text('''<bullet face=courier size=14 color=green>+</bullet>
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   928
There was a bard also to sing to them and play
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   929
his lyre, while two tumblers went about performing in the midst of
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 248
diff changeset
   930
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
   931
    check_text('''<onDraw name="myFunc" label="aaa   bbb">A paragraph''')
1736
dafc17db33d2 Attempt to use sup as well as super
rgbecker
parents: 1683
diff changeset
   932
    check_text('''<para><onDraw name="myFunc" label="aaa   bbb">B paragraph</para>''')
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   933
    # HVB, 30.05.2003: Test for new features
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   934
    _parser.caseSensitive=0
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   935
    check_text('''Here comes <FONT FACE="Helvetica" SIZE="14pt">Helvetica 14</FONT> with <STRONG>strong</STRONG> <EM>emphasis</EM>.''')
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   936
    check_text('''Here comes <font face="Helvetica" size="14pt">Helvetica 14</font> with <Strong>strong</Strong> <em>emphasis</em>.''')
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1932
diff changeset
   937
    check_text('''Here comes <font face="Courier" size="3cm">Courier 3cm</font> and normal again.''')
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   938
    check_text('''Before the break <br/>the middle line <br/> and the last line.''')