aboutsummaryrefslogtreecommitdiffstats
path: root/nikola/plugins/task/indexes.py
blob: 386cc187a59fafb0696ee9cffc869476f708aaf6 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-

# Copyright © 2012-2014 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.

from __future__ import unicode_literals
from collections import defaultdict
import os

from nikola.plugin_categories import Task
from nikola.utils import config_changed


class Indexes(Task):
    """Render the blog indexes."""

    name = "render_indexes"

    def set_site(self, site):
        site.register_path_handler('index', self.index_path)
        return super(Indexes, self).set_site(site)

    def gen_tasks(self):
        self.site.scan_posts()
        yield self.group_task()

        kw = {
            "translations": self.site.config['TRANSLATIONS'],
            "index_display_post_count":
            self.site.config['INDEX_DISPLAY_POST_COUNT'],
            "messages": self.site.MESSAGES,
            "index_teasers": self.site.config['INDEX_TEASERS'],
            "output_folder": self.site.config['OUTPUT_FOLDER'],
            "filters": self.site.config['FILTERS'],
            "show_untranslated_posts": self.site.config['SHOW_UNTRANSLATED_POSTS'],
            "indexes_title": self.site.config['INDEXES_TITLE'],
            "indexes_pages": self.site.config['INDEXES_PAGES'],
            "indexes_pages_main": self.site.config['INDEXES_PAGES_MAIN'],
            "blog_title": self.site.config["BLOG_TITLE"],
            "rss_read_more_link": self.site.config["RSS_READ_MORE_LINK"],
        }

        template_name = "index.tmpl"
        posts = self.site.posts
        for lang in kw["translations"]:
            # Split in smaller lists
            lists = []
            if kw["show_untranslated_posts"]:
                filtered_posts = posts
            else:
                filtered_posts = [x for x in posts if x.is_translation_available(lang)]
            lists.append(filtered_posts[:kw["index_display_post_count"]])
            filtered_posts = filtered_posts[kw["index_display_post_count"]:]
            while filtered_posts:
                lists.append(filtered_posts[-kw["index_display_post_count"]:])
                filtered_posts = filtered_posts[:-kw["index_display_post_count"]]
            num_pages = len(lists)
            for i, post_list in enumerate(lists):
                context = {}
                indexes_title = kw['indexes_title'] or kw['blog_title'](lang)
                if kw["indexes_pages_main"]:
                    ipages_i = i + 1
                    ipages_msg = "page %d"
                else:
                    ipages_i = i
                    ipages_msg = "old posts, page %d"
                if kw["indexes_pages"]:
                    indexes_pages = kw["indexes_pages"] % ipages_i
                else:
                    indexes_pages = " (" + \
                        kw["messages"][lang][ipages_msg] % ipages_i + ")"
                if i > 0 or kw["indexes_pages_main"]:
                    context["title"] = indexes_title + indexes_pages
                else:
                    context["title"] = indexes_title
                context["prevlink"] = None
                context["nextlink"] = None
                context['index_teasers'] = kw['index_teasers']
                if i == 0:  # index.html page
                    context["prevlink"] = None
                    if num_pages > 1:
                        context["nextlink"] = "index-{0}.html".format(num_pages - 1)
                    else:
                        context["nextlink"] = None
                else:  # index-x.html pages
                    if i > 1:
                        context["nextlink"] = "index-{0}.html".format(i - 1)
                    if i < num_pages - 1:
                        context["prevlink"] = "index-{0}.html".format(i + 1)
                    elif i == num_pages - 1:
                        context["prevlink"] = "index.html"
                context["permalink"] = self.site.link("index", i, lang)
                output_name = os.path.join(
                    kw['output_folder'], self.site.path("index", i,
                                                        lang))
                task = self.site.generic_post_list_renderer(
                    lang,
                    post_list,
                    output_name,
                    template_name,
                    kw['filters'],
                    context,
                )
                task_cfg = {1: task['uptodate'][0].config, 2: kw}
                task['uptodate'] = [config_changed(task_cfg)]
                task['basename'] = 'render_indexes'
                yield task

        if not self.site.config["STORY_INDEX"]:
            return
        kw = {
            "translations": self.site.config['TRANSLATIONS'],
            "post_pages": self.site.config["post_pages"],
            "output_folder": self.site.config['OUTPUT_FOLDER'],
            "filters": self.site.config['FILTERS'],
            "index_file": self.site.config['INDEX_FILE'],
        }
        template_name = "list.tmpl"
        for lang in kw["translations"]:
            # Need to group by folder to avoid duplicated tasks (Issue #758)
                # Group all pages by path prefix
                groups = defaultdict(list)
                for p in self.site.timeline:
                    if not p.is_post:
                        dirname = os.path.dirname(p.destination_path(lang))
                        groups[dirname].append(p)
                for dirname, post_list in groups.items():
                    context = {}
                    context["items"] = [
                        (post.title(lang), post.permalink(lang))
                        for post in post_list
                    ]
                    output_name = os.path.join(kw['output_folder'], dirname, kw['index_file'])
                    task = self.site.generic_post_list_renderer(lang, post_list,
                                                                output_name,
                                                                template_name,
                                                                kw['filters'],
                                                                context)
                    task_cfg = {1: task['uptodate'][0].config, 2: kw}
                    task['uptodate'] = [config_changed(task_cfg)]
                    task['basename'] = self.name
                    yield task

    def index_path(self, name, lang):
        if name not in [None, 0]:
            return [_f for _f in [self.site.config['TRANSLATIONS'][lang],
                                  self.site.config['INDEX_PATH'],
                                  'index-{0}.html'.format(name)] if _f]
        else:
            return [_f for _f in [self.site.config['TRANSLATIONS'][lang],
                                  self.site.config['INDEX_PATH'],
                                  self.site.config['INDEX_FILE']]
                    if _f]