aboutsummaryrefslogtreecommitdiffstats
path: root/gendoc.py
blob: b6279f5350773fc69e6ec38e5e3c8a81a836ae92 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python

import datetime
import glob
import os
import rdflib
import re
import shutil
import subprocess
import sys
import xml.dom
import xml.dom.minidom

sys.path.append("./lv2specgen")
import lv2specgen

out_base = os.path.join('build', 'ns')
try:
    shutil.rmtree(out_base)
except:
    pass
    
os.makedirs(out_base)

URIPREFIX  = 'http://lv2plug.in/ns/'
DOXPREFIX  = 'ns/doc/html/'
SPECGENDIR = './specgen'
STYLEURI   = os.path.join('aux', 'style.css')
TAGFILE    = './doclinks'

doap = rdflib.Namespace('http://usefulinc.com/ns/doap#')
lv2  = rdflib.Namespace('http://lv2plug.in/ns/lv2core#')
rdf  = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')

devnull = open(os.devnull, 'w')

# Generate code (headers) documentation
print('** Generating header documentation')
print(' * Calling doxygen in ' + os.getcwd())
subprocess.call('doxygen', stdout=devnull)


# Rescue Doxygen tag file from XML hell

# Return the content of the first child node with a certain tag name
def getChildText(elt, tagname):
    elements = elt.getElementsByTagName(tagname)
    for e in elements:
        if e.parentNode == elt:
            return e.firstChild.nodeValue
    return ''

tagdoc = xml.dom.minidom.parse('c_tags')
root = tagdoc.documentElement
bettertags = open(TAGFILE, 'w')
for cn in root.childNodes:
    if cn.nodeType == xml.dom.Node.ELEMENT_NODE and cn.tagName == 'compound':
        if cn.getAttribute('kind') == 'page':
            continue
        name = getChildText(cn, 'name')
        filename = getChildText(cn, 'filename')
        # Sometimes the .html is there, sometimes it isn't...
        if filename[-5:] != '.html':
            filename += '.html'
        bettertags.write('%s %s%s\n' % (name, DOXPREFIX, filename))
        if cn.getAttribute('kind') == 'file':
            prefix = ''
        else:
            prefix = name + '::'
        members = cn.getElementsByTagName('member')
        for m in members:
            mname = prefix + getChildText(m, 'name')
            mafile = getChildText(m, 'anchorfile')
            manchor = getChildText(m, 'anchor')
            bettertags.write('%s %s%s#%s\n' % (mname, DOXPREFIX, \
                                                   mafile, manchor))
bettertags.close()

print('** Generating core documentation')

lv2_outdir = os.path.join(out_base, 'lv2core')
os.mkdir(lv2_outdir)
shutil.copy('core.lv2/lv2.h',        lv2_outdir)
shutil.copy('core.lv2/lv2.ttl',      lv2_outdir)
shutil.copy('core.lv2/manifest.ttl', lv2_outdir)
shutil.copy('doc/index.php',         lv2_outdir)

oldcwd = os.getcwd()
os.chdir(lv2_outdir)
print(' * Running lv2specgen for lv2core in ' + os.getcwd())
lv2specgen.save('lv2.html',
                lv2specgen.specgen('../../../core.lv2/lv2.ttl',
                                   '../../../lv2specgen',
                                   os.path.join('..', '..', 'ns', 'doc'),
                                   STYLEURI,
                                   os.path.join('..', '..'),
                                   os.path.join('..', '..', '..', TAGFILE),
                                   instances=True))
os.chdir(oldcwd)

footer = open('./lv2specgen/footer.html', 'r')

# Generate main (ontology) documentation and indices
for dir in ['ext', 'extensions']:
    print("** Generating %s%s documentation" % (URIPREFIX, dir))

    outdir = os.path.join(out_base, dir)

    shutil.copytree(dir, outdir, ignore=shutil.ignore_patterns('.*', 'waf', 'wscript', '*.in'))

    index_html = """<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf-8" />
<title>LV2 Extension Index</title>
<link rel="stylesheet" type="text/css" href="../../""" + STYLEURI + """\" />
</head>
<body>
<div id="titleheader"><h1 id="title">LV2 Extension Index</h1></div>
<div class="content">
<table summary="An index of LV2 extensions">
<tr><th>Name</th><th>Description</th><th>Version</th><th>Date</th></tr>\n"""

    extensions = []

    for bundle in glob.glob(os.path.join(dir, '*.lv2')):
        b = bundle.replace('.lv2', '')
        b = b[b.find('/') + 1:]

        try:
            model = rdflib.ConjunctiveGraph()
            model.parse('%s/manifest.ttl' % bundle, format='n3')
            model.parse('%s/%s.ttl' % (bundle, b), format='n3')
        except:
            e = sys.exc_info()[1]
            print('error parsing %s: %s' % (bundle, str(e)))
            continue

        # Get extension URI
        ext_node = model.value(None, rdf.type, lv2.Specification)
        if not ext_node:
            continue

        ext = str(ext_node)

        # Get version
        minor = 0
        micro = 0
        try:
            minor = int(model.value(ext_node, lv2.minorVersion, None))
            micro = int(model.value(ext_node, lv2.microVersion, None))
        except Exception as e:
            print "warning: %s: failed to find version for %s" % (bundle, ext)
            pass

        # Get date
        date = None
        for r in model.triples([ext_node, doap.release, None]):
            revision = model.value(r[2], doap.revision, None)
            if revision != ("%d.%d" % (minor, micro)):
                print("warning: %s: doap:revision %s != %d.%d" % (
                        bundle, revision, minor, micro))
                continue

            date = model.value(r[2], doap.created, None)
            break
        
        # Get short description
        shortdesc = model.value(ext_node, doap.shortdesc, None)

        specgendir = '../../../lv2specgen/'
        if (os.access(outdir + '/%s.lv2/%s.ttl' % (b, b), os.R_OK)):
            oldcwd = os.getcwd()
            os.chdir(outdir)
            print(' * Running lv2specgen for %s in %s' % (b, os.getcwd()))
            lv2specgen.save('%s.lv2/%s.html' % (b, b),
                            lv2specgen.specgen('%s.lv2/%s.ttl' % (b, b),
                                               specgendir,
                                               os.path.join('..', '..', '..', 'ns', 'doc'),
                                               STYLEURI,
                                               os.path.join('..', '..', '..'),
                                               os.path.join('..', '..', '..', TAGFILE),
                                               instances=True))
            os.chdir(oldcwd)

            # Name
            li = '<tr><td><a rel="rdfs:seeAlso" href="%s">%s</a></td>' % (b, b)

            # Description
            if shortdesc:
                li += '<td>' + str(shortdesc) + '</td>'
            else:
                li += '<td></td>'

            # Version
            version_str = '%s.%s' % (minor, micro)
            if minor == 0 or (micro % 2 != 0):
                li += '<td><span style="color: red">' + version_str + ' dev</span></td>'
            else:
                li += '<td>' + version_str + '</td>'

            # Date
            if date:
                li += '<td>' + str(date) + '</td>'

            li += '</tr>'

            extensions.append(li)

        shutil.copy('doc/index.php', os.path.join(outdir, b + '.lv2', 'index.php'))
    
        # Remove .lv2 suffix from bundle name (to make URI resolvable)
        os.rename(outdir + '/%s.lv2' % b, outdir + '/%s' % b)

    extensions.sort()
    for i in extensions:
        index_html += i + '\n'
    
    index_html += '</table>\n</div>\n'

    index_html += '<div id="footer">'
    index_html += '<span class="footer-text">Generated on '
    index_html += datetime.datetime.utcnow().strftime('%F %H:%M UTC')
    index_html += ' by gendoc.py</span>&nbsp;'
    index_html += footer.read() + '</div>'

    index_html += '</body></html>\n'

    index_file = open(os.path.join(outdir, 'index.html'), 'w')
    index_file.write(index_html)
    index_file.close()

# Copy stylesheet
try:
    os.mkdir(os.path.join('build', 'aux'))
except:
    pass
shutil.copy('lv2specgen/style.css', os.path.join('build', STYLEURI))

devnull.close()
footer.close()