12 |
12 |
13 from . import TemplatesListsStyle |
13 from . import TemplatesListsStyle |
14 from . import TemplatesListsStyleCSS |
14 from . import TemplatesListsStyleCSS |
15 |
15 |
16 from Utilities import joinext |
16 from Utilities import joinext |
|
17 |
17 |
18 |
18 class IndexGenerator(object): |
19 class IndexGenerator(object): |
19 """ |
20 """ |
20 Class implementing the index generator for the builtin documentation generator. |
21 Class implementing the index generator for the builtin documentation generator. |
21 """ |
22 """ |
22 def __init__(self, outputDir, colors, stylesheet = None): |
23 def __init__(self, outputDir, colors, stylesheet=None): |
23 """ |
24 """ |
24 Constructor |
25 Constructor |
25 |
26 |
26 @param outputDir The output directory for the files. (string) |
27 @param outputDir The output directory for the files. (string) |
27 @param colors Dictionary specifying the various colors for the output. |
28 @param colors Dictionary specifying the various colors for the output. |
28 (dictionary of strings) |
29 (dictionary of strings) |
29 @param stylesheet the style to be used for the generated pages (string) |
30 @param stylesheet the style to be used for the generated pages (string) |
30 """ |
31 """ |
31 self.outputDir = outputDir |
32 self.outputDir = outputDir |
32 self.packages = { |
33 self.packages = { |
33 "00index" : { |
34 "00index": { |
34 "description" : "", |
35 "description": "", |
35 "subpackages" : {}, |
36 "subpackages": {}, |
36 "modules" : {} |
37 "modules": {} |
37 } |
38 } |
38 } |
39 } |
39 self.remembered = False |
40 self.remembered = False |
40 |
41 |
41 self.stylesheet = stylesheet |
42 self.stylesheet = stylesheet |
64 def remember(self, file, moduleDocument, basename=""): |
65 def remember(self, file, moduleDocument, basename=""): |
65 """ |
66 """ |
66 Public method to remember a documentation file. |
67 Public method to remember a documentation file. |
67 |
68 |
68 @param file The filename to be remembered. (string) |
69 @param file The filename to be remembered. (string) |
69 @param moduleDocument The ModuleDocument object containing the |
70 @param moduleDocument The ModuleDocument object containing the |
70 information for the file. |
71 information for the file. |
71 @param basename The basename of the file hierarchy to be documented. |
72 @param basename The basename of the file hierarchy to be documented. |
72 The basename is stripped off the filename if it starts with |
73 The basename is stripped off the filename if it starts with |
73 the basename. |
74 the basename. |
74 """ |
75 """ |
89 elt = self.packages["00index"] |
90 elt = self.packages["00index"] |
90 package = dir.replace(os.sep, ".") |
91 package = dir.replace(os.sep, ".") |
91 elt["subpackages"][package] = moduleDocument.shortDescription() |
92 elt["subpackages"][package] = moduleDocument.shortDescription() |
92 |
93 |
93 self.packages[package] = { |
94 self.packages[package] = { |
94 "description" : moduleDocument.description(), |
95 "description": moduleDocument.description(), |
95 "subpackages" : {}, |
96 "subpackages": {}, |
96 "modules" : {} |
97 "modules": {} |
97 } |
98 } |
98 |
99 |
99 if moduleDocument.isEmpty(): |
100 if moduleDocument.isEmpty(): |
100 return |
101 return |
101 |
102 |
105 except KeyError: |
106 except KeyError: |
106 elt = self.packages["00index"] |
107 elt = self.packages["00index"] |
107 elt["modules"][moduleDocument.name()] = \ |
108 elt["modules"][moduleDocument.name()] = \ |
108 moduleDocument.shortDescription() |
109 moduleDocument.shortDescription() |
109 |
110 |
110 def __writeIndex(self, packagename, package, newline = None): |
111 def __writeIndex(self, packagename, package, newline=None): |
111 """ |
112 """ |
112 Private method to generate an index file for a package. |
113 Private method to generate an index file for a package. |
113 |
114 |
114 @param packagename The name of the package. (string) |
115 @param packagename The name of the package. (string) |
115 @param package A dictionary with information about the package. |
116 @param package A dictionary with information about the package. |
134 names = sorted(list(subpacks.keys())) |
135 names = sorted(list(subpacks.keys())) |
135 lst = [] |
136 lst = [] |
136 for name in names: |
137 for name in names: |
137 link = joinext("index-{0}".format(name), ".html") |
138 link = joinext("index-{0}".format(name), ".html") |
138 lst.append(self.indexListEntryTemplate.format(**{ |
139 lst.append(self.indexListEntryTemplate.format(**{ |
139 "Description" : subpacks[name], |
140 "Description": subpacks[name], |
140 "Name" : name.split(".")[-1], |
141 "Name": name.split(".")[-1], |
141 "Link" : link, |
142 "Link": link, |
142 })) |
143 })) |
143 subpackages = self.indexListPackagesTemplate.format(**{ |
144 subpackages = self.indexListPackagesTemplate.format(**{ |
144 "Entries" : "".join(lst), |
145 "Entries": "".join(lst), |
145 }) |
146 }) |
146 |
147 |
147 # 2) modules |
148 # 2) modules |
148 if package["modules"]: |
149 if package["modules"]: |
149 mods = package["modules"] |
150 mods = package["modules"] |
153 link = joinext(name, ".html") |
154 link = joinext(name, ".html") |
154 nam = name.split(".")[-1] |
155 nam = name.split(".")[-1] |
155 if nam == "__init__": |
156 if nam == "__init__": |
156 nam = name.split(".")[-2] |
157 nam = name.split(".")[-2] |
157 lst.append(self.indexListEntryTemplate.format(**{ |
158 lst.append(self.indexListEntryTemplate.format(**{ |
158 "Description" : mods[name], |
159 "Description": mods[name], |
159 "Name" : nam, |
160 "Name": nam, |
160 "Link" : link, |
161 "Link": link, |
161 })) |
162 })) |
162 modules = self.indexListModulesTemplate.format(**{ |
163 modules = self.indexListModulesTemplate.format(**{ |
163 "Entries" : "".join(lst), |
164 "Entries": "".join(lst), |
164 }) |
165 }) |
165 |
166 |
166 doc = self.headerTemplate.format(**{ \ |
167 doc = self.headerTemplate.format(**{ \ |
167 "Title" : title, |
168 "Title": title, |
168 "Style" : self.stylesheet}) + \ |
169 "Style": self.stylesheet}) + \ |
169 self.indexBodyTemplate.format(**{ \ |
170 self.indexBodyTemplate.format(**{ \ |
170 "Title" : title, |
171 "Title": title, |
171 "Description" : package["description"], |
172 "Description": package["description"], |
172 "Subpackages" : subpackages, |
173 "Subpackages": subpackages, |
173 "Modules" : modules, |
174 "Modules": modules, |
174 }) + \ |
175 }) + \ |
175 self.footerTemplate |
176 self.footerTemplate |
176 |
177 |
177 f = open(filename, "w", encoding = "utf-8", newline = newline) |
178 f = open(filename, "w", encoding="utf-8", newline=newline) |
178 f.write(doc) |
179 f.write(doc) |
179 f.close() |
180 f.close() |
180 |
181 |
181 return filename |
182 return filename |
182 |
183 |
183 def writeIndices(self, basename = "", newline = None): |
184 def writeIndices(self, basename="", newline=None): |
184 """ |
185 """ |
185 Public method to generate all index files. |
186 Public method to generate all index files. |
186 |
187 |
187 @param basename The basename of the file hierarchy to be documented. |
188 @param basename The basename of the file hierarchy to be documented. |
188 The basename is stripped off the filename if it starts with |
189 The basename is stripped off the filename if it starts with |
198 if not basename.endswith("."): |
199 if not basename.endswith("."): |
199 basename = "{0}.".format(basename) |
200 basename = "{0}.".format(basename) |
200 for package, element in list(self.packages.items()): |
201 for package, element in list(self.packages.items()): |
201 try: |
202 try: |
202 if basename: |
203 if basename: |
203 package = package.replace(basename,"") |
204 package = package.replace(basename, "") |
204 out = self.__writeIndex(package, element, newline) |
205 out = self.__writeIndex(package, element, newline) |
205 except IOError as v: |
206 except IOError as v: |
206 sys.stderr.write("{0} error: {1}\n".format(package, v[1])) |
207 sys.stderr.write("{0} error: {1}\n".format(package, v[1])) |
207 else: |
208 else: |
208 if out: |
209 if out: |