|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright © 2012-2015 Roberto Alsina and others. |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any |
| 6 | +# person obtaining a copy of this software and associated |
| 7 | +# documentation files (the "Software"), to deal in the |
| 8 | +# Software without restriction, including without limitation |
| 9 | +# the rights to use, copy, modify, merge, publish, |
| 10 | +# distribute, sublicense, and/or sell copies of the |
| 11 | +# Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice |
| 15 | +# shall be included in all copies or substantial portions of |
| 16 | +# the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 19 | +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 20 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 21 | +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
| 22 | +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 23 | +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 24 | +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +from __future__ import unicode_literals, print_function |
| 28 | +import os |
| 29 | +import io |
| 30 | +import json |
| 31 | +import time |
| 32 | +try: |
| 33 | + from urlparse import urljoin |
| 34 | +except ImportError: |
| 35 | + from urllib.parse import urljoin # NOQA |
| 36 | + |
| 37 | +from nikola import utils |
| 38 | +from nikola.plugin_categories import Task |
| 39 | + |
| 40 | + |
| 41 | +class RecentPostsJon(Task): |
| 42 | + """Generate JSON with recent posts.""" |
| 43 | + |
| 44 | + name = "recent_posts_json" |
| 45 | + |
| 46 | + def set_site(self, site): |
| 47 | + site.register_path_handler("recent_posts_json", self.json_path) |
| 48 | + return super(RecentPostsJon, self).set_site(site) |
| 49 | + |
| 50 | + def gen_tasks(self): |
| 51 | + """Generate RSS feeds.""" |
| 52 | + kw = { |
| 53 | + "output_folder": self.site.config["OUTPUT_FOLDER"], |
| 54 | + "filters": self.site.config["FILTERS"], |
| 55 | + "index_file": self.site.config["INDEX_FILE"], |
| 56 | + "translations": self.site.config["TRANSLATIONS"], |
| 57 | + "show_untranslated_posts": self.site.config["SHOW_UNTRANSLATED_POSTS"], |
| 58 | + "site_url": self.site.config["SITE_URL"], |
| 59 | + "base_url": self.site.config["BASE_URL"], |
| 60 | + "json_posts_length": self.site.config["RECENT_POSTS_JSON_LENGTH"] if "RECENT_POSTS_JSON_LENGTH" in self.site.config else self.site.config["INDEX_DISPLAY_POST_COUNT"], |
| 61 | + "json_descriptions": self.site.config["RECENT_POSTS_JSON_DESCRIPTION"] if "RECENT_POSTS_JSON_DESCRIPTION" in self.site.config else False, |
| 62 | + "json_previewimage": self.site.conf["RECENT_POSTS_JSON_PREVIEWIMAGE"] if "RECENT_POSTS_JSON_PREVIEWIMAGE" in self.site.config else False, |
| 63 | + } |
| 64 | + self.site.scan_posts() |
| 65 | + yield self.group_task() |
| 66 | + for lang in kw["translations"]: |
| 67 | + output_path = os.path.join(kw["output_folder"], |
| 68 | + self.site.path("recent_posts_json", None, lang)) |
| 69 | + deps = [] |
| 70 | + deps_uptodate = [] |
| 71 | + if kw["show_untranslated_posts"]: |
| 72 | + posts = self.site.posts[:kw["json_posts_length"]] |
| 73 | + else: |
| 74 | + posts = [x for x in self.site.posts if x.is_translation_available(lang)][:kw["json_posts_length"]] |
| 75 | + for post in posts: |
| 76 | + deps += post.deps(lang) |
| 77 | + deps_uptodate += post.deps_uptodate(lang) |
| 78 | + task = { |
| 79 | + "basename": "recent_posts_json", |
| 80 | + "name": os.path.normpath(output_path), |
| 81 | + "file_dep": deps, |
| 82 | + "targets": [output_path], |
| 83 | + "actions": [(self.make_json, |
| 84 | + (posts, kw["json_descriptions"], kw["json_previewimage"], output_path))], |
| 85 | + "task_dep": ["render_posts"], |
| 86 | + "clean": True, |
| 87 | + "uptodate": [utils.config_changed(kw, "nikola.plugins.task.recent_pots_json")] + deps_uptodate, |
| 88 | + } |
| 89 | + yield utils.apply_filters(task, kw["filters"]) |
| 90 | + |
| 91 | + def make_json(self, posts, descriptions, previewimage, output_path): |
| 92 | + recent_posts = [] |
| 93 | + for post in posts: |
| 94 | + date = int(time.mktime(post.date.timetuple()) * 1000) # JavaScript Date |
| 95 | + link = post.permalink(absolute=False) |
| 96 | + title = post.title() |
| 97 | + entry = {"date": date, |
| 98 | + "title": title, |
| 99 | + "loc": link} |
| 100 | + if descriptions: |
| 101 | + entry.update({["desc"]: post.description()}) |
| 102 | + if previewimage: |
| 103 | + entry.update({["img"]: post.previewimage()}) |
| 104 | + recent_posts.append(entry) |
| 105 | + data = json.dumps(recent_posts, indent=2) |
| 106 | + with io.open(output_path, "w+", encoding="utf8") as outf: |
| 107 | + outf.write(data) |
| 108 | + |
| 109 | + def json_path(self, name, lang): |
| 110 | + return [_f for _f in [self.site.config["TRANSLATIONS"][lang], |
| 111 | + os.path.splitext(self.site.config["INDEX_FILE"])[0] + ".json"] if _f] |
0 commit comments