Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Forked from anglepoised/html_compile.py
Created September 17, 2012 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwhatcott/3740224 to your computer and use it in GitHub Desktop.
Save mdwhatcott/3740224 to your computer and use it in GitHub Desktop.
Python script to compile .shtml files with server side includes down to flat HTML suitable for hosting on S3 or where SSIs aren't supported.
#! /usr/bin/env python
import os
import re
import shutil
from os.path import splitext
SOURCE = os.getcwd() + "/www/"
TARGET = os.getcwd() + "/compiled/"
if not os.path.exists(TARGET):
os.makedirs(TARGET)
regex = re.compile(r'<!--#include virtual="([^"]*)" -->')
file_contents = lambda filename: open(filename).read()
match_ssi = lambda match: ssi(file_contents('%s%s' % (SOURCE, match.group(1))))
ssi = lambda txt: regex.sub(match_ssi, txt)
def get_extension(filename):
"""
>>> get_extension("index.shtml")
'shtml'
>>> get_extension("noextension")
''
"""
return splitext(filename)[1].lower()[1:]
def change_extension(filename, new_extension):
"""
>>> change_extension("index.shtml", "html")
'index.html'
>>> change_extension("something.jpg", "png")
'something.png'
>>> change_extension("noextension", "txt")
'noextension.txt'
"""
return "%s.%s" % (splitext(filename)[0].lower()[:], new_extension)
def process_file(filename):
print "Processing %s" % filename
return ssi(file_contents(filename))
print "Compiling site."
print "Source: %s" % SOURCE
print "Target: %s" % TARGET
print
for root, dirs, files in os.walk(SOURCE):
for filename in files:
name = os.path.join(root, filename)
relative_path = name.replace(SOURCE, "")
new_filename = os.path.join(TARGET, relative_path)
new_path = os.path.dirname(new_filename)
if not os.path.exists(new_path):
os.makedirs(new_path)
print "Writing %s" % new_filename
extension = get_extension(name)
if extension in ["shtml", ]:
# we need to do some processing on this extension type
new_content = process_file(name)
new_filename = change_extension(new_filename, "html")
f = open(new_filename, 'w')
f.write(new_content)
f.close()
else:
# otherwise we just do a straight copy
shutil.copy(name, new_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment