From 33c60a78cd897c5ff662b9c3f7da2e4bb003222f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 6 Mar 2013 10:06:42 -0500 Subject: [PATCH] pelican/utils: Add split_all() and rework static URL generation I think the conversion from native paths to URLs is best put off until we are actually trying to generate the URL. The old handling (introduced in 2692586, Fixes #645 - Making cross-content linking windows compatible, 2012-12-19) converted the path at StaticContent initialization, which left you with a bogus StaticContent.src. Once we drop the 'static' subdirectory, we will be able to drop the `dest` and `url` parts from the StaticGenerator.generate_context() handling, which will leave things looking a good deal cleaner than they do now. --- pelican/contents.py | 6 ++++-- pelican/generators.py | 11 +++++++---- pelican/utils.py | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/pelican/contents.py b/pelican/contents.py index 185d80c4..1f659de7 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -16,7 +16,8 @@ from datetime import datetime from pelican import signals from pelican.settings import _DEFAULT_CONFIG from pelican.utils import (slugify, truncate_html_words, memoized, strftime, - python_2_unicode_compatible, deprecated_attribute) + python_2_unicode_compatible, deprecated_attribute, + split_all) # Import these so that they're avalaible when you import from pelican.contents. from pelican.urlwrappers import (URLWrapper, Author, Category, Tag) # NOQA @@ -136,8 +137,9 @@ class Content(object): def url_format(self): """Returns the URL, formatted with the proper values""" metadata = copy.copy(self.metadata) + path = self.metadata.get('path', self.get_relative_source_path()) metadata.update({ - 'path': self.metadata.get('path', self.get_relative_source_path()), + 'path': '/'.join(split_all(path)), 'slug': getattr(self, 'slug', ''), 'lang': getattr(self, 'lang', 'en'), 'date': getattr(self, 'date', datetime.now()), diff --git a/pelican/generators.py b/pelican/generators.py index d39db188..099a2a8d 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -25,6 +25,7 @@ from pelican.contents import ( from pelican.readers import read_file from pelican.utils import copy, process_translations, mkdir_p from pelican import signals +import pelican.utils logger = logging.getLogger(__name__) @@ -518,13 +519,15 @@ class StaticGenerator(Generator): for f in self.get_files( os.path.join(self.path, static_path), extensions=False): f_rel = os.path.relpath(f, self.path) - # On Windows, make sure we end up with Unix-like paths. - if os.name == 'nt': - f_rel = f_rel.replace('\\', '/') # TODO remove this hardcoded 'static' subdirectory + dest = os.path.join('static', f_rel) + url = '/'.join(pelican.utils.split_all(dest)) sc = Static( content=None, - metadata={'save_as': os.path.join('static', f_rel)}, + metadata={ + 'save_as': dest, + 'url': url, + }, settings=self.settings, source_path=f_rel) self.staticfiles.append(sc) diff --git a/pelican/utils.py b/pelican/utils.py index 2278984d..4e1c8156 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -480,3 +480,24 @@ def mkdir_p(path): except OSError as e: if e.errno != errno.EEXIST or not os.path.isdir(path): raise + + +def split_all(path): + """Split a path into a list of components + + While os.path.split() splits a single component off the back of + `path`, this function splits all components: + + >>> split_all(os.path.join('a', 'b', 'c')) + ['a', 'b', 'c'] + """ + components = [] + while path: + head,tail = os.path.split(path) + if tail: + components.insert(0, tail) + elif head == path: + components.insert(0, head) + break + path = head + return components