From 792bd0a6b556204aa501adbf88a2f10a13dc950b Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Tue, 25 Mar 2014 23:27:53 +0100 Subject: [PATCH] Always use slashes for urls (ref #81). --- sigal/gallery.py | 10 ++++++---- sigal/utils.py | 9 +++++++++ sigal/writer.py | 4 +++- tests/test_utils.py | 6 +++++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/sigal/gallery.py b/sigal/gallery.py index 9f039fb..64dbc86 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -42,7 +42,7 @@ from .compat import UnicodeMixin, strxfrm from .image import process_image, get_exif_tags from .log import colored, BLUE from .settings import get_thumb, get_orig -from .utils import copy, check_or_create_dir +from .utils import copy, check_or_create_dir, url_from_path from .video import process_video from .writer import Writer @@ -67,6 +67,7 @@ class Media(UnicodeMixin): def __init__(self, filename, path, settings): self.filename = filename + self.url = filename self.settings = settings self.file_path = join(path, filename) self.src_path = join(settings['source'], path, filename) @@ -182,8 +183,8 @@ class Album(UnicodeMixin): self.url_ext = self.output_file if settings['index_in_url'] else '' self.url = self.name + '/' + self.url_ext - self.index_url = os.path.relpath(settings['destination'], - self.dst_path) + '/' + self.url_ext + self.index_url = url_from_path(os.path.relpath( + settings['destination'], self.dst_path)) + '/' + self.url_ext # sort sub-albums dirnames.sort(key=strxfrm, reverse=settings['albums_sort_reverse']) @@ -355,7 +356,8 @@ class Album(UnicodeMixin): if path == '.': break - url = os.path.relpath(path, self.path) + '/' + self.url_ext + url = (url_from_path(os.path.relpath(path, self.path)) + '/' + + self.url_ext) breadcrumb.append((url, self.gallery.albums[path].title)) breadcrumb.reverse() diff --git a/sigal/utils.py b/sigal/utils.py index 99328cf..52600c8 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -17,3 +17,12 @@ def check_or_create_dir(path): if not os.path.isdir(path): os.makedirs(path) + + +def url_from_path(path): + """Transform path to url, converting backslashes to slashes if needed.""" + + if os.sep == '/': + return path + else: + return '/'.join(path.split(os.sep)) diff --git a/sigal/writer.py b/sigal/writer.py index 94d3581..56ea662 100644 --- a/sigal/writer.py +++ b/sigal/writer.py @@ -34,6 +34,7 @@ from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PrefixLoader from jinja2.exceptions import TemplateNotFound from .pkgmeta import __url__ as sigal_link +from .utils import url_from_path THEMES_PATH = os.path.normpath(os.path.join( os.path.abspath(os.path.dirname(__file__)), 'themes')) @@ -99,7 +100,8 @@ class Writer(object): 'settings': self.settings, 'sigal_link': sigal_link, 'theme': {'name': os.path.basename(self.theme), - 'url': os.path.relpath(self.theme_path, album.dst_path)}, + 'url': url_from_path(os.path.relpath(self.theme_path, + album.dst_path))}, } def write(self, album): diff --git a/tests/test_utils.py b/tests/test_utils.py index 4ecbad3..08e7e26 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import os -from sigal.utils import copy, check_or_create_dir +from sigal.utils import copy, check_or_create_dir, url_from_path CURRENT_DIR = os.path.dirname(__file__) SAMPLE_DIR = os.path.join(CURRENT_DIR, 'sample') @@ -32,3 +32,7 @@ def test_check_or_create_dir(tmpdir): path = str(tmpdir.join('new_directory')) check_or_create_dir(path) assert os.path.isdir(path) + + +def test_url_from_path(): + assert url_from_path(os.sep.join(['foo', 'bar'])) == 'foo/bar'