4373adf1d345a9fa61fd75d070e4571d5f02fbb8
[wl2pdf.git] / wl2pdf.py
1 #!/usr/bin/env jython
2 # -*- coding: utf-8 -*-
3 #
4 #    Copyright © 2009,2010 Łukasz Rekucki
5 #
6 #    This file is part of WL2PDF
7 #
8 #    WL2PDF is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    WL2PDF is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with WL2PDF.  If not, see <http://www.gnu.org/licenses/>.
20
21 from __future__ import with_statement
22
23 import os
24 import sys
25
26 from java.io import *
27 from java.lang import *
28
29 from net.sf.saxon import TransformerFactoryImpl as TransformerFactory
30 from javax.xml.transform import Transformer
31
32 from javax.xml.transform import Source
33 from javax.xml.transform import Result
34 from javax.xml.transform.stream import StreamSource, StreamResult
35 from javax.xml.transform.sax import SAXResult
36
37 from org.apache.fop.fo import ValidationException
38 from net.sf.saxon.trans import XPathException
39
40 from org.apache.fop.apps import *;
41
42 RUNTIME_PATH = os.path.abspath(os.path.join(\
43         os.path.dirname(__file__), '..', 'runtime'))
44
45 CONFIG_PATH = os.path.join(RUNTIME_PATH, 'fop-config.xml')
46
47 fop_factory = FopFactory.newInstance()
48 fop_factory.setUserConfig(File(CONFIG_PATH));
49
50 xfrm_factory = TransformerFactory()
51
52 wl2fo_tmplt = xfrm_factory.newTemplates(StreamSource(\
53         File(os.path.join(RUNTIME_PATH, "xslt", "wl2fo.xslt"))))
54
55 normalize_tmplt = xfrm_factory.newTemplates(StreamSource(\
56         File(os.path.join(RUNTIME_PATH, "xslt", "normalize.xslt"))))
57
58
59 def process_file(filename):
60
61     base, ext = os.path.splitext(filename)
62     print "Transforming %s (%s)... " % (base, os.path.abspath(filename)) ,
63
64     source = File(filename)
65
66     if not source.canRead():
67         print "can't read source. :("
68         return
69
70     dest = File(base + '.pdf')
71     dest_stream = FileOutputStream(dest)
72     agent = fop_factory.newFOUserAgent()
73
74     # configure user agent & factory
75     fop = fop_factory.newFop(MimeConstants.MIME_PDF, agent, dest_stream)
76
77     # stylesheets
78     normalize_xfrm = xfrm_factory.newTransformerHandler(normalize_tmplt)
79     wl2fo_xfrm = xfrm_factory.newTransformerHandler(wl2fo_tmplt)
80     normalize_xfrm.setResult(SAXResult(wl2fo_xfrm))
81     wl2fo_xfrm.setResult(SAXResult(fop.getDefaultHandler()))
82
83     # transform
84     filtered = ByteArrayOutputStream()
85
86     # pre-fetch and prepare
87     with open(filename, 'rb') as input_file:
88         for line in input_file:
89             if line.endswith('/\n'):
90                 filtered.write(line[:-2] + '<br />\n')
91             else:
92                 filtered.write(line)
93
94     source = ByteArrayInputStream(filtered.toByteArray())
95
96     xfrm = xfrm_factory.newTransformer()
97     try:
98         xfrm.transform(StreamSource(source), SAXResult(normalize_xfrm));
99     except (XPathException, ValidationException), exc:
100         print "exception: %s" % exc
101     else:
102         print "done."
103     finally:
104         dest_stream.close()
105         # print some stuff for debuging
106         pass
107
108 def print_usage():
109     print """
110 Usage: book2pdf.py file [file...]"""
111
112 if __name__ == '__main__':
113     print "WLML To PDF converter (c) 2009 Łukasz Rekucki. GPLv3 License."
114
115     if len(sys.argv) == 1:
116         print_usage()
117     else:
118         for filename in sys.argv[1:]:
119             process_file(filename)