File: makeFunctionIndex.py

package info (click to toggle)
libvigraimpex 1.10.0%2Bgit20160211.167be93%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,024 kB
  • ctags: 25,337
  • sloc: cpp: 55,572; python: 8,495; ansic: 1,267; makefile: 150; sh: 38
file content (175 lines) | stat: -rw-r--r-- 6,702 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python
from __future__ import division, print_function

import re
import glob
import sys
import xml.etree.ElementTree as ET
from itertools import chain

if len(sys.argv) != 2:
    print('usage: python makeFunctionIndex.py directory')
    sys.exit(1)

path = str(sys.argv[1])


def getClassList():
    tree = ET.parse("tagfile.tag")
    root = tree.getroot()

    class_list = []
    for classes in root.findall("*[@kind='class']"):
        long_name = classes.find('name').text
        # only classes from vigra namespace
        if long_name[0:5] == 'vigra':
            html = classes.find('filename').text
            short_name = long_name[long_name.rfind('::')+2:]
            namespace = long_name[:long_name.rfind('::')]
            class_list.append([html, short_name, namespace])

    class_list.sort(key = lambda a: a[1])
    return class_list



def getFunctionList():
    tree = ET.parse("tagfile.tag")
    root = tree.getroot()

    function_list = []
    for function in chain(root.findall("*[@kind='group']/*[@kind='function']"),root.findall("*[@kind='namespace']/*[@kind='function']")):
        name = function.find('name').text
        anchorfile = function.find('anchorfile').text
        anchor = function.find('anchor').text
        html = anchorfile + '#' + anchor
        function_list.append((html, name))

    # add special documentation for argument object factories
    for k in ['srcImageRange', 'srcImage', 'destImageRange', 'destImage', 'maskImage']:
        function_list.append(('group__ImageIterators.html#ImageBasedArgumentObjectFactories', k))
    for k in ['srcMultiArrayRange', 'srcMultiArray', 'destMultiArrayRange', 'destMultiArray']:
        function_list.append(('group__ImageIterators.html#MultiArrayBasedArgumentObjectFactories', k))
    for k in ['srcIterRange', 'srcIter', 'destIterRange', 'destIter', 'maskIter']:
        function_list.append(('group__ImageIterators.html#IteratorBasedArgumentObjectFactories', k))

    unique = set()
    set_add = unique.add
    function_list = [ x for x in function_list if x not in unique and not set_add(x)]
    function_list.sort(key = lambda a: a[1])
    function_list = disambiguateOverloadedFunctions(function_list)
    return function_list

def addHeading(index, initial):
    index = index + '<p><a name="index_' + initial + \
    '"><table class="function_index"><tr><th> ' + initial.upper() + \
    ' </th><td align="right" width="100%">VIGRA_NAVIGATOR_PLACEHOLDER</td></tr></table><p>\n'
    return index

def disambiguateOverloadedFunctions(functionList):
    for i in range(len(functionList)):
        overloaded = False
        functionName = functionList[i][1]
        if i > 0:
            lastFunctionName = functionList[i-1][1]
            if functionName == lastFunctionName:
                overloaded = True
        if i < len(functionList) - 1:
            nextFunctionName = functionList[i+1][1]
            if functionName == nextFunctionName:
                overloaded = True
        if overloaded:
            # disambiguate overloaded functions by their group or namespace
            link = functionList[i][0]
            group = re.sub(r'(group__|namespacevigra_1_1)([^\.]+)\.html.*', r'\2', link)
        else:
            group = ""
        functionList[i] = functionList[i] + (group,)

    return functionList


def generateFunctionIndex(functionList):
    index = ""
    initials = []
    for i in range(len(functionList)):
        functionName = functionList[i][1]
        link = functionList[i][0]
        initial = functionName[0]
        if i > 0:
            lastInitial = functionList[i-1][1][0]
            if initial != lastInitial:
                initials.append(initial)
                index = addHeading(index, initial)
        else:
            initials.append(initial)
            index = addHeading(index, initial)

        index = index + '<a href="'+ link + '">' + functionName + '</a>()'
        overloadDisambiguation = functionList[i][2]
        if overloadDisambiguation != "":
            index = index + ' [' + overloadDisambiguation + ']'
        index = index + '<br>\n'

    navigator = '['
    for i in range(len(initials)):
        initial = initials[i]
        if i != 0:
            navigator = navigator + '|'
        navigator = navigator + ' <a href="#index_' + initial + '">' + initial.upper() + '</a> '
    navigator = navigator + ']'
    index = re.sub('VIGRA_NAVIGATOR_PLACEHOLDER', navigator, index)

    # use file "/namespaces.html" as boiler plate for "/functionindex.html"
    text = open(path + "/namespaces.html").read()
    if text.find('</h1>') > -1: # up to doxygen 1.7.1
        header = text[:text.find('</h1>')+5]
    else: # for doxygen 1.7.4 to 1.7.6.1
        header = text[:re.search(r'<div class="title">[^<]*</div>\s*</div>\s*</div>(?:<!--header-->)?\n<div class="contents">',text).end()]
    footer = re.search(r'(?s)(<!-- footer.html -->.*)', text).group(1)

    text = re.sub(r'Namespace List', r'Function Index', header)
    text = text + '\n<p><hr>\n'
    text = text + index
    text = text + footer

    open(path + "/functionindex.html", 'w+').write(text)


classList = getClassList()
functionList = getFunctionList()
generateFunctionIndex(functionList)

# Export class and function list to c_api_replaces.txt for
# crosslinking of vigranumpy documentation.
# Note that '::' are not allowed in reStructuedText link names,
# so we have to use '.' instead.
try:
    replaces=open("../../vigranumpy/docsrc/c_api_replaces.txt","w")
except IOError:
    print("Cannot open the 'c_api_replaces.txt' file, skipping.")
else:
    lowercase_names = set()
    for i in range(len(functionList)):
        functionName = functionList[i][1]
        overloadDisambiguation = functionList[i][2]
        if i > 0 and functionName == functionList[i-1][1] and \
                    overloadDisambiguation == functionList[i-1][2]:
            continue
        if overloadDisambiguation != "":
            functionName = overloadDisambiguation +'.' + functionName
        link = functionList[i][0]
        replaces.write(functionName+":"+link+"\n")
        lowercase_names.add(functionName.lower())
    for i in range(len(classList)):
        className = classList[i][1]
        namespace = classList[i][2]
        if (i > 0 and className == classList[i-1][1]) or \
        (i < len(classList)-1 and className == classList[i+1][1]):
            namespace = namespace.replace('::', '.')
            className = namespace +'.' + className
        if className.lower() in lowercase_names:
            className = className + 'Class'
        link = classList[i][0]
        replaces.write(className+":"+link+"\n")
    replaces.close()