diff --git a/hyde/generator.py b/hyde/generator.py index fb3f994..8c7fa4f 100644 --- a/hyde/generator.py +++ b/hyde/generator.py @@ -23,6 +23,7 @@ class Generator(object): def __init__(self, site): super(Generator, self).__init__() self.site = site + self.generated_once = False self.__context__ = dict(site=site) self.template = None Plugin.load_all(site) @@ -48,8 +49,10 @@ class Generator(object): res = function(*args) if res: targs = list(args) - last = targs.pop() - targs.append(res if res else last) + last = None + if len(targs): + last = targs.pop() + targs.append(res if res else last) args = tuple(targs) return res @@ -123,12 +126,15 @@ class Generator(object): self.__generate_node__(self.site.content) self.events.site_complete() self.finalize() + self.generated_once = True def generate_node_at_path(self, node_path=None): """ Generates a single node. If node_path is non-existent or empty, generates the entire site. """ + if not self.generated_once: + return self.generate_all() self.load_template_if_needed() self.load_site_if_needed() node = None @@ -141,7 +147,7 @@ class Generator(object): Generates the given node. If node is invalid, empty or non-existent, generates the entire website. """ - if not node: + if not node or not self.generated_once: return self.generate_all() self.load_template_if_needed() @@ -159,6 +165,9 @@ class Generator(object): Generates a single resource. If resource_path is non-existent or empty, generats the entire website. """ + if not self.generated_once: + return self.generate_all() + self.load_template_if_needed() self.load_site_if_needed() resource = None @@ -171,7 +180,7 @@ class Generator(object): Generates the given resource. If resource is invalid, empty or non-existent, generates the entire website. """ - if not resource: + if not resource or not self.generated_once: return self.generate_all() self.load_template_if_needed() diff --git a/hyde/tests/test_plugin.py b/hyde/tests/test_plugin.py index 8d996a7..3953fee 100644 --- a/hyde/tests/test_plugin.py +++ b/hyde/tests/test_plugin.py @@ -123,20 +123,39 @@ class TestPlugins(object): gen.generate_all() assert begin_site_stub.call_count == 1 - def test_generator_template_begin_site_not_called_for_single_resource(self): + def test_generator_template_begin_site_called_for_single_resource(self): with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub: gen = Generator(self.site) path = self.site.content.source_folder.child('about.html') gen.generate_resource_at_path(path) - assert begin_site_stub.call_count == 0 + assert begin_site_stub.call_count == 1 + + def test_generator_template_begin_site_not_called_for_single_resource_second_time(self): + with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub: + gen = Generator(self.site) + gen.generate_all() + assert begin_site_stub.call_count == 1 + path = self.site.content.source_folder.child('about.html') + gen.generate_resource_at_path(path) + assert begin_site_stub.call_count == 1 + + def test_generator_template_begin_site_called_for_single_node(self): + with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub: + gen = Generator(self.site) + path = self.site.content.source_folder + gen.generate_node_at_path(path) + + assert begin_site_stub.call_count == 1 - def test_generator_template_begin_site_not_called_for_single_node(self): + def test_generator_template_begin_site_not_called_for_single_node_second_time(self): with patch.object(PluginLoaderStub, 'begin_site') as begin_site_stub: gen = Generator(self.site) + gen.generate_all() + assert begin_site_stub.call_count == 1 path = self.site.content.source_folder gen.generate_node_at_path(path) - assert begin_site_stub.call_count == 0 + assert begin_site_stub.call_count == 1 def test_generator_template_site_complete_called(self): with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: @@ -145,23 +164,44 @@ class TestPlugins(object): assert site_complete_stub.call_count == 1 - def test_generator_template_site_complete_not_called_for_single_resource(self): + def test_generator_template_site_complete_called_for_single_resource(self): with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: gen = Generator(self.site) path = self.site.content.source_folder.child('about.html') gen.generate_resource_at_path(path) - assert site_complete_stub.call_count == 0 + assert site_complete_stub.call_count == 1 + + def test_generator_template_site_complete_not_called_for_single_resource_second_time(self): + + with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: + gen = Generator(self.site) + gen.generate_all() + assert site_complete_stub.call_count == 1 + path = self.site.content.source_folder.child('about.html') + gen.generate_resource_at_path(path) + + assert site_complete_stub.call_count == 1 - def test_generator_template_site_complete_not_called_for_single_node(self): + def test_generator_template_site_complete_called_for_single_node(self): with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: gen = Generator(self.site) path = self.site.content.source_folder gen.generate_node_at_path(path) - assert site_complete_stub.call_count == 0 + assert site_complete_stub.call_count == 1 + + def test_generator_template_site_complete_not_called_for_single_node_second_time(self): + + with patch.object(PluginLoaderStub, 'site_complete') as site_complete_stub: + gen = Generator(self.site) + gen.generate_all() + path = self.site.content.source_folder + gen.generate_node_at_path(path) + + assert site_complete_stub.call_count == 1 def test_generator_template_begin_node_called(self): @@ -173,12 +213,22 @@ class TestPlugins(object): called_with_nodes = sorted([arg[0][0].path for arg in begin_node_stub.call_args_list]) assert called_with_nodes == self.content_nodes - def test_generator_template_begin_node_not_called_for_single_resource(self): + def test_generator_template_begin_node_called_for_single_resource(self): with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub: gen = Generator(self.site) gen.generate_resource_at_path(self.site.content.source_folder.child('about.html')) - assert begin_node_stub.call_count == 0 + assert begin_node_stub.call_count == len(self.content_nodes) + + + def test_generator_template_begin_node_not_called_for_single_resource_second_time(self): + + with patch.object(PluginLoaderStub, 'begin_node') as begin_node_stub: + gen = Generator(self.site) + gen.generate_all() + assert begin_node_stub.call_count == len(self.content_nodes) + gen.generate_resource_at_path(self.site.content.source_folder.child('about.html')) + assert begin_node_stub.call_count == len(self.content_nodes) # No extra calls def test_generator_template_node_complete_called(self): @@ -191,12 +241,21 @@ class TestPlugins(object): called_with_nodes = sorted([arg[0][0].path for arg in node_complete_stub.call_args_list]) assert called_with_nodes == self.content_nodes - def test_generator_template_node_complete_not_called_for_single_resource(self): + def test_generator_template_node_complete_called_for_single_resource(self): + + with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub: + gen = Generator(self.site) + gen.generate_resource_at_path(self.site.content.source_folder.child('about.html')) + assert node_complete_stub.call_count == len(self.content_nodes) + + def test_generator_template_node_complete_not_called_for_single_resource_second_time(self): with patch.object(PluginLoaderStub, 'node_complete') as node_complete_stub: gen = Generator(self.site) + gen.generate_all() + assert node_complete_stub.call_count == len(self.content_nodes) gen.generate_resource_at_path(self.site.content.source_folder.child('about.html')) - assert node_complete_stub.call_count == 0 + assert node_complete_stub.call_count == len(self.content_nodes) # No extra calls def test_generator_template_begin_text_resource_called(self): @@ -214,6 +273,8 @@ class TestPlugins(object): with patch.object(PluginLoaderStub, 'begin_text_resource') as begin_text_resource_stub: begin_text_resource_stub.return_value = '' gen = Generator(self.site) + gen.generate_all() + begin_text_resource_stub.reset_mock() path = self.site.content.source_folder.child('about.html') gen.generate_resource_at_path(path) @@ -235,6 +296,8 @@ class TestPlugins(object): with patch.object(PluginLoaderStub, 'begin_binary_resource') as begin_binary_resource_stub: gen = Generator(self.site) + gen.generate_all() + begin_binary_resource_stub.reset_mock() path = self.site.content.source_folder.child('favicon.ico') gen.generate_resource_at_path(path)