File: makeindex.py

package info (click to toggle)
khronos-opengl-man4 1.0~svn31251-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,368 kB
  • ctags: 267
  • sloc: xml: 93,382; makefile: 730; python: 627; sh: 50; php: 4
file content (313 lines) | stat: -rwxr-xr-x 11,240 bytes parent folder | download | duplicates (4)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python3
#
# Copyright (c) 2013-2014 The Khronos Group Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and/or associated documentation files (the
# "Materials"), to deal in the Materials without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Materials, and to
# permit persons to whom the Materials are furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Materials.
#
# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.

import io, os, re, string, sys;

if __name__ == '__main__':
    if (len(sys.argv) != 5):
        print('Usage:', sys.argv[0], ' gendir srcdir accordfilename flatfilename', file=sys.stderr)
        exit(1)
    else:
        gendir = sys.argv[1]
        srcdir = sys.argv[2]
        accordfilename = sys.argv[3]
        flatfilename = sys.argv[4]
        # print(' gendir = ', gendir, ' srcdir = ', srcdir, 'accordfilename = ', accordfilename, 'flatfilename = ', flatfilename)
else:
    print('Unknown invocation mode', file=sys.stderr)
    exit(1)

# Various levels of indentation in generated HTML
ind1 = '    '
ind2 = ind1 + ind1
ind3 = ind2 + ind1
ind4 = ind2 + ind2

# Symbolic names
notAlias = False
isAlias = True

# Page title
pageTitle = 'OpenGL 4.x Reference Pages'

# Docbook source and generated HTML file extensions
srcext = '.xml'
genext = '.xhtml'

# List of generated files
files = os.listdir(gendir)

# Feature - class representing a command or function to be indexed, used
# as dictionary values keyed by the feature name to be indexed.
#
# Members
#   file - name of file containing the feature
#   feature - feature name for the index (basis for the dictionary key).
#   alias - True if this is an alias of another feature in the file.
#       Usually if alias is False, feature is the basename of file.
#   apiCommand - True if this is an API command, or should be grouped
#       like one
class Feature:
    def __init__(self,
                 file = None,
                 feature = None,
                 alias = False,
                 apiCommand = None):
        self.file = file
        self.feature = feature
        self.alias = alias
        self.apiCommand = apiCommand
    def makeKey(self):
        # Return dictionary / sort key based on the feature name 
        if (self.apiCommand and self.feature[0:2] == 'gl'): 
            return self.feature[2:]
        else:
            return self.feature

# Add dictionary entry for specified Feature.
# The key used is the feature name, with the leading 'gl' stripped 
#  off if this is an API command
def addkey(dict, feature):
    key = feature.makeKey()
    if (key in dict.keys()):
        print('Key', key, ' already exists in dictionary!')
    else:
        dict[key] = feature

# Create list of entry point names to be indexed.
# Unlike the old Perl script, this proceeds as follows:
# - Each .xhtml page with a parent .xml page gets an
#   index entry for its base name.
# - Additionally, each <function> tag inside a <funcdef>
#   in the parent page gets an aliased index entry.
# - Each .xhtml page *without* a parent is reported but
#   not indexed.
# - Each collision in index terms is reported.
# - Index terms are keys in a dictionary whose entries
#   are [ pagename, alias, glPrefix ] where pagename is 
#   the base name of the indexed page and alias is True
#   if this index isn't the same as pagename.
# - API keys have their glPrefix value set to True,
#   GLSL keys to False. There is a simplistic way of 
#   telling the files apart based on the file name:
#
#   * Everything starting with 'gl[A-Z]' is API
#   * 'removedTypes.*' is API (more may be added)
#   * Everything else is GLSL

def isGLfile(entrypoint):
    if (re.match('^gl[A-Z]', entrypoint) or entrypoint == 'removedTypes'):
        return True
    else:
        return False

# Dictionary of all keys mapped to Feature values
refIndex = {}

for file in files:
    # print('Processing file', file)
    (entrypoint,ext) = os.path.splitext(file)
    if (ext == genext):
        parent = srcdir + '/' + entrypoint + srcext
        # Determine if this is an API or GLSL page
        apiCommand = isGLfile(entrypoint)
        if (os.path.exists(parent)):
            addkey(refIndex, Feature(file, entrypoint, False, apiCommand))
            # Search parent file for <function> tags inside <funcdef> tags
            # This doesn't search for <varname> inside <fieldsynopsis>, because
            #   those aren't on the same line and it's hard.
            fp = open(parent)
            for line in fp.readlines():
                # Look for <function> tag contents and add as aliases
                # Don't add the same key twice
                for m in re.finditer(r"<funcdef>.*<function>(.*)</function>.*</funcdef>", line):
                    funcname = m.group(1)
                    if (funcname != entrypoint):
                        addkey(refIndex, Feature(file, funcname, True, apiCommand))
            fp.close()
        else:
            print('No parent page for', file, ', will not be indexed')

# Some utility functions for generating the navigation table
# Opencl_tofc.html uses style.css instead of style-index.css
# flatMenu - if True, don't include accordion JavaScript,
#   generating a flat (expanded) menu.
# letters - if not None, include per-letter links to within
#   the indices for each letter in the list.
# altMenu - if not None, the name of the alternate index to
#   link to.
def printHeader(fp, flatMenu = False, letters = None, altMenu = None):
    if (flatMenu):
        scriptInclude = '    <!-- Don\'t include accord.js -->'
    else:
        scriptInclude = '    <?php include \'accord.js\'; ?>'

    print('<html>',
          '<head>',
          '    <link rel="stylesheet" type="text/css" href="style-index.css" />',
          '    <title>' + pageTitle + '</title>',
               scriptInclude,
          '</head>',
          '<body>',
          sep='\n', file=fp)

    if (altMenu):
        if (flatMenu):
            altLabel = '(accordion-style)'
        else:
            altLabel = '(flat)'
        print('    <a href="' + altMenu + '">' + 
              'Use alternate ' + altLabel + ' index' +
              '</a>', file=fp)

    if (letters):
        print('    <center>\n<div id="container">', file=fp)
        for letter in letters:
            print('        <b><a href="#' + 
                  letter + 
                  '" style="text-decoration:none">' + 
                  letter + 
                  '</a></b> &nbsp;', file=fp)
        print('    </div>\n</center>', file=fp)

    print('    <div id="navwrap">',
          '    <ul id="containerul"> <!-- Must wrap entire list for expand/contract -->',
          '    <li class="Level1">',
          '        <a href="start.html" target="pagedisplay">Introduction</a>',
          '    </li>',
          sep='\n', file=fp)

def printFooter(fp, flatMenu = False):
    print('    </div> <!-- End containerurl -->', file=fp)
    if (not flatMenu):
        print('    <script type="text/javascript">initiate();</script>', file=fp)
    print('</body>',
          '</html>',
          sep='\n', file=fp)

# Add a nav table entry. key = link name, feature = Feature info for key
def addMenuLink(key, feature, fp):
    file = feature.file
    linkname = feature.feature

    print(ind4 + '<li><a href="' + file + '" target="pagedisplay">'
               + linkname + '</a></li>',
          sep='\n', file=fp)

# Begin index section for a letter, include an anchor to link to
def beginLetterSection(letter, fp):
    print(ind2 + '<a name="' + letter + '"></a>',
          ind2 + '<li>' + letter,
          ind3 + '<ul class="Level3">',
          sep='\n', file=fp)

# End index section for a letter
def endLetterSection(opentable, fp):
    if (opentable == 0):
        return
    print(ind3 + '</ul> <!-- End Level3 -->',
          ind2 + '</li>',
          sep='\n', file=fp)

# Return the keys in a dictionary sorted by name.
# Select only keys matching whichKeys (see genDict below)
def sortedKeys(dict, whichKeys):
    list = []
    for key in dict.keys():
        if (whichKeys == 'all' or
            (whichKeys == 'api' and dict[key].apiCommand) or
            (whichKeys == 'glsl' and not dict[key].apiCommand)):
            list.append(key)
    list.sort(key=str.lower)
    return list

# Generate accordion menu for this dictionary, titled as specified.
#
# If whichKeys is 'all', generate index for all features
# If whichKeys is 'api', generate index only for API features
# If whichKeys is 'glsl', generate index only for GLSL features
#
# fp is the file to write to
def genDict(dict, title, whichKeys, fp):
    print(ind1 + '<li class="Level1">' + title,
          ind2 + '<ul class="Level2">',
          sep='\n', file=fp)

    # Print links for sorted keys in each letter section
    curletter = ''
    opentable = 0

    # Determine which letters are in the table of contents for this
    # dictionary. If glPrefix is set, strip the 'gl' prefix from each
    # key containing it first.

    # Generatesorted list of page indexes. Select keys matching whichKeys.
    keys = sortedKeys(dict, whichKeys)

    # print('@ Sorted list of page indexes:\n', keys)

    for key in keys:
        # Character starting this key
        c = str.lower(key[0])

        if (c != curletter):
            endLetterSection(opentable, fp)
            # Start a new subtable for this letter
            beginLetterSection(c, fp)
            opentable = 1
            curletter = c
        addMenuLink(key, dict[key], fp)
    endLetterSection(opentable, fp)

    print(ind2 + '</ul> <!-- End Level2 -->',
          ind1 + '</li> <!-- End Level1 -->',
          sep='\n', file=fp)

######################################################################

# Generate the accordion menu, with separate API and GLSL sections
fp = open(accordfilename, 'w')
printHeader(fp, flatMenu = False, altMenu = flatfilename)

genDict(refIndex, 'API Entry Points', 'api', fp)
genDict(refIndex, 'GLSL Functions', 'glsl', fp)

printFooter(fp, flatMenu = False)
fp.close()

######################################################################

# Generate the non-accordion menu, with combined API and GLSL sections
fp = open(flatfilename, 'w')

# Set containing all index letters
indices = { key[0].lower() for key in refIndex.keys() }
letters = [c for c in indices]
letters.sort()

printHeader(fp, flatMenu = True, letters = letters, altMenu = accordfilename)

genDict(refIndex, 'API and GLSL Index', 'all', fp)

printFooter(fp, flatMenu = True)
fp.close()