forked from mpalmer/jekyll-static-comments
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatic_comments.rb
256 lines (218 loc) · 7.46 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Store and render comments as a static part of a Jekyll site
#
# See README.md for detailed documentation on this plugin.
#
# Homepage: http://theshed.hezmatt.org/jekyll-static-comments
#
# Copyright (C) 2011 Matt Palmer <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licences/>
module StaticComments
def comment_list()
@comment_list ||= StaticComments.find_for(self.site, self.id)
end
def comment_count()
comment_list.length
end
# Returns the comment-specific liquid attributes
def liquid_comment_attributes
{
'comment_list' => self.comment_list,
'comment_count' => self.comment_count
}
end
# Notice that this function means that some id's will be the same.
# Sample output:
# URL: /contact.html SLUG: /contact
# URL: /contact.htm SLUG: /contact
# URL: contact.html SLUG: /contact
# URL: /contact/index.html SLUG: /contact
# URL: /index.html SLUG: /
# URL: index.html SLUG: /
# URL: (empty string) SLUG: /
# URL: /index/index.html SLUG: /index
# URL: /index/page.html SLUG: /index/page
# URL: /dummy/.html SLUG: /dummy # Just don't use stupid page names like this
# URL: /.html SLUG: / # Again, I'm just too lazy to account for stupid cases
# URL: /funny/images/cats.html SLUG: /funny/images/cats
def self.slug(id)
value = id.sub( /(\/)?(index)?\.html?$/ , "" )
value = "/" + value if (value[0, 1] != "/")
value
end
# Find all the comments for a post or page with the specified id
def self.find_for(site, id)
@comment_list ||= read_comments(site)
@comment_list[slug(id)]
end
# Read all the comments files in the site, and return them as a hash of
# arrays containing the comments, where the key to the array is the value
# of the 'page_id' field in the YAML data in the comments files.
def self.read_comments(site)
comment_list = Hash.new() { |h, k| h[k] = Array.new }
source=site.source
Dir["#{source}/**/_comments/**/*"].sort.each do |comment_filename|
next unless File.file?(comment_filename) and File.readable?(comment_filename)
# `comment_filename` includes everything: the full, absolute path, file name, and extension.
comment = Comment.new(site, comment_filename)
if (comment.published?)
comment_list[comment.page_id] << comment
end
end
# TODO: Sort 'comment_list' by date
comment_list
end
class Comment
#include Jekyll::Convertible
attr_accessor :site, :filename, :ext
attr_accessor :data, :content
attr_accessor :page_id, :published
def initialize(site, filename)
@site = site
@filename = filename
self.load_yaml(filename)
@date = self.data['date']
#@name = self.data['name']
#@link = self.data['link']
@published = self.published?
# Reverse compatiblitiy with previous versions of `jekyll-static-comments` wich called the "page_id" field "post_id"
if (data.key?('post_id'))
@page_id = self.data['post_id']
else
@page_id = self.data['page_id']
end
self.transform()
end
def load_yaml(filename)
begin
#Jeyll: self.content = File.read_with_options(filename, self.site.file_read_opts)
self.content = File.read(filename)
self.ext = File.extname(filename)
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH
self.data = YAML.safe_load($1)
#OCTOPRESS: self.data = YAML.load($1)
else
# It's all YAML! (maybe)
# This is to accomodate for comments generated in older versions of Jekyll Static Comments
self.data = YAML.safe_load(self.content)
#OCTOPRESS: self.data = YAML.load(self.content)
if (data.key?('comment'))
self.content = data['comment']
data.delete('comment')
else
self.content = ""
puts "[StaticComments::Comment] WARNING: I don't know how to parse #{filename}; there doesn't seem to be any content or 'comment' property."
end
end
rescue SyntaxError => e
puts "YAML Exception reading #{filename}: #{e.message}"
rescue Exception => e
puts "Error reading file #{filename}: #{e.message}"
end
self.data ||= {}
end
# Transform the contents based on the content type.
#
# Returns nothing.
def transform
self.content = converter.convert(self.content)
rescue => e
Jekyll.logger.error "Conversion error:", "There was an error converting '#{filename}'."
raise e
end
# First the script goes through a few custom converters (I don't want to interfere with the rest
# of Jekyll just in case). If none is profided, just use one of the builtin converters.
# The default converter that handles the type of extension can be found and adjusted in `configuration.rb`
def converter
if (PlaintextConverter::matches(self.ext))
PlaintextConverter.new()
elsif (HTMLConverter::matches(self.ext))
HTMLConverter.new(true, true)
else
self.site.converters.find { |c| c.matches(self.ext) }
end
end
def to_liquid
return self.data.deep_merge({ "content" => self.content })
end
def published?
if self.data.has_key?('published') && self.data['published'] == false
false
else
true
end
end
end
class PlaintextConverter
include Liquid::StandardFilters
def self.matches(ext)
ext.eql?('.txt')
end
def convert(content)
content = escape(content)
content = newline_to_br(content)
content
end
end
class HTMLConverter
include Liquid::StandardFilters
def self.matches(ext)
ext.eql?('.html') or ext.eql?('.htm')
end
def initialize(basic_html, newlines_to_br)
@basic_html = basic_html # Strip out some potentially "unwanted" elements such as <script>
@newlines_to_br = newlines_to_br # Convert all found newlines into <br /> tags
end
def convert(content)
if (@basic_html)
content = content.gsub(/<script.*?<\/script>/m, '')
content = content.gsub(/<!--.*?-->/m, '')
content = content.gsub(/<style.*?<\/style>/m, '')
end
if (@newlines_to_br)
content = newline_to_br(content)
end
content
end
end
end
class Jekyll::Post
include StaticComments
# Already contains 'id' property
alias :to_liquid_without_comments :to_liquid
def to_liquid(attrs = nil)
data = (attrs.nil? ? to_liquid_without_comments() : to_liquid_without_comments(attrs))
data.deep_merge(self.liquid_comment_attributes)
end
end
class Jekyll::Page
include StaticComments
alias :to_liquid_without_comments :to_liquid
def to_liquid(attrs = nil)
data = (attrs.nil? ? to_liquid_without_comments() : to_liquid_without_comments(attrs))
data = data.deep_merge(self.liquid_comment_attributes)
data.deep_merge({ 'id' => self.id })
#OCTOPRESS: data.deep_merge({ 'id' => self.id, 'url' => self.fixed_octopress_url })
end
def id
return data['id'] if self.data.key? 'id'
StaticComments::slug(self.url)
#OCTOPRESS: StaticComments::slug(self.fixed_octopress_url)
end
#OCTOPRESS:
#def fixed_octopress_url
# # The preceeding "/" is because the `self.url` returned from category pages doesn't begin with a slash.
# File.join("/", @dir, self.url)
#end
end