generated from Itsblue/github-pages-deb-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createStaticDirectoryListing.py
75 lines (57 loc) · 2.36 KB
/
createStaticDirectoryListing.py
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
""" Build index from directory listing
Inspired by: https://stackoverflow.com/questions/39048654/how-to-enable-directory-indexing-on-github-pages
usage: python3 createStaticDirectoryListing.py [-h] [--indexPage INDEXPAGE] directory
"""
DEFAULT_TEMPLATE = {
"outputFileName": "index.md",
"icons": {
"DIR": "📁",#":file_folder:",
"FILE": "🗒", #":spiral_notepad:",
"UP": "⤴", #":arrow_heading_up:"
},
"excludedFiles": ["index.md", "_config.yml", "_layouts", "README.md", "CNAME"],
"template": r"""
# Index of ${path}
Files in this directory:
%for displayName, fileName, type in files:
- ${icons[type]} [${displayName}](${fileName})
% endfor
"""
}
import os, argparse
from mako.template import Template
def createDirectoryListing(baseDirectory, template, indexPage = None, subDirectory = ""):
if baseDirectory.endswith("/"):
baseDirectory = baseDirectory[:-1]
thisDirectory = baseDirectory + "/" + subDirectory
files = []
if subDirectory:
files.append((
"Parent Directory",
"../",
"UP"
))
for fileName in sorted(os.listdir(thisDirectory)):
if fileName in template["excludedFiles"]:
continue
fileIsDir = os.path.isdir(thisDirectory + "/" + fileName)
fileType = "DIR" if fileIsDir else "FILE"
fileDisplayName = fileName + "/" if fileIsDir else fileName
files.append((fileDisplayName, fileName, fileType))
if fileIsDir:
createDirectoryListing(baseDirectory, template, indexPage, subDirectory + "/" + fileName)
displayPath = "/" if not subDirectory else subDirectory
fileContents = Template(template["template"]).render(files=files, path=displayPath, icons=template["icons"])
if not subDirectory and indexPage:
with open(indexPage, "r") as indexPageFile:
fileContents = indexPageFile.read() + fileContents
with open(thisDirectory + "/" + template["outputFileName"], "w+") as file:
file.write(fileContents)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("directory")
parser.add_argument("--indexPage", required=False, help="Provide a file which will be placed on the index page.")
args = parser.parse_args()
createDirectoryListing(args.directory, DEFAULT_TEMPLATE, args.indexPage)
if __name__ == '__main__':
main()