diff --git a/hyde/generator.py b/hyde/generator.py index 124dfd1..428472c 100644 --- a/hyde/generator.py +++ b/hyde/generator.py @@ -23,6 +23,8 @@ class Generator(object): self.site = site self.generated_once = False self.__context__ = dict(site=site) + if hasattr(site.config, 'context'): + self.__context__.update(site.config.context) self.template = None Plugin.load_all(site) diff --git a/hyde/tests/test_generate.py b/hyde/tests/test_generate.py index fdd8fef..2bf2ae5 100644 --- a/hyde/tests/test_generate.py +++ b/hyde/tests/test_generate.py @@ -15,82 +15,101 @@ from pyquery import PyQuery TEST_SITE = File(__file__).parent.child_folder('_test') -@nottest -def create_test_site(): - TEST_SITE.make() - TEST_SITE.parent.child_folder('sites/test_jinja').copy_contents_to(TEST_SITE) +class TestGenerator(object): -@nottest -def delete_test_site(): - TEST_SITE.delete() + def setUp(self): + TEST_SITE.make() + TEST_SITE.parent.child_folder('sites/test_jinja').copy_contents_to(TEST_SITE) -@with_setup(create_test_site, delete_test_site) -def test_generate_resource_from_path(): - site = Site(TEST_SITE) - site.load() - gen = Generator(site) - gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) - about = File(Folder(site.config.deploy_root_path).child('about.html')) - assert about.exists - text = about.read_all() - q = PyQuery(text) - assert about.name in q("div#main").text() + def tearDown(self): + TEST_SITE.delete() -@with_setup(create_test_site, delete_test_site) -def test_generate_resource_from_path_with_is_processable_false(): - site = Site(TEST_SITE) - site.load() - resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) - resource.is_processable = False - gen = Generator(site) - gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) - about = File(Folder(site.config.deploy_root_path).child('about.html')) - assert not about.exists + def test_generate_resource_from_path(self): + site = Site(TEST_SITE) + site.load() + gen = Generator(site) + gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) + about = File(Folder(site.config.deploy_root_path).child('about.html')) + assert about.exists + text = about.read_all() + q = PyQuery(text) + assert about.name in q("div#main").text() -@with_setup(create_test_site, delete_test_site) -def test_generate_resource_from_path_with_uses_template_false(): - site = Site(TEST_SITE) - site.load() - resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) - resource.uses_template = False - gen = Generator(site) - gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) - about = File(Folder(site.config.deploy_root_path).child('about.html')) - assert about.exists - text = about.read_all() - expected = resource.source_file.read_all() - assert text == expected + def test_generate_resource_from_path_with_is_processable_false(self): + site = Site(TEST_SITE) + site.load() + resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) + resource.is_processable = False + gen = Generator(site) + gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) + about = File(Folder(site.config.deploy_root_path).child('about.html')) + assert not about.exists -@with_setup(create_test_site, delete_test_site) -def test_generate_resource_from_path_with_deploy_override(): - site = Site(TEST_SITE) - site.load() - resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) - resource.relative_deploy_path = 'about/index.html' - gen = Generator(site) - gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) - about = File(Folder(site.config.deploy_root_path).child('about/index.html')) - assert about.exists - text = about.read_all() - q = PyQuery(text) - assert resource.name in q("div#main").text() + def test_generate_resource_from_path_with_uses_template_false(self): + site = Site(TEST_SITE) + site.load() + resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) + resource.uses_template = False + gen = Generator(site) + gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) + about = File(Folder(site.config.deploy_root_path).child('about.html')) + assert about.exists + text = about.read_all() + expected = resource.source_file.read_all() + assert text == expected -@with_setup(create_test_site, delete_test_site) -def test_has_resource_changed(): - site = Site(TEST_SITE) - site.load() - resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) - gen = Generator(site) - gen.generate_all() - assert not gen.has_resource_changed(resource) - import time - time.sleep(1) - text = resource.source_file.read_all() - resource.source_file.write(text) - assert gen.has_resource_changed(resource) - gen.generate_all() - assert not gen.has_resource_changed(resource) - time.sleep(1) - l = File(TEST_SITE.child('layout/root.html')) - l.write(l.read_all()) - assert gen.has_resource_changed(resource) + def test_generate_resource_from_path_with_deploy_override(self): + site = Site(TEST_SITE) + site.load() + resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) + resource.relative_deploy_path = 'about/index.html' + gen = Generator(site) + gen.generate_resource_at_path(TEST_SITE.child('content/about.html')) + about = File(Folder(site.config.deploy_root_path).child('about/index.html')) + assert about.exists + text = about.read_all() + q = PyQuery(text) + assert resource.name in q("div#main").text() + + def test_has_resource_changed(self): + site = Site(TEST_SITE) + site.load() + resource = site.content.resource_from_path(TEST_SITE.child('content/about.html')) + gen = Generator(site) + gen.generate_all() + assert not gen.has_resource_changed(resource) + import time + time.sleep(1) + text = resource.source_file.read_all() + resource.source_file.write(text) + assert gen.has_resource_changed(resource) + gen.generate_all() + assert not gen.has_resource_changed(resource) + time.sleep(1) + l = File(TEST_SITE.child('layout/root.html')) + l.write(l.read_all()) + assert gen.has_resource_changed(resource) + + def test_has_resource_changed(self): + site = Site(TEST_SITE) + site.load() + site.config.context = { + "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 %} +""" + 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() \ No newline at end of file