Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality for importing Blogger comments #258

Merged
merged 2 commits into from Jul 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 67 additions & 1 deletion lib/jekyll-import/importers/blogger.rb
Expand Up @@ -5,6 +5,7 @@ def self.specify_options(c)
c.option 'source', '--source NAME', '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)
Expand Down Expand Up @@ -41,6 +42,7 @@ def self.process(options)
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)
Expand Down Expand Up @@ -95,11 +97,12 @@ def initialize
extend BloggerAtomStreamListenerMethods

@leave_blogger_info = true
@comments = false
end
end

module BloggerAtomStreamListenerMethods
attr_accessor :leave_blogger_info
attr_accessor :leave_blogger_info, :comments
attr_reader :original_url_base

def tag_start(tag, attrs)
Expand Down Expand Up @@ -143,6 +146,10 @@ def tag_start(tag, attrs)
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

Expand Down Expand Up @@ -185,6 +192,23 @@ def tag_end(tag)

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' and @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)
Expand Down Expand Up @@ -251,6 +275,48 @@ def get_post_data_from_in_entry_elem_info
body.gsub!(/{%/, '{{ "{%" }}')
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]
if not @comment_seq
@comment_seq = 1
end

original_uri = URI.parse(@in_entry_elem[:meta][:original_url])
original_path = original_uri.path.to_s
filename = "%s-%s-%s" %
[timestamp,
File.basename(original_path, File.extname(original_path)),
@comment_seq]

@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 =~ /{{/
body.gsub!(/{{/, '{{ "{{" }}')
end
if body =~ /{%/
body.gsub!(/{%/, '{{ "{%" }}')
end

{ :filename => filename, :header => header, :body => body }
else
nil
Expand Down