-
Notifications
You must be signed in to change notification settings - Fork 29
/
quickref.py
242 lines (217 loc) · 9.89 KB
/
quickref.py
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
# -*- coding: utf-8 -*-
# This script generates the misc/quickref.html file
# by Andres Cabrera June 2006
# Licensed under the GPL licence version 3 or later
# modification for empty arg in command and links on opcodes by Francois Pinot February 2007
from __future__ import print_function
from xml.dom import minidom
import os, glob, sys
# categories holds the list of valid categories for opcodes
from categories import categories
XO = False
opcodelist = []
if sys.argv.count("-x"):
XO = True
file = open("opcode_listXO.txt", 'r')
removedopcodes = file.read().split()
file.close()
entries = []
example_cats = []
for i in categories:
entries.append([])
example_cats.append([])
# Generate a file listing all examples
outfilename = 'misc/examples.xml'
examples = open(outfilename,'w')
examples.write("\n<!-- Don't modify this file. It is generated automatically by quickref.py-->\n")
examples.write('''<appendix id="MiscExamples">
<title>List of examples</title>''')
files = glob.glob('opcodes/*.xml')
try:
files.remove('opcodes/template.xml')
except:
pass
files.sort()
for i,filename in enumerate(files):
entry = ''
#print(filename)
source = open(filename, 'r')
text = source.read()
source.close()
cat_text = text[text.find("<refentryinfo><title>") + 21: text.find("</title></refentryinfo>")]
if categories.count(cat_text) > 0:
cat = categories.index(cat_text);
#print(cat_text, cat)
while text.find("examples-xml/") != -1:
file_to_add = text[text.find("examples-xml/"):text.find(".csd",text.find("examples-xml/") )+ 4]
if len(file_to_add) < 45: #yes, very flaky, I know...
entries[cat].append(file_to_add)
else:
print(file_to_add)
text = text[text.find("examples-xml/") + 10:]
for i,catname in enumerate(categories):
print(catname)
if len(entries[i]) > 0:
title ="<formalpara><title>"
title += catname
#title += "</title><simpara />\n"
title += "</title><simpara />\n"
examples.write(title)
for j,ex in enumerate(entries[i]):
if ex != "":
line = '<para><ulink url="'
line += ex.replace("-xml", '') + '"><citetitle>' + ex.replace("examples-xml/",'')
print(line)
#line += "</citetitle></ulink></para><simpara />\n"
line += "</citetitle></ulink></para><simpara />\n"
examples.write(line)
examples.write("</formalpara>\n")
examples.write('</appendix>\n')
examples.close()
#Now generate the quickref file
#Reset entries
entries = []
for i in categories:
entries.append([])
outfilename = 'misc/quickref.xml' if not XO else 'misc/quickrefXO.xml'
quickref = open(outfilename,'w')
quickref.write("\n<!-- Don't modify this file. It is generated automatically by quickref.py-->\n")
quickref.write('''<part>
<title>Opcode Quick Reference</title>
<chapter id="MiscQuickref">
<title>Opcode Quick Reference</title>''')
manualfilename = 'manual.xml' if not XO else 'manualXO.xml'
manual = open(manualfilename, 'r')
text = manual.read()
manual.close()
files = glob.glob('opcodes/*.xml')
try:
files.remove('opcodes/template.xml')
except:
pass
files[len(files):]=glob.glob('opcodes/*/*.xml')
files[len(files):]=glob.glob('vectorial/*.xml')
files[len(files):]=glob.glob('utility/*.xml')
try:
files.remove('utility/mkdb.xml')
except:
pass
files.sort()
headerText = text[0:text.find('<book id="index"')]
for i,filename in enumerate(files):
entry = ''
#print(file)
source = open(filename, 'r')
newfile = source.read()
source.close()
refStart = newfile.find("<refentry")
if(refStart < 0):
continue
elif(refStart > 0):
# print('Trimming file: ', filename, ' ', refStart)
newfile = newfile[refStart:]
# Necessary to define entities
newfile = headerText + newfile
#print(text)
try:
xmldoc = minidom.parseString(newfile)
except:
print('>>> Failed to parse:', filename)
continue
xmldocId = xmldoc.documentElement.getAttribute('id')
# Some files need special treatment (adds, dollar, divides, modulus, multiplies,
# opbitor, opor, raises, subtracts, assign, ifdef, ifndef, define, include, undef)
# There must be a better way to avoid loosing the entities when parsing the XML
# file. Anyone???
if (filename == 'opcodes' + os.sep + 'adds.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'+</link> b (no rate restriction)</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'dollar.xml'):
entry = '<synopsis><link linkend="'+xmldocId+'">'+'$NAME</link> </synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'divides.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'/</link> b (no rate restriction)</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'modulus.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'%</link> b (no rate restriction)</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'multiplies.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'*</link> b (no rate restriction)</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'opbitor.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'|</link> b (bitwise OR)</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'opor.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'||</link> b (logical OR; not audio-rate)</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'raises.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'ˆ</link> b (b not audio-rate)</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'subtracts.xml'):
entry = '<synopsis>a <link linkend="'+xmldocId+'">'+'−</link> b (no rate restriction)</synopsis>\n<para/>'
# elif (filename == 'opcodes' + os.sep + 'assign.xml'):
# entry = '''<synopsis> <link linkend="'+xmldocId+'">'+'−</link>a (no rate restriction)</synopsis>
# <synopsis> <link linkend="'+xmldocId+'">'+'+</link>a (no rate restriction)</synopsis>\n'''
elif (filename == 'opcodes' + os.sep + 'ifdef.xml'):
entry = '<synopsis><link linkend="'+xmldocId+'">'+'#ifdef</link> NAME</synopsis><synopsis> ....</synopsis>' + \
'<synopsis><link linkend="'+xmldocId+'">'+'#else</link> </synopsis><synopsis> ....</synopsis>' + \
'<synopsis><link linkend="'+xmldocId+'">'+'#end</link> </synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'ifndef.xml'):
entry='<synopsis><link linkend="'+xmldocId+'">'+'#ifndef</link> NAME</synopsis><synopsis> ....</synopsis>' + \
'<synopsis><link linkend="'+xmldocId+'">'+'#else</link> </synopsis><synopsis> ....</synopsis>' + \
'<synopsis><link linkend="'+xmldocId+'">'+'#end</link> </synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'define.xml'):
entry='<synopsis><link linkend="'+xmldocId+'">'+'#define</link> NAME # replacement text #</synopsis><para/>\n' + \
'<synopsis><link linkend="'+xmldocId+'">'+'#define</link> NAME(a' b' c') # replacement text #</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'include.xml'):
entry='<synopsis><link linkend="'+xmldocId+'">'+'#include</link> "filename"</synopsis>\n<para/>'
elif (filename == 'opcodes' + os.sep + 'undef.xml'):
entry='<synopsis><link linkend="'+xmldocId+'">'+'#undef</link> NAME</synopsis>\n<para/>'
else:
synopsis = xmldoc.getElementsByTagName('synopsis')
if (len(synopsis) != 0):
# There can be more than 1 synopsis per file
for num in range(len(synopsis)):
tmp = synopsis[num].toxml()
if XO:
opcodename = tmp[tmp.find('<command>') + 9:tmp.find('</command>')]
else:
opcodename = ""
if XO and removedopcodes.count(opcodename) == 0:
print("Removed ----------------------", opcodename)
else:
if tmp[-21:] == "</command></synopsis>": # no arg, insert nbsp
tmp = tmp[:-11] + " </synopsis>"
tmp = tmp.replace('<command>', '<link linkend="' + xmldocId + '">')
entry += tmp.replace('</command>', '</link>')
if entry != '':
entry += '<para/>'
else:
#print("no synopsis tag for file: " + file)
entry = ''
#print("Entry ------ ", entry)
info = xmldoc.getElementsByTagName('refentryinfo')
if (len(info)!=0 and entry != ''):
category = info[0].toxml()
category = category[21:-23]
print(category)
else:
print("no refentryinfo tag for file " + filename)
category = "Miscellaneous"
if (entry!=''):
print(filename + " sent to Miscellaneous")
#print(category)
match = False
for j, thiscategory in enumerate(categories):
if (category == thiscategory):
entries[j].append(entry+ '\n')
match = True
if match == False:
print(filename + "WARNING! No Category Match!")
for i in range(len(categories)):
if (len(entries[i])==0):
print("No entries for category: "+categories[i]+"...Skipping")
continue
quickref.write("<para></para><formalpara>\n")
quickref.write("<title>"+ categories[i] + "</title>\n<para>\n<para/>\n")
count = 0
for j in range(len(entries[i])):
quickref.write(entries[i].pop(0)) # + '\n')
count += 1
quickref.write("</para></formalpara>\n<para></para>")
print(str(count) + " entries in category: " + categories[i])
quickref.write('</chapter></part>\n')
quickref.close()
print(entries)