Skip to content
14 changes: 6 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
Complexity
==========

.. image:: https://badge.fury.io/py/complexity.png
:target: http://badge.fury.io/py/complexity

.. image:: https://travis-ci.org/audreyr/complexity.png?branch=master
:target: https://travis-ci.org/audreyr/complexity
A refreshingly simple static site generator, for those who like to work in HTML.

.. image:: https://pypip.in/d/complexity/badge.png
:target: https://crate.io/packages/complexity?version=latest
Changes
-------

A refreshingly simple static site generator, for those who like to work in HTML.
- New url argument that allows you to (--watch) watch a folder for changes, and any changes will fire off complexity
- New config variable to turn on/off the auto-expand system

Documentation
-------------
Expand Down Expand Up @@ -69,3 +66,4 @@ Community
* We love contributions. Read about `how to contribute`_.

.. _`how to contribute`: https://github.com/audreyr/complexity/blob/master/CONTRIBUTING.rst

10 changes: 10 additions & 0 deletions complexity/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
import yaml


DEFAULTS = {
"templates_dir": "templates/",
"assets_dir": "assets/",
"context_dir": "context/",
"output_dir": "../www/",
"macro_dirs": ["macros/"],
"expand": True
}


def read_conf(directory):
"""
Reads and parses the `complexity.yml` configuration file from a
Expand Down
86 changes: 57 additions & 29 deletions complexity/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

import json
import logging
import os
import os.path
import shutil
import re

from yaml import safe_load
from binaryornot.check import is_binary
from jinja2 import FileSystemLoader
from jinja2.environment import Environment
Expand All @@ -22,7 +23,7 @@
from .utils import make_sure_path_exists, unicode_open


def get_output_filename(template_filepath, output_dir, force_unexpanded):
def get_output_filename(template_filepath, output_dir, force_unexpanded, expand):
"""
Given an input filename, return the corresponding output filename.

Expand All @@ -41,7 +42,7 @@ def get_output_filename(template_filepath, output_dir, force_unexpanded):
if basename.startswith('base'):
return False
# Put index and unexpanded templates in the root.
elif force_unexpanded or basename == 'index.html':
elif force_unexpanded or basename == 'index.html' or not expand:
output_filename = os.path.join(output_dir, template_filepath)
# Put other pages in page/index.html, for better URL formatting.
else:
Expand All @@ -68,7 +69,7 @@ def minify_html(html):

def generate_html_file(template_filepath,
output_dir, env,
context, force_unexpanded=False, minify=False):
context, force_unexpanded=False, minify=False, expand=True):
"""
Renders and writes a single HTML file to its corresponding output location.

Expand All @@ -79,6 +80,8 @@ def generate_html_file(template_filepath,
:param env: Jinja2 environment with a loader already set up.
:param context: Jinja2 context that holds template variables. See
http://jinja.pocoo.org/docs/api/#the-context
:param expand: Shall we expand the filenames to folder/index.html
as pretty URLS?
"""

# Ignore templates starting with "base". They're treated as special cases.
Expand All @@ -96,7 +99,7 @@ def generate_html_file(template_filepath,
rendered_html = minify_html(rendered_html)

output_filename = get_output_filename(template_filepath,
output_dir, force_unexpanded)
output_dir, force_unexpanded, expand)
if output_filename:
make_sure_path_exists(os.path.dirname(output_filename))

Expand All @@ -106,8 +109,20 @@ def generate_html_file(template_filepath,
return True


def generate_html(templates_dir, output_dir, context=None,
unexpanded_templates=()):
def _ignore(path):
fn = os.path.basename(path)
_, ext = os.path.splitext(path)
if is_binary(path):
return True
if fn == 'complexity.yml':
return True
if ext in ('.j2','.yml'):
return True
return False


def generate_html(templates_dir, macro_dirs, output_dir, context=None,
unexpanded_templates=(), expand=True, quiet=False):
"""
Renders the HTML templates from `templates_dir`, and writes them to
`output_dir`.
Expand All @@ -119,6 +134,9 @@ def generate_html(templates_dir, output_dir, context=None,
:paramtype output_dir: directory
:param context: Jinja2 context that holds template variables. See
http://jinja.pocoo.org/docs/api/#the-context
:param expand: Shall we expand the filenames to folder/index.html
as pretty URLS?
:param quiet: show no output!
"""

logging.debug('Templates dir is {0}'.format(templates_dir))
Expand All @@ -129,9 +147,11 @@ def generate_html(templates_dir, output_dir, context=None,
)

context = context or {}
env = Environment()
# os.chdir(templates_dir)
env.loader = FileSystemLoader(templates_dir)

_dirs = [templates_dir]
_dirs.extend(macro_dirs)

env = Environment(loader=FileSystemLoader(_dirs))

# Create the output dir if it doesn't already exist
make_sure_path_exists(output_dir)
Expand All @@ -151,15 +171,17 @@ def generate_html(templates_dir, output_dir, context=None,
force_unexpanded
))

if is_binary(os.path.join(templates_dir, template_filepath)):
print('Non-text file found: {0}. Skipping.'.
format(template_filepath))
if _ignore(os.path.join(templates_dir, template_filepath)):
if quiet == False:
print('Ignore: {0}. Skipping.'.
format(template_filepath))
else:
outfile = get_output_filename(template_filepath, output_dir,
force_unexpanded)
print('Copying {0} to {1}'.format(template_filepath, outfile))
force_unexpanded, expand)
if quiet == False:
print('Copying {0} to {1}'.format(template_filepath, outfile))
generate_html_file(template_filepath, output_dir, env, context,
force_unexpanded)
force_unexpanded, expand)


def generate_context(context_dir):
Expand Down Expand Up @@ -194,24 +216,27 @@ def generate_context(context_dir):
"""
context = {}

json_files = os.listdir(context_dir)

for file_name in json_files:

if file_name.endswith('json'):
all_files = os.listdir(context_dir)
for fn in all_files:
path = os.path.join(context_dir, fn)
name, ext = os.path.splitext(fn)

# Open the JSON file and convert to Python object
json_file = os.path.join(context_dir, file_name)
with unicode_open(json_file) as f:
obj = None
if ext == '.json':
with unicode_open(path) as f:
obj = json.load(f)
elif ext in {'.yml', '.yaml'}:
with unicode_open(path) as f:
obj = safe_load(f)

# Add the Python object to the context dictionary
context[file_name[:-5]] = obj
if obj is not None:
print('Parsed {0} to context as {1}'.format(fn, name))
context[name] = obj

return context


def copy_assets(assets_dir, output_dir):
def copy_assets(assets_dir, output_dir, quiet=False):
"""
Copies static assets over from `assets_dir` to `output_dir`.

Expand All @@ -220,6 +245,7 @@ def copy_assets(assets_dir, output_dir):
:paramtype assets_dir: directory
:param output_dir: The Complexity output directory, e.g. `www/`.
:paramtype output_dir: directory
:param quiet: output to user
"""

assets = os.listdir(assets_dir)
Expand All @@ -229,11 +255,13 @@ def copy_assets(assets_dir, output_dir):
# Only copy allowed dirs
if os.path.isdir(item_path) and item != 'scss' and item != 'less':
new_dir = os.path.join(output_dir, item)
print('Copying directory {0} to {1}'.format(item, new_dir))
if quiet == False:
print('Copying directory {0} to {1}'.format(item, new_dir))
shutil.copytree(item_path, new_dir)

# Copy over files in the root of assets_dir
if os.path.isfile(item_path):
new_file = os.path.join(output_dir, item)
print('Copying file {0} to {1}'.format(item, new_file))
if quiet == False:
print('Copying file {0} to {1}'.format(item, new_file))
shutil.copyfile(item_path, new_file)
Loading