From 8ba4781ddb80aa7ef27dd11c21b92ebe96116ac5 Mon Sep 17 00:00:00 2001 From: Shigeya Suzuki Date: Sat, 2 Nov 2013 21:59:44 +0900 Subject: [PATCH 1/2] Static comment exporting hack --- mt_static_comments/README.md | 25 ++++ .../mt_exported_static_comments.rb | 118 ++++++++++++++++++ mt_static_comments/post.html | 35 ++++++ 3 files changed, 178 insertions(+) create mode 100644 mt_static_comments/README.md create mode 100644 mt_static_comments/mt_exported_static_comments.rb create mode 100644 mt_static_comments/post.html diff --git a/mt_static_comments/README.md b/mt_static_comments/README.md new file mode 100644 index 0000000..d3d33b3 --- /dev/null +++ b/mt_static_comments/README.md @@ -0,0 +1,25 @@ +Static Comment Generator for MovableType imported sites + +This plugin and template are a skeleton to generate a Jekyll static +site with data exported from MovableType. + +The template is based on +[Matt Palmer's jekyll-static-comments](https://github.com/mpalmer/jekyll-static-comments). +With the small hack added to MovableType Jeykll exporter, user can +export MovableType entries with comments. + +jekyll-import require +(a hack)[https://github.com/shigeya/jekyll-import/tree/mt_comment_hack] +which (possibly) merged into jekyll-import. + +Generation Steps: + +- Export using jekyll/jekyll-import/mt.rb as directed. By adding + option `comments: true`, `_comments` directories are created and + all the comments are exported alongside with `_posts` directory. +- Copy `mt_exported_static_comments.rb` to `_plugins` directory of + jekyll project directory +- Copy `post.html` to `_layout` directory of project directory +- Build using Jekyll! + + diff --git a/mt_static_comments/mt_exported_static_comments.rb b/mt_static_comments/mt_exported_static_comments.rb new file mode 100644 index 0000000..e06394e --- /dev/null +++ b/mt_static_comments/mt_exported_static_comments.rb @@ -0,0 +1,118 @@ +# Store and render comments as a static part of a Jekyll site +# +# See README.mdwn for detailed documentation on this plugin. +# based on the work at +# Homepage: http://theshed.hezmatt.org/jekyll-static-comments +# Copyright (C) 2011 Matt Palmer +# +# Additional work by: +# Copyright (C) 2013 Shigeya Suzuki +# +# 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(attr = nil) + data = to_liquid_without_comments(attr) + data['comments'] = StaticComments::find_for_post(self, data) + data['comment_count'] = data['comments'].length + data + end +end + +# +# Following code is a quick hack, consists of Matt Palmer's code and +# some codes combined with Post/Site. this code should be refactored later +# to make consistent with other latest Jekyll modules. +# +# expected output for _comments directory is by the hack added +# as https://github.com/shigeya/jekyll-import/tree/mt_comment_hack +# +# Note: +# 1) post output has a unique post_id (copy of entry_id of mt_entry table) +# 2) comment output has post_id, too +# 3) Comments are sorted by comment_id, which is copy of comment_id of +# mt_comment table +# + +module StaticComments + + class StaticComment + include Jekyll::Convertible + + # Attributes for Liquid templates + EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[ + author + url + date + ] + + ATTRIBUTES_FOR_LIQUID = EXCERPT_ATTRIBUTES_FOR_LIQUID + %w[ + content + ] + + attr_accessor :site, :data, :ext, :output, :name, :content + attr_accessor :date + + def initialize(site, source, dir, name) + @site = site + @dir = dir + @base = self.containing_dir(source, dir) + @name = name + self.read_yaml(@base, name) + @date = Time.parse(data["date"].to_s) + end + + # Get the full path to the directory containing the post files + def containing_dir(source, dir) + return File.join(source, dir, '_comments') + end + + def comment_id + data["comment_id"] + end + + def author + data["author"] + end + + def url + data["url"] + end + + def post_id + data['post_id'] + end + end + + # Find all the comments for a post + def self.find_for_post(post, data) + @comments ||= read_comments(post.site) + @comments[data['post_id']].sort {|a,b| a.comment_id <=> b.comment_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(site, dir = '') + comments = Hash.new() { |h, k| h[k] = Array.new } + + comment_files = site.get_entries(dir, "_comments") + comment_files.each do |f| + c = StaticComment.new(site, site.source, dir, f) + comments[c.post_id] << c + end + comments + end +end diff --git a/mt_static_comments/post.html b/mt_static_comments/post.html new file mode 100644 index 0000000..21fb66b --- /dev/null +++ b/mt_static_comments/post.html @@ -0,0 +1,35 @@ +--- +layout: default +--- +

{{ page.title }}

+

{{ page.date | date_to_string }}

+ +
+{{ content }} +
+ +
+ {% 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.author}} + {% endif %} +
+ {{c.date | date_to_string}} +

+

+ {{c.content | newline_to_br}} +

+
+ {% endfor %} +
From e50f75d66bc5b13fb7545fd4a713b7a3903abae2 Mon Sep 17 00:00:00 2001 From: Shigeya Suzuki Date: Sat, 10 May 2014 15:57:18 +0900 Subject: [PATCH 2/2] Quick hack to make this plugin to work with Jekyll 2.0 - Due to plugin architecture change. This change seems working. Will fix to reflect changes to Jekyll 2.0 later. --- mt_static_comments/mt_exported_static_comments.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mt_static_comments/mt_exported_static_comments.rb b/mt_static_comments/mt_exported_static_comments.rb index e06394e..2a63166 100644 --- a/mt_static_comments/mt_exported_static_comments.rb +++ b/mt_static_comments/mt_exported_static_comments.rb @@ -1,3 +1,4 @@ +require 'awesome_print' # Store and render comments as a static part of a Jekyll site # # See README.mdwn for detailed documentation on this plugin. @@ -94,6 +95,10 @@ def url def post_id data['post_id'] end + + def relative_path # make jekyll 2.0 happy + "_comments" + end end # Find all the comments for a post