Skip to content

Commit

Permalink
Finish list page builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jul 10, 2015
1 parent b810dea commit 81cb6dd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
18 changes: 15 additions & 3 deletions handroll/extensions/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, **kwargs):
self.source_file = kwargs['source_file']
self.summary = kwargs['summary']
self.title = kwargs['title']
self.route = kwargs['route']
self.url = kwargs['url']


Expand Down Expand Up @@ -78,12 +79,15 @@ def on_frontmatter_loaded(self, source_file, frontmatter):
'{blog_value}').format(blog_value=is_post))
# TODO: Validate that the post has the required fields.
# TODO: add dirty checking so the output won't write all the time.
# TODO: Log some messages to show that the feed and list page generate.
if is_post:
post = BlogPost(
date=frontmatter['date'],
source_file=source_file,
summary=frontmatter.get('summary'),
title=frontmatter['title'],
# TODO: get route from resolver.
route='/a_source_file.html',
url=self._resolver.as_url(source_file),
)
self.posts[source_file] = post
Expand Down Expand Up @@ -170,11 +174,19 @@ class ListPageBuilder(BlogBuilder):

def __init__(self, template):
self._template = template
self._blog_list = ''

def add(self, posts):
"""Add the posts and generate a blog list."""
# TODO: Generate the HTML.
li_html = []
for post in posts:
li_html.append(
'<li><a href="{route}">{title}</a></li>\n'.format(
route=post.route, title=post.title))
self._blog_list = ''.join(li_html)

def _generate_output(self):
# TODO: Fetch the template and render it.
return ''
context = {
'blog_list': self._blog_list,
}
return self._template.render(context)
1 change: 1 addition & 0 deletions handroll/tests/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def make_blog_post(self):
'source_file': 'a_source_file.md',
'summary': 'The post summary',
'title': 'A Blog Post',
'route': '/a_source_file.html',
'url': 'http://www.example.com/a_source_file.html',
}
return BlogPost(**kwargs)
Expand Down
29 changes: 29 additions & 0 deletions handroll/tests/test_blog_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,32 @@ def test_title_type_is_html(self):
post = self.factory.make_blog_post()
builder.add([post])
self.assertEqual('html', builder._feed.entries[0].title_type)


class TestListPageBuilder(TestCase):

def test_output_is_rendered(self):
mock_template = mock.Mock()
mock_template.render.return_value = 'fake HTML'
builder = ListPageBuilder(mock_template)
output = builder._generate_output()
self.assertEqual('fake HTML', output)

def test_blog_list_in_context(self):
mock_template = mock.Mock()
builder = ListPageBuilder(mock_template)
builder._blog_list = 'fake li items'
builder._generate_output()
context = mock_template.render.call_args[0][0]
self.assertEqual('fake li items', context['blog_list'])

def test_adds_posts_to_blog_list_html(self):
post = self.factory.make_blog_post()
another = self.factory.make_blog_post()
another.title = 'Another Blog Post'
builder = ListPageBuilder(None)
builder.add([post, another])
self.assertEqual(
'<li><a href="/a_source_file.html">A Blog Post</a></li>\n'
'<li><a href="/a_source_file.html">Another Blog Post</a></li>\n',
builder._blog_list)

0 comments on commit 81cb6dd

Please sign in to comment.