summaryrefslogtreecommitdiff
path: root/gtkdoc/mkpdf.py
blob: c7c75bfb48092f3be42885045428197238872860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- python; coding: utf-8 -*-
#
# gtk-doc - GTK DocBook documentation generator.
# Copyright (C) 2009-2017  Stefan Sauer
#               2017  Jussi Pakkanen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

import logging
import os
import sys
import subprocess

from . import config


def run_xsltproc(options, args):
    command = [config.xsltproc]
    # we could do "--path $PWD " to avoid needing rewriting entities that are
    # copied from the header into docs under xml
    if len(options.path):
        command += ['--path', ':'.join(options.path)]
    logging.info('running "%s"', ' '.join(command + args))
    pc = subprocess.Popen(command + args, stderr=subprocess.PIPE)
    (o, stde) = pc.communicate()
    with open('profile.txt', 'wb') as h:
        h.write(stde)
    return pc.returncode


def run(options):
    logging.info('options: %s', str(options.__dict__))

    module = options.args[0]
    document = options.args[1]

    gtkdocdir, _ = config.get_dirs(options.uninstalled)

    if config.dblatex != '':
        # extra options to consider
        # -I FIG_PATH
        # -V is useful for debugging
        # -T db2latex : different style
        # -d : keep transient files (for debugging)
        # -P abc.def=$quiet : once the stylesheets have a quiet mode
        # -x "--path /path/to/more/files"
        # xsltproc is already called with --xinclude
        # does not work: --xslt-opts "--path $searchpath --nonet $@"
        dblatex_options = ['-o', module + '.pdf']
        for i in options.imgdir:
            dblatex_options += ['-I', i]
        if len(options.path):
            dblatex_options += ['-x', '--path ' + ':'.join(options.path)]
        dblatex_options.append(document)
        if not options.verbose:
            pc = subprocess.Popen([config.dblatex, '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            (stdo, stde) = pc.communicate()
            if b'--quiet' in stdo or b'--quiet' in stde:
                dblatex_options = ['--quiet'] + dblatex_options
        dbcmd = [config.dblatex] + dblatex_options
        pc = subprocess.Popen(dbcmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        (stde, _) = pc.communicate()
        for line in stde.decode('utf-8').split('\n'):
            if not line.strip():
                continue
            if 'programlisting or screen' in line:
                continue
            # This happens when dblatex has no support for some special chars
            if 'Missing character' in line:
                continue
            print(line)
        res = pc.returncode
    elif config.fop != '':
        if options.verbose:
            quiet = '0'
        else:
            quiet = '1'
        res = run_xsltproc(options, ['--nonet',
                                     '--xinclude',
                                     '--stringparam',
                                     'gtkdoc.bookname',
                                     module,
                                     '--stringparam',
                                     'gtkdoc.version',
                                     config.version,
                                     '--stringparam',
                                     'chunk.quietly',
                                     quiet,
                                     '--stringparam',
                                     'chunker.output.quiet',
                                     quiet,
                                     module,
                                     document,
                                     '-o',
                                     module + '.fo',
                                     gtkdocdir + '/gtk-doc-fo.xsl',
                                     document])
        # TODO: fop dies too easily :(
        # res = subprocess.call([config.fop, module + '.fo', module + '.pdf'))
        fname = module + '.fo'
        if os.path.exists(fname):
            os.unlink(fname)
    else:
        print("dblatex or fop must be installed to use gtkdoc-mkpdf.")
        res = 1

    with open('pdf.stamp', 'w') as h:
        h.write('timestamp')
    return res