140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 140
def self.process(options)
source = options.fetch("source", "wordpress.xml")
fetch = !options.fetch("no_fetch_images", false)
assets_folder = options.fetch("assets_folder", "assets")
FileUtils.mkdir_p(assets_folder)
import_count = Hash.new(0)
doc = Hpricot::XML(File.read(source))
authors = Hash[
(doc / :channel / "wp:author").map do |author|
[author.at("wp:author_login").inner_text.strip, {
"login" => author.at("wp:author_login").inner_text.strip,
"email" => author.at("wp:author_email").inner_text,
"display_name" => author.at("wp:author_display_name").inner_text,
"first_name" => author.at("wp:author_first_name").inner_text,
"last_name" => author.at("wp:author_last_name").inner_text,
},]
end
] rescue {}
(doc / :channel / :item).each do |node|
item = Item.new(node)
categories = node.search('category[@domain="category"]').map(&:inner_text).reject { |c| c == "Uncategorized" }.uniq
tags = node.search('category[@domain="post_tag"]').map(&:inner_text).uniq
metas = {}
node.search("wp:postmeta").each do |meta|
key = meta.at("wp:meta_key").inner_text
value = meta.at("wp:meta_value").inner_text
metas[key] = value
end
author_login = item.text_for("dc:creator").strip
= {
"layout" => item.post_type,
"title" => item.title,
"date" => item.published_at,
"type" => item.post_type,
"parent_id" => item.parent_id,
"published" => item.published?,
"password" => item.post_password,
"status" => item.status,
"categories" => categories,
"tags" => tags,
"meta" => metas,
"author" => authors[author_login],
"permalink" => item.permalink,
}
begin
content = Hpricot(item.text_for("content:encoded"))
["excerpt"] = item.excerpt if item.excerpt
if fetch
assets_dir_path = if item.published_at
File.join(assets_folder, item.published_at.strftime("/%Y/%m"))
else
assets_folder
end
download_images(item.title, content, assets_dir_path)
end
FileUtils.mkdir_p item.directory_name
File.open(File.join(item.directory_name, item.file_name), "w") do |f|
f.puts .to_yaml
f.puts "---"
f.puts Util.wpautop(content.to_html)
end
rescue StandardError => e
Jekyll.logger.error "Couldn't import post!"
Jekyll.logger.error "Title: #{item.title}"
Jekyll.logger.error "Name/Slug: #{item.file_name}\n"
Jekyll.logger.error "Error: #{e.message}"
next
end
import_count[item.post_type] += 1
end
import_count.each do |key, value|
Jekyll.logger.info "Imported #{value} #{key}s"
end
end
|