Skip to content

Instantly share code, notes, and snippets.

@ianpreston
Created December 10, 2012 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ianpreston/4248453 to your computer and use it in GitHub Desktop.
Save ianpreston/4248453 to your computer and use it in GitHub Desktop.
Tiny, fabric-based static site generator
from fabric.api import local
import jinja2
import sys
import os
import os.path
import webbrowser
proj_base_path = sys.path[0]
pages_path = os.path.join(proj_base_path, 'pages')
build_path = os.path.join(proj_base_path, 'build')
static_path = os.path.join(proj_base_path, 'static')
def build():
env = jinja2.Environment(loader=jinja2.FileSystemLoader(proj_base_path))
for root, _, filenames in os.walk(pages_path):
for pfn in filenames:
template_abs_path = os.path.join(root, pfn)
template_path_relative_to_project = template_abs_path.replace(proj_base_path, '')
template = env.get_template(template_path_relative_to_project)
template_save_path = os.path.join(build_path, template_path_relative_to_project.replace('/pages/', ''))
template_save_dir = os.path.dirname(template_save_path)
local ('echo "Building {0}..."'.format(template_path_relative_to_project))
local('mkdir -p {0}'.format(template_save_dir))
with open(template_save_path, 'w') as f:
c = template.render()
f.write(c)
local('cp -r {0}/* {1}/'.format(static_path, build_path))
def browse(port=4200):
build()
webbrowser.open('http://localhost:{0}/'.format(port))
local('cd {0} && python -m SimpleHTTPServer {1}'.format(build_path, port))
def deploy(user='sissel', remote='noveria.0x-1.com', webroot='/var/www'):
build()
local('scp -r {0}/* {1}@{2}:{3}'.format(build_path, user, remote, webroot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment