File: test_platypus_xref.py

package info (click to toggle)
python-reportlab 1.20debian-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,068 kB
  • ctags: 5,801
  • sloc: python: 53,293; xml: 1,494; makefile: 85
file content (139 lines) | stat: -rw-r--r-- 5,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#Copyright ReportLab Europe Ltd. 2000-2004
#see license.txt for license details
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/test/test_platypus_xref.py
"""Test long documents with indexes, tables and cross-references
"""

import sys, os, time
from string import split, strip, join, whitespace, find
from operator import truth
from types import StringType, ListType

from reportlab.test import unittest
from reportlab.test.utils import makeSuiteForClasses, outputfile

from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import Paragraph, Flowable, Frame, PageTemplate, BaseDocTemplate
from reportlab.platypus.frames import Frame
from reportlab.lib.randomtext import randomText, PYTHON
from reportlab.platypus.tableofcontents import TableOfContents, SimpleIndex


def myMainPageFrame(canvas, doc):
    "The page frame used for all PDF documents."

    canvas.saveState()
    canvas.setFont('Times-Roman', 12)
    pageNumber = canvas.getPageNumber()
    canvas.drawString(10*cm, cm, str(pageNumber))
    canvas.restoreState()


class MyDocTemplate(BaseDocTemplate):
    _invalidInitArgs = ('pageTemplates',)

    def __init__(self, filename, **kw):
        frame1 = Frame(2.5*cm, 2.5*cm, 16*cm, 25*cm, id='Frame1')
        self.allowSplitting = 0
        self.showBoundary = 1
        apply(BaseDocTemplate.__init__, (self, filename), kw)
        template = PageTemplate('normal', [frame1], myMainPageFrame)
        self.addPageTemplates(template)

    def afterFlowable(self, flowable):
        "Registers TOC and Index entries and makes outline entries."
        if flowable.__class__.__name__ == 'Paragraph':
            styleName = flowable.style.name
            if styleName == 'Heading1':
                level = 0
                text = flowable.getPlainText()
                pageNum = self.page
                self.notify('TOCEntry', (level, text, pageNum))

                # Add PDF outline entries (not really needed/tested here).
                key = str(hash(flowable))
                c = self.canv
                c.bookmarkPage(key)
                c.addOutlineEntry(text, key, level=level, closed=0)

            # index a bunch of pythonic buzzwords.  In real life this
            # would be driven by markup.
            try:
                text = flowable.getPlainText()
            except:
                return
            for phrase in ['uniform','depraved','finger', 'Fraudulin']:
                if find(text, phrase) > -1:
                    self.notify('IndexEntry', (phrase, self.page))
                    #print 'IndexEntry:',phrase, self.page


def _test0(self):
    "This makes one long multi-page paragraph."

    # Build story.
    story = []

    styleSheet = getSampleStyleSheet()
    h1 = styleSheet['Heading1']
    h1.pageBreakBefore = 1
    h1.keepWithNext = 1
    h1.outlineLevel = 0

    h2 = styleSheet['Heading2']
    h2.backColor = colors.cyan
    h2.keepWithNext = 1
    h2.outlineLevel = 1

    bt = styleSheet['BodyText']

    story.append(Paragraph("""Cross-Referencing Test""", styleSheet["Title"]))
    story.append(Paragraph("""
        Subsequent pages test cross-references: indexes, tables and individual
        cross references.  The number in brackets at the end of each paragraph
        is its position in the story. (%d)""" % len(story), bt))

    story.append(Paragraph("""Table of Contents:""", styleSheet["Title"]))
    toc = TableOfContents()
    story.append(toc)

    chapterNum = 1
    for i in range(10):
        story.append(Paragraph('Chapter %d: Chapters always starts a new page' % chapterNum, h1))
        chapterNum = chapterNum + 1
        for j in range(3):
            story.append(Paragraph('Heading1 paragraphs should always'
                            'have a page break before.  Heading 2 on the other hand'
                            'should always have a FRAME break before (%d)' % len(story), bt))
            story.append(Paragraph('Heading 2 should always be kept with the next thing (%d)' % len(story), h2))
            for j in range(3):
                story.append(Paragraph(randomText(theme=PYTHON, sentences=2)+' (%d)' % len(story), bt))
                story.append(Paragraph('I should never be at the bottom of a frame (%d)' % len(story), h2))
                story.append(Paragraph(randomText(theme=PYTHON, sentences=1)+' (%d)' % len(story), bt))

    story.append(Paragraph('The Index which goes at the back', h1))
    story.append(SimpleIndex())

    doc = MyDocTemplate(outputfile('test_platypus_xref.pdf'))
    doc.multiBuild(story)


class BreakingTestCase(unittest.TestCase):
    "Test multi-page splitting of paragraphs (eyeball-test)."
    def test0(self):
        _test0(self)


def makeSuite():
    return makeSuiteForClasses(BreakingTestCase)


#noruntests
if __name__ == "__main__":
    if 'debug' in sys.argv:
        _test1(None)
    else:
        unittest.TextTestRunner().run(makeSuite())