From ae8cf9defdaae215f9b3b514436db35f8375b0f9 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 25 Oct 2012 22:43:58 +0200 Subject: [PATCH 1/4] refactor the check of executable for unit tests pandoc was checked directly with a `os.system` call, and the output with version and copyright of pandoc was displayed when running tests. - replace the pandoc check with the `skipIfNoExecutable` function. - in `skipIfNoExecutable`, the `valid_exit_code` is not needed, the executable is not found if an `OSError` exception is catched. --- tests/support.py | 14 +++++++------- tests/test_importer.py | 9 +++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/support.py b/tests/support.py index e5bfcca1..13bbfb15 100644 --- a/tests/support.py +++ b/tests/support.py @@ -114,7 +114,6 @@ def mute(returns_output=False): return decorator - def get_article(title, slug, content, lang, extra_metadata=None): metadata = {'slug': slug, 'title': title, 'lang': lang} if extra_metadata is not None: @@ -122,19 +121,20 @@ def get_article(title, slug, content, lang, extra_metadata=None): return Article(content, metadata=metadata) -def skipIfNoExecutable(executable, valid_exit_code=1): - """Tries to run an executable to make sure it's in the path, Skips the tests - if not found. +def skipIfNoExecutable(executable): + """Skip test if `executable` is not found + + Tries to run `executable` with subprocess to make sure it's in the path, + and skips the tests if not found (if subprocess raises a `OSError`). """ - # calling with no params the command should exit with 1 with open(os.devnull, 'w') as fnull: try: res = subprocess.call(executable, stdout=fnull, stderr=fnull) except OSError: res = None - if res != valid_exit_code: - return unittest.skip('{0} compiler not found'.format(executable)) + if res is None: + return unittest.skip('{0} executable not found'.format(executable)) return lambda func: func diff --git a/tests/test_importer.py b/tests/test_importer.py index 5504b12e..d4ff8205 100644 --- a/tests/test_importer.py +++ b/tests/test_importer.py @@ -3,25 +3,24 @@ import os from pelican.tools.pelican_import import wp2fields, fields2pelican -from .support import unittest, temporary_folder, mute +from .support import unittest, temporary_folder, mute, skipIfNoExecutable CUR_DIR = os.path.dirname(__file__) WORDPRESS_XML_SAMPLE = os.path.join(CUR_DIR, 'content', 'wordpressexport.xml') -PANDOC = os.system('pandoc --version') == 0 try: import BeautifulSoup except ImportError: BeautifulSoup = False # NOQA +@skipIfNoExecutable(['pandoc', '--version']) +@unittest.skipUnless(BeautifulSoup, 'Needs BeautifulSoup module') class TestWordpressXmlImporter(unittest.TestCase): def setUp(self): self.posts = wp2fields(WORDPRESS_XML_SAMPLE) - @unittest.skipUnless(PANDOC and BeautifulSoup, - 'Needs Pandoc and BeautifulSoup') def test_ignore_empty_posts(self): posts = list(self.posts) @@ -29,8 +28,6 @@ class TestWordpressXmlImporter(unittest.TestCase): for title, content, fname, date, author, categ, tags, format in posts: self.assertTrue(title.strip()) - @unittest.skipUnless(PANDOC and BeautifulSoup, - 'Needs Pandoc and BeautifulSoup') def test_can_toggle_raw_html_code_parsing(self): posts = list(self.posts) From 318518ee1f0d384c62ad48d4c4d827ef70d090ed Mon Sep 17 00:00:00 2001 From: Edward Delaporte Date: Sun, 28 Oct 2012 14:32:48 -0500 Subject: [PATCH 2/4] Added a second link to the issue tracker - for those who jump directly to the section without seeing the earlier section. --- docs/faq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/faq.rst b/docs/faq.rst index e76bea6a..4225ab5f 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -21,7 +21,7 @@ How can I help? ================ There are several ways to help out. First, you can use Pelican and report any -suggestions or problems you might have via IRC or the issue tracker. +suggestions or problems you might have via IRC or the `issue tracker `_. If you want to contribute, please fork `the git repository `_, create a new feature branch, make From 47c972e21a6f3f118cfac142503a7722e80199e7 Mon Sep 17 00:00:00 2001 From: Martin Brochhaus Date: Sun, 23 Sep 2012 10:04:51 +0200 Subject: [PATCH 3/4] Added USE_FOLDER_AS_CATEGORY setting. This allows users to organize their files in ways where the subfolder name would not make a good category name (i.e. /2012/09/). Set this to ``False`` and the subfolder will no longer be used as a standard category, `DEFAULT_CATEGORY` will be used instead. --- docs/getting_started.rst | 4 +++- docs/settings.rst | 4 ++++ pelican/contents.py | 3 ++- pelican/generators.py | 11 +++++++---- pelican/settings.py | 1 + 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 93c626c0..09840e8a 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -176,7 +176,9 @@ Note that, aside from the title, none of this metadata is mandatory: if the date is not specified, Pelican will rely on the file's "mtime" timestamp, and the category can be determined by the directory in which the file resides. For example, a file located at ``python/foobar/myfoobar.rst`` will have a category of -``foobar``. +``foobar``. If you would like to organize your files in other ways where the +name of the subfolder would not be a good category name, you can set the +setting ``USE_FOLDER_AS_CATEGORY`` to ``False``. Generate your blog ------------------ diff --git a/docs/settings.rst b/docs/settings.rst index af6bc8c0..1e3704d2 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -34,6 +34,10 @@ Setting name (default value) What doe `DATE_FORMATS` (``{}``) If you do manage multiple languages, you can set the date formatting here. See "Date format and locales" section below for details. +`USE_FOLDER_AS_CATEGORY` (``True``) When you don't specify a category in your post metadata and set this + setting to ``True`` and organize your articles in subfolders, the + subfolder will become the category of your post. If set to ``False`` + ``DEFAULT_CATEGORY`` will be used as a fallback. `DEFAULT_CATEGORY` (``'misc'``) The default category to fall back on. `DEFAULT_DATE_FORMAT` (``'%a %d %B %Y'``) The default date format you want to use. `DISPLAY_PAGES_ON_MENU` (``True``) Whether to display pages on the menu of the diff --git a/pelican/contents.py b/pelican/contents.py index 0dee19f3..74a66934 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -121,7 +121,8 @@ class Page(object): 'lang': getattr(self, 'lang', 'en'), 'date': getattr(self, 'date', datetime.now()), 'author': self.author, - 'category': getattr(self, 'category', 'misc'), + 'category': getattr(self, 'category', + self.settings['DEFAULT_CATEGORY']), } def _expand_settings(self, key): diff --git a/pelican/generators.py b/pelican/generators.py index fdec93fa..c38409a1 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -280,11 +280,14 @@ 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) == article_path: # if the article is not in a subdirectory - category = self.settings['DEFAULT_CATEGORY'] - else: + if (self.settings['USE_FOLDER_AS_CATEGORY'] + and os.path.dirname(f) != article_path): + # if the article is in a subdirectory category = os.path.basename(os.path.dirname(f))\ - .decode('utf-8') + .decode('utf-8') + else: + # if the article is not in a subdirectory + category = self.settings['DEFAULT_CATEGORY'] if category != '': metadata['category'] = Category(category, self.settings) diff --git a/pelican/settings.py b/pelican/settings.py index 4d1ed81e..610d0733 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -34,6 +34,7 @@ _DEFAULT_CONFIG = {'PATH': '.', 'PDF_GENERATOR': False, 'OUTPUT_SOURCES': False, 'OUTPUT_SOURCES_EXTENSION': '.text', + 'USE_FOLDER_AS_CATEGORY': True, 'DEFAULT_CATEGORY': 'misc', 'DEFAULT_DATE': 'fs', 'WITH_FUTURE_DATES': True, From c0ed9e1cffc413c386816cc1c4b23fc6d6b5a994 Mon Sep 17 00:00:00 2001 From: Bruno Binet Date: Sun, 28 Oct 2012 20:54:39 +0100 Subject: [PATCH 4/4] add test for USE_FOLDER_AS_CATEGORY setting (#517) --- tests/test_generators.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_generators.py b/tests/test_generators.py index 374172d8..8caa7213 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -85,6 +85,20 @@ class TestArticlesGenerator(unittest.TestCase): categories_expected = ['Default', 'TestCategory', 'Yeah', 'test', 'yeah'] self.assertEquals(categories, categories_expected) + def test_do_not_use_folder_as_category(self): + + settings = _DEFAULT_CONFIG.copy() + settings['ARTICLE_DIR'] = 'content' + settings['DEFAULT_CATEGORY'] = 'Default' + settings['USE_FOLDER_AS_CATEGORY'] = False + generator = ArticlesGenerator(settings.copy(), settings, + CUR_DIR, _DEFAULT_CONFIG['THEME'], None, + _DEFAULT_CONFIG['MARKUP']) + generator.generate_context() + + categories = [cat.name for cat, _ in generator.categories] + self.assertEquals(categories, ['Default', 'Yeah', 'test', 'yeah']) + def test_direct_templates_save_as_default(self): settings = _DEFAULT_CONFIG.copy()