common: port more functions and fix one
[gtk-doc.git] / gtkdoc / mkpdf.py
blob28de017524e824205a90a2660953cbad9d596de2
1 # -*- python; coding: utf-8 -*-
3 # gtk-doc - GTK DocBook documentation generator.
4 # Copyright (C) 2009-2017 Stefan Sauer
5 # 2017 Jussi Pakkanen
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 # Support both Python 2 and 3
23 from __future__ import print_function
25 import os
26 import sys
27 import argparse
28 import subprocess
30 from . import config
33 def run_xsltproc(options, args):
34 # we could do "--path $PWD " to avoid needing rewriting entities that are
35 # copied from the header into docs under xml
36 if len(options.path) == 0:
37 cmd = [config.xsltproc] + args
38 else:
39 cmd = [config.xsltproc, '--path'] + options.searchpath + args
40 pc = subprocess.Popen(cmd, stderr=subprocess.PIPE)
41 (o, stde) = pc.communicate()
42 open('profile.txt', 'wb').write(stde)
43 return pc.returncode
46 def run(options):
47 module = options.args[0]
48 document = options.args[1]
50 if options.uninstalled:
51 # this does not work from buiddir!=srcdir
52 # we could try this
53 # MAKE_SCRDIR=$(abs_srcdir) MAKE_BUILDDIR=$(abs_builddir) gtkdoc-mkpdf ...
54 gtkdocdir = os.path.split(sys.argv[0])[0]
55 else:
56 gtkdocdir = os.path.join(config.datadir, 'gtk-doc/data')
58 if config.dblatex != '':
59 # extra options to consider
60 # -I FIG_PATH
61 # -V is useful for debugging
62 # -T db2latex : different style
63 # -d : keep transient files (for debugging)
64 # -P abc.def=$quiet : once the stylesheets have a quiet mode
65 # xsltproc is already called with --xinclude
66 # does not work: --xslt-opts "--path $searchpath --nonet $@"
67 dblatex_options = ['-o', module + '.pdf']
68 for i in options.imgdir:
69 dblatex_options += ['-I', i]
70 dblatex_options.append(document)
71 if not options.verbose:
72 pc = subprocess.Popen([config.dblatex, '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
73 (stdo, stde) = pc.communicate()
74 if b'--quiet' in stdo or b'--quiet' in stde:
75 dblatex_options = ['--quiet'] + dblatex_options
76 dbcmd = [config.dblatex] + dblatex_options
77 pc = subprocess.Popen(dbcmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
78 (stde, _) = pc.communicate()
79 for line in stde.split('\n'):
80 if not line.strip():
81 continue
82 if 'programlisting or screen' in line:
83 continue
84 # This happens when dblatex has no support for some special chars
85 if 'Missing character' in line:
86 continue
87 print(line)
88 res = pc.returncode
89 elif config.fop != '':
90 if options.verbose:
91 quiet = '0'
92 else:
93 quiet = '1'
94 res = run_xsltproc(options, ['--nonet',
95 '--xinclude',
96 '--stringparam',
97 'gtkdoc.bookname',
98 module,
99 '--stringparam',
100 'gtkdoc.version',
101 config.version,
102 '--stringparam',
103 'chunk.quietly',
104 quiet,
105 '--stringparam',
106 'chunker.output.quiet',
107 quiet,
108 module,
109 document,
110 '-o',
111 module + '.fo',
112 gtkdocdir + '/gtk-doc-fo.xsl',
113 document])
114 # TODO: fop dies too easily :(
115 # res = subprocess.call([config.fop, module + '.fo', module + '.pdf'))
116 fname = module + '.fo'
117 if os.path.exists(fname):
118 os.unlink(fname)
119 else:
120 print("dblatex or fop must be installed to use gtkdoc-mkpdf.")
121 res = 1
123 open('pdf.stamp', 'w').write('timestamp')
124 return res