Skip to content

Instantly share code, notes, and snippets.

@pop
Last active October 15, 2015 20:06
Show Gist options
  • Save pop/0ee1ef21692f3cbb6934 to your computer and use it in GitHub Desktop.
Save pop/0ee1ef21692f3cbb6934 to your computer and use it in GitHub Desktop.
A quick Static Site Generator demo

How to an SSG

A Cat Barcamp presentation demo code thing.

Usage

$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ python ssg.py <rst file> <jinja template file>

What is this?

ssg.py is a very simple static site (static page really) generator. Pass it a content.rst file and a template.jinja (or whatever you want to call your content and template files) and it produces an index.html file.

Included in this repo are the content files, the script, the template, the requirements file, and the produced index.html file from running the script.

this is my title

this is a list:

key value
1 one
2 two
3 three
<head>
<title>this is my title</title>
<link href='style.css' type='text/css' rel='stylesheet'>
</head>
<body class='blink_text'>
<marquee direction="down" width="100%" height="100%" behavior="alternate" style="border:solid">
<marquee behavior="alternate">
<div class="document" id="this-is-my-title"> <h1 class="title">this is my title</h1> <p>this is a list:</p> <table border="1" class="docutils"> <colgroup> <col width="38%" /> <col width="63%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">key</th> <th class="head">value</th> </tr> </thead> <tbody valign="top"> <tr><td>1</td> <td>one</td> </tr> <tr><td>2</td> <td>two</td> </tr> <tr><td>3</td> <td>three</td> </tr> </tbody> </table> </div>
The website was last update at:
<marquee direction="down" width="600px" height="200px" behavior="alternate" style="border:solid">
<marquee behavior="alternate">
1995-10-12 22:29:50.966281
</marquee>
</marquee>
</marquee>
</marquee>
</body>
docutils==0.12
Jinja2==2.8
MarkupSafe==0.23
python-dateutil==2.4.2
six==1.10.0
wheel==0.24.0
#!/usr/bin/env python
from docutils.core import publish_parts
from jinja2 import Environment, FileSystemLoader
from os import getcwd
from sys import argv
from datetime import datetime
from dateutil.relativedelta import relativedelta
def build(content=None, template='template.jinja'):
"""
Builds a website given a content rst file and a jinja template.
"""
with open(content) as f:
html_content = publish_parts(f.read(), writer_name='html')
title = html_content['title']
body = html_content['html_body'].replace('\n',' ')
t = datetime.now()-relativedelta(years=20)
loader = FileSystemLoader(getcwd())
env = Environment(loader=loader)
template = env.get_template(template)
page = template.render(title=title,
content=body,
time=t)
with open('index.html', 'w') as f:
f.write(page)
if __name__ == '__main__':
try:
build(content=argv[1], template=argv[2])
except IndexError:
try:
build(content=argv[1])
except IndexError:
print("Usage: \n"+
" $ ssg.py <content file> [<template file>]\n"+
" template file defaults to template.jinja")
h1 {
color: red;
}
.blink_text {
-webkit-animation-name: blinker;
-webkit-animation-duration: 1s;
-webkit-animation-timing-function: linear;
-webkit-animation-iteration-count: infinite;
-moz-animation-name: blinker;
-moz-animation-duration: 1s;
-moz-animation-timing-function: linear;
-moz-animation-iteration-count: infinite;
animation-name: blinker;
animation-duration: 1s;
animation-timing-function: linear;
animation-iteration-count: infinite;
color: red;
}
@-moz-keyframes blinker {
0% { opacity: 1.0; }
50% { opacity: 0.0; }
100% { opacity: 1.0; }
}
@-webkit-keyframes blinker {
0% { opacity: 1.0; }
50% { opacity: 0.0; }
100% { opacity: 1.0; }
}
@keyframes blinker {
0% { opacity: 1.0; }
50% { opacity: 0.0; }
100% { opacity: 1.0; }
}
<head>
<title>{{ title }}</title>
<link href='style.css' type='text/css' rel='stylesheet'>
</head>
<body class='blink_text'>
<marquee direction="down" width="100%" height="100%" behavior="alternate" style="border:solid">
<marquee behavior="alternate">
{{ content }}
The website was last update at:
<marquee direction="down" width="600px" height="200px" behavior="alternate" style="border:solid">
<marquee behavior="alternate">
{{ time }}
</marquee>
</marquee>
</marquee>
</marquee>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment