Attach all post resources to all page resources. Now `hyde gen' works. Also fix up a couple of the unit tests.main
@@ -1,6 +1,6 @@ | |||
# -*- coding: utf-8 -*- | |||
""" | |||
Pagination plugin. Groups a sorted set of resources into pages and supplies | |||
Paginator plugin. Groups a sorted set of resources into pages and supplies | |||
each page to a copy of the original resource. | |||
""" | |||
import os | |||
@@ -30,7 +30,7 @@ class Paginator: | |||
def _relative_url(self, source_path, number, basename, ext): | |||
""" | |||
Create a new URL for a new page. The first page keeps the same name; | |||
the subsequent pages are formatted according to file_pattern. | |||
the subsequent pages are named according to file_pattern. | |||
""" | |||
path = File(source_path) | |||
if number != 1: | |||
@@ -40,21 +40,37 @@ class Paginator: | |||
path = path.parent.child(os.path.normpath(filename)) | |||
return path | |||
def _new_resource(self, base_resource, node, page): | |||
def _new_resource(self, base_resource, node, page_number): | |||
""" | |||
Create a new resource as a copy of a base_resource, with a page of | |||
resources associated with it. | |||
""" | |||
res = Resource(base_resource.source_file, node) | |||
res.page = page | |||
page.resource = res | |||
path = self._relative_url(base_resource.relative_path, | |||
page.number, | |||
page_number, | |||
base_resource.source_file.name_without_extension, | |||
base_resource.source_file.extension) | |||
res.set_relative_deploy_path(path) | |||
return res | |||
@staticmethod | |||
def _attach_page_to_resource(page, resource): | |||
""" | |||
Hook up a page and a resource. | |||
""" | |||
resource.page = page | |||
page.resource = resource | |||
@staticmethod | |||
def _add_dependencies_to_resource(dependencies, resource): | |||
""" | |||
Add a bunch of resources as dependencies to another resource. | |||
""" | |||
if not hasattr(resource, 'depends'): | |||
resource.depends = [] | |||
resource.depends.extend([dep.relative_path for dep in dependencies | |||
if dep.relative_path not in resource.depends]) | |||
def _walk_pages_in_node(self, node): | |||
""" | |||
Segregate each resource into a page. | |||
@@ -77,8 +93,16 @@ class Paginator: | |||
""" | |||
added_resources = [] | |||
pages = list(self._walk_pages_in_node(node)) | |||
for page in pages: | |||
added_resources.append(self._new_resource(resource, node, page)) | |||
deps = reduce(list.__add__, [page.posts for page in pages], []) | |||
Paginator._attach_page_to_resource(pages[0], resource) | |||
Paginator._add_dependencies_to_resource(deps, resource) | |||
for page in pages[1:]: | |||
# make new resource | |||
new_resource = self._new_resource(resource, node, page.number) | |||
Paginator._attach_page_to_resource(page, new_resource) | |||
new_resource.depends = resource.depends | |||
added_resources.append(new_resource) | |||
for prev, next in pairwalk(pages): | |||
next.previous = prev | |||
@@ -115,14 +139,10 @@ class PaginatorPlugin(Plugin): | |||
def begin_site(self): | |||
for node in self.site.content.walk(): | |||
added_resources = [] | |||
removed_resources = [] | |||
paged_resources = (res for res in node.resources | |||
if hasattr(res.meta, 'paginator')) | |||
for resource in paged_resources: | |||
paginator = Paginator(resource.meta.paginator) | |||
removed_resources.append(resource) | |||
added_resources += paginator.walk_paged_resources(node, resource) | |||
node.resources += added_resources | |||
for removed in removed_resources: | |||
node.resources.remove(removed) |
@@ -13,7 +13,7 @@ from hyde.site import Site | |||
TEST_SITE = File(__file__).parent.parent.child_folder('_test') | |||
class TestTagger(object): | |||
class TestPaginator(object): | |||
def setUp(self): | |||
TEST_SITE.make() | |||
@@ -22,10 +22,10 @@ class TestTagger(object): | |||
self.s = Site(TEST_SITE) | |||
self.deploy = TEST_SITE.child_folder('deploy') | |||
gen = Generator(self.s) | |||
gen.load_site_if_needed() | |||
gen.load_template_if_needed() | |||
gen.generate_all() | |||
self.gen = Generator(self.s) | |||
self.gen.load_site_if_needed() | |||
self.gen.load_template_if_needed() | |||
self.gen.generate_all() | |||
def tearDown(self): | |||
@@ -42,30 +42,41 @@ class TestTagger(object): | |||
page5 = File(self.deploy.child('page5/pages_of_one.txt')) | |||
assert not page5.exists | |||
def test_pages_of_one_content(self): | |||
expected_page1_content = dedent('''\ | |||
Another Sad Post | |||
page2/pages_of_one.txt | |||
''') | |||
page2/pages_of_one.txt''') | |||
expected_page2_content = dedent('''\ | |||
A Happy Post | |||
page2/pages_of_one.txt | |||
page3/pages_of_one.txt | |||
''') | |||
pages_of_one.txt | |||
page3/pages_of_one.txt''') | |||
expected_page3_content = dedent('''\ | |||
An Angry Post | |||
page3/pages_of_one.txt | |||
page4/pages_of_one.txt | |||
''') | |||
page2/pages_of_one.txt | |||
page4/pages_of_one.txt''') | |||
expected_page4_content = dedent('''\ | |||
A Sad Post | |||
page4/pages_of_one.txt | |||
page3/pages_of_one.txt | |||
''') | |||
page1 = self.deploy.child('pages_of_one.txt') | |||
content = File(page1).read_all() | |||
assert expected_page1_content == content | |||
page2 = self.deploy.child('page2/pages_of_one.txt') | |||
content = File(page2).read_all() | |||
assert expected_page2_content == content | |||
page3 = self.deploy.child('page3/pages_of_one.txt') | |||
content = File(page3).read_all() | |||
assert expected_page3_content == content | |||
page4 = self.deploy.child('page4/pages_of_one.txt') | |||
content = File(page4).read_all() | |||
assert expected_page4_content == content | |||
def test_pages_of_ten(self): | |||
page1 = self.deploy.child('pages_of_ten.txt') | |||
@@ -75,7 +86,18 @@ class TestTagger(object): | |||
assert not File(page2).exists | |||
def test_pages_of_one_content(self): | |||
def test_pages_of_ten_depends(self): | |||
depends = self.gen.deps['pages_of_ten.txt'] | |||
assert depends | |||
assert len(depends) == 4 | |||
assert 'blog/sad-post.html' in depends | |||
assert 'blog/another-sad-post.html' in depends | |||
assert 'blog/angry-post.html' in depends | |||
assert 'blog/happy-post.html' in depends | |||
def test_pages_of_ten_content(self): | |||
expected_content = dedent('''\ | |||
Another Sad Post | |||
A Happy Post | |||
@@ -88,9 +110,20 @@ class TestTagger(object): | |||
assert expected_content == content | |||
def text_custom_file_pattern(self): | |||
page1 = 'custom_file_pattern.txt' | |||
page2 = 'custom_file_pattern-2.txt' | |||
def test_pages_of_one_depends(self): | |||
depends = self.gen.deps['pages_of_one.txt'] | |||
assert depends | |||
assert len(depends) == 4 | |||
assert 'blog/sad-post.html' in depends | |||
assert 'blog/another-sad-post.html' in depends | |||
assert 'blog/angry-post.html' in depends | |||
assert 'blog/happy-post.html' in depends | |||
def test_custom_file_pattern(self): | |||
page1 = self.deploy.child('custom_file_pattern.txt') | |||
page2 = self.deploy.child('custom_file_pattern-2.txt') | |||
assert File(page1).exists | |||
assert File(page2).exists |
@@ -2,7 +2,7 @@ | |||
paginator: | |||
sorter: time | |||
size: 2 | |||
file_pattern: $FILE-$PAGE.$EXT | |||
file_pattern: $FILE-$PAGE$EXT | |||
--- | |||
{% for res in resource.page.posts %} | |||
{{ res.meta.title }} | |||
@@ -4,10 +4,8 @@ media_url: /media # URL where the media files are served from. | |||
base_url: / # The base url for autogenerated links. | |||
plugins: | |||
- hyde.ext.plugins.meta.MetaPlugin | |||
- hyde.ext.plugins.auto_extend.AutoExtendPlugin | |||
- hyde.ext.plugins.sorter.SorterPlugin | |||
- hyde.ext.plugins.paginator.PaginatorPlugin | |||
- hyde.ext.plugins.textlinks.TextlinksPlugin | |||
meta: | |||
nodemeta: meta.yaml | |||
created: !!timestamp 2010-01-01 00:00:00 | |||