-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.rb
80 lines (72 loc) · 2.17 KB
/
import.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require "rubygems"
require "activerecord"
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "root",
:database => "tmpweb",
:encoding => "utf8")
class Site < ActiveRecord::Base
end
class User < ActiveRecord::Base
end
class AssignedSection < ActiveRecord::Base
belongs_to :article
belongs_to :section, :counter_cache => 'articles_count'
end
class Section < ActiveRecord::Base
belongs_to :site
has_many :assigned_sections, :dependent => :delete_all
has_many :articles, :order => 'position', :through => :assigned_sections
end
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, :polymorphic => true
end
class Tag < ActiveRecord::Base
has_many :taggings
end
class Content < ActiveRecord::Base
belongs_to :user
belongs_to :site
end
class Article < Content
has_many :assigned_sections, :dependent => :destroy
has_many :sections, :through => :assigned_sections, :order => 'sections.name'
has_many :taggings, :as => :taggable, :class_name => '::Tagging'
has_many :tags, :through => :taggings, :order => 'tags.name', :class_name => '::Tag'
end
FileUtils.mkdir_p("content/_posts")
posts = Article.find(:all, :conditions => { :site_id => 3})
posts.each do |post|
author = post.user.login
slug = post.permalink
content = post.body
created_at = post.published_at || Time.now
directory = created_at.strftime('%Y/%m/%d')
name = "#{created_at.strftime('%Y-%m-%d')}-#{slug}.textile"
filename = File.basename(name, File.extname(name))
p filename
title = post.title
categories = post.sections.map{|s|s.name.downcase}
tags = post.tags.map{|tag| tag.name}
data = {
'created_at' => created_at,
'directory' => directory,
'filename' => slug,
'extension' => "html",
'categories' => categories,
'tags' => tags,
'title' => title,
'author' => author,
'layout' => 'post',
'blog_post' => true,
'filter' => %w(erb textile)
}.delete_if { |k,v| v.nil? || v == '' || v.empty? rescue false }.to_yaml
File.open("content/_posts/#{name}", "wb") do |f|
f.puts data
f.puts "---"
f.puts content.gsub(//, "")
end
end