HTDE HTDE - 2 months ago 24
Git Question

How to Enable Directory Indexing on GitHub Pages

I need to display directory contents on GH Pages.

Would prefer


  1. Automatically, without index.html

  2. A tool or library for automatically generating the index.html

  3. Any other method



So, if I have a FS in my GH Pages repository:

http://github.com/[username]/[username].github.io/
:

script/
- app/
- core/
- init.js
- lib/
- Element.animate.js
- Object.overlay.js
- mod/
- anim/
- global/
- carousel/
- carousel.js
- global.js
- ext/
- cfgs.js
index.html


I would want each directory URL to index as usual, like so.

http://[username].github.io/script/
:

- app/
- lib/
- mod/
- ext/


http://[username].github.io/script/mod/anim/global/
:

- carousel/
- global.js


The only thing I can think of is preference #2, write or find a script to automatically generate the index.html from the GitHub Repo page or the local Repo on my FS.

Answer

I also wanted to do this. I tried uploading an .htaccess file with contents Options +Indexes to the relevant directory, but that did not work.

So, I used your option #2, writing a tiny Python script to generate an index file file for the directory.

""" 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 __name__ == '__main__':
    main()