From 4efca13c33b8bf122cc56d78478f49557ab52fff Mon Sep 17 00:00:00 2001 From: draftcode Date: Fri, 16 Mar 2012 15:04:06 +0900 Subject: [PATCH 1/2] Fix a bug that ArticlesGenerator doesn't see DEFAULT_CATEGORY. When you set ARTICLE_DIR which is not equal to PATH, ArticlesGenerator doesn't use DEFAULT_CATEGORY but use ARTICLE_DIR's dirname. --- pelican/generators.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pelican/generators.py b/pelican/generators.py index 4e756213..d7ebb0b0 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -219,9 +219,10 @@ class ArticlesGenerator(Generator): def generate_context(self): """change the context""" + article_path = os.path.join(self.path, self.settings['ARTICLE_DIR']) all_articles = [] for f in self.get_files( - os.path.join(self.path, self.settings['ARTICLE_DIR']), + article_path, exclude=self.settings['ARTICLE_EXCLUDES']): try: content, metadata = read_file(f, settings=self.settings) @@ -232,7 +233,7 @@ class ArticlesGenerator(Generator): # if no category is set, use the name of the path as a category if 'category' not in metadata: - if os.path.dirname(f) == self.path: + if os.path.dirname(f) == article_path: category = self.settings['DEFAULT_CATEGORY'] else: category = os.path.basename(os.path.dirname(f))\ From cffb44891a8eb436fb4070735873ee774a26eb75 Mon Sep 17 00:00:00 2001 From: draftcode Date: Sat, 24 Mar 2012 13:52:03 +0900 Subject: [PATCH 2/2] Add an test related to articles' category. --- .../TestCategory/article_with_category.rst | 6 +++++ .../TestCategory/article_without_category.rst | 4 +++ tests/content/article_without_category.rst | 6 +++++ tests/test_generators.py | 25 ++++++++++++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/content/TestCategory/article_with_category.rst create mode 100644 tests/content/TestCategory/article_without_category.rst create mode 100644 tests/content/article_without_category.rst diff --git a/tests/content/TestCategory/article_with_category.rst b/tests/content/TestCategory/article_with_category.rst new file mode 100644 index 00000000..7ed6e6d1 --- /dev/null +++ b/tests/content/TestCategory/article_with_category.rst @@ -0,0 +1,6 @@ +This is an article with category ! +################################## + +:category: yeah + +This article should be in 'yeah' category. diff --git a/tests/content/TestCategory/article_without_category.rst b/tests/content/TestCategory/article_without_category.rst new file mode 100644 index 00000000..4bc5d78d --- /dev/null +++ b/tests/content/TestCategory/article_without_category.rst @@ -0,0 +1,4 @@ +This is an article without category ! +##################################### + +This article should be in 'TestCategory' category. diff --git a/tests/content/article_without_category.rst b/tests/content/article_without_category.rst new file mode 100644 index 00000000..ff47f6ef --- /dev/null +++ b/tests/content/article_without_category.rst @@ -0,0 +1,6 @@ + +This is an article without category ! +##################################### + +This article should be in the DEFAULT_CATEGORY. + diff --git a/tests/test_generators.py b/tests/test_generators.py index e30b0c98..45461cb7 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- from mock import MagicMock +import os from pelican.generators import ArticlesGenerator from pelican.settings import _DEFAULT_CONFIG from .support import unittest +CUR_DIR = os.path.dirname(__file__) class TestArticlesGenerator(unittest.TestCase): @@ -13,7 +15,7 @@ class TestArticlesGenerator(unittest.TestCase): generator = ArticlesGenerator(None, {'FEED': _DEFAULT_CONFIG['FEED']}, None, _DEFAULT_CONFIG['THEME'], None, - None) + _DEFAULT_CONFIG['MARKUP']) writer = MagicMock() generator.generate_feeds(writer) writer.write_feed.assert_called_with([], None, 'feeds/all.atom.xml') @@ -23,3 +25,24 @@ class TestArticlesGenerator(unittest.TestCase): writer = MagicMock() generator.generate_feeds(writer) self.assertFalse(writer.write_feed.called) + + def test_generate_context(self): + + settings = _DEFAULT_CONFIG + settings['ARTICLE_DIR'] = 'content' + settings['DEFAULT_CATEGORY'] = 'Default' + generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR, + _DEFAULT_CONFIG['THEME'], None, + _DEFAULT_CONFIG['MARKUP']) + generator.generate_context() + for article in generator.articles: + relfilepath = os.path.relpath(article.filename, CUR_DIR) + if relfilepath == os.path.join("TestCategory", + "article_with_category.rst"): + self.assertEquals(article.category.name, 'yeah') + elif relfilepath == os.path.join("TestCategory", + "article_without_category.rst"): + self.assertEquals(article.category.name, 'TestCategory') + elif relfilepath == "article_without_category.rst": + self.assertEquals(article.category.name, 'Default') +