From 702eb577e391e296f40bbe638660b3469536b8ce Mon Sep 17 00:00:00 2001 From: Carl Boettiger Date: Mon, 8 Apr 2013 16:10:26 -0700 Subject: [PATCH] template and plugin for testing out static comments #83 Should add mechansim to extract comments from disqus exported xml and move into yaml database --- _includes/static_comments.html | 28 ++++++++++++++++++ _plugins/static_comments.rb | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 _includes/static_comments.html create mode 100644 _plugins/static_comments.rb diff --git a/_includes/static_comments.html b/_includes/static_comments.html new file mode 100644 index 0000000000..e0aec62f49 --- /dev/null +++ b/_includes/static_comments.html @@ -0,0 +1,28 @@ + +{% case page.comment_count %} + {% when 0 %} + {% when 1 %} +

1 Comment

+ {% else %} +

{{page.comment_count}} Comments

+{% endcase %} +{% for c in page.comments %} +
+

+ From: {% if c.link and c.link != '' %} + {{c.name}} + {% else %} + {{c.name}} + {% endif %} +
+ {{c.date}} +

+

+ {{c.comment | newline_to_br}} +

+
+{% endfor %} + + Leave a comment + + diff --git a/_plugins/static_comments.rb b/_plugins/static_comments.rb new file mode 100644 index 0000000000..184b059ec4 --- /dev/null +++ b/_plugins/static_comments.rb @@ -0,0 +1,54 @@ +# Store and render comments as a static part of a Jekyll site +# +# See README.mdwn for detailed documentation on this plugin. +# +# Homepage: http://theshed.hezmatt.org/jekyll-static-comments +# +# Copyright (C) 2011 Matt Palmer +# +# 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 + +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 + # 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 +end