Skip to content

Commit a31939b

Browse files
committedOct 18, 2016
Preparing conversion of archive plugin.
1 parent c15e75e commit a31939b

File tree

6 files changed

+81
-5
lines changed

6 files changed

+81
-5
lines changed
 

‎nikola/plugin_categories.py

+20
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,13 @@ class Taxonomy(BasePlugin):
484484
# If True, the list for a classification includes all posts with a
485485
# sub-classification (in case has_hierarchy is True).
486486
include_posts_from_subhierarchies = False
487+
# If not False, for every classification which has at least one
488+
# subclassification, create a list of subcategories instead of a list/index
489+
# of posts. This is only used when has_hierarchy = True. If not False, this
490+
# must be the template name for the list; usually "list.tmpl".
491+
# If this is set to a string, it is recommended to set
492+
# include_posts_from_subhierarchies to True to get correct post counts.
493+
show_list_as_subcategories_list = False
487494
# Whether to show the posts for one classification as an index or
488495
# as a post list.
489496
show_list_as_index = False
@@ -541,6 +548,19 @@ def sort_classifications(self, classifications, lang):
541548
"""
542549
pass
543550

551+
def get_classification_printable_name(self, classification, lang, only_last_component=False):
552+
"""Extract a printable name from the classification.
553+
554+
For hierarchical taxonomies, the result of extract_hierarchy is provided
555+
as `classification`. For non-hierarchical taxonomies, the classification
556+
string itself is provided as `classification`.
557+
558+
The argument `only_last_component` is only relevant to hierarchical
559+
taxonomies. If it is set, the printable name should only describe the
560+
last component of `classification` if possible.
561+
"""
562+
raise NotImplementedError()
563+
544564
def get_list_path(self, lang, type='page'):
545565
"""A path handler for the list of all classifications.
546566

‎nikola/plugins/task/authors.py

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def classify(self, post, lang):
6767
"""Classify the given post for the given language."""
6868
return [post.author()]
6969

70+
def get_classification_printable_name(self, author, lang, only_last_component=False):
71+
"""Extract a printable name from the classification."""
72+
return author
73+
7074
def get_list_path(self, lang, type='page'):
7175
"""A path handler for the list of all classifications."""
7276
return [self.site.config['AUTHOR_PATH']], True

‎nikola/plugins/task/indexes.py

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def classify(self, post, lang):
5959
"""Classify the given post for the given language."""
6060
return [""]
6161

62+
def get_classification_printable_name(self, classification, lang, only_last_component=False):
63+
"""Extract a printable name from the classification."""
64+
return self.site.config["BLOG_TITLE"](lang)
65+
6266
def get_path(self, classification, lang, type='page'):
6367
"""A path handler for the given classification."""
6468
if type == 'rss':

‎nikola/plugins/task/page_index.py

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def classify(self, post, lang):
6363
i = destpath.rfind('/')
6464
return destpath[:i] if i >= 0 else ''
6565

66+
def get_classification_printable_name(self, hierarchy, lang, only_last_component=False):
67+
"""Extract a printable name from the classification."""
68+
return '/'.join(hierarchy)
69+
6670
def get_path(self, hierarchy, lang, type='page'):
6771
"""A path handler for the given classification."""
6872
return hierarchy, True

‎nikola/plugins/task/sections.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ def classify(self, post, lang):
6666
"""Classify the given post for the given language."""
6767
return [post.section_slug(lang)]
6868

69+
def _get_section_name(self, section, lang):
70+
# Check whether we have a name for this section
71+
if section in self.site.config['POSTS_SECTION_NAME'](lang):
72+
return self.site.config['POSTS_SECTION_NAME'](lang)[section]
73+
else:
74+
return section.replace('-', ' ').title()
75+
76+
def get_classification_printable_name(self, section, lang, only_last_component=False):
77+
"""Extract a printable name from the classification."""
78+
return self._get_section_name(section, lang)
79+
6980
def get_path(self, section, lang, type='page'):
7081
"""A path handler for the given classification."""
7182
return [_f for _f in [self.site.config['TRANSLATIONS'][lang], section] if _f], True
@@ -82,11 +93,7 @@ def provide_context_and_uptodate(self, section, lang):
8293
kw = {
8394
"messages": self.site.MESSAGES,
8495
}
85-
# Check whether we have a name for this section
86-
if section in self.site.config['POSTS_SECTION_NAME'](lang):
87-
section_name = self.site.config['POSTS_SECTION_NAME'](lang)[section]
88-
else:
89-
section_name = section.replace('-', ' ').title()
96+
section_name = self._get_Section_name(section, lang)
9097
# Compose section title
9198
section_title = section_name
9299
posts_section_title = self.site.config['POSTS_SECTION_TITLE'](lang)

‎nikola/plugins/task/taxonomies.py

+37
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,35 @@ def _filter_list(self, post_list, lang):
211211
else:
212212
return [x for x in post_list if x.is_translation_available(lang)]
213213

214+
def _generate_subclassification_page(self, taxonomy, node, context, kw, lang):
215+
"""Render a list of subclassifications."""
216+
def get_subnode_data(subnode):
217+
return [
218+
taxonomy.get_classification_printable_name(subnode.classification_path, lang, only_last_component=True),
219+
self.site.link(taxonomy.classification_name, subnode.classification_name, lang),
220+
len(self._filter_list(self.site.posts_per_classification[taxonomy.classification_name][lang][subnode.classification_name]))
221+
]
222+
223+
items = [get_subnode_data(subnode) for subnode in node.children]
224+
context = copy(context)
225+
context["lang"] = lang
226+
context["permalink"] = self.site.link(taxonomy.classification_name, node.classification_name, lang)
227+
if "pagekind" not in context:
228+
context["pagekind"] = ["list", "archive_page"]
229+
context["items"] = items
230+
task = self.site.generic_post_list_renderer(
231+
lang,
232+
[],
233+
os.path.join(kw['output_folder'], self.site.path(taxonomy.classification_name, node.classification_name, lang)),
234+
taxonomy.show_list_as_subcategories_list,
235+
kw['filters'],
236+
context,
237+
)
238+
task_cfg = {1: kw, 2: items}
239+
task['uptodate'] = task['uptodate'] + [utils.config_changed(task_cfg, 'nikola.plugins.task.taxonomy')]
240+
task['basename'] = self.name
241+
return task
242+
214243
def _generate_classification_page(self, taxonomy, classification, post_list, lang):
215244
"""Render index or post list and associated feeds per classification."""
216245
# Filter list
@@ -235,6 +264,14 @@ def _generate_classification_page(self, taxonomy, classification, post_list, lan
235264
kw["output_folder"] = self.site.config['OUTPUT_FOLDER']
236265
context = copy(context)
237266
context["permalink"] = self.site.link(taxonomy.classification_name, classification, lang)
267+
# Decide what to do
268+
if taxonomy.has_hierarchy and taxonomy.show_list_as_subcategories_list:
269+
# Determine whether there are subcategories
270+
node = self.site.hierarchy_lookup_per_classification[taxonomy.classification_name][lang][classification]
271+
# Are there subclassifications?
272+
if len(node.children) > 0:
273+
# Yes: create list with subclassifications instead of list of posts
274+
return self._generate_subclassification_page(taxonomy, node, context, kw, lang)
238275
# Generate RSS feed
239276
if kw["generate_rss"]:
240277
yield self._generate_classification_page_as_rss(taxonomy, classification, filtered_posts, context['title'], context.get("description"), kw, lang)

0 commit comments

Comments
 (0)
Please sign in to comment.