-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenIndex.py
More file actions
394 lines (350 loc) · 15.3 KB
/
GenIndex.py
File metadata and controls
394 lines (350 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""
This module generates indices for github pages. Github repo which false in category of code base more than a static site and
we try to deploy such files to github pages then for nested folder either we have to create a index.html or a readme to access these code
bases. This Module help this this particular case.
Classes:
- GenIndex - Configures and generates the index pages.
"""
import traceback
import shutil
import os
import sys
# Path ./DSA-Bootcamp-Java/lectures/
class GenIndex:
"""Class to handle all configuration and to generate The index files
...
Attributes
----------
Common:
parent_dir : str
path to the parent directory of site or repo
type : str
Type of the index file to be generated. This can be HTML and MD(Markdown).Default is HTML.
override : bool
Whether to override or not an existing index file of specified type
debug : bool
Flag to run code in debug mode. In Debug mode add content to be written in index files in genarated_files list.
Which can be observed to get inside.
genarated_files : list
contains content to be written to file in debug mode.
gen_ignore : list
contains list of files to be excluded in generated index pages. It also consider .gitignore and .geignore to
it's content.
Type specific attrs:
HTML attrs:
item_type : str
Specifies the type of item used for each dir or file when type is HTML. It can be LIST or CARD. Default value is LIST.
MD attrs:
num : bool
Flag to tell wether to show a numeric list for item list.
icon : bool
Flag to tell wether to show icon. Icon are some text or emojis shown in the start of item.
folder_icon : str
Text or emojis to be shown before each item list whose type is a file.
file_icon : str
Text or emojis to be shown before each item list whose type is a folder.
Methods
-------
gen_index(path) -> None
Configures the gen_ignore files from root of repo or site. Coppies resource to repo and calls subsequent method according
to type of index.
add_ignored_item(file) -> None
Adds item to gen_ignore list from specified file.
gen_html(path,depth=0) -> None
Generates HTML index pages.
gen_md(path) -> None
Generates MD index pages.
write_to_md_file(path,file,value) -> None
Write generated code for md index pages to md files.
write_to_html_file(path,file,value):
Write generated code for HTML index pages to HTML files.
check_presence(self,file) ->
check for presence of a file in excluded files and folders.
"""
def __init__(self, override=False, item_type="LIST", num=False, icon=False, folder_icon='📁',
file_icon='📄', type="HTML", output_file='index.html', debug=False,item_template="templates/item", page_template="templates/basic.html"
):
"""Set up all configuration parameter
Parameters
----
Common:
type : str
Type of the index file to be generated. This can be HTML and MD(Markdown).Default is HTML.
override : bool
Whether to override or not an existing index file of specified type
debug : bool
Flag to run code in debug mode. In Debug mode add content to be written in index files in genarated_files list.
Which can be observed to get inside.
output_file: str
Name for the output file. This name is used for the file to be generated.
Type specific attrs:
HTML attrs:
item_type : str
Specifies the type of item used for each dir or file when type is HTML. It can be LIST or CARD. Default value is LIST.
item_template : str
If you want to use custom item then this argument specifies the file path of code for that item. which is mainly in Html.
page_template : str
This psecifies the page template to which items are going to be added. i.e A page with header and footer for site and space
place for elements to be placed.
MD attrs:
num : bool
Flag to tell wether to show a numeric list for item list.
icon : bool
Flag to tell wether to show icon. Icon are some text or emojis shown in the start of item.
folder_icon : str
Text or emojis to be shown before each item list whose type is a file.
file_icon : str
Text or emojis to be shown before each item list whose type is a folder.
"""
self.num = num
self.icon = icon
self.folder_icon = folder_icon
self.file_icon = file_icon
self.type = type
self.item_template = item_template
self.page_template = page_template
self.item_type = item_type
self.debug = debug
self.gen_ingore = []
self.override = override
if debug:
self.generated_files = []
if type == "MD":
self.output_file="Readme"
else:
with open(self.item_template, "r", encoding="utf-8") as f:
self.item = f.read()
if self.item_type == "CARD":
self.page_template = "templates/card.html"
with open(self.page_template, "r", encoding="utf-8") as f:
self.html = f.read()
print("Using",self.item_template,"as Item template...")
print("Using",self.page_template,"as page template...")
self.output_file=output_file
def gen_index(self, path):
"""
Configures repo or site directory for resources
"""
self.parent_dir = path
self.add_ignored_item(os.path.join(path, "/.genignore"))
self.add_ignored_item(os.path.join(path, "/.gitignore"))
self.add_ignored_item("./.gitignore")
self.gen_ingore.append("genc")
self.gen_ingore.append("genstatic")
if self.type == "MD":
self.gen_md(path)
else:
if os.path.exists(path+'/genstatic'):
shutil.rmtree(path+'/genstatic')
shutil.copytree('genstatic', path+'/genstatic')
self.gen_html(path)
def add_ignored_item(self, file):
"""
Add ignore files to excluded list.
This method goes throught the list of files in provided file to add them in excluded list.
"""
if os.path.exists(file):
with open(file) as f:
self.gen_ingore.extend(f.readlines())
def check_presence(self, file):
"""
Checks if a file is excluded or not
Parameter
---------
file -> str
file to be checked for presence
"""
import re
for item in self.gen_ingore:
i = item.split("/")[-1]
if re.match(i, file):
return True
return False
def gen_html(self, path, depth=0):
""" Generates html index pages
Recursively goes throught all directory and add index file in them.
Parameters:
path -> str
path of current directory
depth -> str
Depth is used to measure depth of current directory from root directory. It help to build path to refer to static
resources added to repo or site root directory.
"""
dirs = os.listdir(path)
value = ""
c_dirs = os.listdir(os.path.join(path))
for c_dir in c_dirs:
try:
if self.check_presence(c_dir):
continue
c_path = os.path.join(path, c_dir)
if not os.path.isfile(c_path):
ch_dir = c_dir.strip().replace(" ", "%20")
value += self.item.format(
icon=f"./{'../'*depth}genstatic/folder.png", href=ch_dir, title=c_dir)
#value += f"{'1.' if self.num else '- '}{self.folder_icon if self.icon else ''} [{c_dir}](./{ch_dir})\n"
self.gen_html(c_path, depth+1)
else:
if c_dir != self.output_file:
extension = ""
parts = c_dir.split(".")
if len(parts) > 1:
extension = parts[-1]
ch_dir = c_dir.strip().replace(" ", "%20")
if extension in ["java", "yaml", "yml", "py", "php","sh","html","css","js"]:
content_file = os.path.join("genc", parts[0]+".md")
content_path = os.path.join(path, content_file)
genc_path = os.path.join(path, "genc")
if not os.path.exists(genc_path):
os.makedirs(genc_path, exist_ok=True)
ch_dir = os.path.join(
"genc", parts[0]).strip().replace(" ", "%20")
with open(content_path, "w", encoding="utf-8") as f:
with open(os.path.join(path, c_dir), "r", encoding="utf-8",) as inp:
file_content = inp.read()
f.write(
f"```{extension}\n{file_content}\n```")
if extension =="md":
ch_dir=parts[0].strip().replace(" ", "%20")
value += self.item.format(
icon=f"./{'../'*depth}genstatic/file.png", href=ch_dir, title=c_dir)
#value += f"{'1.' if self.num else '- '}{self.file_icon if self.icon else ''} [{c_dir}](./{ch_dir})\n"
except Exception as e:
print(e, e, "Line:240 - Missed something or tried to open list readme.md")
readme = os.path.join(path, self.output_file)
self.write_to_html_file(path, readme, value, depth)
def gen_md(self, path):
"""Generates Markdown index pages
Recursively goes throught all directory and add readme file in them.
Parameters:
path -> str
path of current directory
"""
dirs = os.listdir(path)
try:
value = "## Tables of content\n"
c_dirs = os.listdir(os.path.join(path))
print(c_dirs)
for c_dir in c_dirs:
if self.check_presence(c_dir):
continue
c_path = os.path.join(path, c_dir)
if not os.path.isfile(c_path):
ch_dir = c_dir.strip().replace(" ", "%20")
value += f"{'1.' if self.num else '- '}{self.folder_icon if self.icon else ''} [{c_dir}](./{ch_dir})\n"
self.gen_md(c_path)
else:
ch_dir = c_dir.strip().replace(" ", "%20")
if c_dir != self.output_file:
value += f"{'1.' if self.num else '- '}{self.file_icon if self.icon else ''} [{c_dir}](./{ch_dir})\n"
readme = os.path.join(path, self.output_file)
print(readme)
self.write_to_md_file(path, readme, value)
except Exception as e:
print(e, "Line:273 - Missed something or tried to open list readme.md")
traceback.print_exc()
def write_to_md_file(self, path, file, value):
"""
Write index content to a index file
Parameter
--------
path -> str
path of file
file -> str
file to which the content to be written
value -> str
value to be written in file
"""
if self.override or (not os.path.exists(file)):
with open(file, "w", encoding='utf-8') as f:
if self.debug:
self.generated_files.append(value)
else:
f.write(value)
print("Written:\n", value)
def write_to_html_file(self, path, file, value, depth):
"""
Write index content to a index file
Parameter
--------
path -> str
path of file
file -> str
file to which the content to be written
value -> str
value to be written in file
"""
if self.override or (not os.path.exists(file)):
with open(file, "w", encoding='utf-8') as f:
content = self.html.replace("{body}", value)
content = content.replace(
"{Title}", path.replace(self.parent_dir, ""))
content = content.replace(
"{content_title}", "Table of content")
content = content.replace(
"{root}", "../"*depth)
if self.debug:
self.generated_files.append(content)
else:
f.write(content)
f.close()
print("Generated:", path, file)
# Driver code for module
if __name__ == "__main__":
par = sys.argv[1]
icon = True
num = False
folder_icon = '📁'
file_icon = '📄'
index_type = "HTML"
item_type = "LIST"
over_ride = False
item_template="templates/item"
page_template="templates/basic.html"
output_file=""
if (len(sys.argv) >= 3):
over_ride = False if sys.argv[2].upper() == "FALSE" else True
if (len(sys.argv) >= 4):
index_type = sys.argv[3].upper() if sys.argv[3] else "HTML"
if index_type == "MD":
output_file='README.md'
if (len(sys.argv) >= 5):
icon = False if sys.argv[4].upper() == "FALSE" else True
if (len(sys.argv) >= 6):
num = False if sys.argv[5].upper() == "FALSE" else True
if (len(sys.argv) >= 7):
folder_icon = sys.argv[6] if sys.argv[6] else '📁'
if (len(sys.argv) >= 8):
file_icon = sys.argv[7] if sys.argv[7] else '📄'
if (len(sys.argv) >= 9):
output_file = sys.argv[8] if sys.argv[8] else 'README.md'
elif index_type == "HTML":
output_file='index.html'
if (len(sys.argv) >= 5):
item_type = sys.argv[4].upper()
print(item_type)
if item_type == "CUSTOM":
if (len(sys.argv) >= 6):
item_template = sys.argv[5]
if (len(sys.argv) >= 7):
page_template = sys.argv[6]
if (len(sys.argv) >= 8):
output_file = sys.argv[7] if sys.argv[7] else 'index.html'
else:
print("Invalid index type")
exit()
if over_ride:
print("Warning: It will make destructive changes,Do you want to continue(Y/N) ?")
if input().lower() == "n":
exit()
gen = GenIndex(num=num,
icon=icon,
folder_icon=folder_icon,
file_icon=file_icon,
type=index_type,
override=over_ride,
item_type=item_type,
item_template=item_template,
page_template=page_template,
output_file=output_file)
gen.gen_index(par)