Skip to content

Commit

Permalink
Preparing conversion of archive plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Oct 18, 2016
1 parent c15e75e commit a31939b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
20 changes: 20 additions & 0 deletions nikola/plugin_categories.py
Expand Up @@ -484,6 +484,13 @@ class Taxonomy(BasePlugin):
# If True, the list for a classification includes all posts with a
# sub-classification (in case has_hierarchy is True).
include_posts_from_subhierarchies = False
# If not False, for every classification which has at least one
# subclassification, create a list of subcategories instead of a list/index
# of posts. This is only used when has_hierarchy = True. If not False, this
# must be the template name for the list; usually "list.tmpl".
# If this is set to a string, it is recommended to set
# include_posts_from_subhierarchies to True to get correct post counts.
show_list_as_subcategories_list = False
# Whether to show the posts for one classification as an index or
# as a post list.
show_list_as_index = False
Expand Down Expand Up @@ -541,6 +548,19 @@ def sort_classifications(self, classifications, lang):
"""
pass

def get_classification_printable_name(self, classification, lang, only_last_component=False):
"""Extract a printable name from the classification.
For hierarchical taxonomies, the result of extract_hierarchy is provided
as `classification`. For non-hierarchical taxonomies, the classification
string itself is provided as `classification`.
The argument `only_last_component` is only relevant to hierarchical
taxonomies. If it is set, the printable name should only describe the
last component of `classification` if possible.
"""
raise NotImplementedError()

def get_list_path(self, lang, type='page'):
"""A path handler for the list of all classifications.
Expand Down
4 changes: 4 additions & 0 deletions nikola/plugins/task/authors.py
Expand Up @@ -67,6 +67,10 @@ def classify(self, post, lang):
"""Classify the given post for the given language."""
return [post.author()]

def get_classification_printable_name(self, author, lang, only_last_component=False):
"""Extract a printable name from the classification."""
return author

def get_list_path(self, lang, type='page'):
"""A path handler for the list of all classifications."""
return [self.site.config['AUTHOR_PATH']], True
Expand Down
4 changes: 4 additions & 0 deletions nikola/plugins/task/indexes.py
Expand Up @@ -59,6 +59,10 @@ def classify(self, post, lang):
"""Classify the given post for the given language."""
return [""]

def get_classification_printable_name(self, classification, lang, only_last_component=False):
"""Extract a printable name from the classification."""
return self.site.config["BLOG_TITLE"](lang)

def get_path(self, classification, lang, type='page'):
"""A path handler for the given classification."""
if type == 'rss':
Expand Down
4 changes: 4 additions & 0 deletions nikola/plugins/task/page_index.py
Expand Up @@ -63,6 +63,10 @@ def classify(self, post, lang):
i = destpath.rfind('/')
return destpath[:i] if i >= 0 else ''

def get_classification_printable_name(self, hierarchy, lang, only_last_component=False):
"""Extract a printable name from the classification."""
return '/'.join(hierarchy)

def get_path(self, hierarchy, lang, type='page'):
"""A path handler for the given classification."""
return hierarchy, True
Expand Down
17 changes: 12 additions & 5 deletions nikola/plugins/task/sections.py
Expand Up @@ -66,6 +66,17 @@ def classify(self, post, lang):
"""Classify the given post for the given language."""
return [post.section_slug(lang)]

def _get_section_name(self, section, lang):
# Check whether we have a name for this section
if section in self.site.config['POSTS_SECTION_NAME'](lang):
return self.site.config['POSTS_SECTION_NAME'](lang)[section]
else:
return section.replace('-', ' ').title()

def get_classification_printable_name(self, section, lang, only_last_component=False):
"""Extract a printable name from the classification."""
return self._get_section_name(section, lang)

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
Expand All @@ -82,11 +93,7 @@ def provide_context_and_uptodate(self, section, lang):
kw = {
"messages": self.site.MESSAGES,
}
# Check whether we have a name for this section
if section in self.site.config['POSTS_SECTION_NAME'](lang):
section_name = self.site.config['POSTS_SECTION_NAME'](lang)[section]
else:
section_name = section.replace('-', ' ').title()
section_name = self._get_Section_name(section, lang)
# Compose section title
section_title = section_name
posts_section_title = self.site.config['POSTS_SECTION_TITLE'](lang)
Expand Down
37 changes: 37 additions & 0 deletions nikola/plugins/task/taxonomies.py
Expand Up @@ -211,6 +211,35 @@ def _filter_list(self, post_list, lang):
else:
return [x for x in post_list if x.is_translation_available(lang)]

def _generate_subclassification_page(self, taxonomy, node, context, kw, lang):
"""Render a list of subclassifications."""
def get_subnode_data(subnode):
return [
taxonomy.get_classification_printable_name(subnode.classification_path, lang, only_last_component=True),
self.site.link(taxonomy.classification_name, subnode.classification_name, lang),
len(self._filter_list(self.site.posts_per_classification[taxonomy.classification_name][lang][subnode.classification_name]))
]

items = [get_subnode_data(subnode) for subnode in node.children]
context = copy(context)
context["lang"] = lang
context["permalink"] = self.site.link(taxonomy.classification_name, node.classification_name, lang)
if "pagekind" not in context:
context["pagekind"] = ["list", "archive_page"]
context["items"] = items
task = self.site.generic_post_list_renderer(
lang,
[],
os.path.join(kw['output_folder'], self.site.path(taxonomy.classification_name, node.classification_name, lang)),
taxonomy.show_list_as_subcategories_list,
kw['filters'],
context,
)
task_cfg = {1: kw, 2: items}
task['uptodate'] = task['uptodate'] + [utils.config_changed(task_cfg, 'nikola.plugins.task.taxonomy')]
task['basename'] = self.name
return task

def _generate_classification_page(self, taxonomy, classification, post_list, lang):
"""Render index or post list and associated feeds per classification."""
# Filter list
Expand All @@ -235,6 +264,14 @@ def _generate_classification_page(self, taxonomy, classification, post_list, lan
kw["output_folder"] = self.site.config['OUTPUT_FOLDER']
context = copy(context)
context["permalink"] = self.site.link(taxonomy.classification_name, classification, lang)
# Decide what to do
if taxonomy.has_hierarchy and taxonomy.show_list_as_subcategories_list:
# Determine whether there are subcategories
node = self.site.hierarchy_lookup_per_classification[taxonomy.classification_name][lang][classification]
# Are there subclassifications?
if len(node.children) > 0:
# Yes: create list with subclassifications instead of list of posts
return self._generate_subclassification_page(taxonomy, node, context, kw, lang)
# Generate RSS feed
if kw["generate_rss"]:
yield self._generate_classification_page_as_rss(taxonomy, classification, filtered_posts, context['title'], context.get("description"), kw, lang)
Expand Down

0 comments on commit a31939b

Please sign in to comment.