MAINTAINERS: fix my last name
[gtk-doc.git] / gtkdoc-mkpdf.in
blob8041551f6f67630b5da042a491234cd168209f33
1 #!@PYTHON@
3 # Support both Python 2 and 3
4 from __future__ import print_function
6 import os, sys, argparse, subprocess
7 import logging
9 version = '@VERSION@'
10 xsltproc = '@XSLTPROC@'
11 dblatex = '@DBLATEX@'
12 fop = '@FOP@'
14 parser = argparse.ArgumentParser(description='gtkdoc-mkpdf version %s - generate documentation in pdf format' % version)
16 parser.add_argument('--verbose', default=False, action='store_true',
17             help='Print extra output while processing.')
18 parser.add_argument('--path', default=[], action='append',
19             help='Extra source directories.')
20 parser.add_argument('--imgdir', default=[], action='append',
21             help='Extra image directories.')
22 parser.add_argument('--version', default=False, action='store_true',
23             help='Print the version of this program')
24 parser.add_argument('--uninstalled', action='store_true', default=False,
25                     help='???')
26 parser.add_argument('args', nargs=2,
27             help='MODULE DRIVER_FILE')
29 def cleanexit(exitval):
30     global module
31     fname = module + '.fo'
32     if os.path.exists(fname):
33         os.unlink(fname)
34     sys.exit(exitval)
36 options = parser.parse_args()
38 if options.version:
39     print(version)
40     sys.exit(0)
42 module=options.args[0]
43 document=options.args[1]
45 if options.uninstalled:
46     # this does not work from buiddir!=srcdir
47     # we could try this
48     # MAKE_SCRDIR=$(abs_srcdir) MAKE_BUILDDIR=$(abs_builddir) gtkdoc-mkpdf ...
49     gtkdocdir=os.path.split(sys.argv[0])[0]
50     logging.debug("uninstalled, gtkdocdir=" + gtkdocdir)
51 else:
52     # the first vars are needed to resolve datadir
53     prefix='@prefix@'
54     datarootdir="@datarootdir@".replace('${prefix}', prefix)
55     datadir="@datadir@".replace('${datarootdir}', datarootdir)
56     gtkdocdir=os.path.join(datadir, 'gtk-doc/data')
58 # We need to use a wrapper because there's no other way to conditionally pass
59 # a `--path $searchpath` argument with proper quoting for the path
60 def run_xsltproc(args):
61     global options, xsltproc
62     # we could do "--path $PWD "
63     # to avoid needing rewriting entities that are copied from the header
64     # into docs under xml
65     if len(options.path) == 0:
66         cmd = [xsltproc] + args
67     else:
68         cmd = [xsltproc, '--path'] + options.searchpath + args
69     pc = subprocess.Popen(cmd, stderr=subprocess.PIPE)
70     (o, stde) = pc.communicate()
71     open('profile.txt', 'wb').write(stde)
72     if pc.returncode != 0:
73         cleanexit(pc.returncode)
75 if dblatex != '':
76     # extra options to consider
77     # -I FIG_PATH
78     # -V is useful for debugging
79     # -T db2latex : different style
80     # -d : keep transient files (for debugging)
81     # -P abc.def=$quiet : once the stylesheets have a quiet mode
82     # xsltproc is already called with --xinclude
83     # does not work: --xslt-opts "--path $searchpath --nonet $@"
84     dblatex_options=['-o', module + '.pdf']
85     for i in options.imgdir:
86         dblatex_options += ['-I', i]
87     dblatex_options.append(document)
88     if not options.verbose:
89         pc = subprocess.Popen([dblatex, '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
90         (stdo, stde) = pc.communicate()
91         if b'--quiet' in stdo or b'--quiet' in stde:
92             dblatex_options= ['--quiet'] + dblatex_options
93     dbcmd = [dblatex] + dblatex_options
94     pc = subprocess.Popen(dbcmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
95     (stde, _) = pc.communicate()
96     for line in stde.split('\n'):
97         if not line.strip():
98             continue
99         if 'programlisting or screen' in line:
100             continue
101         # This happens when dblatex has no support for some special chars
102         if 'Missing character' in line:
103             continue
104         print(line)
105 elif fop != '':
106     if options.verbose:
107         quiet = '0'
108     else:
109         quiet = '1'
110     run_xsltproc(['--nonet',
111                   '--xinclude',
112                   '--stringparam',
113                   'gtkdoc.bookname',
114                   module,
115                   '--stringparam',
116                   'gtkdoc.version',
117                   version,
118                   '--stringparam',
119                   'chunk.quietly',
120                   quiet,
121                   '--stringparam',
122                   'chunker.output.quiet',
123                   quiet,
124                   module,
125                   document,
126                   '-o',
127                   module + '.fo',
128                   gtkdocdir + '/gtk-doc-fo.xsl',
129                   document])
130     # fop dies too easily :(
131     # @FOP@ $module.fo $module.pdf
132 else:
133     print("dblatex or fop must be installed to use gtkdoc-mkpdf.")
134     cleanexit(1)
136 open('pdf.stamp', 'w').write('timestamp')
137 cleanexit(0)