From e5c0a54c57eb7d9cd718a382f66977ea8becf24a Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Tue, 24 Sep 2013 14:19:47 +0200 Subject: [PATCH 1/5] Removed obsolete comment. --- pelican/contents.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index b453f61b..1858013c 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -197,10 +197,6 @@ class Content(object): value = m.group('value') origin = m.group('path') - # we support only filename for now. the plan is to support - # categories, tags, etc. in the future, but let's keep things - # simple for now. - # XXX Put this in a different location. if what == 'filename': if value.startswith('/'): From 7d43c4fa00fa31c9ef445a5f923255da870bf650 Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Tue, 24 Sep 2013 15:18:09 +0200 Subject: [PATCH 2/5] Support for params and fragments in intrasite links. Adresses #1063. --- pelican/contents.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index 1858013c..83fbad8f 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -5,6 +5,7 @@ import six import copy import locale import logging +import urlparse import functools import os import re @@ -194,30 +195,36 @@ class Content(object): def replacer(m): what = m.group('what') - value = m.group('value') + value = urlparse.urlparse(m.group('value')) + path = value.path origin = m.group('path') # XXX Put this in a different location. if what == 'filename': - if value.startswith('/'): - value = value[1:] + if path.startswith('/'): + path = path[1:] else: # relative to the source path of this content - value = self.get_relative_source_path( - os.path.join(self.relative_dir, value) + path = self.get_relative_source_path( + os.path.join(self.relative_dir, path) ) - if value in self._context['filenames']: + if path in self._context['filenames']: origin = '/'.join((siteurl, - self._context['filenames'][value].url)) - origin = origin.replace('\\', '/') # Fow windows paths. + self._context['filenames'][path].url)) + origin = origin.replace('\\', '/') # for Windows paths. else: logger.warning("Unable to find {fn}, skipping url" - " replacement".format(fn=value)) + " replacement".format(fn=path)) elif what == 'category': - origin = Category(value, self.settings).url + origin = Category(path, self.settings).url elif what == 'tag': - origin = Tag(value, self.settings).url + origin = Tag(path, self.settings).url + + # keep all other parts, such as query, fragment, etc. + parts = list(value) + parts[2] = origin + origin = urlparse.urlunparse(parts) return ''.join((m.group('markup'), m.group('quote'), origin, m.group('quote'))) From 6fb0335269e9d9dbd79c2f4cb020cd9a6b03313a Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Wed, 25 Sep 2013 11:39:29 +0200 Subject: [PATCH 3/5] Attempt to be compilant with Python 3. --- pelican/contents.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index 83fbad8f..dbc33716 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -5,12 +5,16 @@ import six import copy import locale import logging -import urlparse import functools import os import re import sys +try: + from urlparse import urlparse, urlunparse +except ImportError: + from urllib.parse import urlparse, urlunparse + from datetime import datetime @@ -195,7 +199,7 @@ class Content(object): def replacer(m): what = m.group('what') - value = urlparse.urlparse(m.group('value')) + value = urlparse(m.group('value')) path = value.path origin = m.group('path') @@ -224,7 +228,7 @@ class Content(object): # keep all other parts, such as query, fragment, etc. parts = list(value) parts[2] = origin - origin = urlparse.urlunparse(parts) + origin = urlunparse(parts) return ''.join((m.group('markup'), m.group('quote'), origin, m.group('quote'))) From 7415d370e6a9c6f9b96da927685ffe9a6bc6eada Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Wed, 25 Sep 2013 16:13:28 +0200 Subject: [PATCH 4/5] Added tests. --- pelican/tests/test_contents.py | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index 936903c1..2e44253a 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -204,6 +204,66 @@ class TestPage(unittest.TestCase): ('A simple test, with a ' 'link')) + def test_intrasite_link(self): + article = type(b'_DummyArticle', (object,), {'url': 'article.html'}) + + args = self.page_kwargs.copy() + args['settings'] = get_settings() + args['source_path'] = 'content' + args['context']['filenames'] = {'article.rst': article} + + # Classic intrasite link via filename + args['content'] = ( + 'A simple test, with a ' + 'link' + ) + content = Page(**args).get_content('http://notmyidea.org') + self.assertEquals( + content, + 'A simple test, with a ' + 'link' + ) + + # fragment + args['content'] = ( + 'A simple test, with a ' + 'link' + ) + content = Page(**args).get_content('http://notmyidea.org') + self.assertEquals( + content, + 'A simple test, with a ' + 'link' + ) + + # query + args['content'] = ( + 'A simple test, with a ' + 'link' + ) + content = Page(**args).get_content('http://notmyidea.org') + self.assertEquals( + content, + 'A simple test, with a ' + 'link' + ) + + # combination + args['content'] = ( + 'A simple test, with a ' + 'link' + ) + content = Page(**args).get_content('http://notmyidea.org') + self.assertEquals( + content, + 'A simple test, with a ' + 'link' + ) + class TestArticle(TestPage): def test_template(self): From 6ed23fec7dc1c907b9dcc3eb3d08eca61c1bb9b5 Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Wed, 25 Sep 2013 16:31:23 +0200 Subject: [PATCH 5/5] Tuned the tests so they are PY3 compilant. --- pelican/tests/test_contents.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index 2e44253a..437d0228 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import six from datetime import datetime from sys import platform @@ -205,7 +206,10 @@ class TestPage(unittest.TestCase): 'link')) def test_intrasite_link(self): - article = type(b'_DummyArticle', (object,), {'url': 'article.html'}) + # type does not take unicode in PY2 and bytes in PY3, which in + # combination with unicode literals leads to following insane line: + cls_name = '_DummyArticle' if six.PY3 else b'_DummyArticle' + article = type(cls_name, (object,), {'url': 'article.html'}) args = self.page_kwargs.copy() args['settings'] = get_settings()