lib/jekyll-import/importers/tumblr.rb in jekyll-import-0.11.0 vs lib/jekyll-import/importers/tumblr.rb in jekyll-import-0.12.0
- old
+ new
@@ -40,14 +40,11 @@
current_page = (current_page || -1) + 1
feed_url = url + "?num=#{per_page}&start=#{current_page * per_page}"
puts "Fetching #{feed_url}"
feed = open(feed_url)
contents = feed.readlines.join("\n")
- beginning = contents.index("{")
- ending = contents.rindex("}")
- json = contents[beginning..ending] # Strip Tumblr's JSONP chars.
- blog = JSON.parse(json)
+ blog = extract_json(contents)
puts "Page: #{current_page + 1} - Posts: #{blog["posts"].size}"
batch = blog["posts"].map { |post| post_to_hash(post, format) }
# If we're rewriting, save the posts for later. Otherwise, go ahead and
# dump these to disk now
@@ -66,10 +63,17 @@
end
end
private
+ def self.extract_json(contents)
+ beginning = contents.index("{")
+ ending = contents.rindex("}")+1
+ json = contents[beginning...ending] # Strip Tumblr's JSONP chars.
+ blog = JSON.parse(json)
+ end
+
# Writes a post out to disk
def self.write_post(post, use_markdown, add_highlights)
content = post[:content]
if content
@@ -133,16 +137,16 @@
title = post["conversation-title"]
content = "<section><dialog>"
post["conversation"].each do |line|
content << "<dt>#{line['label']}</dt><dd>#{line['phrase']}</dd>"
end
- content << "</section></dialog>"
+ content << "</dialog></section>"
when "video"
title = post["video-title"]
content = post["video-player"]
unless post["video-caption"].nil?
- unless content.nil?
+ if content
content << "<br/>" + post["video-caption"]
else
content = post["video-caption"]
end
end
@@ -207,13 +211,17 @@
def self.rewrite_urls_and_redirects(posts)
site = Jekyll::Site.new(Jekyll.configuration({}))
urls = Hash[posts.map { |post|
# Create an initial empty file for the post so that
# we can instantiate a post object.
- File.open("_posts/tumblr/#{post[:name]}", "w")
+ File.write("_posts/tumblr/#{post[:name]}", "")
tumblr_url = URI.parse(URI.encode(post[:slug])).path
- jekyll_url = Jekyll::Post.new(site, Dir.pwd, "", "tumblr/" + post[:name]).url
+ jekyll_url = if Jekyll.const_defined? :Post
+ Jekyll::Post.new(site, Dir.pwd, "", "tumblr/" + post[:name]).url
+ else
+ Jekyll::Document.new(File.expand_path("_posts/tumblr/#{post[:name]}"), site: site, collection: site.posts).url
+ end
redirect_dir = tumblr_url.sub(/\//, "") + "/"
FileUtils.mkdir_p redirect_dir
File.open(redirect_dir + "index.html", "w") do |f|
f.puts "<html><head><link rel=\"canonical\" href=\"" +
"#{jekyll_url}\"><meta http-equiv=\"refresh\" content=\"0; " +
@@ -279,10 +287,10 @@
FileUtils.mkdir_p "tumblr_files"
# Don't fetch if we've already cached this file
unless File.size? path
puts "Fetching photo #{url}"
- File.open(path, "w") { |f| f.write(open(url).read) }
+ File.open(path, "wb") { |f| f.write(open(url).read) }
end
url = "/" + path
end
url
end