Skip to content

Instantly share code, notes, and snippets.

@dheaney
Created June 8, 2015 04:42
Show Gist options
  • Select an option

  • Save dheaney/1f3025e08bf77dfa0c47 to your computer and use it in GitHub Desktop.

Select an option

Save dheaney/1f3025e08bf77dfa0c47 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
template = """
<html>
<head>
<title>Directory Listing</title>
</head>
<body>
{{content}}
</body>
</html>
"""
def html_div(s):
return "<div>%s</div>\n" % (s)
def html_a(path, title=None):
if title is None:
title = path
return "<a href='%s'>%s</a>" % (path, title)
class Page():
def __init__(self, render_path):
self.template_data = template
self.is_valid = ( '{{' in self.template_data and '}}' in self.template_data )
self.beginning = self.template_data.split('{{')[0] or ''
self.end = self.template_data.split('}}')[1] or ''
self.render_path = render_path
def insert_html(self, html):
self.beginning += html
def render(self):
with open(self.render_path, 'w') as r:
r.write(self.beginning + self.end)
all_files = os.listdir('.')
all_files = [ f for f in all_files if f != 'index.html' ]
page = Page('index.html')
for path in all_files:
listing = html_div(html_a(path))
page.insert_html(listing)
page.render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment