4330
|
1 |
#Copyright ReportLab Europe Ltd. 2000-2017
|
2963
|
2 |
#see license.txt for license details
|
|
3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/tools/docco/yaml2pdf.py
|
|
4 |
# yaml2pdf - turns stuff in Yet Another Markup Language
|
|
5 |
# into PDF documents. Very crude - it assumes a
|
|
6 |
# doc template and stylesheet (hard coded for now)
|
|
7 |
# and basically cranks out paragraphs in each style
|
|
8 |
"""yaml2pdf.py - converts Yet Another Markup Language
|
|
9 |
to reasonable PDF documents. This is ReportLab's
|
|
10 |
basic documentation tool.
|
|
11 |
|
|
12 |
Usage:
|
|
13 |
. "yaml2pdf.py filename.ext" will create "filename.pdf"
|
|
14 |
"""
|
|
15 |
|
|
16 |
import sys
|
|
17 |
import os
|
|
18 |
import imp
|
|
19 |
|
3721
|
20 |
from . import yaml
|
|
21 |
from .rltemplate import RLDocTemplate
|
2963
|
22 |
from reportlab.lib.styles import ParagraphStyle
|
|
23 |
from reportlab.lib.enums import *
|
|
24 |
from reportlab.lib.pagesizes import A4
|
|
25 |
from reportlab.platypus import *
|
|
26 |
from reportlab.lib import colors
|
|
27 |
from reportlab.lib.units import inch
|
|
28 |
|
|
29 |
|
3721
|
30 |
from .stylesheet import getStyleSheet
|
2963
|
31 |
|
|
32 |
|
|
33 |
def run(infilename, outfilename):
|
|
34 |
p = yaml.Parser()
|
|
35 |
results = p.parseFile(infilename)
|
|
36 |
|
|
37 |
ss = getStyleSheet()
|
|
38 |
|
|
39 |
#now make flowables from the results
|
|
40 |
story = []
|
|
41 |
for thingy in results:
|
|
42 |
typ = thingy[0]
|
|
43 |
if typ == 'Paragraph':
|
|
44 |
(typ2, stylename, text) = thingy
|
|
45 |
if stylename == 'bu':
|
|
46 |
bulletText='\267'
|
|
47 |
else:
|
|
48 |
bulletText=None
|
|
49 |
try:
|
|
50 |
style = ss[stylename]
|
|
51 |
except KeyError:
|
3721
|
52 |
print('Paragraph style "%s" not found in stylesheet, using Normal instead' % stylename)
|
2963
|
53 |
style = ss['Normal']
|
|
54 |
story.append(Paragraph(text, style, bulletText=bulletText))
|
|
55 |
elif typ == 'Preformatted':
|
|
56 |
(typ2, stylename, text) = thingy
|
|
57 |
try:
|
|
58 |
style = ss[stylename]
|
|
59 |
except KeyError:
|
3721
|
60 |
print('Preformatted style "%s" not found in stylesheet, using Normal instead' % stylename)
|
2963
|
61 |
style = ss['Normal']
|
|
62 |
story.append(Preformatted(text, style, bulletText=bulletText))
|
|
63 |
elif typ == 'Image':
|
|
64 |
filename = thingy[1]
|
|
65 |
img = Image(filename)
|
|
66 |
story.append(img)
|
|
67 |
elif typ == 'PageBreak':
|
|
68 |
story.append(PageBreak())
|
|
69 |
elif typ == 'VSpace':
|
|
70 |
height = thingy[1]
|
|
71 |
story.append(Spacer(0, height))
|
|
72 |
elif typ == 'NextPageTemplate':
|
|
73 |
story.append(NextPageTemplate(thingy[1]))
|
|
74 |
elif typ == 'Custom':
|
|
75 |
# go find it
|
|
76 |
searchPath = [os.getcwd()+'\\']
|
|
77 |
(typ2, moduleName, funcName) = thingy
|
|
78 |
found = imp.find_module(moduleName, searchPath)
|
|
79 |
assert found, "Custom object module %s not found" % moduleName
|
|
80 |
(file, pathname, description) = found
|
|
81 |
mod = imp.load_module(moduleName, file, pathname, description)
|
|
82 |
|
|
83 |
#now get the function
|
|
84 |
func = getattr(mod, funcName)
|
|
85 |
story.append(func())
|
|
86 |
|
|
87 |
else:
|
3721
|
88 |
print('skipping',typ, 'for now')
|
2963
|
89 |
|
|
90 |
|
|
91 |
#print it
|
|
92 |
doc = RLDocTemplate(outfilename, pagesize=A4)
|
|
93 |
doc.build(story)
|
|
94 |
|
|
95 |
if __name__ == '__main__': #NORUNTESTS
|
|
96 |
if len(sys.argv) == 2:
|
|
97 |
infilename = sys.argv[1]
|
|
98 |
outfilename = os.path.splitext(infilename)[0] + '.pdf'
|
|
99 |
if os.path.isfile(infilename):
|
|
100 |
run(infilename, outfilename)
|
|
101 |
else:
|
3721
|
102 |
print('File not found %s' % infilename)
|
2963
|
103 |
else:
|
3721
|
104 |
print(__doc__)
|