tests/test_platypus_index.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4367 9960d82643bf
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: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
     2
#see license.txt for license details
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
     3
"""Tests for the Platypus SimpleIndex and AlphabeticIndex classes.
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
     4
"""
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4178
diff changeset
     5
__version__='3.3.0'
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
     6
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
     7
setOutDir(__name__)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
     8
import sys, os
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
     9
from os.path import join, basename, splitext
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    10
from math import sqrt
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    11
import unittest
3169
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    12
from reportlab.lib.units import cm
4178
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
    13
from reportlab.lib.utils import commajoin, asUnicode
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    14
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    15
from reportlab.platypus.paragraph import Paragraph
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    16
from reportlab.platypus.xpreformatted import XPreformatted
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    17
from reportlab.platypus.frames import Frame
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    18
from reportlab.platypus.doctemplate \
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    19
     import PageTemplate, BaseDocTemplate
3164
0c60143b507e Added setup method and headers parameter. AlphabeticalIndex is now deprecated.
jonas
parents: 3163
diff changeset
    20
from reportlab.platypus.tableofcontents import SimpleIndex
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    21
from reportlab.lib import randomtext
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    22
import re
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    23
from xml.sax.saxutils import quoteattr
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    24
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    25
def myMainPageFrame(canvas, doc):
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    26
    "The page frame used for all PDF documents."
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    27
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    28
    canvas.saveState()
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    29
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    30
    canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    31
    canvas.setFont('Times-Roman', 12)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    32
    pageNumber = canvas.getPageNumber()
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    33
    canvas.drawString(10*cm, cm, str(pageNumber))
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    34
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    35
    canvas.restoreState()
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    36
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    37
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    38
class MyDocTemplate(BaseDocTemplate):
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    39
    "The document template used for all PDF documents."
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    40
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    41
    _invalidInitArgs = ('pageTemplates',)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    42
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    43
    def __init__(self, filename, **kw):
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    44
        frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    45
        self.allowSplitting = 0
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3171
diff changeset
    46
        BaseDocTemplate.__init__(self, filename, **kw)
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    47
        template = PageTemplate('normal', [frame1], myMainPageFrame)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    48
        self.addPageTemplates(template)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    49
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    50
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    51
    def afterFlowable(self, flowable):
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    52
        "Registers TOC entries."
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    53
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    54
        if flowable.__class__.__name__ == 'Paragraph':
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    55
            styleName = flowable.style.name
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    56
            if styleName[:7] == 'Heading':
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    57
                key = str(hash(flowable))
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    58
                self.canv.bookmarkPage(key)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    59
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    60
                # Register TOC entries.
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    61
                level = int(styleName[7:])
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    62
                text = flowable.getPlainText()
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    63
                pageNum = self.page
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    64
                # Try calling this with and without a key to test both
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    65
                # Entries of every second level will have links, others won't
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    66
                if level % 2 == 1:
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    67
                    self.notify('TOCEntry', (level, text, pageNum, key))
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    68
                else:
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    69
                    self.notify('TOCEntry', (level, text, pageNum))
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    70
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    71
def makeBodyStyle():
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    72
    "Body text style - the default will do"
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    73
    return ParagraphStyle('body', spaceBefore=20)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    74
    
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    75
class IndexTestCase(unittest.TestCase):
3169
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    76
    "Test SimpleIndex classes (eyeball-test)."
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    77
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    78
    def test0(self):
3169
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    79
        '''
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    80
        Test case for Indexes. This will draw an index %sat the end of the
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    81
        document with dots seperating the indexing terms from the page numbers.
3171
c94c900d9263 Better syntax for listing indexing terms inside an index paragraph tag attribute.
jonas
parents: 3169
diff changeset
    82
        Index terms are grouped by their first 2, and first 3 characters.
3169
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    83
        The page numbers should be clickable and link to the indexed word.
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    84
        '''
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    85
        # Build story.
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    86
        
3164
0c60143b507e Added setup method and headers parameter. AlphabeticalIndex is now deprecated.
jonas
parents: 3163
diff changeset
    87
        for headers in False, True:
0c60143b507e Added setup method and headers parameter. AlphabeticalIndex is now deprecated.
jonas
parents: 3163
diff changeset
    88
            path = outputfile('test_platypus_index%s.pdf' % (headers and '_headers' or ''))
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    89
            doc = MyDocTemplate(path)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    90
            story = []
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    91
            styleSheet = getSampleStyleSheet()
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    92
            bt = styleSheet['BodyText']
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
    93
    
3169
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    94
            description = '<font color=red>%s</font>' % (self.test0.__doc__  % (headers and 'with alphabetic headers ' or ''))
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
    95
            story.append(Paragraph(description, bt))
3164
0c60143b507e Added setup method and headers parameter. AlphabeticalIndex is now deprecated.
jonas
parents: 3163
diff changeset
    96
            index = SimpleIndex(dot=' . ', headers=headers)
3349
b67514b01536 Index display code now handles 'M&S'-like attributes however they are escaped
andy
parents: 3326
diff changeset
    97
4178
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
    98
            def addParas(words):
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
    99
                words = [asUnicode(w) for w in words]
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   100
                txt = u' '.join([(len(w) > 5 and u'<index item=%s/>%s' % (quoteattr(commajoin([w[:2], w[:3], w])), w) or w) for w in words])
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   101
                para = Paragraph(txt, makeBodyStyle())
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   102
                story.append(para)
4178
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   103
    
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   104
            for i in xrange(20):
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   105
                addParas(randomtext.randomText(randomtext.PYTHON, 5).split(' '))
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   106
            addParas([u+w for u in u'E\xc8\xc9\xca\xcb' for w in (u'erily',u'asily')])
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   107
            addParas([u+w for u in u'A\xc0\xc4\xc1\xc3\xc2' for w in (u'dvance',u'ttend')])
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   108
            addParas([u+w for u in u'O\xd2\xd6\xd3\xd2' for w in (u'rdinary',u'verflow')])
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   109
            addParas([u+w for u in u'U\xd9\xdc\xdb' for w in (u'ndertow',u'nbeliever')])
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   110
            addParas([u+w for u in u'e\xe8\xea\xeb\xe9' for w in (u'ventide',u'lision')])
b2c73e04465c fix indexing for accented stuff contributed by alexandrel_sgi @ bitbucket, version-->3.1.50
robin
parents: 3857
diff changeset
   111
            addParas([u+w for u in u'o\xf2\xf5\xf3\xf4' for w in (u'verture',u'ntology')])
3349
b67514b01536 Index display code now handles 'M&S'-like attributes however they are escaped
andy
parents: 3326
diff changeset
   112
b67514b01536 Index display code now handles 'M&S'-like attributes however they are escaped
andy
parents: 3326
diff changeset
   113
            #test ampersand in index term
3857
e9b94b4da956 test_platypus_index.py: fix ampersands in paragraph text
robin
parents: 3617
diff changeset
   114
            txt = '\nMarks &amp; Spencer - purveyors of fine groceries, underwear and ampersands - should have their initials displayed however they were input.\n<index item="M&amp;S,groceries"/><index item="M&amp;S,underwear"/><index item="M&amp;S,ampersands"/>'
3349
b67514b01536 Index display code now handles 'M&S'-like attributes however they are escaped
andy
parents: 3326
diff changeset
   115
            para = Paragraph(txt, makeBodyStyle())
b67514b01536 Index display code now handles 'M&S'-like attributes however they are escaped
andy
parents: 3326
diff changeset
   116
            story.append(para)
b67514b01536 Index display code now handles 'M&S'-like attributes however they are escaped
andy
parents: 3326
diff changeset
   117
        
b67514b01536 Index display code now handles 'M&S'-like attributes however they are escaped
andy
parents: 3326
diff changeset
   118
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   119
            story.append(index)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   120
    
3169
de765959892b Cleaned up index test case.
jonas
parents: 3166
diff changeset
   121
            doc.build(story, canvasmaker=index.getCanvasMaker())
3162
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   122
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   123
def makeSuite():
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   124
    return makeSuiteForClasses(IndexTestCase)
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   125
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   126
#noruntests
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   127
if __name__ == "__main__":
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   128
    unittest.TextTestRunner().run(makeSuite())
77afde998aa9 Made Index entries clickable.
jonas
parents:
diff changeset
   129
    printLocation()