src/eric7/DocumentationTools/IndexGenerator.py

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

eric ide

mercurial