Skip to content

Instantly share code, notes, and snippets.

@Steve-Tech
Created November 9, 2019 23:49
Show Gist options
  • Save Steve-Tech/93cd1c59f1c0933686c06073a2a8531a to your computer and use it in GitHub Desktop.
Save Steve-Tech/93cd1c59f1c0933686c06073a2a8531a to your computer and use it in GitHub Desktop.
Recursive Index for GitHub Pages. make_index.py is modified from https://stackoverflow.com/questions/39048654/how-to-enable-directory-indexing-on-github-pages
import os
import subprocess
dirs = []
for i in os.listdir('.'):
if os.path.isdir(i):
dirs += [i]
for i in dirs:
for j in os.listdir(i):
if os.path.isdir(i + '/' + j):
dirs += [i + '/' + j]
print(dirs)
for i in dirs:
subprocess.call(["python", "make_index.py", i])
""" Build index from directory listing
make_index.py </path/to/directory> [--header <header text>]
"""
INDEX_TEMPLATE = r"""
<html>
<body>
<h2>${header}</h2>
<p>
% for name in names:
<li><a href="${name}">${name}</a></li>
% endfor
</p>
</body>
</html>
"""
EXCLUDED = ['index.html']
import os
import argparse
# May need to do "pip install mako"
from mako.template import Template
def main():
parser = argparse.ArgumentParser()
parser.add_argument("directory")
parser.add_argument("--header")
args = parser.parse_args()
fnames = [fname for fname in sorted(os.listdir(args.directory))
if fname not in EXCLUDED]
header = (args.header if args.header else os.path.basename(args.directory))
print(Template(INDEX_TEMPLATE).render(names=fnames, header=header))
if not os.path.exists(args.directory + '\index.html'):
f = open(args.directory + "\index.html", "w")
f.write(Template(INDEX_TEMPLATE).render(names=fnames, header=header))
f.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment