File: build_contents.py

package info (click to toggle)
openclonk 8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 169,520 kB
  • sloc: cpp: 180,479; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 145; sh: 101; javascript: 34
file content (199 lines) | stat: -rw-r--r-- 7,575 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
import xml.sax
from xml.sax.saxutils import escape, quoteattr
import experimental

class ClonkEntityResolver(xml.sax.handler.EntityResolver):
    def resolveEntity(self, publicId, systemId):
        s = 'file:sdk/script/fn/' + systemId
        return s
    def setrelpath(self, path):
        dirlist = path.split('/')
        self.relpath = ''
        for d in dirlist[1:]:
            self.relpath = self.relpath + '../'

class Clonkparser(xml.sax.handler.ContentHandler):
    def __init__(self):
        self.cats = { }
        self.subcats = { }
        self.versions = { }
        self.extversions = { }
        self.funcs = { }
        self.files = { }
    def setfilename(self, filename):
        self.filename = filename
        self.htmlfilename = filename[:-3] + 'html'
    def _setcurcat(self, curcat):
        self.curcat = curcat
        if self.curcat not in self.cats:
            self.cats[self.curcat] = { }
            self.subcats[self.curcat] = { }
    def _addToIndex(self, title, href):
        if not title in self.files:
            self.files[title] = { self.title: href }
        else:
            if self.title in self.files[title]:
                print "WARNING: duplicate " + title + " in " + href + " and " + self.files[title][self.title]
            self.files[title][self.title] = href
    def startDocument(self):
        self.cur = ""
        self.curcat = ""
        self.title = ""
        self.func = 0
        self.state = None
        self.id = None
        self.idTitle = None
        self.idStackdepth = 0
        self.Stackdepth = 0
    def startElement(self, name, attr):
        # subcat inside category?
        if self.state == 'category' and self.cur != "":
            self._setcurcat(self.cur)
        # is func
        if name == 'funcs':
            self.func = 1
        self.cur = unicode("")
        self.state = name
        self.Stackdepth = self.Stackdepth + 1
        if 'id' in attr:
            self.id = attr["id"]
            self.idTitle = unicode("")
            self.idStackdepth = self.Stackdepth
    def characters(self, content):
        if self.state in ['category', 'subcat', 'version', 'extversion', 'title', 'funcs']:
            self.cur += content
        if self.id:
            self.idTitle += content
    def endElement(self, name):
        self.cur = self.cur.strip()
        if name == 'category':
            if self.cur != "":
                self._setcurcat(self.cur)
                if self.title == "":
                    print "WARNING: category before title in " + self.filename
                self.cats[self.curcat][self.title] = self.htmlfilename
            else:
                print "WARNING: possibly broken category in " + self.filename
        elif name == 'subcat':
            if self.curcat != "" and self.cur != "":
                self.cats[self.curcat].pop(self.title, None)
                if self.cur not in self.subcats[self.curcat]:
                    self.subcats[self.curcat][self.cur] = { }
                self.subcats[self.curcat][self.cur][self.title] = self.htmlfilename
            else:
                print "WARNING: possibly broken subcategory in " + self.filename
        elif name == 'version':
            self.cur = self.cur.upper()
            if self.cur != "":
                if self.cur not in self.extversions:
                    self.extversions[self.cur] = { }
                if self.cur not in self.versions:
                    self.versions[self.cur] = { }
                self.versions[self.cur][self.title] = self.htmlfilename
        elif name == 'extversion':
            self.cur = self.cur.upper()
            if self.cur != "":
                if self.cur not in self.extversions:
                    self.extversions[self.cur] = { }
                if self.cur not in self.versions:
                    self.versions[self.cur] = { }
                self.extversions[self.cur][self.title] = self.htmlfilename
        elif name == 'funcs':
            self.func = 0
        elif name == 'title':
            self.title = self.cur
            if self.func == 1:
                self._addToIndex(self.title, self.htmlfilename + '#' + self.title.encode('utf-8'))
                self.funcs[self.title] = self.htmlfilename
            else:
                self._addToIndex(self.title, self.htmlfilename)

        self.Stackdepth = self.Stackdepth - 1
        if self.id and (self.idStackdepth > self.Stackdepth or name == 'col' or name == 'literal_col'):
            title = self.idTitle.strip()
            href = self.htmlfilename + '#' + self.id.encode('utf-8')
            if title == "":
                print "WARNING: id " + self.id.encode('utf-8') + " without text content in " + self.filename
            else:
                self._addToIndex(title, href)
            self.id = None
            self.idTitle = None
        self.cur = ""
        self.state = None

def printfunctions(f, _):
    def folder(name):
        f.write("<li>" + escape(name) + "\n<ul>\n")
    def sheet(url, name):
        f.write("<li><emlink href=" + quoteattr(url[4:]) + ">" + escape(name) + "</emlink></li>\n")
    def sheetE(url, name):
        f.write("<li><emlink href=" + quoteattr(url[4:]) + ">" + escape(name) + "</emlink> (extended)</li>\n")
    folder("Functions by Category")
    cats = parser.cats.keys()
    cats.sort()
    for cat in cats:
        folder(_(cat))
        subcats = parser.subcats[cat].keys()
        subcats.sort()
        for subcat in subcats:
            folder(_(subcat))
            titles = parser.subcats[cat][subcat].keys()
            titles.sort()
            for title in titles:
                sheet(parser.subcats[cat][subcat][title] + '#' + _(title), _(title))
            f.write('</ul></li>\n')
        titles = parser.cats[cat].keys()
        titles.sort()
        for title in titles:
            sheet(parser.cats[cat][title] + '#' + _(title), _(title))
        f.write('</ul></li>\n')
    f.write('</ul></li>\n')

def printindex(f, _):
    def folder(name):
        f.write("<li class='index'>" + escape(name) + "\n<ul>\n")
    def sheet(url, name):
        f.write("<li><emlink href=" + quoteattr(url[4:]) + ">" + escape(name) + "</emlink></li>\n")
    folder("Index")
    titles = parser.files.keys()
    titles.sort(key=unicode.lower)
    for title in titles:
        ctitles = parser.files[title].keys()
        ctitles.sort(key=unicode.lower)
        if len(ctitles) == 1:
            sheet(parser.files[title][ctitles[0]], _(title))
        else:
            for ctitle in ctitles:
                sheet(parser.files[title][ctitle], _(title + " (" + ctitle + ")"))
    f.write('</ul></li>\n')

parser = Clonkparser()
reader = xml.sax.make_parser()
reader.setContentHandler(parser)
reader.setEntityResolver(ClonkEntityResolver())
for filename in sys.argv[1:]:
    reader.getEntityResolver().setrelpath(filename)
    parser.setfilename(filename)
    reader.parse(filename)

if 0:
    reader.setContentHandler(experimental.ExperimentParser())
    for filename in sys.argv[1:]:
        reader.getEntityResolver().setrelpath(filename)
        reader.parse(filename)
    experimental.Result()

_ = lambda s: s.encode('utf-8')
f, fin = (file("sdk/content.xml", "w"), file("sdk/content.xml.in", "r"))
for line in fin:
    if line.find("<!-- Insert Functions here -->") != -1:
        printfunctions(f, _)
    elif line.find("<!-- Insert Index here -->") != -1:
        printindex(f, _)
    else:
        f.write(line)
f.close()
fin.close()