Skip to content

Instantly share code, notes, and snippets.

@wereHamster
Created July 15, 2013 18:58
Show Gist options
  • Save wereHamster/6002452 to your computer and use it in GitHub Desktop.
Save wereHamster/6002452 to your computer and use it in GitHub Desktop.
require 'mail'
class Jekyll::Post
alias :to_liquid_without_comments :to_liquid
def to_liquid
data = to_liquid_without_comments
data['comments'] = Comments::load(self)
data['comments_length'] = data['comments'].length
data['comment_email_address'] = comment_email_address
data
end
def comment_email_address
"comment%2B#{id[1..-1].gsub('/', '-')}@blog.caurea.org"
end
end
module Comments
def self.load(post)
@comments ||= read_comments(post.site)
@comments[post.id]
end
def self.read_comments(site)
comments = Hash.new { |h, k| h[k] = Array.new }
Dir["#{site.source}/**/_comments/**/*"].sort.each do |comment|
next unless File.file?(comment) and File.readable?(comment)
mail = Mail.read(comment)
mail.to.join('|') =~ /^comment\+(.+)@blog.caurea.org$/
next unless $1
postid = $1.gsub(/^(....)-(..)-(..)-(.*)$/, '/\1/\2/\3/\4')
comments[postid] << Comment.new(site, mail)
end
comments
end
end
class Comment
attr_reader :site, :mail
def initialize(site, mail)
@site, @mail = site, mail
end
def to_liquid
data = {}
data['date'] = mail.date
data['sender'] = mail.header[:from].display_names.first
data['body'] = converter.convert(mail.body.to_s)
data
end
def converter
site.converters.find { |c| c.matches('md') }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment