Skip to content

Commit ee1bf7c

Browse files
committed
Transform: Move code in transform scripts to main()
1 parent 45e0d9f commit ee1bf7c

File tree

6 files changed

+181
-157
lines changed

6 files changed

+181
-157
lines changed

index2autolinker.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,10 @@
4848
from index_transform import IndexTransform
4949
from xml_utils import xml_escape
5050

51-
if len(sys.argv) != 3:
52-
print('''Please provide the file name of the index as the first argument
53-
and the file name of the destination as the second ''')
54-
sys.exit(1)
55-
56-
out_f = open(sys.argv[2], 'w', encoding='utf-8')
57-
58-
groups = {}
59-
links = []
60-
6151
def get_rel_name(full_name):
6252
pos = full_name.rfind("::")
6353
return full_name[pos+2:]
6454

65-
6655
def is_group(el):
6756
curr_el = el
6857
while True:
@@ -84,14 +73,14 @@ class Index2AutolinkerGroups(IndexTransform):
8473

8574
def __init__(self):
8675
super(Index2AutolinkerGroups, self).__init__(ignore_typedefs = True)
76+
self.groups = {}
8777
self.curr_group = None
8878

8979
def process_item_hook(self, el, full_name, full_link):
90-
global groups
9180
if is_group(el):
9281
saved_group = self.curr_group
9382

94-
groups[full_name] = {
83+
self.groups[full_name] = {
9584
'name' : full_name,
9685
'base_url' : full_link,
9786
'urls' : ['']
@@ -103,22 +92,22 @@ def process_item_hook(self, el, full_name, full_link):
10392
IndexTransform.process_item_hook(self, el, full_name, full_link)
10493

10594
if is_group(el.getparent()):
106-
base_url = groups[self.curr_group]['base_url']
95+
base_url = self.groups[self.curr_group]['base_url']
10796
if full_link.find(base_url) == 0:
10897
rel_link = full_link[len(base_url):]
109-
if not rel_link in groups[self.curr_group]['urls']:
110-
groups[self.curr_group]['urls'].append(rel_link)
98+
if not rel_link in self.groups[self.curr_group]['urls']:
99+
self.groups[self.curr_group]['urls'].append(rel_link)
111100
# else: an error has occurred somewhere - ignore
112101

113102
class Index2AutolinkerLinks(IndexTransform):
114103

115104
def __init__(self):
116105
super(Index2AutolinkerLinks, self).__init__()
106+
self.links = []
117107
self.curr_group = None
118108

119109
def process_item_hook(self, el, full_name, full_link):
120-
global links
121-
links.append({ 'string' : full_name, 'target' : full_link })
110+
self.links.append({ 'string' : full_name, 'target' : full_link })
122111

123112
if is_group(el):
124113
saved_group = self.curr_group
@@ -130,24 +119,35 @@ def process_item_hook(self, el, full_name, full_link):
130119

131120
if is_group(el.getparent()) and self.curr_group and needs_entry_in_group(el):
132121

133-
links.append({ 'string' : get_rel_name(full_name),
122+
self.links.append({ 'string' : get_rel_name(full_name),
134123
'target' : full_link,
135124
'on_group' : self.curr_group
136125
})
137126

127+
def main():
128+
if len(sys.argv) != 3:
129+
print('''Please provide the file name of the index as the first argument
130+
and the file name of the destination as the second ''')
131+
sys.exit(1)
132+
133+
out_f = open(sys.argv[2], 'w', encoding='utf-8')
138134

135+
tr = Index2AutolinkerGroups()
136+
tr.transform(sys.argv[1])
137+
groups = tr.groups
139138

140-
tr = Index2AutolinkerGroups()
141-
tr.transform(sys.argv[1])
139+
tr = Index2AutolinkerLinks()
140+
tr.transform(sys.argv[1])
141+
links = tr.links
142142

143-
tr = Index2AutolinkerLinks()
144-
tr.transform(sys.argv[1])
143+
json_groups = [ v for v in groups.values() ]
145144

146-
json_groups = [ v for v in groups.values() ]
145+
json_groups = sorted(json_groups, key=lambda x: x['name'])
146+
links = sorted(links, key=lambda x: x['target'])
147147

148-
json_groups = sorted(json_groups, key=lambda x: x['name'])
149-
links = sorted(links, key=lambda x: x['target'])
148+
out_f.write(json.dumps({ 'groups' : json_groups, 'links' : links}, indent=None,
149+
separators=(',\n', ': '), sort_keys=True))
150+
out_f.close()
150151

151-
out_f.write(json.dumps({ 'groups' : json_groups, 'links' : links}, indent=None,
152-
separators=(',\n', ': '), sort_keys=True))
153-
out_f.close()
152+
if __name__ == '__main__':
153+
main()

index2browser.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@
2222
from xml_utils import xml_escape
2323
import sys
2424

25-
if len(sys.argv) != 3:
26-
print ('''Please provide the file name of the index as the first argument
27-
and the file name of the destination as the second ''')
28-
sys.exit(1)
29-
30-
out_f = open(sys.argv[2], 'w', encoding='utf-8')
31-
3225
class Index2Browser(IndexTransform):
3326

27+
def __init__(self, out_file):
28+
super().__init__()
29+
self.out_file = out_file
30+
3431
def output_item(self, el, full_name, full_link):
3532
mark = ''
3633
if el.tag == 'const': mark = '(const)'
@@ -51,12 +48,19 @@ def output_item(self, el, full_name, full_link):
5148
return res
5249

5350
def process_item_hook(self, el, full_name, full_link):
54-
global out_f
55-
out_f.write('<li>' + self.output_item(el, full_name, full_link) + '<ul>')
51+
self.out_file.write('<li>' + self.output_item(el, full_name, full_link) + '<ul>')
5652
IndexTransform.process_item_hook(self, el, full_name, full_link)
57-
out_f.write('</ul></li>\n')
53+
self.out_file.write('</ul></li>\n')
54+
55+
def main():
56+
if len(sys.argv) != 3:
57+
print ('''Please provide the file name of the index as the first argument
58+
and the file name of the destination as the second ''')
59+
sys.exit(1)
5860

59-
out_f.write('''
61+
out_f = open(sys.argv[2], 'w', encoding='utf-8')
62+
63+
out_f.write('''
6064
<html>
6165
<head>
6266
<style type="text/css">
@@ -78,14 +82,19 @@ def process_item_hook(self, el, full_name, full_link):
7882
<ul>
7983
''')
8084

81-
tr = Index2Browser()
82-
tr.transform(sys.argv[1])
85+
tr = Index2Browser(out_f)
86+
tr.transform(sys.argv[1])
8387

84-
out_f.write('''
88+
out_f.write('''
8589
</ul>
8690
</body>
8791
</html>
8892
''')
8993

94+
if __name__ == '__main__':
95+
main()
96+
97+
98+
9099

91100

index2devhelp.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,12 @@
2222
from xml_utils import xml_escape
2323
import sys
2424

25-
if len(sys.argv) != 8:
26-
print ('''Please provide the following 7 arguments:
27-
* a link to the location of the book
28-
* the chapters file to include
29-
* the title of the book
30-
* the name of the package
31-
* the link relative to the root of the documentation
32-
* the file name of the source file
33-
* the file name of the destination file
34-
''')
35-
36-
book_base = sys.argv[1]
37-
chapters_fn = sys.argv[2]
38-
book_title = sys.argv[3]
39-
book_name = sys.argv[4]
40-
rel_link = sys.argv[5]
41-
in_fn = sys.argv[6]
42-
dest_fn = sys.argv[7]
43-
44-
out_f = open(dest_fn, 'w', encoding='utf-8')
45-
46-
4725
class Index2Devhelp(IndexTransform):
4826

27+
def __init__(self, out_file):
28+
super().__init__()
29+
self.out_file = out_file
30+
4931
def get_mark(self, el):
5032
if el.tag == 'const': return 'macro'
5133
elif el.tag == 'function': return 'function'
@@ -61,28 +43,52 @@ def get_mark(self, el):
6143
return ''
6244

6345
def process_item_hook(self, el, full_name, full_link):
64-
global out_f
65-
out_f.write('<keyword type="' + xml_escape(self.get_mark(el))
46+
self.out_file.write('<keyword type="' + xml_escape(self.get_mark(el))
6647
+ '" name="' + xml_escape(full_name)
6748
+ '" link="' + xml_escape(full_link) + '"/>\n')
6849
IndexTransform.process_item_hook(self, el, full_name, full_link)
6950

70-
out_f.write('<?xml version="1.0"?>\n'
71-
+ '<book title="' + xml_escape(book_title)
72-
+ '" xmlns="http://www.devhelp.net/book'
73-
+ '" name="' + xml_escape(book_name)
74-
+ '" base="' + xml_escape(book_base)
75-
+ '" link="' + xml_escape(rel_link)
76-
+ '" version="2" language="c++">\n')
77-
78-
chapters_f = open(chapters_fn, encoding='utf-8')
79-
out_f.write(chapters_f.read() + '\n')
80-
out_f.write('<functions>')
81-
82-
tr = Index2Devhelp()
83-
tr.transform(in_fn)
8451

85-
out_f.write('''
52+
def main():
53+
if len(sys.argv) != 8:
54+
print ('''Please provide the following 7 arguments:
55+
* a link to the location of the book
56+
* the chapters file to include
57+
* the title of the book
58+
* the name of the package
59+
* the link relative to the root of the documentation
60+
* the file name of the source file
61+
* the file name of the destination file
62+
''')
63+
64+
book_base = sys.argv[1]
65+
chapters_fn = sys.argv[2]
66+
book_title = sys.argv[3]
67+
book_name = sys.argv[4]
68+
rel_link = sys.argv[5]
69+
in_fn = sys.argv[6]
70+
dest_fn = sys.argv[7]
71+
72+
out_f = open(dest_fn, 'w', encoding='utf-8')
73+
74+
out_f.write('<?xml version="1.0"?>\n'
75+
+ '<book title="' + xml_escape(book_title)
76+
+ '" xmlns="http://www.devhelp.net/book'
77+
+ '" name="' + xml_escape(book_name)
78+
+ '" base="' + xml_escape(book_base)
79+
+ '" link="' + xml_escape(rel_link)
80+
+ '" version="2" language="c++">\n')
81+
82+
chapters_f = open(chapters_fn, encoding='utf-8')
83+
out_f.write(chapters_f.read() + '\n')
84+
out_f.write('<functions>')
85+
86+
tr = Index2Devhelp(out_f)
87+
tr.transform(in_fn)
88+
89+
out_f.write('''
8690
</functions>
8791
</book>
8892
''')
93+
if __name__ == '__main__':
94+
main()

0 commit comments

Comments
 (0)