Fix in README.md formatting.
[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.dirname(__file__))
43
44 CONFIG_PATH = os.path.join(RUNTIME_PATH, 'fop-config.xml')
45
46 fop_factory = FopFactory.newInstance()
47 fop_factory.setUserConfig(File(CONFIG_PATH));
48
49 xfrm_factory = TransformerFactory()
50
51 wl2fo_tmplt = xfrm_factory.newTemplates(StreamSource(\
52         File(os.path.join(RUNTIME_PATH, "xslt", "wl2fo.xslt"))))
53
54 normalize_tmplt = xfrm_factory.newTemplates(StreamSource(\
55         File(os.path.join(RUNTIME_PATH, "xslt", "normalize.xslt"))))
56
57
58 def process_file(filename):
59
60     base, ext = os.path.splitext(filename)
61     print "Transforming %s (%s)... " % (base, os.path.abspath(filename)) ,
62
63     source = File(filename)
64
65     if not source.canRead():
66         print "can't read source. :("
67         return
68
69     dest = File(base + '.pdf')
70     dest_stream = FileOutputStream(dest)
71     agent = fop_factory.newFOUserAgent()
72
73     # configure user agent & factory
74     fop = fop_factory.newFop(MimeConstants.MIME_PDF, agent, dest_stream)
75
76     # stylesheets
77     normalize_xfrm = xfrm_factory.newTransformerHandler(normalize_tmplt)
78     wl2fo_xfrm = xfrm_factory.newTransformerHandler(wl2fo_tmplt)
79     normalize_xfrm.setResult(SAXResult(wl2fo_xfrm))
80     wl2fo_xfrm.setResult(SAXResult(fop.getDefaultHandler()))
81
82     # transform
83     filtered = ByteArrayOutputStream()
84
85     # pre-fetch and prepare
86     with open(filename, 'rb') as input_file:
87         for line in input_file:
88             if line.endswith('/\n'):
89                 filtered.write(line[:-2] + '<br />\n')
90             else:
91                 filtered.write(line)
92
93     source = ByteArrayInputStream(filtered.toByteArray())
94
95     xfrm = xfrm_factory.newTransformer()
96     try:
97         xfrm.transform(StreamSource(source), SAXResult(normalize_xfrm));
98     except (XPathException, ValidationException), exc:
99         print "exception: %s" % exc
100     else:
101         print "done."
102     finally:
103         dest_stream.close()
104         # print some stuff for debuging
105         pass
106
107 def print_usage():
108     print """
109 Usage: book2pdf.py file [file...]"""
110
111 if __name__ == '__main__':
112     print "WLML To PDF converter. Copyright © Łukasz Rekucki under GPLv3 License."
113
114     if len(sys.argv) == 1:
115         print_usage()
116     else:
117         for filename in sys.argv[1:]:
118             process_file(filename)