tests/test_platypus_toc.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4272 a3e48855dd48
child 4369 46b44c48cdc0
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4272
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     3
"""Tests for the Platypus TableOfContents class.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     4
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     5
Currently there is only one such test. Most such tests, like this
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     6
one, will be generating a PDF document that needs to be eye-balled
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     7
in order to find out if it is 'correct'.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     8
"""
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3898
diff changeset
     9
__version__='3.3.0'
2984
c63f149d55aa tests.utils --> reportlab.lib.testutils
rgbecker
parents: 2967
diff changeset
    10
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
c63f149d55aa tests.utils --> reportlab.lib.testutils
rgbecker
parents: 2967
diff changeset
    11
setOutDir(__name__)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    12
import sys, os
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    13
from os.path import join, basename, splitext
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    14
from math import sqrt
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
    15
import random
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    16
import unittest
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    17
from reportlab.lib.units import inch, cm
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    18
from reportlab.lib.pagesizes import A4
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    19
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    20
from reportlab.platypus.xpreformatted import XPreformatted
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    21
from reportlab.platypus.frames import Frame
3468
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
    22
from reportlab.platypus.paragraph import Paragraph
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
    23
from reportlab.platypus.flowables import Flowable, Spacer
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    24
from reportlab.platypus.doctemplate \
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    25
     import PageTemplate, BaseDocTemplate
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
    26
from reportlab.platypus import tableofcontents, PageBreak
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    27
from reportlab.lib import randomtext
3898
b7b0eea4946d test_fixes
robin
parents: 3721
diff changeset
    28
from xml.sax.saxutils import escape as xmlEscape
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    29
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    30
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    31
def myMainPageFrame(canvas, doc):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    32
    "The page frame used for all PDF documents."
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    33
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    34
    canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    35
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    36
    canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    37
    canvas.setFont('Times-Roman', 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    38
    pageNumber = canvas.getPageNumber()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    39
    canvas.drawString(10*cm, cm, str(pageNumber))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    40
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    41
    canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    42
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    43
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    44
class MyDocTemplate(BaseDocTemplate):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    45
    "The document template used for all PDF documents."
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    46
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    47
    _invalidInitArgs = ('pageTemplates',)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    48
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    49
    def __init__(self, filename, **kw):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    50
        frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    51
        self.allowSplitting = 0
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3060
diff changeset
    52
        BaseDocTemplate.__init__(self, filename, **kw)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    53
        template = PageTemplate('normal', [frame1], myMainPageFrame)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    54
        self.addPageTemplates(template)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    55
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    56
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    57
    def afterFlowable(self, flowable):
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    58
        "Registers TOC entries."
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    59
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    60
        if flowable.__class__.__name__ == 'Paragraph':
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    61
            styleName = flowable.style.name
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    62
            if styleName[:7] == 'Heading':
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    63
                key = str(hash(flowable))
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    64
                self.canv.bookmarkPage(key)
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    65
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    66
                # Register TOC entries.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    67
                level = int(styleName[7:])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    68
                text = flowable.getPlainText()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    69
                pageNum = self.page
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    70
                # Try calling this with and without a key to test both
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    71
                # Entries of every second level will have links, others won't
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    72
                if level % 2 == 1:
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    73
                    self.notify('TOCEntry', (level, text, pageNum, key))
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    74
                else:
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
    75
                    self.notify('TOCEntry', (level, text, pageNum))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    76
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    77
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    78
def makeHeaderStyle(level, fontName='Times-Roman'):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    79
    "Make a header style for different levels."
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    80
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    81
    assert level >= 0, "Level must be >= 0."
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    82
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    83
    PS = ParagraphStyle
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    84
    size = 24.0 / sqrt(1+level)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    85
    style = PS(name = 'Heading' + str(level),
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    86
               fontName = fontName,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    87
               fontSize = size,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    88
               leading = size*1.2,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    89
               spaceBefore = size/4.0,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    90
               spaceAfter = size/8.0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    91
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    92
    return style
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    93
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    94
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    95
def makeBodyStyle():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    96
    "Body text style - the default will do"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    97
    return ParagraphStyle('body')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    98
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    99
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   100
def makeTocHeaderStyle(level, delta, epsilon, fontName='Times-Roman'):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   101
    "Make a header style for different levels."
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   102
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   103
    assert level >= 0, "Level must be >= 0."
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   104
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   105
    PS = ParagraphStyle
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   106
    size = 12
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   107
    style = PS(name = 'Heading' + str(level),
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   108
               fontName = fontName,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   109
               fontSize = size,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   110
               leading = size*1.2,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   111
               spaceBefore = size/4.0,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   112
               spaceAfter = size/8.0,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   113
               firstLineIndent = -epsilon,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   114
               leftIndent = level*delta + epsilon)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   115
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   116
    return style
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   117
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   118
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   119
class TocTestCase(unittest.TestCase):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   120
    "Test TableOfContents class (eyeball-test)."
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   121
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   122
    def test0(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   123
        """Test story with TOC and a cascaded header hierarchy.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   124
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   125
        The story should contain exactly one table of contents that is
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   126
        immediatly followed by a list of of cascaded levels of header
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   127
        lines, each nested one level deeper than the previous one.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   128
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   129
        Features to be visually confirmed by a human being are:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   130
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   131
            1. TOC lines are indented in multiples of 1 cm.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   132
            2. Wrapped TOC lines continue with additional 0.5 cm indentation.
3060
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
   133
            3. Only entries of every second level has links
eeedb611fa67 Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents: 3059
diff changeset
   134
            ...
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   135
        """
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   136
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   137
        maxLevels = 12
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   138
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   139
        # Create styles to be used for document headers
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   140
        # on differnet levels.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   141
        headerLevelStyles = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   142
        for i in range(maxLevels):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   143
            headerLevelStyles.append(makeHeaderStyle(i))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   144
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   145
        # Create styles to be used for TOC entry lines
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   146
        # for headers on differnet levels.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   147
        tocLevelStyles = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   148
        d, e = tableofcontents.delta, tableofcontents.epsilon
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   149
        for i in range(maxLevels):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   150
            tocLevelStyles.append(makeTocHeaderStyle(i, d, e))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   151
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   152
        # Build story.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   153
        story = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   154
        styleSheet = getSampleStyleSheet()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   155
        bt = styleSheet['BodyText']
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   156
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   157
        description = '<font color=red>%s</font>' % self.test0.__doc__
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   158
        story.append(XPreformatted(description, bt))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   159
3059
3a6ff201e927 Added documentation for the table of contents to the userguide.
jonas
parents: 2984
diff changeset
   160
        toc = tableofcontents.TableOfContents()
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   161
        toc.levelStyles = tocLevelStyles
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   162
        story.append(toc)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   163
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   164
        for i in range(maxLevels):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   165
            story.append(Paragraph('HEADER, LEVEL %d' % i,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   166
                                   headerLevelStyles[i]))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   167
            #now put some body stuff in.
3898
b7b0eea4946d test_fixes
robin
parents: 3721
diff changeset
   168
            txt = xmlEscape(randomtext.randomText(randomtext.PYTHON, 5))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   169
            para = Paragraph(txt, makeBodyStyle())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   170
            story.append(para)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   171
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   172
        path = outputfile('test_platypus_toc.pdf')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   173
        doc = MyDocTemplate(path)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   174
        doc.multiBuild(story)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   175
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   176
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   177
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   178
    def test1(self):
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   179
        """This shows a table which would take more than one page,
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   180
        and need multiple passes to render.  But we preload it
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   181
        with the right headings to make it go faster.  We used
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   182
        a simple 100-chapter document with one level.
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   183
        """
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   184
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   185
        chapters = 30   #goes over one page
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   186
        
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   187
        headerStyle = makeHeaderStyle(0)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   188
        d, e = tableofcontents.delta, tableofcontents.epsilon
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   189
        tocLevelStyle = makeTocHeaderStyle(0, d, e)
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   190
        bodyStyle = makeBodyStyle()
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   191
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   192
        # Build most of the story; we'll re-use it to 
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   193
        # make documents with different numbers of passes.
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   194
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   195
        story = []
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   196
        styleSheet = getSampleStyleSheet()
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   197
        bt = styleSheet['BodyText']
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   198
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   199
        description = '<font color=red>%s</font>' % self.test1.__doc__
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   200
        story.append(XPreformatted(description, bt))
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   201
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   202
        for i in range(chapters):
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   203
            story.append(PageBreak())
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   204
            story.append(Paragraph('This is chapter %d' % (i+1),
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   205
                                   headerStyle))
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   206
            #now put some lengthy body stuff in.  
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   207
            for paras in range(random.randint(1,3)):
3898
b7b0eea4946d test_fixes
robin
parents: 3721
diff changeset
   208
                txt = xmlEscape(randomtext.randomText(randomtext.PYTHON, 5))
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   209
                para = Paragraph(txt, bodyStyle)
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   210
                story.append(para)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   211
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   212
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   213
        #try 1: empty TOC, 3 passes
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   214
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   215
        toc = tableofcontents.TableOfContents()
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   216
        toc.levelStyles = [tocLevelStyle]   #only need one
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   217
        story1 = [toc] + story
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   218
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   219
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   220
        path = outputfile('test_platypus_toc_preload_1.pdf')
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   221
        doc = MyDocTemplate(path)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   222
        passes = doc.multiBuild(story1)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   223
        self.assertEquals(passes, 3)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   224
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   225
        #try 2: now preload the TOC with the entries
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   226
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   227
        toc = tableofcontents.TableOfContents()
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   228
        toc.levelStyles = [tocLevelStyle]   #only need one
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   229
        tocEntries = []
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   230
        for i in range(chapters):
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   231
            #add tuple of (level, text, pageNum, key)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   232
            #with an initial guess of pageNum=0
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   233
            tocEntries.append((0, 'This is chapter %d' % (i+1), 0, None))
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   234
        toc.addEntries(tocEntries)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   235
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   236
        story2 = [toc] + story
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   237
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   238
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   239
        path = outputfile('test_platypus_toc_preload_2.pdf')
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   240
        doc = MyDocTemplate(path)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   241
        passes = doc.multiBuild(story2)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   242
        self.assertEquals(passes, 2)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   243
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   244
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   245
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   246
        #try 3: preload again but try to be really smart and work out
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   247
        #in advance what page everything starts on.  We cannot
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   248
        #use a random story for this.
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   249
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   250
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   251
        toc3 = tableofcontents.TableOfContents()
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   252
        toc3.levelStyles = [tocLevelStyle]   #only need one
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   253
        tocEntries = []
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   254
        for i in range(chapters):
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   255
            #add tuple of (level, text, pageNum, key)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   256
            #with an initial guess of pageNum= 3
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   257
            title = ('This is chapter %d' % (i+1)) if i!=9 else '<a href="http://www.reportlab.com">onelink</a>'
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   258
            tocEntries.append((0, title, 2+i, None))
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   259
        toc3.addEntries(tocEntries)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   260
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   261
        story3 = [toc3] 
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   262
        for i in range(chapters):
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   263
            story3.append(PageBreak())
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   264
            title = ('This is chapter %d' % (i+1)) if i!=9 else '<a href="http://www.reportlab.com">onelink</a>'
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   265
            story3.append(Paragraph(title, headerStyle))
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   266
            txt = """
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   267
                The paragraphs in this are not at all random, because
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   268
                we need to be absolutely, totally certain they will fit 
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   269
                on one page.  Each chapter will be one page long.
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   270
            """
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   271
            para = Paragraph(txt, bodyStyle)
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   272
            story3.append(para)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   273
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   274
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   275
        path = outputfile('test_platypus_toc_preload_3.pdf')
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   276
        doc = MyDocTemplate(path)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   277
        passes = doc.multiBuild(story3)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   278
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   279
        # I can't get one pass yet'
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   280
        #self.assertEquals(passes, 1)
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   281
3468
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   282
    def test2(self):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   283
        chapters = 20   #so we know we use only one page
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   284
        from reportlab.lib.colors import pink
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   285
3468
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   286
        #TOC and this HParagraph class just handle the collection
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   287
        TOC = []
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   288
        fontSize = 14
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   289
        leading = fontSize*1.2
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   290
        descent = 0.2*fontSize
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   291
        x = 2.5*cm          #these come from the frame size
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   292
        y = (25+2.5)*cm - leading
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   293
        x1 = (15+2.5)*cm
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   294
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   295
        class HParagraph(Paragraph):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   296
            def __init__(self,key,text,*args,**kwds):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   297
                self._label = text
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   298
                self._key = key
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   299
                Paragraph.__init__(self,text,*args,**kwds)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   300
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   301
            def draw(self):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   302
                Paragraph.draw(self)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   303
                TOC.append((self._label,self.canv.getPageNumber(),self._key))
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   304
                self.canv.bookmarkHorizontal('TOC_%s' % self._key,0,+20)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   305
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   306
        class UseForm(Flowable):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   307
            _ZEROSIZE = 1
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   308
            def __init__(self,formName):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   309
                self._formName = formName
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   310
                self.width = self.height = 0
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   311
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   312
            def draw(self):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   313
                self.canv.doForm(self._formName)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   314
                for i in range(chapters):
3468
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   315
                    yb = y - i*leading      #baseline
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   316
                    self.canv.linkRect('','TOC_%s' % i,(x,yb-descent,x1,yb+fontSize),thickness=0.5,color=pink,relative=0)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   317
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   318
            def drawOn(self,canvas,x,y,_sW=0):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   319
                Flowable.drawOn(self,canvas,0,0,canvas._pagesize[0])
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   320
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   321
        class MakeForm(UseForm):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   322
            def draw(self):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   323
                canv = self.canv
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   324
                canv.saveState()
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   325
                canv.beginForm(self._formName)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   326
                canv.setFont("Helvetica",fontSize)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   327
                for i,(text,pageNumber,key) in enumerate(TOC):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   328
                    yb = y - i*leading      #baseline
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   329
                    canv.drawString(x,yb,text)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   330
                    canv.drawRightString(x1,y-i*leading,str(pageNumber))
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   331
                canv.endForm()
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   332
                canv.restoreState()
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   333
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   334
        headerStyle = makeHeaderStyle(0)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   335
        S = [Spacer(0,180),UseForm('TOC')]
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   336
        RT = 'STARTUP COMPUTERS BLAH BUZZWORD STARTREK PRINTING PYTHON CHOMSKY'.split()
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   337
        for i in range(chapters):
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   338
            S.append(PageBreak())
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   339
            S.append(HParagraph(i,'This is chapter %d' % (i+1), headerStyle))
3898
b7b0eea4946d test_fixes
robin
parents: 3721
diff changeset
   340
            txt = xmlEscape(randomtext.randomText(RT[i*13 % len(RT)], 15))
3468
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   341
            para = Paragraph(txt, makeBodyStyle())
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   342
            S.append(para)
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   343
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   344
        S.append(MakeForm('TOC'))
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   345
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   346
        doc = MyDocTemplate(outputfile('test_platypus_toc_simple.pdf'))
4a75ba27637d test_platypus_toc.py: add in a single pass TOC example
rgbecker
parents: 3374
diff changeset
   347
        doc.build(S)
3374
348f9bcb4c11 added example of preloading table of contents
andy
parents: 3326
diff changeset
   348
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   349
def makeSuite():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   350
    return makeSuiteForClasses(TocTestCase)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   351
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   352
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   353
#noruntests
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   354
if __name__ == "__main__":
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   355
    unittest.TextTestRunner().run(makeSuite())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   356
    printLocation()