This repository has been archived by the owner on Apr 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_comments.rb
113 lines (95 loc) · 2.75 KB
/
static_comments.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
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
class Jekyll::Site
attr_accessor :comments
alias :site_payload_without_comments :site_payload
def site_payload
if self.comments
payload = {
"site" => { "comments" => self.comments.values.flatten.sort.reverse },
}.deep_merge(site_payload_without_comments)
else
payload = site_payload_without_comments
end
payload
end
end
class Jekyll::Post
alias :to_liquid_without_comments :to_liquid
def to_liquid
data = to_liquid_without_comments
data['comments'] = StaticComments::find_for_post(self)
data['comment_count'] = data['comments'].length
data
end
end
module StaticComments
class StaticComment
include Comparable
include Jekyll::Convertible
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)-([0-9]+)(\.[^.]+)$/
attr_accessor :site
attr_accessor :id, :url, :post_title, :date, :author, :email, :link
attr_accessor :content, :data, :ext, :output
def initialize(post, comment_file)
@site = post.site
@post = post
self.process(comment_file)
self.read_yaml('', comment_file)
if self.data.has_key?('date')
self.date = Time.parse(self.data["date"])
end
if self.data.has_key?('name')
self.author = self.data["name"]
end
if self.data.has_key?('email')
self.email = self.data["email"]
end
if self.data.has_key?('link')
self.link = self.data["link"]
end
self.url = "#{post.url}#comment-#{id}"
self.post_title = post.slug.split('-').select {|w| w.capitalize! || w }.join(' ')
payload = {
"page" => self.to_liquid
}
do_layout(payload, {})
end
def process(file_name)
m, cats, date, slug, index, ext = *file_name.match(MATCHER)
self.date = Time.parse(date)
self.id = index
self.ext = ext
end
def <=>(other)
cmp = self.date <=> other.date
if 0 == cmp
cmp = self.id <=> other.id
end
return cmp
end
def to_liquid
self.data.deep_merge({
"id" => self.id,
"url" => self.url,
"post_title" => self.post_title,
"date" => self.date,
"author" => self.author,
"email" => self.email,
"link" => self.link,
"content" => self.content
})
end
end
def self.find_for_post(post)
post.site.comments ||= Hash.new
post.site.comments[post.id] ||= read_comments(post)
end
def self.read_comments(post)
comments = Array.new
Dir["#{post.site.source}/_comments/#{post.date.strftime('%Y-%m-%d')}-#{post.slug}-*"].sort.each do |comment_file|
next unless File.file?(comment_file) and File.readable?(comment_file)
comment = StaticComment.new(post, comment_file)
comments << comment
end
comments.sort
end
end