Skip to content

Commit f6619dc

Browse files
committedMay 10, 2015
Merge pull request #79 from getnikola/new-recent-posts-json
New plug-in: Recent posts as JSON
2 parents 116ec29 + 823cc0f commit f6619dc

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
 

Diff for: ‎v7/recent_posts_json/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This plug-in generates a /index.json file containing the same posts
2+
as /index.html. Intended to be used in JavaScripts. For example to
3+
promote the most recent posts at the bottom of older posts.
4+
5+
By default, the JSON file will include:
6+
[
7+
{
8+
"date": <JS Date object>,
9+
"title": <post-title>,
10+
"loc": <relative-link-to-post>
11+
},
12+
]
13+
14+
Optionally, it can be expanded to include thumbnails or descriptions:
15+
[ {
16+
"date" <JS Date object>, "title": <post-title>, "loc": <post-relative-link>,
17+
"desc": <post-meta-description>,
18+
"img": <post-meta-thumbnail>
19+
], }
20+
21+
Posts are sorted by their post.meta.date as JavaScript dates.
22+
23+
Pro tip: Set a sensible cache header for the JSON file.

Diff for: ‎v7/recent_posts_json/config.py.sample

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Generates a /index.json with the most recent posts from /index.html.
2+
# Can be expanded with these options:
3+
4+
# Number of posts to include, defaults to INDEX_DISPLAY_POST_COUNT (10)
5+
RECENT_POSTS_JSON_LENGTH = 5
6+
7+
# Include {"desc": post.description}, defaults to False
8+
RECENT_POSTS_JSON_DESCRIPTION = False
9+
10+
# Include {"img": post.meta.previewimage}, defaults to False
11+
RECENT_POSTS_JSON_PREVIEWIMAGE = False

Diff for: ‎v7/recent_posts_json/recent_posts_json.plugin

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Core]
2+
Name = recent_posts_json
3+
Module = recent_posts_json
4+
5+
[Documentation]
6+
Author = Daniel Aleksandersen
7+
Version = 0.9.0
8+
Website = https://www.aeyoun.com
9+
Description = Generate JSON with recent posts.
10+

Diff for: ‎v7/recent_posts_json/recent_posts_json.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)
Please sign in to comment.