From 64d097a3e9362db2ba381cfdefa99f133ac296b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20G=C3=B3mez?= Date: Mon, 18 May 2015 22:00:54 +0200 Subject: [PATCH] Fixed #72: template settings for collections is now effective. --- logya/core.py | 20 ++++++++++++++++---- logya/sites/starter/site.yaml | 6 ++++-- tests/__init__.py | 19 ++++++++++--------- tests/test_index.py | 26 ++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 tests/test_index.py diff --git a/logya/core.py b/logya/core.py index 3d6fb25..fb1033c 100644 --- a/logya/core.py +++ b/logya/core.py @@ -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.""" @@ -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.""" @@ -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) diff --git a/logya/sites/starter/site.yaml b/logya/sites/starter/site.yaml index e08b6f5..3c66e82 100644 --- a/logya/sites/starter/site.yaml +++ b/logya/sites/starter/site.yaml @@ -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 @@ -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: diff --git a/tests/__init__.py b/tests/__init__.py index b7a6ab7..5738a2a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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'} + } + } \ No newline at end of file diff --git a/tests/test_index.py b/tests/test_index.py new file mode 100644 index 0000000..3d9a73d --- /dev/null +++ b/tests/test_index.py @@ -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))