Skip to content
Permalink
Browse files

Improved path generation.

  • Loading branch information
felixfontein committed Oct 20, 2016
1 parent 0953956 commit d3f05d990fdad792d4bd9df5e247911327865405
@@ -578,15 +578,16 @@ def get_list_path(self, lang, type='page'):
Must return one or two values (in this order):
* a list or tuple of strings: the path relative to OUTPUT_DIRECTORY;
* a boolean indicating whether INDEX_FILE should always be added or not;
* a string with values 'auto', 'always' or 'never', indicating whether
INDEX_FILE should be added or not.
Note that this function must always return a list or tuple of strings;
the other return value is optional with default value `False`.
the other return value is optional with default value `'auto'`.
In case INDEX_FILE should not be added, the last element in the returned
path must have no extension, and the PRETTY_URLS config must be ignored
by this handler. The return value will be modified based on the
PRETTY_URLS and INDEX_FILE settings.
In case INDEX_FILE should potentially be added, the last element in the
returned path must have no extension, and the PRETTY_URLS config must
be ignored by this handler. The return value will be modified based on
the PRETTY_URLS and INDEX_FILE settings.
Type can be either 'page', 'feed' (for Atom feed) or 'rss'.
"""
@@ -597,18 +598,19 @@ def get_path(self, classification, lang, type='page'):
Must return one to three values (in this order):
* a list or tuple of strings: the path relative to OUTPUT_DIRECTORY;
* a boolean indicating whether INDEX_FILE should always be added or not;
* a string with values 'auto', 'always' or 'never', indicating whether
INDEX_FILE should be added or not;
* an integer if a specific page of the index is to be targeted (will be
ignored for post lists), or `None` if the most current page is targeted.
Note that this function must always return a list or tuple of strings;
the other two return values are optional with default values `False` and
the other two return values are optional with default values `'auto'` and
`None`.
In case INDEX_FILE should not be added, the last element in the returned
path must have no extension, and the PRETTY_URLS config must be ignored
by this handler. The return value will be modified based on the
PRETTY_URLS and INDEX_FILE settings.
In case INDEX_FILE should potentially be added, the last element in the
returned path must have no extension, and the PRETTY_URLS config must
be ignored by this handler. The return value will be modified based on
the PRETTY_URLS and INDEX_FILE settings.
Type can be either 'page', 'feed' (for Atom feed) or 'rss'.
@@ -205,21 +205,22 @@ def _compute_number_of_pages(self, filtered_posts, posts_count):
"""Given a list of posts and the maximal number of posts per page, computes the number of pages needed."""
return min(1, (len(filtered_posts) + posts_count - 1) // posts_count)

def _postprocess_path(self, path, lang, always_append_index=False, type='page', page_info=None):
def _postprocess_path(self, path, lang, append_index='auto', type='page', page_info=None):
# Forcing extension for Atom feeds and RSS feeds
force_extension = None
if type == 'feed':
force_extension = '.atom'
elif type == 'rss':
force_extension = '.xml'
# Determine how to extend path
path = [_f for _f in path if _f]
if force_extension is not None:
if len(path) == 0 and type == 'rss':
path = ['rss']
elif len(path) == 0 or always_append_index:
elif len(path) == 0 or append_index == 'always':
path = path + [os.path.splitext(self.site.config['INDEX_FILE'])[0]]
path[-1] += force_extension
elif self.site.config['PRETTY_URLS'] or len(path) == 0 or always_append_index:
elif (self.site.config['PRETTY_URLS'] and append_index != 'never') or len(path) == 0 or append_index == 'always':
path = path + [self.site.config['INDEX_FILE']]
else:
path[-1] += '.html'
@@ -239,13 +240,13 @@ def _parse_path_result(result):
if not isinstance(result[0], (list, tuple)):
# The result must be a list or tuple of strings. Wrap into a tuple
result = (result, )
return result[0], result[1] if len(result) > 1 else False, result[2] if len(result) > 2 else None
return result[0], result[1] if len(result) > 1 else 'auto', result[2] if len(result) > 2 else None

def _taxonomy_index_path(self, lang, taxonomy):
"""Return path to the classification overview."""
result = taxonomy.get_list_path(lang)
path, append_index, _ = self._parse_path_result(result)
return self._postprocess_path(path, lang, always_append_index=append_index, type='list')
return self._postprocess_path(path, lang, append_index=append_index, type='list')

def _taxonomy_path(self, name, lang, taxonomy, type='page'):
"""Return path to a classification."""
@@ -261,7 +262,7 @@ def _taxonomy_path(self, name, lang, taxonomy, type='page'):
number_of_pages = self._compute_number_of_pages(self._get_filtered_list(name, lang), self.site.config['INDEX_DISPLAY_POST_COUNT'])
self.site.page_count_per_classification[taxonomy.classification_name][lang][name] = number_of_pages
page_info = (page, number_of_pages)
return self._postprocess_path(path, lang, always_append_index=append_index, type=type, page_info=page_info)
return self._postprocess_path(path, lang, append_index=append_index, type=type, page_info=page_info)

def _taxonomy_atom_path(self, name, lang, taxonomy):
"""Return path to a classification Atom feed."""
@@ -111,11 +111,11 @@ def get_path(self, classification, lang, type='page'):
components = [self.site.config['ARCHIVE_PATH']]
if classification:
components.extend(classification)
always_add_index = True
add_index = 'always'
else:
components.append(os.path.splitext(self.site.config['ARCHIVE_FILENAME'])[0])
always_add_index = False
return [_f for _f in components if _f], always_add_index
add_index = 'never'
return [_f for _f in components if _f], add_index

def extract_hierarchy(self, classification):
"""Given a classification, return a list of parts in the hierarchy."""
@@ -160,7 +160,7 @@ def provide_context_and_uptodate(self, classification, lang):
"pagekind": [page_kind, "archive_page"],
}
if page_kind == 'index':
context["archive_name"] = classification
context["archive_name"] = classification if classification else None
context["is_feed_stale"] = kw["is_feed_stale"]
kw.update(context)
return context, kw
@@ -74,15 +74,15 @@ def get_classification_printable_name(self, author, lang, only_last_component=Fa

def get_list_path(self, lang, type='page'):
"""A path handler for the list of all classifications."""
return [self.site.config['AUTHOR_PATH']], True
return [self.site.config['AUTHOR_PATH']], 'always'

def get_path(self, author, lang, type='page'):
"""A path handler for the given classification."""
if self.site.config['SLUG_AUTHOR_PATH']:
slug = utils.slugify(author, lang)
else:
slug = author
return [self.site.config['AUTHOR_PATH'], slug], False
return [self.site.config['AUTHOR_PATH'], slug], 'auto'

def provide_list_context_and_uptodate(self, lang):
"""Provide data for the context and the uptodate list for the list of all classifiations."""
@@ -79,7 +79,7 @@ def get_path(self, classification, lang, type='page'):
page_number = int(classification)
except:
pass
return [self.site.config['INDEX_PATH']], True, page_number
return [self.site.config['INDEX_PATH']], 'always', page_number

def provide_context_and_uptodate(self, classification, lang):
"""Provide data for the context and the uptodate list for the list of the given classifiation."""
@@ -71,7 +71,7 @@ def get_classification_printable_name(self, hierarchy, lang, only_last_component

def get_path(self, hierarchy, lang, type='page'):
"""A path handler for the given classification."""
return hierarchy, True
return hierarchy, 'always'

def extract_hierarchy(self, dirname):
"""Given a classification, return a list of parts in the hierarchy."""
@@ -80,7 +80,7 @@ def get_classification_printable_name(self, section, lang, only_last_component=F

def get_path(self, section, lang, type='page'):
"""A path handler for the given classification."""
return [_f for _f in [self.site.config['TRANSLATIONS'][lang], section] if _f], True
return [_f for _f in [self.site.config['TRANSLATIONS'][lang], section] if _f], 'always'

def provide_context_and_uptodate(self, section, lang):
"""Provide data for the context and the uptodate list for the list of the given classifiation.

0 comments on commit d3f05d9

Please sign in to comment.