4848from index_transform import IndexTransform
4949from 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-
6151def get_rel_name (full_name ):
6252 pos = full_name .rfind ("::" )
6353 return full_name [pos + 2 :]
6454
65-
6655def 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
113102class 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 ()
0 commit comments