From a0e46c91066bd8338d0e067875d6e040fec88477 Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Tue, 26 Jun 2012 19:26:43 -0700 Subject: [PATCH 1/4] Add support for `status: hidden` in pages Resolves #380 If the status metadata is set to 'hidden' on a page it is translated and rendered but not linked anywhere in the site. --- docs/getting_started.rst | 4 ++++ pelican/generators.py | 17 +++++++++++++++-- samples/content/pages/hidden_page.rst | 9 +++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 samples/content/pages/hidden_page.rst diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 8accf658..580c43a1 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -136,6 +136,10 @@ generate static pages. Then, use the `DISPLAY_PAGES_ON_MENU` setting, which will add all the pages to the menu. +If you want to exclude any pages from being linked to or listed in the menu +then add a ``status: hidden`` attribute to it's metadata. This is useful for +things like making error pages that fit the generated theme of your site. + Importing an existing blog -------------------------- diff --git a/pelican/generators.py b/pelican/generators.py index 1ddc13c2..6790fcd6 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -357,10 +357,13 @@ class PagesGenerator(Generator): def __init__(self, *args, **kwargs): self.pages = [] + self.hidden_pages = [] + self.hidden_translations = [] super(PagesGenerator, self).__init__(*args, **kwargs) def generate_context(self): all_pages = [] + hidden_pages = [] for f in self.get_files( os.path.join(self.path, self.settings['PAGE_DIR']), exclude=self.settings['PAGE_EXCLUDES']): @@ -373,15 +376,25 @@ class PagesGenerator(Generator): filename=f) if not is_valid_content(page, f): continue - all_pages.append(page) + if page.status == "published": + all_pages.append(page) + elif page.status == "hidden": + hidden_pages.append(page) + else: + logger.warning(u"Unknown status %s for file %s, skipping it." % + (repr(unicode.encode(article.status, 'utf-8')), + repr(f))) + self.pages, self.translations = process_translations(all_pages) + self.hidden_pages, self.hidden_translations = process_translations(hidden_pages) self._update_context(('pages', )) self.context['PAGES'] = self.pages def generate_output(self, writer): - for page in chain(self.translations, self.pages): + for page in chain(self.translations, self.pages, + self.hidden_translations, self.hidden_pages): writer.write_file(page.save_as, self.get_template('page'), self.context, page=page, relative_urls=self.settings.get('RELATIVE_URLS')) diff --git a/samples/content/pages/hidden_page.rst b/samples/content/pages/hidden_page.rst new file mode 100644 index 00000000..ab8704ed --- /dev/null +++ b/samples/content/pages/hidden_page.rst @@ -0,0 +1,9 @@ +This is a test hidden page +########################## + +:category: test +:status: hidden + +This is great for things like error(404) pages +Anyone can see this page but it's not linked to anywhere! + From de251bc9998b1245238e69707c21a0dd5143756b Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Tue, 26 Jun 2012 19:51:48 -0700 Subject: [PATCH 2/4] Fixes typo in error message for bad status Bugfix #380 We want the bad status of page, not article. --- pelican/generators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pelican/generators.py b/pelican/generators.py index 6790fcd6..4e9312cc 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -382,7 +382,7 @@ class PagesGenerator(Generator): hidden_pages.append(page) else: logger.warning(u"Unknown status %s for file %s, skipping it." % - (repr(unicode.encode(article.status, 'utf-8')), + (repr(unicode.encode(page.status, 'utf-8')), repr(f))) From c2993c4d4e19b1eceb3c367c38005dd89d04b53c Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Wed, 27 Jun 2012 07:02:25 -0700 Subject: [PATCH 3/4] Documentation typo --- docs/getting_started.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 580c43a1..96038c2a 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -137,7 +137,7 @@ Then, use the `DISPLAY_PAGES_ON_MENU` setting, which will add all the pages to the menu. If you want to exclude any pages from being linked to or listed in the menu -then add a ``status: hidden`` attribute to it's metadata. This is useful for +then add a ``status: hidden`` attribute to its metadata. This is useful for things like making error pages that fit the generated theme of your site. Importing an existing blog From cf696939f8c49d54c8b6f5e6fa13422e3278734e Mon Sep 17 00:00:00 2001 From: tBunnyMan Date: Wed, 4 Jul 2012 10:06:53 -0700 Subject: [PATCH 4/4] Added tests for page generation. Currently tests rst & markdown generation, ignoring pages w/ bad status, and status hidden vs published --- tests/TestPages/bad_page.rst | 8 +++++ tests/TestPages/hidden_page.rst | 8 +++++ tests/TestPages/hidden_page_markdown.md | 12 +++++++ tests/TestPages/page.rst | 4 +++ tests/TestPages/page_markdown.md | 9 +++++ tests/test_generators.py | 44 ++++++++++++++++++++++++- 6 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tests/TestPages/bad_page.rst create mode 100644 tests/TestPages/hidden_page.rst create mode 100644 tests/TestPages/hidden_page_markdown.md create mode 100644 tests/TestPages/page.rst create mode 100644 tests/TestPages/page_markdown.md diff --git a/tests/TestPages/bad_page.rst b/tests/TestPages/bad_page.rst new file mode 100644 index 00000000..bc62948b --- /dev/null +++ b/tests/TestPages/bad_page.rst @@ -0,0 +1,8 @@ +This is a test bad page +####################### + +:status: invalid + +The quick brown fox jumped over the lazy dog's back. + +The status here is invalid, the page should not render. diff --git a/tests/TestPages/hidden_page.rst b/tests/TestPages/hidden_page.rst new file mode 100644 index 00000000..57ca329c --- /dev/null +++ b/tests/TestPages/hidden_page.rst @@ -0,0 +1,8 @@ +This is a test hidden page +########################## + +:status: hidden + +The quick brown fox jumped over the lazy dog's back. + +This page is hidden diff --git a/tests/TestPages/hidden_page_markdown.md b/tests/TestPages/hidden_page_markdown.md new file mode 100644 index 00000000..1e532fe7 --- /dev/null +++ b/tests/TestPages/hidden_page_markdown.md @@ -0,0 +1,12 @@ +title: This is a markdown test hidden page +status: hidden + +Test Markdown File Header +========================= + +Used for pelican test +--------------------- + +The quick brown fox jumped over the lazy dog's back. + +This page is hidden diff --git a/tests/TestPages/page.rst b/tests/TestPages/page.rst new file mode 100644 index 00000000..2d13976d --- /dev/null +++ b/tests/TestPages/page.rst @@ -0,0 +1,4 @@ +This is a test page +################### + +The quick brown fox jumped over the lazy dog's back. diff --git a/tests/TestPages/page_markdown.md b/tests/TestPages/page_markdown.md new file mode 100644 index 00000000..d5416a6f --- /dev/null +++ b/tests/TestPages/page_markdown.md @@ -0,0 +1,9 @@ +title: This is a markdown test page + +Test Markdown File Header +========================= + +Used for pelican test +--------------------- + +The quick brown fox jumped over the lazy dog's back. diff --git a/tests/test_generators.py b/tests/test_generators.py index e62551fa..cc84c80f 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -4,7 +4,7 @@ from mock import MagicMock import os import re -from pelican.generators import ArticlesGenerator, LessCSSGenerator +from pelican.generators import ArticlesGenerator, LessCSSGenerator, PagesGenerator from pelican.settings import _DEFAULT_CONFIG from .support import unittest, temporary_folder, skipIfNoExecutable @@ -94,6 +94,48 @@ class TestArticlesGenerator(unittest.TestCase): write.assert_called_count == 0 +class TestPageGenerator(unittest.TestCase): + """ + Every time you want to test for a new field; + Make sure the test pages in "TestPages" have all the fields + Add it to distilled in distill_pages_for_test + Then update the assertItemsEqual in test_generate_context to match expected + """ + + def distill_pages_for_test(self, pages): + distilled = [] + for page in pages: + distilled.append([ + page.title, + page.status + ] + ) + return distilled + + def test_generate_context(self): + settings = _DEFAULT_CONFIG.copy() + + settings['PAGE_DIR'] = 'TestPages' + generator = PagesGenerator(settings.copy(), settings, CUR_DIR, + _DEFAULT_CONFIG['THEME'], None, + _DEFAULT_CONFIG['MARKUP']) + generator.generate_context() + pages = self.distill_pages_for_test(generator.pages) + hidden_pages = self.distill_pages_for_test(generator.hidden_pages) + + pages_expected = [ + [u'This is a test page', 'published'], + [u'This is a markdown test page', 'published'] + ] + hidden_pages_expected = [ + [u'This is a test hidden page', 'hidden'], + [u'This is a markdown test hidden page', 'hidden'] + ] + + self.assertItemsEqual(pages_expected,pages) + self.assertItemsEqual(hidden_pages_expected,hidden_pages) + + class TestLessCSSGenerator(unittest.TestCase): LESS_CONTENT = """