Skip to content
Permalink
Browse files

recent_posts_json for every section

  • Loading branch information
da2x committed Sep 2, 2015
1 parent e91d078 commit 30f9c39a31d7de32190bccec25786b84c66fb984
@@ -20,4 +20,6 @@ Optionally, it can be expanded to include thumbnails or descriptions:

Posts are sorted by their post.meta.date as JavaScript dates.

JSON files will also be created for any sections.

Pro tip: Set a sensible cache header for the JSON file.
@@ -1,4 +1,5 @@
# Generates a /index.json with the most recent posts from /index.html.
# Will also create /$section/index.json for each section.
# Can be expanded with these options:

# Number of posts to include, defaults to INDEX_DISPLAY_POST_COUNT (10)
@@ -4,7 +4,9 @@ Module = recent_posts_json

[Documentation]
Author = Daniel Aleksandersen
Version = 1.0.0
Version = 1.2.0
Website = https://www.aeyoun.com/projects/nikola/
Description = Generate JSON with recent posts.

[Nikola]
MinVersion = 7.6.5
@@ -25,6 +25,7 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import unicode_literals, print_function
from collections import defaultdict
import os
import io
import json
@@ -39,7 +40,7 @@


class RecentPostsJon(Task):
"""Generate JSON with recent posts."""
"""Generate JSON with recent posts for each section."""

name = "recent_posts_json"

@@ -48,7 +49,7 @@ def set_site(self, site):
return super(RecentPostsJon, self).set_site(site)

def gen_tasks(self):
"""Generate RSS feeds."""
"""Generate JSON with recent post for each section."""
kw = {
"output_folder": self.site.config["OUTPUT_FOLDER"],
"filters": self.site.config["FILTERS"],
@@ -69,10 +70,10 @@ def gen_tasks(self):
deps = []
deps_uptodate = []
if kw["show_untranslated_posts"]:
posts = self.site.posts[:kw["json_posts_length"]]
posts = self.site.posts
else:
posts = [x for x in self.site.posts if x.is_translation_available(lang)][:kw["json_posts_length"]]
for post in posts:
posts = [x for x in self.site.posts if x.is_translation_available(lang)]
for post in posts[:kw["json_posts_length"]]:
deps += post.deps(lang)
deps_uptodate += post.deps_uptodate(lang)
task = {
@@ -81,13 +82,39 @@ def gen_tasks(self):
"file_dep": deps,
"targets": [output_path],
"actions": [(self.make_json,
(posts, kw["json_descriptions"], kw["json_previewimage"], output_path))],
(posts[:kw["json_posts_length"]], kw["json_descriptions"], kw["json_previewimage"], output_path))],
"task_dep": ["render_posts"],
"clean": True,
"uptodate": [utils.config_changed(kw, "nikola.plugins.task.recent_pots_json")] + deps_uptodate,
}
yield utils.apply_filters(task, kw["filters"])

if self.site.config['POSTS_SECTIONS']:
groups = defaultdict(list)
for post in posts:
groups[post.section_slug(lang)].append(post)

for section_slug, post_list in groups.items():
output_path = os.path.join(kw["output_folder"],
self.site.path("recent_posts_json", section_slug, lang))
deps = []
deps_uptodate = []
for post in post_list:
deps += post.deps(lang)
deps_uptodate += post.deps_uptodate(lang)
task = {
"basename": "recent_posts_json",
"name": os.path.normpath(output_path),
"file_dep": deps,
"targets": [output_path],
"actions": [(self.make_json,
(post_list[:kw["json_posts_length"]], kw["json_descriptions"], kw["json_previewimage"], output_path))],
"task_dep": ["render_posts"],
"clean": True,
"uptodate": [utils.config_changed(kw, "nikola.plugins.task.recent_pots_json")] + deps_uptodate,
}
yield utils.apply_filters(task, kw["filters"])

def make_json(self, posts, descriptions, previewimage, output_path):
recent_posts = []
for post in posts:
@@ -107,5 +134,12 @@ def make_json(self, posts, descriptions, previewimage, output_path):
outf.write(data)

def json_path(self, name, lang):
return [_f for _f in [self.site.config["TRANSLATIONS"][lang],
os.path.splitext(self.site.config["INDEX_FILE"])[0] + ".json"] if _f]
if name: # section
paths = [_f for _f in [self.site.config["TRANSLATIONS"][lang],
name,
os.path.splitext(self.site.config["INDEX_FILE"])[0] + ".json"] if _f]
else:
paths = [_f for _f in [self.site.config["TRANSLATIONS"][lang],
os.path.splitext(self.site.config["INDEX_FILE"])[0] + ".json"] if _f]

return paths

0 comments on commit 30f9c39

Please sign in to comment.
You can’t perform that action at this time.