-
-
Save mergulhao/3088055 to your computer and use it in GitHub Desktop.
Rails 3 Models To Export Mephisto To Jekyll & Disqus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'builder' | |
require 'digest/md5' | |
class Content < ActiveRecord::Base | |
belongs_to :user | |
end | |
class Section < ActiveRecord::Base | |
has_many :assigned_sections | |
has_many :articles, :order => 'position', :through => :assigned_sections | |
end | |
class Tag < ActiveRecord::Base | |
end | |
class Tagging < ActiveRecord::Base | |
belongs_to :tag | |
belongs_to :taggable, :polymorphic => true | |
end | |
class Comment < Content | |
belongs_to :article | |
scope :approved, where(:approved => true) | |
scope :unapproved_comments, where(:approved => false) | |
end | |
class AssignedSection < ActiveRecord::Base | |
belongs_to :article | |
belongs_to :section | |
end | |
class Article < Content | |
include ActionView::Helpers::SanitizeHelper, ERB::Util | |
has_many :taggings, :as => :taggable | |
has_many :tags, :through => :taggings | |
has_many :comments | |
has_many :assigned_sections, :dependent => :destroy | |
has_many :sections, :through => :assigned_sections, :order => 'sections.name' | |
scope :ordered, order('published_at ASC') | |
def puttable_content | |
%|--- | |
layout: post | |
title: '#{title}' | |
disqus_id: #{disqus_identifier2} | |
categories: | |
#{categories.to_yaml.from(4).strip.insert(0,"\n").gsub("\n","\n ").from(1)} | |
tags: | |
#{tags.map(&:name).map(&:downcase).to_yaml.from(4).strip.insert(0,"\n").gsub("\n","\n ").from(1)} | |
--- | |
\n\n#{body}\n\n| | |
end | |
def categories | |
sections.map(&:name).map(&:downcase) - ['home'] | |
end | |
def rewrite_rule | |
"RewriteRule ^#{disqus_identifier2.from(1)}$ #{disqus_identifier} [L]" | |
end | |
def jekyll_filename | |
"#{published_at.to_s(:db).split(' ').first}-#{permalink}.html" | |
end | |
def published_at | |
self['published_at'] || Time.now | |
end | |
def disqus_identifier | |
date_slashes = published_at.to_s(:db).split(' ').first.gsub('-','/') | |
"/#{date_slashes}/#{permalink}/" | |
end | |
def disqus_identifier2 | |
"/#{ymd_dirs('/')}/#{permalink}" | |
end | |
def ymd_dirs(joiner) | |
year = published_at.year.to_s | |
month = published_at.month.to_s | |
month = month.first == '0' ? month.second : month | |
day = published_at.day.to_s | |
day = day.first == '0' ? day.second : day | |
[year, month, day].join(joiner) | |
end | |
end | |
class Site < ActiveRecord::Base | |
MYHOST = 'mergulhao.info' | |
def self.export_jekyll | |
rewrite_rules = [] | |
process_folder = Rails.root + 'tmp' + 'jekyll_posts' | |
FileUtils.rm_rf(process_folder.to_s) | |
FileUtils.mkdir_p(process_folder.to_s) | |
Article.ordered.all.each do |article| | |
post_file = (process_folder + article.jekyll_filename).to_s | |
rewrite_rules << article.rewrite_rule | |
File.open(post_file,'w') { |f| f.write(article.puttable_content) } | |
end | |
File.open((process_folder+'rewrites.txt').to_s,'w') { |f| f.write(rewrite_rules.join("\n")) } | |
end | |
def self.export_jskit | |
xml = Builder::XmlMarkup.new(:indent => 2) | |
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" | |
xml.rss 'version' => "2.0", 'xmlns:dc' => "http://purl.org/dc/elements/1.1/", 'xmlns:atom' => "http://www.w3.org/2005/Atom", 'xmlns:media' => "http://search.yahoo.com/mrss/" do | |
Article.ordered.all.each do |article| | |
next if article.comments.approved.blank? | |
xml.channel do | |
xml.title article.title | |
xml.link "http://#{MYHOST}#{article.disqus_identifier2}" | |
xml.tag! 'atom:link', '', :rel => 'self', :type => 'application/rss+xml', :href => "http://js-kit.com/rss/#{MYHOST}/" | |
xml.tag! 'jskit:attribute', '', :key => 'md5path', :value => Digest::MD5.hexdigest(article.disqus_identifier2) | |
xml.tag! 'jskit:attribute', '', :key => 'path', :value => '' | |
xml.description article.title | |
xml.generator 'JS-Kit.com Bulk Site Exporter 0.8' | |
xml.lastBuildDate article.published_at.strftime("%a, %d %b %Y %H:%M:%S +0000") | |
article.comments.approved.each_with_index do |comment, comment_index| | |
xml.item do | |
xml.guid "jsid-2#{article.id}-#{comment_index}" | |
xml.pubDate comment.published_at.strftime("%a, %d %b %Y %H:%M:%S +0000") | |
xml.tag! 'jskit:attribute', '', :key => 'IP', :value => comment.author_ip | |
xml.author comment.author | |
xml.description comment.body | |
xml.tag! 'jskit:attribute', '', :key => 'sync_peer_3', :value => "#{article.id}-#{comment_index}" | |
xml.tag! 'jskit:attribute', '', :key => 'foreign', :value => '3' | |
end | |
end | |
end | |
end | |
end | |
File.open((Rails.root + 'tmp' + 'jskit.xml').to_s,'w') { |f| f.write(xml.target!) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My updated version to migrate my blog from mephisto. Original post at http://metaskills.net/2010/12/27/let-it-go-moving-from-mephisto-to-jekyll/