-
Notifications
You must be signed in to change notification settings - Fork 1
/
posts_json.rb
54 lines (42 loc) · 1.22 KB
/
posts_json.rb
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
require 'json'
require 'fileutils'
module Jekyll
class PostsJsonGenerator < Generator
priority :low
def generate(site)
site.config['posts_json_path'] = 'posts.json' if !site.config['posts_json_path']
json_file = File.new(File.join(site.config['destination'], site.config['posts_json_path']), 'w')
posts = []
site.posts.each do |post|
posts << {
:date => post.date,
:url => post.url,
:title => post.title,
:excerpt => post.excerpt,
:tags => post.tags
} if post.published?
end
json_file.write(posts.to_json)
json_file.close
site.static_files << Jekyll::JsonFile.new(site, site.config['destination'], "/", site.config['posts_json_path'])
end
end
class JsonFile < StaticFile
def write(dest)
begin
super(dest)
rescue
end
true
end
end
class PostsJsonTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
end
def render(context)
IO.read(File.join(context.registers[:site].config['destination'], context.registers[:site].config['posts_json_path']))
end
end
end
Liquid::Template.register_tag('posts_json', Jekyll::PostsJsonTag)