File: doc-generator.py

package info (click to toggle)
telepathy-spec 0.27.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,420 kB
  • sloc: xml: 33,793; python: 1,093; makefile: 110; sh: 11
file content (126 lines) | stat: -rwxr-xr-x 4,003 bytes parent folder | download
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
123
124
125
126
#!/usr/bin/env python3
#
# doc-generator.py
#
# Generates HTML documentation from the parsed spec
#
# Copyright (C) 2009 Collabora Ltd.
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at
# your option) any later version.
#
# This library 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 Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Authors: Davyd Madeley <davyd.madeley@collabora.co.uk>
#

import sys
import os
import os.path
import shutil
import itertools

try:
    from jinja2 import Template
except ImportError as e:
    print(e, file=sys.stderr)
    print("Install `jinja2'?", file=sys.stderr)
    sys.exit(-1)

import specparser

# one day, OptionParser
allow_externals = False
if '--allow-externals' in sys.argv:
    allow_externals = True
    sys.argv.remove('--allow-externals')

program, spec_file, output_path, project, namespace = sys.argv

template_path = os.path.join(os.path.dirname(program), '../doc/templates')

# make the output path
try:
    os.mkdir(output_path)
except OSError:
    pass

# copy in the static files
static = [ 'style.css',
           'jquery.min.js',
           'ui-icons_222222_256x240.png',
           'magic.js',
           'favicon.png'
         ]
for s in static:
    shutil.copy(os.path.join(template_path, s), output_path)

def load_template(filename):
    try:
        file = open(os.path.join(template_path, filename))
        template_def = file.read()
        file.close()
    except IOError as e:
        print("Could not load template file `%s'" % filename, file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(-1)

    return template_def

def render_template(name, context, target=None):
    if target is None:
        target = name
    template_def = load_template(name)
    t = Template(template_def).render(context)
    with open(os.path.join(output_path, target), 'w') as out:
      out.write(t)

spec = specparser.parse(spec_file, namespace, allow_externals=allow_externals)

# write out HTML files for each of the interfaces

all_values = list(spec.everything.values()) + list(spec.errors.values()) + list(spec.types.values())

star = [ (item.short_name, item) for item in all_values ]
star.sort(key = lambda t: t[0].title())
groups = [ (l, list(g)) for l, g in (itertools.groupby(star, key = lambda t: t[0][0].upper())) ]
letters = set(map(lambda t: t[0], groups))
all_letters = list(map(chr, range(ord('A'), ord('Z')+1)))

context = { 'spec': spec, 'star': star, 'groups': groups,
            'letters': letters, 'all_letters': all_letters }
render_template('fullindex.html', context)

context = { 'spec': spec, 'name': project, 'all_values': all_values }
render_template('devhelp.devhelp2', context, target=('%s.devhelp2' % project))

# Not using render_template here to avoid recompiling it n times.
context = { 'spec': spec }
template_def = load_template('interface.html')
t = Template(template_def)
for interface in spec.interfaces:
    context['interface'] = interface
    # open the output file
    with open(os.path.join(output_path, '%s.html'
        % interface.name_for_bindings), 'w') as out:
      out.write(t.render(context))

context = { 'spec': spec }
if len(spec.generic_types) > 0:
    render_template('generic-types.html', context)
if len(spec.errors) > 0:
    render_template('errors.html', context)
render_template('interfaces.html', context)

# write out the TOC last, because this is the file used as the target in the
# Makefile.
render_template('index.html', context)