mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
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.
This commit is contained in:
parent
49bf80ec39
commit
33c60a78cd
3 changed files with 32 additions and 6 deletions
|
|
@ -16,7 +16,8 @@ from datetime import datetime
|
||||||
from pelican import signals
|
from pelican import signals
|
||||||
from pelican.settings import _DEFAULT_CONFIG
|
from pelican.settings import _DEFAULT_CONFIG
|
||||||
from pelican.utils import (slugify, truncate_html_words, memoized, strftime,
|
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.
|
# Import these so that they're avalaible when you import from pelican.contents.
|
||||||
from pelican.urlwrappers import (URLWrapper, Author, Category, Tag) # NOQA
|
from pelican.urlwrappers import (URLWrapper, Author, Category, Tag) # NOQA
|
||||||
|
|
@ -136,8 +137,9 @@ class Content(object):
|
||||||
def url_format(self):
|
def url_format(self):
|
||||||
"""Returns the URL, formatted with the proper values"""
|
"""Returns the URL, formatted with the proper values"""
|
||||||
metadata = copy.copy(self.metadata)
|
metadata = copy.copy(self.metadata)
|
||||||
|
path = self.metadata.get('path', self.get_relative_source_path())
|
||||||
metadata.update({
|
metadata.update({
|
||||||
'path': self.metadata.get('path', self.get_relative_source_path()),
|
'path': '/'.join(split_all(path)),
|
||||||
'slug': getattr(self, 'slug', ''),
|
'slug': getattr(self, 'slug', ''),
|
||||||
'lang': getattr(self, 'lang', 'en'),
|
'lang': getattr(self, 'lang', 'en'),
|
||||||
'date': getattr(self, 'date', datetime.now()),
|
'date': getattr(self, 'date', datetime.now()),
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ from pelican.contents import (
|
||||||
from pelican.readers import read_file
|
from pelican.readers import read_file
|
||||||
from pelican.utils import copy, process_translations, mkdir_p
|
from pelican.utils import copy, process_translations, mkdir_p
|
||||||
from pelican import signals
|
from pelican import signals
|
||||||
|
import pelican.utils
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
@ -518,13 +519,15 @@ class StaticGenerator(Generator):
|
||||||
for f in self.get_files(
|
for f in self.get_files(
|
||||||
os.path.join(self.path, static_path), extensions=False):
|
os.path.join(self.path, static_path), extensions=False):
|
||||||
f_rel = os.path.relpath(f, self.path)
|
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
|
# TODO remove this hardcoded 'static' subdirectory
|
||||||
|
dest = os.path.join('static', f_rel)
|
||||||
|
url = '/'.join(pelican.utils.split_all(dest))
|
||||||
sc = Static(
|
sc = Static(
|
||||||
content=None,
|
content=None,
|
||||||
metadata={'save_as': os.path.join('static', f_rel)},
|
metadata={
|
||||||
|
'save_as': dest,
|
||||||
|
'url': url,
|
||||||
|
},
|
||||||
settings=self.settings,
|
settings=self.settings,
|
||||||
source_path=f_rel)
|
source_path=f_rel)
|
||||||
self.staticfiles.append(sc)
|
self.staticfiles.append(sc)
|
||||||
|
|
|
||||||
|
|
@ -480,3 +480,24 @@ def mkdir_p(path):
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno != errno.EEXIST or not os.path.isdir(path):
|
if e.errno != errno.EEXIST or not os.path.isdir(path):
|
||||||
raise
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue