Skip to content

Commit

Permalink
Merge pull request #3085 from getnikola/category_destpath_names_compa…
Browse files Browse the repository at this point in the history
…t_for_follow

Support CATEGORY_DESTPATH_NAMES with pages following destpath
  • Loading branch information
Kwpolska committed May 10, 2018
2 parents 866d38f + 41232c1 commit 9e63e14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -14,6 +14,7 @@ Bugfixes

* Make ``smartjoin`` more flexible (Issue #3080)
* Make post-list and post_list synonymous (Issue #3083)
* Support ``CATEGORY_DESTPATH_NAMES`` with pages following destpath
* Make ``CATEGORY_PAGES_FOLLOW_DESTPATH`` more resilient (Issue #3081)
* Guard against null items in gallery meta files (Issues #3076, #3077)
* Respect ``USE_FILENAME_AS_TITLE`` in galleries with a meta file
Expand Down
1 change: 1 addition & 0 deletions nikola/conf.py.in
Expand Up @@ -385,6 +385,7 @@ HIDDEN_CATEGORIES = []
# CATEGORY_DESTPATH_NAMES = {
# DEFAULT_LANG: {
# 'webdev': 'Web Development',
# 'webdev/django': 'Web Development/Django',
# 'random': 'Odds and Ends',
# },
# }
Expand Down
25 changes: 23 additions & 2 deletions nikola/plugins/task/categories.py
Expand Up @@ -84,6 +84,16 @@ def set_site(self, site):
self.template_for_single_list = "tagindex.tmpl" if self.show_list_as_index else "tag.tmpl"
self.translation_manager = utils.ClassificationTranslationManager()

# Needed to undo names for CATEGORY_PAGES_FOLLOW_DESTPATH
self.destpath_names_reverse = {}
for lang in self.site.config['TRANSLATIONS']:
self.destpath_names_reverse[lang] = {}
for k, v in self.site.config['CATEGORY_DESTPATH_NAMES'](lang).items():
self.destpath_names_reverse[lang][v] = k
self.destpath_names_reverse = utils.TranslatableSetting(
'_CATEGORY_DESTPATH_NAMES_REVERSE', self.destpath_names_reverse,
self.site.config['TRANSLATIONS'])

def is_enabled(self, lang=None):
"""Return True if this taxonomy is enabled, or False otherwise."""
return True
Expand Down Expand Up @@ -125,17 +135,28 @@ def slugify_category_name(self, path, lang):
def get_path(self, classification, lang, dest_type='page'):
"""Return a path for the given classification."""
cat_string = '/'.join(classification)
classification_raw = classification # needed to undo CATEGORY_DESTPATH_NAMES
destpath_names_reverse = self.destpath_names_reverse(lang)
if self.site.config['CATEGORY_PAGES_FOLLOW_DESTPATH']:
base_dir = None
for post in self.site.posts_per_category[cat_string]:
if post.category_from_destpath:
base_dir = post.folder_base(lang)
# Handle CATEGORY_DESTPATH_NAMES
if cat_string in destpath_names_reverse:
cat_string = destpath_names_reverse[cat_string]
classification_raw = cat_string.split('/')
break
# fallback: first POSTS entry + classification

if not self.site.config['CATEGORY_DESTPATH_TRIM_PREFIX']:
# If prefixes are not trimmed, we'll already have the base_dir in classification_raw
base_dir = ''

if base_dir is None:
# fallback: first POSTS entry + classification
base_dir = self.site.config['POSTS'][0][1]
base_dir_list = base_dir.split(os.sep)
sub_dir = [self.slugify_tag_name(part, lang) for part in classification]
sub_dir = [self.slugify_tag_name(part, lang) for part in classification_raw]
return [_f for _f in (base_dir_list + sub_dir) if _f], 'auto'
else:
return [_f for _f in [self.site.config['CATEGORY_PATH'](lang)] if _f] + self.slugify_category_name(
Expand Down

0 comments on commit 9e63e14

Please sign in to comment.