Skip to content

Instantly share code, notes, and snippets.

@dheaney
Created June 7, 2015 21:09
Show Gist options
  • Select an option

  • Save dheaney/923d277f4ec38a00afa7 to your computer and use it in GitHub Desktop.

Select an option

Save dheaney/923d277f4ec38a00afa7 to your computer and use it in GitHub Desktop.
import os
def html_p(s):
return "<p>%s</p>\n" % (s)
def html_file_listing(path, title=None):
if title is None:
title = path
return "<a href='%s'>%s</a>" % (path, title)
class Page():
def __init__(self, template_path, render_path):
with open(template_path, 'r') as template:
self.template_data = template.read()
template.close()
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('.')
excluded_files = []
for f in all_files:
# If index.html or doesn't end in .html...
if (f == 'index.html') or (f.split('.html')[-1] != ''):
excluded_files.append(f)
htmls = [ f for f in all_files if f not in excluded_files ]
page = Page('index.template', 'index.html')
for i, path in enumerate(htmls):
listing = html_p(str(i) + " " + html_file_listing(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