aboutsummaryrefslogtreecommitdiffstats
path: root/extra_plugins/task_mustache/__init__.py
blob: 8b5ec138492f948bf29e4ed7a4d2f1323c48a357 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright (c) 2012 Roberto Alsina y otros.

# 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.

import json
import os

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


class Mustache(Task):
    """Render the blog posts as JSON data."""

    name = "render_mustache"

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

        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'],
            "blog_title": self.site.config['BLOG_TITLE'],
            "content_footer": self.site.config['CONTENT_FOOTER'],
        }

        # TODO: timeline is global, get rid of it
        posts = [x for x in self.site.timeline if x.use_in_feeds]
        if not posts:
            yield {
                'basename': 'render_mustache',
                'actions': [],
            }
            return

        def write_file(path, post, lang):

            # Prev/Next links
            prev_link = False
            if post.prev_post:
                prev_link = post.prev_post.permalink(lang).replace(".html",
                                                                   ".json")
            next_link = False
            if post.next_post:
                next_link = post.next_post.permalink(lang).replace(".html",
                                                                   ".json")
            data = {}

            # Configuration
            for k, v in self.site.config.items():
                # FIXME: not py3 ready
                if isinstance(v, (str, unicode)):  # NOQA
                    data[k] = v

            # Tag data
            tags = []
            for tag in post.tags:
                tags.append({'name': tag, 'link': self.site.link("tag", tag,
                                                                 lang)})
            data.update({
                "tags": tags,
                "tags?": True if tags else False,
            })

            # Template strings
            for k, v in kw["messages"][lang].items():
                data["message_" + k] = v

            # Post data
            data.update({
                "title": post.title(lang),
                "text": post.text(lang),
                "prev": prev_link,
                "next": next_link,
                "date":
                post.date.strftime(self.site.GLOBAL_CONTEXT['date_format']),
            })

            # Disqus comments
            data["disqus_html"] = ('<div id="disqus_thread"></div> <script '
                                   'type="text/javascript">var disqus_'
                                   'shortname="%s";var disqus_url="%s";'
                                   '(function(){var a=document.createElement'
                                   '("script");a.type="text/javascript";'
                                   'a.async=true;a.src="http://"+disqus_'
                                   'shortname+".disqus.com/embed.js";('
                                   'document.getElementsByTagName("head")'
                                   '[0]||document.getElementsByTagName("body")'
                                   '[0]).appendChild(a)})();        </script>'
                                   '<noscript>Please enable JavaScript to view'
                                   ' the <a href="http://disqus.com/'
                                   '?ref_noscript">comments powered by DISQUS.'
                                   '</a></noscript><a href="http://disqus.com"'
                                   'class="dsq-brlink">comments powered by <sp'
                                   'an class="logo-disqus">DISQUS</span></a>' %
                                   (self.site.config['DISQUS_FORUM'],
                                    post.permalink(absolute=True)))

            # Post translations
            translations = []
            for langname in kw["translations"]:
                if langname == lang:
                    continue
                translations.append({'name':
                                     kw["messages"][langname]["Read in English"],
                                    'link': "javascript:load_data('%s');"
                                    % post.permalink(langname).replace(
                                        ".html", ".json")})
            data["translations"] = translations

            try:
                os.makedirs(os.path.dirname(path))
            except:
                pass
            with open(path, 'wb+') as fd:
                fd.write(json.dumps(data))

        for lang in kw["translations"]:
            for i, post in enumerate(posts):
                out_path = post.destination_path(lang, ".json")
                out_file = os.path.join(kw['output_folder'], out_path)
                task = {
                    'basename': 'render_mustache',
                    'name': str(out_path),
                    'file_dep': post.fragment_deps(lang),
                    'targets': [out_file],
                    'actions': [(write_file, (out_file, post, lang))],
                    'task_dep': ['render_posts'],
                }
                yield task

        if posts:
            first_post_data = posts[0].permalink(
                self.site.config["DEFAULT_LANG"]).replace(".html", ".json")

        # Copy mustache template
        src = os.path.join(os.path.dirname(__file__), 'mustache-template.html')
        dst = os.path.join(kw['output_folder'], 'mustache-template.html')
        yield {
            'basename': 'render_mustache',
            'name': 'mustache-template.html',
            'targets': [dst],
            'file_dep': [src],
            'actions': [(copy_file, (src, dst))],
        }

        # Copy mustache.html with the right starting file in it
        src = os.path.join(os.path.dirname(__file__), 'mustache.html')
        dst = os.path.join(kw['output_folder'], 'mustache.html')

        def copy_mustache():
            with open(src, 'rb') as in_file:
                with open(dst, 'wb+') as out_file:
                    data = in_file.read().replace('{{first_post_data}}',
                                                  first_post_data)
                    out_file.write(data)
        yield {
            'basename': 'render_mustache',
            'name': 'mustache.html',
            'targets': [dst],
            'file_dep': [src],
            'uptodate': [config_changed({1: first_post_data})],
            'actions': [(copy_mustache, [])],
        }