author | rgbecker |
Wed, 25 Oct 2000 08:57:46 +0000 | |
changeset 494 | 54257447cfe9 |
parent 255 | ee9e321e747d |
child 684 | 2a43c747527a |
permissions | -rw-r--r-- |
494 | 1 |
#copyright ReportLab Inc. 2000 |
2 |
#see license.txt for license details |
|
3 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/demos/odyssey/fodyssey.py?cvsroot=reportlab |
|
4 |
#$Header: /tmp/reportlab/reportlab/demos/odyssey/fodyssey.py,v 1.14 2000/10/25 08:57:44 rgbecker Exp $ |
|
5 |
__version__=''' $Id: fodyssey.py,v 1.14 2000/10/25 08:57:44 rgbecker Exp $ ''' |
|
73 | 6 |
__doc__='' |
7 |
||
8 |
#REPORTLAB_TEST_SCRIPT |
|
9 |
import sys, copy, string, os |
|
253 | 10 |
from reportlab.platypus import * |
73 | 11 |
from reportlab.lib.units import inch |
129 | 12 |
from reportlab.lib.styles import getSampleStyleSheet |
13 |
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY |
|
253 | 14 |
from reportlab.lib.pagesizes import DEFAULT_PAGE_SIZE |
73 | 15 |
|
129 | 16 |
styles = getSampleStyleSheet() |
73 | 17 |
|
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
18 |
Title = "The Odyssey" |
73 | 19 |
Author = "Homer" |
20 |
||
21 |
def myFirstPage(canvas, doc): |
|
22 |
canvas.saveState() |
|
23 |
canvas.restoreState() |
|
24 |
||
25 |
def myLaterPages(canvas, doc): |
|
26 |
canvas.saveState() |
|
27 |
canvas.setFont('Times-Roman',9) |
|
28 |
canvas.drawString(inch, 0.75 * inch, "Page %d" % doc.page) |
|
29 |
canvas.restoreState() |
|
30 |
||
31 |
def go(): |
|
255 | 32 |
doc = SimpleDocTemplate('fodyssey.pdf',showBoundary='showboundary' in sys.argv) |
225 | 33 |
doc.allowSplitting = not 'nosplitting' in sys.argv |
34 |
doc.build(Elements,myFirstPage,myLaterPages) |
|
73 | 35 |
|
36 |
Elements = [] |
|
37 |
||
38 |
ChapterStyle = copy.copy(styles["Heading1"]) |
|
129 | 39 |
ChapterStyle.alignment = TA_CENTER |
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
40 |
ChapterStyle.fontsize = 16 |
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
41 |
InitialStyle = copy.deepcopy(ChapterStyle) |
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
42 |
InitialStyle.fontsize = 16 |
203 | 43 |
InitialStyle.leading = 20 |
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
44 |
PreStyle = styles["Code"] |
73 | 45 |
|
46 |
def newPage(): |
|
225 | 47 |
Elements.append(PageBreak()) |
73 | 48 |
|
49 |
def chapter(txt, style=ChapterStyle): |
|
50 |
newPage() |
|
129 | 51 |
Elements.append(Paragraph(txt, style)) |
225 | 52 |
Elements.append(Spacer(0.2*inch, 0.3*inch)) |
73 | 53 |
|
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
54 |
def fTitle(txt,style=InitialStyle): |
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
55 |
Elements.append(Paragraph(txt, style)) |
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
56 |
|
119 | 57 |
ParaStyle = copy.deepcopy(styles["Normal"]) |
207 | 58 |
ParaStyle.spaceBefore = 0.1*inch |
119 | 59 |
if 'right' in sys.argv: |
129 | 60 |
ParaStyle.alignment = TA_RIGHT |
119 | 61 |
elif 'left' in sys.argv: |
129 | 62 |
ParaStyle.alignment = TA_LEFT |
119 | 63 |
elif 'justify' in sys.argv: |
129 | 64 |
ParaStyle.alignment = TA_JUSTIFY |
119 | 65 |
elif 'center' in sys.argv or 'centre' in sys.argv: |
129 | 66 |
ParaStyle.alignment = TA_CENTER |
119 | 67 |
else: |
129 | 68 |
ParaStyle.alignment = TA_JUSTIFY |
73 | 69 |
|
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
70 |
def spacer(inches): |
225 | 71 |
Elements.append(Spacer(0.1*inch, inches*inch)) |
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
72 |
|
73 | 73 |
def p(txt, style=ParaStyle): |
129 | 74 |
Elements.append(Paragraph(txt, style)) |
73 | 75 |
|
76 |
def pre(txt, style=PreStyle): |
|
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
77 |
spacer(0.1) |
225 | 78 |
p = Preformatted(txt, style) |
73 | 79 |
Elements.append(p) |
80 |
||
81 |
def parseOdyssey(fn): |
|
82 |
from time import time |
|
83 |
E = [] |
|
84 |
t0=time() |
|
85 |
L = open(fn,'r').readlines() |
|
86 |
t1 = time() |
|
119 | 87 |
print "open(%s,'r').readlines() took %.4f seconds" %(fn,t1-t0) |
73 | 88 |
for i in xrange(len(L)): |
89 |
if L[i][-1]=='\012': |
|
90 |
L[i] = L[i][:-1] |
|
91 |
t2 = time() |
|
92 |
print "Removing all linefeeds took %.4f seconds" %(t2-t1) |
|
93 |
L.append('') |
|
94 |
L.append('-----') |
|
95 |
||
96 |
def findNext(L, i): |
|
97 |
while 1: |
|
98 |
if string.strip(L[i])=='': |
|
99 |
del L[i] |
|
100 |
kind = 1 |
|
101 |
if i<len(L): |
|
102 |
while string.strip(L[i])=='': |
|
103 |
del L[i] |
|
104 |
||
105 |
if i<len(L): |
|
106 |
kind = L[i][-1]=='-' and L[i][0]=='-' |
|
107 |
if kind: |
|
108 |
del L[i] |
|
109 |
if i<len(L): |
|
110 |
while string.strip(L[i])=='': |
|
111 |
del L[i] |
|
112 |
break |
|
113 |
else: |
|
114 |
i = i + 1 |
|
115 |
||
116 |
return i, kind |
|
117 |
||
118 |
f = s = 0 |
|
119 |
while 1: |
|
120 |
f, k = findNext(L,0) |
|
121 |
if k: break |
|
122 |
||
195
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
123 |
E.append([spacer,2]) |
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
124 |
E.append([fTitle,'<font color=red>%s</font>' % Title, InitialStyle]) |
9ec081129a84
Minor fixes and changes for comparison with dodyssey.py
rgbecker
parents:
129
diff
changeset
|
125 |
E.append([fTitle,'<font size=-4>by</font> <font color=green>%s</font>' % Author, InitialStyle]) |
73 | 126 |
|
127 |
while 1: |
|
128 |
if f>=len(L): break |
|
129 |
||
130 |
if string.upper(L[f][0:5])=='BOOK ': |
|
131 |
E.append([chapter,L[f]]) |
|
132 |
f=f+1 |
|
133 |
while string.strip(L[f])=='': del L[f] |
|
134 |
style = ParaStyle |
|
135 |
func = p |
|
136 |
else: |
|
137 |
style = PreStyle |
|
138 |
func = pre |
|
139 |
||
140 |
while 1: |
|
141 |
s=f |
|
142 |
f, k=findNext(L,s) |
|
143 |
sep= (func is pre) and '\012' or ' ' |
|
144 |
E.append([func,string.join(L[s:f],sep),style]) |
|
145 |
if k: break |
|
146 |
t3 = time() |
|
147 |
print "Parsing into memory took %.4f seconds" %(t3-t2) |
|
148 |
del L |
|
149 |
t4 = time() |
|
150 |
print "Deleting list of lines took %.4f seconds" %(t4-t3) |
|
151 |
for i in xrange(len(E)): |
|
152 |
apply(E[i][0],E[i][1:]) |
|
153 |
t5 = time() |
|
154 |
print "Moving into platypus took %.4f seconds" %(t5-t4) |
|
155 |
del E |
|
156 |
t6 = time() |
|
157 |
print "Deleting list of actions took %.4f seconds" %(t6-t5) |
|
158 |
go() |
|
159 |
t7 = time() |
|
160 |
print "saving to PDF took %.4f seconds" %(t7-t6) |
|
117 | 161 |
print "Total run took %.4f seconds"%(t7-t0) |
73 | 162 |
|
251 | 163 |
for fn in ('odyssey.full.txt','odyssey.txt'): |
73 | 164 |
if os.path.isfile(fn): |
165 |
break |
|
198
5604fb99b2e7
Added 'if __name__=='__main__'' handlers to demos
andy_robinson
parents:
195
diff
changeset
|
166 |
if __name__=='__main__': |
5604fb99b2e7
Added 'if __name__=='__main__'' handlers to demos
andy_robinson
parents:
195
diff
changeset
|
167 |
parseOdyssey(fn) |