Skip to content

Commit

Permalink
Fixed #72: template settings for collections is now effective.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed May 18, 2015
1 parent 45aa119 commit 64d097a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
20 changes: 16 additions & 4 deletions logya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
Collection = namedtuple('Collection', ['docs', 'template'])


def get_collection_var(url, collection_map):
"""Get collections var from site.yaml if url is in a subdirectory of the
paths defined in collections."""

parent_url = '/'.join(url.split('/')[:-1])
if parent_url in collection_map:
return collection_map[parent_url]


class Logya(object):
"""Main logic for creating, building and serving a static site."""

Expand Down Expand Up @@ -75,7 +84,10 @@ def init_env(self):
'rss': self.config['content']['rss']['template']
}

self.collection_urls = self.config['collections'].keys()
# Map collection paths to vars to make collecion settings accessible
# via index URLs.
self.collection_paths = {
v['path']: k for k, v in self.config['collections'].items()}

def info(self, msg):
"""Print message if in verbose mode."""
Expand Down Expand Up @@ -106,9 +118,9 @@ def _update_index(self, doc, url=None):
# If a collection is set in site.yaml for this URL, use the
# corresponding template if set.
template = self.templates['index']
# FIXME 'tags/tag1' is not in collection_urls whereas 'tags' is
if fullpath in self.collection_urls:
template = self.config['collections'][fullpath].get(
var = get_collection_var(fullpath, self.collection_paths)
if var:
template = self.config['collections'][var].get(
'template', template)

self.index[fullpath] = Collection(docs=[], template=template)
Expand Down
6 changes: 4 additions & 2 deletions logya/sites/starter/site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ site:
author: Author


# Settings that affect collections in the document index.
# Settings that affect collections in the document index. Top-level keys of
# collections can be used as document attributes for grouping it in the
# corresponding collecion.
collections:
tags:
path: tags
Expand All @@ -15,7 +17,7 @@ collections:
# Content specific settings, at the moment only templates are specified.
content:
index:
template: index.html
template: index.html # default template used for collections
doc:
template: page.html
rss:
Expand Down
19 changes: 10 additions & 9 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def setUp(self):
'site': {
'base_url': 'http://localhost:8080'
},
'content': [
{'index': {'template': 'index.html'}},
{'doc': {'template': 'page.html'}},
{'rss': {'template': 'rss2.xml'}}
],
'collections': [
{'var': 'tags', 'path': 'tags'}
]
}
'content': {
'index': {'template': 'index.html'},
'doc': {'template': 'page.html'},
'rss': {'template': 'rss2.xml'}
},
'collections': {
'tags': {'path': 'tags', 'template': 'tag.html'},
'shoptags': {'path': 'shop/tags'}
}
}
26 changes: 26 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from . import LogyaBaseTestCase
from logya.core import get_collection_var


class TestIndex(LogyaBaseTestCase):

def test_get_collection_var(self):
collection_paths = {
v['path']: k for k, v in self.config['collections'].items()}

testdata = [
{'input': 'post', 'expected': None},
{'input': 'tags', 'expected': None},
{'input': 'tags/tag1', 'expected': 'tags'},
{'input': 'tags/tag-all', 'expected': 'tags'},
{'input': 'tags/tag2', 'expected': 'tags'},
{'input': 'shop/tags/tag1', 'expected': 'shoptags'},
{'input': 'shop/tags/tag-all', 'expected': 'shoptags'},
{'input': 'shop/tags/tag2', 'expected': 'shoptags'}
]

for data in testdata:
self.assertEquals(
data['expected'],
get_collection_var(data['input'], collection_paths))

0 comments on commit 64d097a

Please sign in to comment.