@@ -11,6 +11,7 @@ from hyde.site import Site | |||||
from hyde.version import __version__ | from hyde.version import __version__ | ||||
from hyde.util import getLoggerWithConsoleHandler | from hyde.util import getLoggerWithConsoleHandler | ||||
import codecs | |||||
import os | import os | ||||
import yaml | import yaml | ||||
@@ -118,6 +119,6 @@ class Engine(Application): | |||||
config_file = sitepath.child(config) | config_file = sitepath.child(config) | ||||
logger.info("Reading site configuration from [%s]", config_file) | logger.info("Reading site configuration from [%s]", config_file) | ||||
conf = {} | conf = {} | ||||
with open(config_file) as stream: | |||||
with codecs.open(config_file, 'r', 'utf-8') as stream: | |||||
conf = yaml.load(stream) | conf = yaml.load(stream) | ||||
return Site(sitepath, Config(sitepath, conf)) | return Site(sitepath, Config(sitepath, conf)) |
@@ -4,6 +4,7 @@ The generator class and related utility functions. | |||||
""" | """ | ||||
from hyde.exceptions import HydeException | from hyde.exceptions import HydeException | ||||
from hyde.fs import File, Folder | from hyde.fs import File, Folder | ||||
from hyde.model import Context | |||||
from hyde.plugin import Plugin | from hyde.plugin import Plugin | ||||
from hyde.template import Template | from hyde.template import Template | ||||
@@ -24,7 +25,9 @@ class Generator(object): | |||||
self.generated_once = False | self.generated_once = False | ||||
self.__context__ = dict(site=site) | self.__context__ = dict(site=site) | ||||
if hasattr(site.config, 'context'): | if hasattr(site.config, 'context'): | ||||
self.__context__.update(site.config.context) | |||||
self.__context__.update( | |||||
Context.load(site.sitepath, site.config.context)) | |||||
self.template = None | self.template = None | ||||
Plugin.load_all(site) | Plugin.load_all(site) | ||||
@@ -2,6 +2,8 @@ | |||||
""" | """ | ||||
Contains data structures and utilities for hyde. | Contains data structures and utilities for hyde. | ||||
""" | """ | ||||
from hyde.fs import File, Folder | |||||
import yaml | |||||
class Expando(object): | class Expando(object): | ||||
""" | """ | ||||
@@ -38,7 +40,28 @@ class Expando(object): | |||||
else: | else: | ||||
return primitive | return primitive | ||||
from hyde.fs import File, Folder | |||||
class Context(object): | |||||
""" | |||||
Wraps the context related functions and utilities. | |||||
""" | |||||
@staticmethod | |||||
def load(sitepath, ctx): | |||||
""" | |||||
Load context from config data and providers. | |||||
""" | |||||
context = {} | |||||
try: | |||||
context.update(ctx.data.__dict__) | |||||
for provider_name, resource_name in ctx.providers.__dict__.items(): | |||||
res = File(Folder(sitepath).child(resource_name)) | |||||
if res.exists: | |||||
context[provider_name] = yaml.load(res.read_all()) | |||||
except AttributeError: | |||||
# No context data found | |||||
pass | |||||
return context | |||||
class Config(Expando): | class Config(Expando): | ||||
@@ -5,9 +5,9 @@ Use nose | |||||
`$ nosetests` | `$ nosetests` | ||||
""" | """ | ||||
from hyde.generator import Generator | from hyde.generator import Generator | ||||
from hyde.fs import FS, File, Folder | from hyde.fs import FS, File, Folder | ||||
from hyde.model import Config | |||||
from hyde.site import Site | from hyde.site import Site | ||||
from nose.tools import raises, with_setup, nottest | from nose.tools import raises, with_setup, nottest | ||||
@@ -90,16 +90,58 @@ class TestGenerator(object): | |||||
l.write(l.read_all()) | l.write(l.read_all()) | ||||
assert gen.has_resource_changed(resource) | assert gen.has_resource_changed(resource) | ||||
def test_has_resource_changed(self): | |||||
site = Site(TEST_SITE) | |||||
def test_context(self): | |||||
site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ | |||||
"context": { | |||||
"data": { | |||||
"abc": "def" | |||||
} | |||||
} | |||||
})) | |||||
text = """ | |||||
{% extends "base.html" %} | |||||
{% block main %} | |||||
abc = {{ abc }} | |||||
Hi! | |||||
I am a test template to make sure jinja2 generation works well with hyde. | |||||
{{resource.name}} | |||||
{% endblock %} | |||||
""" | |||||
site.load() | site.load() | ||||
site.config.context = { | |||||
"abc": "def" | |||||
} | |||||
resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) | |||||
gen = Generator(site) | |||||
resource.source_file.write(text) | |||||
gen.generate_all() | |||||
target = File(site.config.deploy_root_path.child(resource.name)) | |||||
assert "abc = def" in target.read_all() | |||||
def test_context_providers(self): | |||||
site = Site(TEST_SITE, Config(TEST_SITE, config_dict={ | |||||
"context": { | |||||
"data": { | |||||
"abc": "def" | |||||
}, | |||||
"providers": { | |||||
"nav": "nav.yaml" | |||||
} | |||||
} | |||||
})) | |||||
nav = """ | |||||
main: | |||||
- home | |||||
- articles | |||||
- projects | |||||
""" | |||||
text = """ | text = """ | ||||
{% extends "base.html" %} | {% extends "base.html" %} | ||||
{% block main %} | {% block main %} | ||||
{{nav}} | |||||
{% for item in nav.main %} | |||||
{{item}} | |||||
{% endfor %} | |||||
abc = {{ abc }} | abc = {{ abc }} | ||||
Hi! | Hi! | ||||
@@ -107,9 +149,16 @@ class TestGenerator(object): | |||||
{{resource.name}} | {{resource.name}} | ||||
{% endblock %} | {% endblock %} | ||||
""" | """ | ||||
File(TEST_SITE.child('nav.yaml')).write(nav) | |||||
site.load() | |||||
resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) | resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) | ||||
gen = Generator(site) | gen = Generator(site) | ||||
resource.source_file.write(text) | resource.source_file.write(text) | ||||
gen.generate_all() | gen.generate_all() | ||||
target = File(site.config.deploy_root_path.child(resource.name)) | target = File(site.config.deploy_root_path.child(resource.name)) | ||||
assert "abc = def" in target.read_all() | |||||
out = target.read_all() | |||||
print out | |||||
assert "abc = def" in out | |||||
assert "home" in out | |||||
assert "articles" in out | |||||
assert "projects" in out |