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-- |
4330 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2017 |
2963 | 2 |
#see license.txt for license details |
3 |
"""Tests for the Platypus TableOfContents class. |
|
4 |
||
5 |
Currently there is only one such test. Most such tests, like this |
|
6 |
one, will be generating a PDF document that needs to be eye-balled |
|
7 |
in order to find out if it is 'correct'. |
|
8 |
""" |
|
4252 | 9 |
__version__='3.3.0' |
2984 | 10 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation |
11 |
setOutDir(__name__) |
|
2963 | 12 |
import sys, os |
13 |
from os.path import join, basename, splitext |
|
14 |
from math import sqrt |
|
3374 | 15 |
import random |
2966 | 16 |
import unittest |
2963 | 17 |
from reportlab.lib.units import inch, cm |
18 |
from reportlab.lib.pagesizes import A4 |
|
19 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
|
20 |
from reportlab.platypus.xpreformatted import XPreformatted |
|
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 | 24 |
from reportlab.platypus.doctemplate \ |
25 |
import PageTemplate, BaseDocTemplate |
|
3374 | 26 |
from reportlab.platypus import tableofcontents, PageBreak |
2963 | 27 |
from reportlab.lib import randomtext |
3898 | 28 |
from xml.sax.saxutils import escape as xmlEscape |
2963 | 29 |
|
30 |
||
31 |
def myMainPageFrame(canvas, doc): |
|
32 |
"The page frame used for all PDF documents." |
|
33 |
||
34 |
canvas.saveState() |
|
35 |
||
36 |
canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm) |
|
37 |
canvas.setFont('Times-Roman', 12) |
|
38 |
pageNumber = canvas.getPageNumber() |
|
39 |
canvas.drawString(10*cm, cm, str(pageNumber)) |
|
40 |
||
41 |
canvas.restoreState() |
|
42 |
||
43 |
||
44 |
class MyDocTemplate(BaseDocTemplate): |
|
45 |
"The document template used for all PDF documents." |
|
46 |
||
47 |
_invalidInitArgs = ('pageTemplates',) |
|
48 |
||
49 |
def __init__(self, filename, **kw): |
|
50 |
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1') |
|
51 |
self.allowSplitting = 0 |
|
3326 | 52 |
BaseDocTemplate.__init__(self, filename, **kw) |
2963 | 53 |
template = PageTemplate('normal', [frame1], myMainPageFrame) |
54 |
self.addPageTemplates(template) |
|
55 |
||
56 |
||
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 | 59 |
|
60 |
if flowable.__class__.__name__ == 'Paragraph': |
|
61 |
styleName = flowable.style.name |
|
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 | 66 |
# Register TOC entries. |
67 |
level = int(styleName[7:]) |
|
68 |
text = flowable.getPlainText() |
|
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 | 76 |
|
77 |
||
78 |
def makeHeaderStyle(level, fontName='Times-Roman'): |
|
79 |
"Make a header style for different levels." |
|
80 |
||
81 |
assert level >= 0, "Level must be >= 0." |
|
82 |
||
83 |
PS = ParagraphStyle |
|
84 |
size = 24.0 / sqrt(1+level) |
|
85 |
style = PS(name = 'Heading' + str(level), |
|
86 |
fontName = fontName, |
|
87 |
fontSize = size, |
|
88 |
leading = size*1.2, |
|
89 |
spaceBefore = size/4.0, |
|
90 |
spaceAfter = size/8.0) |
|
91 |
||
92 |
return style |
|
93 |
||
94 |
||
95 |
def makeBodyStyle(): |
|
96 |
"Body text style - the default will do" |
|
97 |
return ParagraphStyle('body') |
|
98 |
||
99 |
||
100 |
def makeTocHeaderStyle(level, delta, epsilon, fontName='Times-Roman'): |
|
101 |
"Make a header style for different levels." |
|
102 |
||
103 |
assert level >= 0, "Level must be >= 0." |
|
104 |
||
105 |
PS = ParagraphStyle |
|
106 |
size = 12 |
|
107 |
style = PS(name = 'Heading' + str(level), |
|
108 |
fontName = fontName, |
|
109 |
fontSize = size, |
|
110 |
leading = size*1.2, |
|
111 |
spaceBefore = size/4.0, |
|
112 |
spaceAfter = size/8.0, |
|
113 |
firstLineIndent = -epsilon, |
|
114 |
leftIndent = level*delta + epsilon) |
|
115 |
||
116 |
return style |
|
117 |
||
118 |
||
119 |
class TocTestCase(unittest.TestCase): |
|
120 |
"Test TableOfContents class (eyeball-test)." |
|
121 |
||
122 |
def test0(self): |
|
123 |
"""Test story with TOC and a cascaded header hierarchy. |
|
124 |
||
125 |
The story should contain exactly one table of contents that is |
|
126 |
immediatly followed by a list of of cascaded levels of header |
|
127 |
lines, each nested one level deeper than the previous one. |
|
128 |
||
129 |
Features to be visually confirmed by a human being are: |
|
130 |
||
131 |
1. TOC lines are indented in multiples of 1 cm. |
|
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 | 135 |
""" |
136 |
||
137 |
maxLevels = 12 |
|
138 |
||
139 |
# Create styles to be used for document headers |
|
140 |
# on differnet levels. |
|
141 |
headerLevelStyles = [] |
|
142 |
for i in range(maxLevels): |
|
143 |
headerLevelStyles.append(makeHeaderStyle(i)) |
|
144 |
||
145 |
# Create styles to be used for TOC entry lines |
|
146 |
# for headers on differnet levels. |
|
147 |
tocLevelStyles = [] |
|
148 |
d, e = tableofcontents.delta, tableofcontents.epsilon |
|
149 |
for i in range(maxLevels): |
|
150 |
tocLevelStyles.append(makeTocHeaderStyle(i, d, e)) |
|
151 |
||
152 |
# Build story. |
|
153 |
story = [] |
|
154 |
styleSheet = getSampleStyleSheet() |
|
155 |
bt = styleSheet['BodyText'] |
|
156 |
||
157 |
description = '<font color=red>%s</font>' % self.test0.__doc__ |
|
158 |
story.append(XPreformatted(description, bt)) |
|
159 |
||
3059
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
2984
diff
changeset
|
160 |
toc = tableofcontents.TableOfContents() |
2963 | 161 |
toc.levelStyles = tocLevelStyles |
162 |
story.append(toc) |
|
163 |
||
164 |
for i in range(maxLevels): |
|
165 |
story.append(Paragraph('HEADER, LEVEL %d' % i, |
|
166 |
headerLevelStyles[i])) |
|
167 |
#now put some body stuff in. |
|
3898 | 168 |
txt = xmlEscape(randomtext.randomText(randomtext.PYTHON, 5)) |
2963 | 169 |
para = Paragraph(txt, makeBodyStyle()) |
170 |
story.append(para) |
|
171 |
||
172 |
path = outputfile('test_platypus_toc.pdf') |
|
173 |
doc = MyDocTemplate(path) |
|
174 |
doc.multiBuild(story) |
|
175 |
||
176 |
||
3374 | 177 |
|
178 |
def test1(self): |
|
179 |
"""This shows a table which would take more than one page, |
|
180 |
and need multiple passes to render. But we preload it |
|
181 |
with the right headings to make it go faster. We used |
|
182 |
a simple 100-chapter document with one level. |
|
183 |
""" |
|
184 |
||
185 |
chapters = 30 #goes over one page |
|
186 |
||
187 |
headerStyle = makeHeaderStyle(0) |
|
188 |
d, e = tableofcontents.delta, tableofcontents.epsilon |
|
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 | 191 |
|
192 |
# Build most of the story; we'll re-use it to |
|
193 |
# make documents with different numbers of passes. |
|
194 |
||
195 |
story = [] |
|
196 |
styleSheet = getSampleStyleSheet() |
|
197 |
bt = styleSheet['BodyText'] |
|
198 |
||
199 |
description = '<font color=red>%s</font>' % self.test1.__doc__ |
|
200 |
story.append(XPreformatted(description, bt)) |
|
201 |
||
202 |
for i in range(chapters): |
|
203 |
story.append(PageBreak()) |
|
204 |
story.append(Paragraph('This is chapter %d' % (i+1), |
|
205 |
headerStyle)) |
|
206 |
#now put some lengthy body stuff in. |
|
207 |
for paras in range(random.randint(1,3)): |
|
3898 | 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 | 210 |
story.append(para) |
211 |
||
212 |
||
213 |
#try 1: empty TOC, 3 passes |
|
214 |
||
215 |
toc = tableofcontents.TableOfContents() |
|
216 |
toc.levelStyles = [tocLevelStyle] #only need one |
|
217 |
story1 = [toc] + story |
|
218 |
||
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 | 221 |
doc = MyDocTemplate(path) |
222 |
passes = doc.multiBuild(story1) |
|
223 |
self.assertEquals(passes, 3) |
|
224 |
||
225 |
#try 2: now preload the TOC with the entries |
|
226 |
||
227 |
toc = tableofcontents.TableOfContents() |
|
228 |
toc.levelStyles = [tocLevelStyle] #only need one |
|
229 |
tocEntries = [] |
|
230 |
for i in range(chapters): |
|
231 |
#add tuple of (level, text, pageNum, key) |
|
232 |
#with an initial guess of pageNum=0 |
|
233 |
tocEntries.append((0, 'This is chapter %d' % (i+1), 0, None)) |
|
234 |
toc.addEntries(tocEntries) |
|
235 |
||
236 |
story2 = [toc] + story |
|
237 |
||
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 | 240 |
doc = MyDocTemplate(path) |
241 |
passes = doc.multiBuild(story2) |
|
242 |
self.assertEquals(passes, 2) |
|
243 |
||
244 |
||
245 |
||
246 |
#try 3: preload again but try to be really smart and work out |
|
247 |
#in advance what page everything starts on. We cannot |
|
248 |
#use a random story for this. |
|
249 |
||
250 |
||
251 |
toc3 = tableofcontents.TableOfContents() |
|
252 |
toc3.levelStyles = [tocLevelStyle] #only need one |
|
253 |
tocEntries = [] |
|
254 |
for i in range(chapters): |
|
255 |
#add tuple of (level, text, pageNum, key) |
|
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 | 259 |
toc3.addEntries(tocEntries) |
260 |
||
261 |
story3 = [toc3] |
|
262 |
for i in range(chapters): |
|
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 | 266 |
txt = """ |
267 |
The paragraphs in this are not at all random, because |
|
268 |
we need to be absolutely, totally certain they will fit |
|
269 |
on one page. Each chapter will be one page long. |
|
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 | 272 |
story3.append(para) |
273 |
||
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 | 276 |
doc = MyDocTemplate(path) |
277 |
passes = doc.multiBuild(story3) |
|
278 |
||
279 |
# I can't get one pass yet' |
|
280 |
#self.assertEquals(passes, 1) |
|
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 | 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 | 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 | 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 | 348 |
|
2963 | 349 |
def makeSuite(): |
350 |
return makeSuiteForClasses(TocTestCase) |
|
351 |
||
352 |
||
353 |
#noruntests |
|
354 |
if __name__ == "__main__": |
|
355 |
unittest.TextTestRunner().run(makeSuite()) |
|
356 |
printLocation() |