|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the index generator for the builtin documentation |
|
8 generator. |
|
9 """ |
|
10 |
|
11 import sys |
|
12 import os |
|
13 |
|
14 from Utilities import joinext |
|
15 |
|
16 |
|
17 class IndexGenerator: |
|
18 """ |
|
19 Class implementing the index generator for the builtin documentation |
|
20 generator. |
|
21 """ |
|
22 def __init__(self, outputDir, colors, stylesheet=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param outputDir The output directory for the files. (string) |
|
27 @param colors Dictionary specifying the various colors for the output. |
|
28 (dictionary of strings) |
|
29 @param stylesheet the style to be used for the generated pages (string) |
|
30 """ |
|
31 self.outputDir = outputDir |
|
32 self.packages = { |
|
33 "00index": { |
|
34 "description": "", |
|
35 "subpackages": {}, |
|
36 "modules": {} |
|
37 } |
|
38 } |
|
39 self.remembered = False |
|
40 |
|
41 self.stylesheet = stylesheet |
|
42 |
|
43 if self.stylesheet: |
|
44 from . import TemplatesListsStyleCSS |
|
45 self.headerTemplate = TemplatesListsStyleCSS.headerTemplate |
|
46 self.footerTemplate = TemplatesListsStyleCSS.footerTemplate |
|
47 self.indexBodyTemplate = TemplatesListsStyleCSS.indexBodyTemplate |
|
48 self.indexListPackagesTemplate = ( |
|
49 TemplatesListsStyleCSS.indexListPackagesTemplate) |
|
50 self.indexListModulesTemplate = ( |
|
51 TemplatesListsStyleCSS.indexListModulesTemplate) |
|
52 self.indexListEntryTemplate = ( |
|
53 TemplatesListsStyleCSS.indexListEntryTemplate) |
|
54 else: |
|
55 from . import TemplatesListsStyle |
|
56 self.headerTemplate = ( |
|
57 TemplatesListsStyle.headerTemplate.format(**colors)) |
|
58 self.footerTemplate = ( |
|
59 TemplatesListsStyle.footerTemplate.format(**colors)) |
|
60 self.indexBodyTemplate = ( |
|
61 TemplatesListsStyle.indexBodyTemplate.format(**colors)) |
|
62 self.indexListPackagesTemplate = ( |
|
63 TemplatesListsStyle.indexListPackagesTemplate.format(**colors)) |
|
64 self.indexListModulesTemplate = ( |
|
65 TemplatesListsStyle.indexListModulesTemplate.format(**colors)) |
|
66 self.indexListEntryTemplate = ( |
|
67 TemplatesListsStyle.indexListEntryTemplate.format(**colors)) |
|
68 |
|
69 def remember(self, file, moduleDocument, basename=""): |
|
70 """ |
|
71 Public method to remember a documentation file. |
|
72 |
|
73 @param file The filename to be remembered. (string) |
|
74 @param moduleDocument The ModuleDocument object containing the |
|
75 information for the file. |
|
76 @param basename The basename of the file hierarchy to be documented. |
|
77 The basename is stripped off the filename if it starts with |
|
78 the basename. |
|
79 """ |
|
80 self.remembered = True |
|
81 if basename: |
|
82 file = file.replace(basename, "") |
|
83 |
|
84 if "__init__" in file: |
|
85 dirName = os.path.dirname(file) |
|
86 udir = os.path.dirname(dirName) |
|
87 if udir: |
|
88 upackage = udir.replace(os.sep, ".") |
|
89 try: |
|
90 elt = self.packages[upackage] |
|
91 except KeyError: |
|
92 elt = self.packages["00index"] |
|
93 else: |
|
94 elt = self.packages["00index"] |
|
95 package = dirName.replace(os.sep, ".") |
|
96 elt["subpackages"][package] = moduleDocument.shortDescription() |
|
97 |
|
98 self.packages[package] = { |
|
99 "description": moduleDocument.description(), |
|
100 "subpackages": {}, |
|
101 "modules": {} |
|
102 } |
|
103 |
|
104 if moduleDocument.isEmpty(): |
|
105 return |
|
106 |
|
107 package = os.path.dirname(file).replace(os.sep, ".") |
|
108 try: |
|
109 elt = self.packages[package] |
|
110 except KeyError: |
|
111 elt = self.packages["00index"] |
|
112 elt["modules"][moduleDocument.name()] = ( |
|
113 moduleDocument.shortDescription()) |
|
114 |
|
115 def __writeIndex(self, packagename, package, newline=None): |
|
116 """ |
|
117 Private method to generate an index file for a package. |
|
118 |
|
119 @param packagename The name of the package. (string) |
|
120 @param package A dictionary with information about the package. |
|
121 @param newline newline character to be used (string) |
|
122 @return The name of the generated index file. |
|
123 """ |
|
124 if packagename == "00index": |
|
125 f = os.path.join(self.outputDir, "index") |
|
126 title = "Table of contents" |
|
127 else: |
|
128 f = os.path.join(self.outputDir, "index-{0}".format(packagename)) |
|
129 title = packagename |
|
130 |
|
131 filename = joinext(f, ".html") |
|
132 |
|
133 subpackages = "" |
|
134 modules = "" |
|
135 |
|
136 # 1) subpackages |
|
137 if package["subpackages"]: |
|
138 subpacks = package["subpackages"] |
|
139 names = sorted(list(subpacks.keys())) |
|
140 lst = [] |
|
141 for name in names: |
|
142 link = joinext("index-{0}".format(name), ".html") |
|
143 lst.append(self.indexListEntryTemplate.format(**{ |
|
144 "Description": subpacks[name], |
|
145 "Name": name.split(".")[-1], |
|
146 "Link": link, |
|
147 })) |
|
148 subpackages = self.indexListPackagesTemplate.format(**{ |
|
149 "Entries": "".join(lst), |
|
150 }) |
|
151 |
|
152 # 2) modules |
|
153 if package["modules"]: |
|
154 mods = package["modules"] |
|
155 names = sorted(list(mods.keys())) |
|
156 lst = [] |
|
157 for name in names: |
|
158 link = joinext(name, ".html") |
|
159 nam = name.split(".")[-1] |
|
160 if nam == "__init__": |
|
161 nam = name.split(".")[-2] |
|
162 lst.append(self.indexListEntryTemplate.format(**{ |
|
163 "Description": mods[name], |
|
164 "Name": nam, |
|
165 "Link": link, |
|
166 })) |
|
167 modules = self.indexListModulesTemplate.format(**{ |
|
168 "Entries": "".join(lst), |
|
169 }) |
|
170 |
|
171 doc = ( |
|
172 self.headerTemplate.format( |
|
173 **{"Title": title, |
|
174 "Style": self.stylesheet} |
|
175 ) + self.indexBodyTemplate.format( |
|
176 **{"Title": title, |
|
177 "Description": package["description"], |
|
178 "Subpackages": subpackages, |
|
179 "Modules": modules} |
|
180 ) + self.footerTemplate |
|
181 ) |
|
182 |
|
183 with open(filename, "w", encoding="utf-8", newline=newline) as f: |
|
184 f.write(doc) |
|
185 |
|
186 return filename |
|
187 |
|
188 def writeIndices(self, basename="", newline=None): |
|
189 """ |
|
190 Public method to generate all index files. |
|
191 |
|
192 @param basename The basename of the file hierarchy to be documented. |
|
193 The basename is stripped off the filename if it starts with |
|
194 the basename. |
|
195 @param newline newline character to be used (string) |
|
196 """ |
|
197 if not self.remembered: |
|
198 sys.stderr.write("No index to generate.\n") |
|
199 return |
|
200 |
|
201 if basename: |
|
202 basename = basename.replace(os.sep, ".") |
|
203 if not basename.endswith("."): |
|
204 basename = "{0}.".format(basename) |
|
205 for package, element in list(self.packages.items()): |
|
206 try: |
|
207 if basename: |
|
208 package = package.replace(basename, "") |
|
209 out = self.__writeIndex(package, element, newline) |
|
210 except OSError as v: |
|
211 sys.stderr.write("{0} error: {1}\n".format(package, v[1])) |
|
212 else: |
|
213 if out: |
|
214 sys.stdout.write("{0} ok\n".format(out)) |
|
215 |
|
216 sys.stdout.write("Indices written.\n") |
|
217 sys.stdout.flush() |
|
218 sys.stderr.flush() |