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
|
#Copyright ReportLab Europe Ltd. 2000-2012
#see license.txt for license details
"""Tests for the Platypus SimpleIndex and AlphabeticIndex classes.
"""
__version__='''$Id$'''
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import sys, os
from os.path import join, basename, splitext
from math import sqrt
import unittest
from reportlab.lib.units import cm
from reportlab.lib.utils import commajoin
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.xpreformatted import XPreformatted
from reportlab.platypus.frames import Frame
from reportlab.platypus.doctemplate \
import PageTemplate, BaseDocTemplate
from reportlab.platypus.tableofcontents import SimpleIndex
from reportlab.lib import randomtext
import re
from xml.sax.saxutils import quoteattr
def myMainPageFrame(canvas, doc):
"The page frame used for all PDF documents."
canvas.saveState()
canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
canvas.setFont('Times-Roman', 12)
pageNumber = canvas.getPageNumber()
canvas.drawString(10*cm, cm, str(pageNumber))
canvas.restoreState()
class MyDocTemplate(BaseDocTemplate):
"The document template used for all PDF documents."
_invalidInitArgs = ('pageTemplates',)
def __init__(self, filename, **kw):
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
self.allowSplitting = 0
BaseDocTemplate.__init__(self, filename, **kw)
template = PageTemplate('normal', [frame1], myMainPageFrame)
self.addPageTemplates(template)
def afterFlowable(self, flowable):
"Registers TOC entries."
if flowable.__class__.__name__ == 'Paragraph':
styleName = flowable.style.name
if styleName[:7] == 'Heading':
key = str(hash(flowable))
self.canv.bookmarkPage(key)
# Register TOC entries.
level = int(styleName[7:])
text = flowable.getPlainText()
pageNum = self.page
# Try calling this with and without a key to test both
# Entries of every second level will have links, others won't
if level % 2 == 1:
self.notify('TOCEntry', (level, text, pageNum, key))
else:
self.notify('TOCEntry', (level, text, pageNum))
def makeBodyStyle():
"Body text style - the default will do"
return ParagraphStyle('body', spaceBefore=20)
class IndexTestCase(unittest.TestCase):
"Test SimpleIndex classes (eyeball-test)."
def test0(self):
'''
Test case for Indexes. This will draw an index %sat the end of the
document with dots seperating the indexing terms from the page numbers.
Index terms are grouped by their first 2, and first 3 characters.
The page numbers should be clickable and link to the indexed word.
'''
# Build story.
for headers in False, True:
path = outputfile('test_platypus_index%s.pdf' % (headers and '_headers' or ''))
doc = MyDocTemplate(path)
story = []
styleSheet = getSampleStyleSheet()
bt = styleSheet['BodyText']
description = '<font color=red>%s</font>' % (self.test0.__doc__ % (headers and 'with alphabetic headers ' or ''))
story.append(Paragraph(description, bt))
index = SimpleIndex(dot=' . ', headers=headers)
for i in range(20):
words = randomtext.randomText(randomtext.PYTHON, 5).split(' ')
txt = ' '.join([(len(w) > 5 and '<index item=%s/>%s' % (quoteattr(commajoin([w[:2], w[:3], w])), w) or w) for w in words])
para = Paragraph(txt, makeBodyStyle())
story.append(para)
#test ampersand in index term
txt = '\nMarks & Spencer - purveyors of fine groceries, underwear and ampersands - should have their initials displayed however they were input.\n<index item="M&S,groceries"/><index item="M&S,underwear"/><index item="M&S,ampersands"/>'
para = Paragraph(txt, makeBodyStyle())
story.append(para)
story.append(index)
doc.build(story, canvasmaker=index.getCanvasMaker())
def makeSuite():
return makeSuiteForClasses(IndexTestCase)
#noruntests
if __name__ == "__main__":
unittest.TextTestRunner().run(makeSuite())
printLocation()
|