From 49668f711a451e6fa2caa67a938d5e4b12172a51 Mon Sep 17 00:00:00 2001 From: Elana Hashman Date: Fri, 2 Jan 2015 00:03:18 -0700 Subject: [PATCH] Generate {tag}-style links on pages correctly. Fixes #1513 --- pelican/contents.py | 4 +- .../page_with_category_and_tag_links.md | 7 ++++ pelican/tests/test_contents.py | 38 ++++++++++++++++--- pelican/tests/test_generators.py | 23 +++++++++++ 4 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 pelican/tests/TestPages/page_with_category_and_tag_links.md diff --git a/pelican/contents.py b/pelican/contents.py index c3d1230b..69c01438 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -257,9 +257,9 @@ class Content(object): 'limit_msg': ("Other resources were not found " "and their urls not replaced")}) elif what == 'category': - origin = Category(path, self.settings).url + origin = '/'.join((siteurl, Category(path, self.settings).url)) elif what == 'tag': - origin = Tag(path, self.settings).url + origin = '/'.join((siteurl, Tag(path, self.settings).url)) # keep all other parts, such as query, fragment, etc. parts = list(value) diff --git a/pelican/tests/TestPages/page_with_category_and_tag_links.md b/pelican/tests/TestPages/page_with_category_and_tag_links.md new file mode 100644 index 00000000..6806a570 --- /dev/null +++ b/pelican/tests/TestPages/page_with_category_and_tag_links.md @@ -0,0 +1,7 @@ +Title: Page with a bunch of links + +My links: + +[Link 1]({tag}マック) + +[Link 2]({category}Yeah) diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index 01ee9ca2..de297985 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -212,17 +212,20 @@ class TestPage(unittest.TestCase): 'link') page = Page(**args) content = page.get_content('http://notmyidea.org') - self.assertEqual(content, ('A simple test, with a ' - 'link')) + self.assertEqual( + content, + ('A simple test, with a ' + 'link')) # Category args['content'] = ('A simple test, with a ' 'link') page = Page(**args) content = page.get_content('http://notmyidea.org') - self.assertEqual(content, - ('A simple test, with a ' - 'link')) + self.assertEqual( + content, + ('A simple test, with a ' + 'link')) def test_intrasite_link(self): # type does not take unicode in PY2 and bytes in PY3, which in @@ -543,6 +546,31 @@ class TestStatic(unittest.TestCase): self.assertEqual(self.static.save_as, expected_save_as) self.assertEqual(self.static.url, path_to_url(expected_save_as)) + def test_tag_link_syntax(self): + "{tag} link syntax triggers url replacement." + + html = 'link' + page = Page( + content=html, + metadata={'title': 'fakepage'}, settings=self.settings, + source_path=os.path.join('dir', 'otherdir', 'fakepage.md'), + context=self.context) + content = page.get_content('') + + self.assertNotEqual(content, html) + + def test_category_link_syntax(self): + "{category} link syntax triggers url replacement." + + html = 'link' + page = Page(content=html, + metadata={'title': 'fakepage'}, settings=self.settings, + source_path=os.path.join('dir', 'otherdir', 'fakepage.md'), + context=self.context) + content = page.get_content('') + + self.assertNotEqual(content, html) + class TestURLWrapper(unittest.TestCase): def test_comparisons(self): diff --git a/pelican/tests/test_generators.py b/pelican/tests/test_generators.py index 4be1b35e..c3e36bc1 100644 --- a/pelican/tests/test_generators.py +++ b/pelican/tests/test_generators.py @@ -428,6 +428,7 @@ class TestPageGenerator(unittest.TestCase): ['This is a markdown test page', 'published', 'page'], ['This is a test page with a preset template', 'published', 'custom'], + ['Page with a bunch of links', 'published', 'page'], ['A Page (Test) for sorting', 'published', 'page'], ] hidden_pages_expected = [ @@ -517,6 +518,7 @@ class TestPageGenerator(unittest.TestCase): ['This is a test page', 'published', 'page'], ['This is a markdown test page', 'published', 'page'], ['A Page (Test) for sorting', 'published', 'page'], + ['Page with a bunch of links', 'published', 'page'], ['This is a test page with a preset template', 'published', 'custom'], ] @@ -530,6 +532,7 @@ class TestPageGenerator(unittest.TestCase): # sort by title pages_expected_sorted_by_title = [ ['A Page (Test) for sorting', 'published', 'page'], + ['Page with a bunch of links', 'published', 'page'], ['This is a markdown test page', 'published', 'page'], ['This is a test page', 'published', 'page'], ['This is a test page with a preset template', 'published', @@ -543,6 +546,26 @@ class TestPageGenerator(unittest.TestCase): pages = self.distill_pages(generator.pages) self.assertEqual(pages_expected_sorted_by_title, pages) + def test_tag_and_category_links_on_generated_pages(self): + """ + Test to ensure links of the form {tag}tagname and {category}catname + are generated correctly on pages + """ + settings = get_settings(filenames={}) + settings['PAGE_PATHS'] = ['TestPages'] # relative to CUR_DIR + settings['CACHE_PATH'] = self.temp_cache + settings['DEFAULT_DATE'] = (1970, 1, 1) + + generator = PagesGenerator( + context=settings.copy(), settings=settings, + path=CUR_DIR, theme=settings['THEME'], output_path=None) + generator.generate_context() + pages_by_title = {p.title: p.content for p in generator.pages} + + test_content = pages_by_title['Page with a bunch of links'] + self.assertIn('', test_content) + self.assertIn('', test_content) + class TestTemplatePagesGenerator(unittest.TestCase):