aboutsummaryrefslogtreecommitdiffstats
path: root/nikola/plugins/task/indexes.py
blob: 1007c579afa1eb9dcb8617b401337312f2cff69c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-

# Copyright © 2012-2021 Roberto Alsina and others.

# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Render the blog's main index."""


from nikola.plugin_categories import Taxonomy


class Indexes(Taxonomy):
    """Classify for the blog's main index."""

    name = "classify_indexes"

    classification_name = "index"
    overview_page_variable_name = None
    more_than_one_classifications_per_post = False
    has_hierarchy = False
    show_list_as_index = True
    template_for_single_list = "index.tmpl"
    template_for_classification_overview = None
    apply_to_posts = True
    apply_to_pages = False
    omit_empty_classifications = False
    path_handler_docstrings = {
        'index_index': False,
        'index': """Link to a numbered index.

Example:

link://index/3 => /index-3.html""",
        'index_atom': """Link to a numbered Atom index.

Example:

link://index_atom/3 => /index-3.atom""",
        'index_rss': """A link to the RSS feed path.

Example:

link://rss => /blog/rss.xml""",
    }

    def set_site(self, site):
        """Set Nikola site."""
        # Redirect automatically generated 'index_rss' path handler to 'rss' for compatibility with old rss plugin
        site.register_path_handler('rss', lambda name, lang: site.path_handlers['index_rss'](name, lang))
        site.path_handlers['rss'].__doc__ = """A link to the RSS feed path.

Example:

    link://rss => /blog/rss.xml
        """.strip()
        return super().set_site(site)

    def get_implicit_classifications(self, lang):
        """Return a list of classification strings which should always appear in posts_per_classification."""
        return [""]

    def classify(self, post, lang):
        """Classify the given post for the given language."""
        return [""]

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

    def get_path(self, classification, lang, dest_type='page'):
        """Return a path for the given classification."""
        if dest_type == 'rss':
            return [
                self.site.config['RSS_PATH'](lang),
                self.site.config['RSS_FILENAME_BASE'](lang)
            ], 'auto'
        if dest_type == 'feed':
            return [
                self.site.config['ATOM_PATH'](lang),
                self.site.config['ATOM_FILENAME_BASE'](lang)
            ], 'auto'
        page_number = None
        if dest_type == 'page':
            # Interpret argument as page number
            try:
                page_number = int(classification)
            except (ValueError, TypeError):
                pass
        return [self.site.config['INDEX_PATH'](lang)], 'always', page_number

    def provide_context_and_uptodate(self, classification, lang, node=None):
        """Provide data for the context and the uptodate list for the list of the given classifiation."""
        kw = {
            "show_untranslated_posts": self.site.config["SHOW_UNTRANSLATED_POSTS"],
        }
        context = {
            "title": self.site.config["INDEXES_TITLE"](lang) or self.site.config["BLOG_TITLE"](lang),
            "description": self.site.config["BLOG_DESCRIPTION"](lang),
            "pagekind": ["main_index", "index"],
            "featured": [p for p in self.site.posts if p.post_status == 'featured' and
                         (lang in p.translated_to or kw["show_untranslated_posts"])],
        }
        kw.update(context)
        return context, kw

    def should_generate_classification_page(self, classification, post_list, lang):
        """Only generates list of posts for classification if this function returns True."""
        return not self.site.config["DISABLE_INDEXES"]

    def should_generate_atom_for_classification_page(self, classification, post_list, lang):
        """Only generates Atom feed for list of posts for classification if this function returns True."""
        return not self.site.config["DISABLE_MAIN_ATOM_FEED"]

    def should_generate_rss_for_classification_page(self, classification, post_list, lang):
        """Only generates RSS feed for list of posts for classification if this function returns True."""
        return not self.site.config["DISABLE_MAIN_RSS_FEED"]