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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/jekyll-import/importers/drupal_common.rb', line 41
def process(options)
dbname = options.fetch("dbname")
user = options.fetch("user")
pass = options.fetch("password", DEFAULTS["password"])
host = options.fetch("host", DEFAULTS["host"])
prefix = options.fetch("prefix", DEFAULTS["prefix"])
types = options.fetch("types", DEFAULTS["types"])
db = Sequel.mysql2(dbname, :user => user, :password => pass, :host => host, :encoding => "utf8")
query = self.build_query(prefix, types)
conf = Jekyll.configuration({})
src_dir = conf["source"]
dirs = {
:_posts => File.join(src_dir, "_posts").to_s,
:_drafts => File.join(src_dir, "_drafts").to_s,
:_layouts => Jekyll.sanitized_path(src_dir, conf["layouts_dir"].to_s),
}
dirs.each do |_key, dir|
FileUtils.mkdir_p dir
end
File.open(File.join(dirs[:_layouts], "refresh.html"), "w") do |f|
f.puts <<-HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url={{ page.refresh_to_post_id }}.html" />
</head>
</html>
HTML
end
db[query].each do |post|
data, content = self.post_data(post)
data["layout"] = post[:type]
title = data["title"] = post[:title].strip.force_encoding("UTF-8")
time = data["created"] = post[:created]
data = data.delete_if { |_k, v| v.nil? || v == "" }.each_pair do |_k, v|
(v.is_a? String ? v.force_encoding("UTF-8") : v)
end
is_published = post[:status] == 1
node_id = post[:nid]
dir = is_published ? dirs[:_posts] : dirs[:_drafts]
slug = title.strip.downcase.gsub(%r!(&|&)!, " and ").gsub(%r![\s\.\/\\]!, "-").gsub(%r![^\w-]!, "").gsub(%r![-_]{2,}!, "-").gsub(%r!^[-_]!, "").gsub(%r![-_]$!, "")
filename = Time.at(time).to_datetime.strftime("%Y-%m-%d-") + slug + ".md"
File.open("#{dir}/#{filename}", "w") do |f|
f.puts data.to_yaml
f.puts "---"
f.puts content
end
next unless is_published
alias_query = self.aliases_query(prefix)
type = post[:type]
aliases = db[alias_query, "#{type}/#{node_id}"].all
aliases.push(:alias => "#{type}/#{node_id}")
aliases.each do |url_alias|
FileUtils.mkdir_p url_alias[:alias]
File.open("#{url_alias[:alias]}/index.md", "w") do |f|
f.puts "---"
f.puts "layout: refresh"
f.puts "refresh_to_post_id: /#{Time.at(time).to_datetime.strftime("%Y/%m/%d/") + slug}"
f.puts "---"
end
end
end
end
|