Fixes wrongly generated relative urls for pagination.

When `RELATIVE_URLS` is `True` and the `PAGINATION_PATTERNS` adds folder to the path, the relative urls to these pages does not respect these additional folders.

Example:
	Settings:
			RELATIVE_URLS = True
			PAGINATION_PATTERNS = (
					(1, '{base_name}/', '{base_name}/index.html'),
					(2, '{base_name}/page/{number}/', '{base_name}/page/{number}/index.html'),)

	Theme:
			<link href="{{ SITEURL }}/theme/css/style.css" rel="stylesheet">

If you are on page 2 then "{{ SITEURL }}/theme/css/style.css" expands to "./theme/css/style.css" instead of "./../../theme/css/style.css".

Fix:
Simply compute the relative url from the paginated url instead of the not paginated one.
This commit is contained in:
Bernhard Scheirle 2014-06-16 16:02:11 +02:00
commit af0899ae2a

View file

@ -151,6 +151,9 @@ class Writer(object):
def _write_file(template, localcontext, output_path, name, override): def _write_file(template, localcontext, output_path, name, override):
"""Render the template write the file.""" """Render the template write the file."""
# set localsiteurl for context so that Contents can adjust links
if localcontext['localsiteurl']:
context['localsiteurl'] = localcontext['localsiteurl']
output = template.render(localcontext) output = template.render(localcontext)
path = os.path.join(output_path, name) path = os.path.join(output_path, name)
try: try:
@ -166,14 +169,16 @@ class Writer(object):
# local context. # local context.
signals.content_written.send(path, context=localcontext) signals.content_written.send(path, context=localcontext)
localcontext = context.copy() def _get_localcontext(context, name, kwargs, relative_urls):
if relative_urls: localcontext = context.copy()
relative_url = path_to_url(get_relative_path(name)) localcontext['localsiteurl'] = localcontext.get('localsiteurl', None)
context['localsiteurl'] = relative_url if relative_urls:
localcontext['SITEURL'] = relative_url relative_url = path_to_url(get_relative_path(name))
localcontext['SITEURL'] = relative_url
localcontext['output_file'] = name localcontext['localsiteurl'] = relative_url
localcontext.update(kwargs) localcontext['output_file'] = name
localcontext.update(kwargs)
return localcontext
# pagination # pagination
if paginated: if paginated:
@ -184,7 +189,7 @@ class Writer(object):
# generated pages, and write # generated pages, and write
for page_num in range(list(paginators.values())[0].num_pages): for page_num in range(list(paginators.values())[0].num_pages):
paginated_localcontext = localcontext.copy() paginated_kwargs = kwargs.copy()
for key in paginators.keys(): for key in paginators.keys():
paginator = paginators[key] paginator = paginators[key]
previous_page = paginator.page(page_num) \ previous_page = paginator.page(page_num) \
@ -192,15 +197,17 @@ class Writer(object):
page = paginator.page(page_num + 1) page = paginator.page(page_num + 1)
next_page = paginator.page(page_num + 2) \ next_page = paginator.page(page_num + 2) \
if page_num + 1 < paginator.num_pages else None if page_num + 1 < paginator.num_pages else None
paginated_localcontext.update( paginated_kwargs.update(
{'%s_paginator' % key: paginator, {'%s_paginator' % key: paginator,
'%s_page' % key: page, '%s_page' % key: page,
'%s_previous_page' % key: previous_page, '%s_previous_page' % key: previous_page,
'%s_next_page' % key: next_page}) '%s_next_page' % key: next_page})
_write_file(template, paginated_localcontext, self.output_path, localcontext = _get_localcontext(context, page.save_as, paginated_kwargs, relative_urls)
_write_file(template, localcontext, self.output_path,
page.save_as, override_output) page.save_as, override_output)
else: else:
# no pagination # no pagination
localcontext = _get_localcontext(context, name, kwargs, relative_urls)
_write_file(template, localcontext, self.output_path, name, _write_file(template, localcontext, self.output_path, name,
override_output) override_output)