From 9ef1001b2ed8ceb48e5a2ae47b24a0b2d0ae92a7 Mon Sep 17 00:00:00 2001 From: Christian Winkler Date: Thu, 12 Nov 2015 20:30:01 +0100 Subject: [PATCH 1/2] changes for jekyll 3.0 --- static_comments.rb | 66 +++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/static_comments.rb b/static_comments.rb index 86c00f2..5fea2e0 100644 --- a/static_comments.rb +++ b/static_comments.rb @@ -18,37 +18,43 @@ # You should have received a copy of the GNU General Public License along # with this program; if not, see -class Jekyll::Post - alias :to_liquid_without_comments :to_liquid - - def to_liquid(*args) - data = to_liquid_without_comments(*args) - data['comments'] = StaticComments::find_for_post(self) - data['comment_count'] = data['comments'].length - data - end +Jekyll::Hooks.register :posts, :pre_render do |post, payload| + # code to call before Jekyll renders a post + id = payload["page"]["id"] + comments = StaticComments::find_for_post(payload["page"]["id"], payload["site"]["source"]) + payload["page"]["comments"] = comments + payload["page"]["comment_count"] = comments.length + payload["page"]["has_comments"] = (comments.length > 0) +end + +Jekyll::Hooks.register :site, :pre_render do |post, payload| + payload["site"]["posts"].each do |d| + comments = StaticComments::find_for_post(d.id, payload["site"]["source"]) + + d.data["comment_count"] = comments.length + end end module StaticComments - # Find all the comments for a post - def self.find_for_post(post) - @comments ||= read_comments(post.site.source) - @comments[post.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 'post_id' field in the YAML data in the comments files. - def self.read_comments(source) - comments = Hash.new() { |h, k| h[k] = Array.new } - - Dir["#{source}/**/_comments/**/*"].sort.each do |comment| - next unless File.file?(comment) and File.readable?(comment) - yaml_data = YAML::load_file(comment) - post_id = yaml_data.delete('post_id') - comments[post_id] << yaml_data - end - - comments - end + # Find all the comments for a post + def self.find_for_post(post_id, site) + @comments ||= read_comments(site) + @comments[post_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 'post_id' field in the YAML data in the comments files. + def self.read_comments(source) + comments = Hash.new() { |h, k| h[k] = Array.new } + + Dir["#{source}/**/_comments/**/*"].sort.each do |comment| + next unless File.file?(comment) and File.readable?(comment) + yaml_data = YAML::load_file(comment) + post_id = yaml_data.delete('post_id') + comments[post_id] << yaml_data + end + + comments + end end From 8cdf0900c55c6b1a1e8f35adb37558f65cdcbc80 Mon Sep 17 00:00:00 2001 From: Christian Winkler Date: Thu, 12 Nov 2015 20:32:18 +0100 Subject: [PATCH 2/2] tabs --- static_comments.rb | 58 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/static_comments.rb b/static_comments.rb index 5fea2e0..5ad5be6 100644 --- a/static_comments.rb +++ b/static_comments.rb @@ -19,42 +19,42 @@ # with this program; if not, see Jekyll::Hooks.register :posts, :pre_render do |post, payload| - # code to call before Jekyll renders a post - id = payload["page"]["id"] - comments = StaticComments::find_for_post(payload["page"]["id"], payload["site"]["source"]) - payload["page"]["comments"] = comments - payload["page"]["comment_count"] = comments.length - payload["page"]["has_comments"] = (comments.length > 0) + # code to call before Jekyll renders a post + id = payload["page"]["id"] + comments = StaticComments::find_for_post(payload["page"]["id"], payload["site"]["source"]) + payload["page"]["comments"] = comments + payload["page"]["comment_count"] = comments.length + payload["page"]["has_comments"] = (comments.length > 0) end Jekyll::Hooks.register :site, :pre_render do |post, payload| - payload["site"]["posts"].each do |d| - comments = StaticComments::find_for_post(d.id, payload["site"]["source"]) - - d.data["comment_count"] = comments.length - end + payload["site"]["posts"].each do |d| + comments = StaticComments::find_for_post(d.id, payload["site"]["source"]) + + d.data["comment_count"] = comments.length + end end module StaticComments - # Find all the comments for a post - def self.find_for_post(post_id, site) - @comments ||= read_comments(site) - @comments[post_id] - end + # Find all the comments for a post + def self.find_for_post(post_id, site) + @comments ||= read_comments(site) + @comments[post_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 'post_id' field in the YAML data in the comments files. - def self.read_comments(source) - comments = Hash.new() { |h, k| h[k] = Array.new } + # 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 'post_id' field in the YAML data in the comments files. + def self.read_comments(source) + comments = Hash.new() { |h, k| h[k] = Array.new } - Dir["#{source}/**/_comments/**/*"].sort.each do |comment| - next unless File.file?(comment) and File.readable?(comment) - yaml_data = YAML::load_file(comment) - post_id = yaml_data.delete('post_id') - comments[post_id] << yaml_data - end + Dir["#{source}/**/_comments/**/*"].sort.each do |comment| + next unless File.file?(comment) and File.readable?(comment) + yaml_data = YAML::load_file(comment) + post_id = yaml_data.delete('post_id') + comments[post_id] << yaml_data + end - comments - end + comments + end end