The purpose of this project completed as an assignment for 3W subject was to design Python3 compatible static site generator using Jinja2 template engine. Then using this self-written engine generate remastered version of polish presidental elections site.
The original site is available at:
Β Β Β Β Β Presidental elections 2000
The remake is available at:
Β Β Β Β Β http://styczynski.in/pl-election-2000
This projects requires bash shell (or at least some kind of emulation) and Python 3.
The generator performs auto-configuration and then run server at localhost:8000 with autoreload. To begin just type the following command into the bash shell:
./generate up
Then wait for completion and navigate to localhost:8000/build/index.html
to see the results.
Please note: in generate
file you must specify python executable you are using (if it's not default python or python3)
You can manually configure you virtual environment or use your global one: Execute the following command to install all requirements:
pip install -r requirements.txt
Generator script generate
supports the following commands:
-
init
Automatically create virtual environment for execution Skip if it actually exists.
-
reset
Automatically create virtual environment for execution Override any previous one.
-
build
Build static site. Output files to ./build
-
server (alias: dev)
Run static server at localhost:8000 with auto rebuild
-
release
Prepare release-ready version of files
-
up
Alias for executing setup and then server command
Generate script loads main page and subpages form subfolders of templates folders:
- Firstly it generates DataGenerator from index.py if it exists
- Then creates index.html from index.html Jinja2-compatible template
- Copies needed index.css/index.js files mangling them as Jinja templates
- These steps are repeated for all folders inside templates directory (if the index.html/.py/.css etc. is placed inside
templates/<dir>
then the generated subpage is named<dir>.html
Let's assume we call main index.html and subpages' index'es as single page.
For each page index.html is required anything to be generated.
But index.js/.css/.py are optional.
If the index.py is not provided then template is rendered with no data.
If the index.py is provided then it must define the following class:
class DataGenerator:
def __init__(self, path, config, globalGenerator):
# ...
def getFileNames(self):
# ...
def prepareData(self, data, fileName, fileIndex):
# ...
The DataGenerator contructor is invoked when the render occurs.
path
- path to the rendered .html template
config
- generator configuration
globalGenerator
- generator for top-level index.html (for index.html generator that argument is None, because the generator is not yet created)
Optional method that returns array of the files that generator should generate instead of default subpage page.
Example:
def getFileNames(self):
return [ 'override' ];
If the generator is placed inside templates/subpage
directory then only subpage_override.html
file will be generated.
Note: Top-level generator cannot generate anything more than 'index.html' so this method has no effect there.
data
- initial data (data inherited from generator configuration and top-level index generator)
fileName
- file to be generated ('default'
when getFileNames method was not defined; elements of getFileNames() otherwise)
fileIndex
- index of current file on the list of all files that will be generated (files are indexed from 0)
The method is required and should return data object with added keys.
The entire returned data object will be available when rendering files.