-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogger.rb
320 lines (276 loc) · 12.1 KB
/
blogger.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
module JekyllImport
module Importers
class Blogger < Importer
def self.specify_options(c)
c.option "source", "--source blogger.xml", "The XML file (blog-MM-DD-YYYY.xml) path to import"
c.option "no-blogger-info", "--no-blogger-info", "not to leave blogger-URL info (id and old URL) in the front matter (default: false)"
c.option "replace-internal-link", "--replace-internal-link", "replace internal links using the post_url liquid tag. (default: false)"
c.option "comments", "--comments", "import comments to _comments collection"
end
def self.validate(options)
if options["source"].nil?
raise "Missing mandatory option: --source"
elsif !File.exist?(options["source"])
raise Errno::ENOENT, "File not found: #{options["source"]}"
end
end
def self.require_deps
JekyllImport.require_with_fallback(%w(
rexml/document
rexml/streamlistener
rexml/parsers/streamparser
uri
time
fileutils
safe_yaml
open-uri
))
end
# Process the import.
#
# source:: a local file String (or IO object for internal use purpose)..
# no-blogger-info:: a boolean if not leave blogger info (id and original URL).
# replace-internal-link:: a boolean if replace internal link
#
# Returns nothing.
def self.process(options)
source = options.fetch("source")
listener = BloggerAtomStreamListener.new
listener.leave_blogger_info = !options.fetch("no-blogger-info", false),
listener.comments = options.fetch("comments", false),
File.open(source, "r") do |f|
f.flock(File::LOCK_SH)
REXML::Parsers::StreamParser.new(f, listener).parse
end
options["original-url-base"] = listener.original_url_base
postprocess(options)
end
# Post-process after import.
#
# replace-internal-link:: a boolean if replace internal link
#
# Returns nothing.
def self.postprocess(options)
# Replace internal link URL
if options.fetch("replace-internal-link", false)
original_url_base = options.fetch("original-url-base", nil)
if original_url_base
orig_url_pattern = Regexp.new(" href=([\"\'])(?:#{Regexp.escape(original_url_base)})?/([0-9]{4})/([0-9]{2})/([^\"\']+\.html)\\1")
Dir.glob("_posts/*.*") do |filename|
body = nil
File.open(filename, "r") do |f|
f.flock(File::LOCK_SH)
body = f.read
end
body.gsub!(orig_url_pattern) do
# for post_url
quote = Regexp.last_match(1)
post_file = Dir.glob("_posts/#{Regexp.last_match(2)}-#{Regexp.last_match(3)}-*-#{Regexp.last_match(4).to_s.tr("/", "-")}").first
raise "Could not found: _posts/#{Regexp.last_match(2)}-#{Regexp.last_match(3)}-*-#{Regexp.last_match(4).to_s.tr("/", "-")}" if post_file.nil?
" href=#{quote}{{ site.baseurl }}{% post_url #{File.basename(post_file, ".html")} %}#{quote}"
end
File.open(filename, "w") do |f|
f.flock(File::LOCK_EX)
f << body
end
end
end
end
end
class BloggerAtomStreamListener
def initialize
# use `extend` instead of `include` to use `require_deps` instead of `require`.
extend REXML::StreamListener
extend BloggerAtomStreamListenerMethods
@leave_blogger_info = true
@comments = false
end
end
module BloggerAtomStreamListenerMethods
attr_accessor :leave_blogger_info, :comments
attr_reader :original_url_base
def tag_start(tag, attrs)
@tag_bread = [] unless @tag_bread
@tag_bread.push(tag)
case tag
when "entry"
raise "nest entry element" if @in_entry_elem
@in_entry_elem = { :meta => {}, :body => nil }
when "title"
if @in_entry_elem
raise 'only <title type="text"></title> is supported' if attrs["type"] != "text"
end
when "category"
if @in_entry_elem
if attrs["scheme"] == "http://www.blogger.com/atom/ns#"
@in_entry_elem[:meta][:category] = [] unless @in_entry_elem[:meta][:category]
@in_entry_elem[:meta][:category] << attrs["term"]
elsif attrs["scheme"] == "http://schemas.google.com/g/2005#kind"
kind = attrs["term"]
kind.sub!(Regexp.new("^http://schemas\\.google\\.com/blogger/2008/kind\\#"), "")
@in_entry_elem[:meta][:kind] = kind
end
end
when "content"
if @in_entry_elem
@in_entry_elem[:meta][:content_type] = attrs["type"]
end
when "link"
if @in_entry_elem
if attrs["rel"] == "alternate" && attrs["type"] == "text/html"
@in_entry_elem[:meta][:original_url] = attrs["href"]
elsif attrs["rel"] == "replies" && attrs["type"] == "text/html"
unless @in_entry_elem[:meta][:original_url]
@in_entry_elem[:meta][:original_url] = attrs["href"].sub(%r!\#comment-form$!, "")
end
end
end
when "media:thumbnail"
if @in_entry_elem
@in_entry_elem[:meta][:thumbnail] = attrs["url"]
end
when "thr:in-reply-to"
if @in_entry_elem
@in_entry_elem[:meta][:post_id] = attrs["ref"]
end
end
end
def text(text)
if @in_entry_elem
case @tag_bread.last
when "id"
@in_entry_elem[:meta][:id] = text
when "published"
@in_entry_elem[:meta][:published] = text
when "updated"
@in_entry_elem[:meta][:updated] = text
when "title"
@in_entry_elem[:meta][:title] = text
when "content"
@in_entry_elem[:body] = text
when "name"
if @tag_bread[-2..-1] == %w(author name)
@in_entry_elem[:meta][:author] = text
end
when "app:draft"
if @tag_bread[-2..-1] == %w(app:control app:draft)
@in_entry_elem[:meta][:draft] = true if text == "yes"
end
end
end
end
def tag_end(tag)
case tag
when "entry"
raise "nest entry element" unless @in_entry_elem
if @in_entry_elem[:meta][:kind] == "post"
post_data = get_post_data_from_in_entry_elem_info
if post_data
target_dir = "_posts"
target_dir = "_drafts" if @in_entry_elem[:meta][:draft]
FileUtils.mkdir_p(target_dir)
file_name = URI.decode("#{post_data[:filename]}.html")
File.open(File.join(target_dir, file_name), "w") do |f|
f.flock(File::LOCK_EX)
f << post_data[:header].to_yaml
f << "---\n\n"
f << post_data[:body]
end
end
elsif @in_entry_elem[:meta][:kind] == "comment" && @comments
post_data = get_post_data_from_in_entry_elem_info
if post_data
target_dir = "_comments"
FileUtils.mkdir_p(target_dir)
file_name = URI.decode("#{post_data[:filename]}.html")
File.open(File.join(target_dir, file_name), "w") do |f|
f.flock(File::LOCK_EX)
f << post_data[:header].to_yaml
f << "---\n\n"
f << post_data[:body]
end
end
end
@in_entry_elem = nil
end
@tag_bread.pop
end
def get_post_data_from_in_entry_elem_info
if @in_entry_elem.nil? || !@in_entry_elem.key?(:meta) || !@in_entry_elem[:meta].key?(:kind)
nil
elsif @in_entry_elem[:meta][:kind] == "post"
timestamp = Time.parse(@in_entry_elem[:meta][:published]).strftime("%Y-%m-%d")
if @in_entry_elem[:meta][:original_url]
original_uri = URI.parse(@in_entry_elem[:meta][:original_url])
original_path = original_uri.path.to_s
filename = format("%s-%s", timestamp, File.basename(original_path, File.extname(original_path)))
@original_url_base = "#{original_uri.scheme}://#{original_uri.host}"
elsif @in_entry_elem[:meta][:draft]
# Drafts don't have published urls
name = @in_entry_elem[:meta][:title]
filename = if name.nil?
timestamp
else
format("%s-%s", timestamp, CGI.escape(name.downcase.tr("+/\\:'\"<>{}?%*|.", "-")))
end
else
raise "Original URL is missing"
end
header = {
"layout" => "post",
"title" => @in_entry_elem[:meta][:title],
"date" => @in_entry_elem[:meta][:published],
"author" => @in_entry_elem[:meta][:author],
"tags" => @in_entry_elem[:meta][:category],
}
header["modified_time"] = @in_entry_elem[:meta][:updated] if @in_entry_elem[:meta][:updated] && @in_entry_elem[:meta][:updated] != @in_entry_elem[:meta][:published]
header["thumbnail"] = @in_entry_elem[:meta][:thumbnail] if @in_entry_elem[:meta][:thumbnail]
header["blogger_id"] = @in_entry_elem[:meta][:id] if @leave_blogger_info
header["blogger_orig_url"] = @in_entry_elem[:meta][:original_url] if @leave_blogger_info && @in_entry_elem[:meta][:original_url]
body = @in_entry_elem[:body]
# body escaping associated with liquid
if body =~ %r!{{!
body.gsub!(%r!{{!, '{{ "{{" }}')
end
if body =~ %r!{%!
body.gsub!(%r!{%!, '{{ "{%" }}')
end
{ :filename => filename, :header => header, :body => body }
elsif @in_entry_elem[:meta][:kind] == "comment"
timestamp = Time.parse(@in_entry_elem[:meta][:published]).strftime("%Y-%m-%d")
if @in_entry_elem[:meta][:original_url]
unless @comment_seq
@comment_seq = 1
end
original_uri = URI.parse(@in_entry_elem[:meta][:original_url])
original_path = original_uri.path.to_s
filename = format("%s-%s-%s", timestamp, File.basename(original_path, File.extname(original_path)), @comment_seq)
@comment_seq += 1
@original_url_base = "#{original_uri.scheme}://#{original_uri.host}"
else
raise "Original URL is missing"
end
header = {
"date" => @in_entry_elem[:meta][:published],
"author" => @in_entry_elem[:meta][:author],
"blogger_post_id" => @in_entry_elem[:meta][:post_id],
}
header["modified_time"] = @in_entry_elem[:meta][:updated] if @in_entry_elem[:meta][:updated] && @in_entry_elem[:meta][:updated] != @in_entry_elem[:meta][:published]
header["thumbnail"] = @in_entry_elem[:meta][:thumbnail] if @in_entry_elem[:meta][:thumbnail]
header["blogger_id"] = @in_entry_elem[:meta][:id] if @leave_blogger_info
header["blogger_orig_url"] = @in_entry_elem[:meta][:original_url] if @leave_blogger_info && @in_entry_elem[:meta][:original_url]
body = @in_entry_elem[:body]
# body escaping associated with liquid
if body =~ %r!{{!
body.gsub!(%r!{{!, '{{ "{{" }}')
end
if body =~ %r!{%!
body.gsub!(%r!{%!, '{{ "{%" }}')
end
{ :filename => filename, :header => header, :body => body }
end
end
end
end
end
end