Clean up Tumblr importer and update History.

This commit is contained in:
Tom Preston-Werner 2011-05-09 00:54:26 -07:00
parent 9cc9c233d0
commit b86bc97c5f
3 changed files with 13 additions and 13 deletions

View File

@ -9,6 +9,7 @@
* Fixes for Wordpress importer (#274, #252, #271) * Fixes for Wordpress importer (#274, #252, #271)
* Better error message for invalid post date (#291) * Better error message for invalid post date (#291)
* Print formatted fatal exceptions to stdout on build failure * Print formatted fatal exceptions to stdout on build failure
* Add Tumblr importer (#323)
* Bug Fixes * Bug Fixes
* Secure additional path exploits * Secure additional path exploits

View File

@ -164,6 +164,7 @@ if ARGV.size > 0
:mephisto => 'Mephisto', :mephisto => 'Mephisto',
:mt => 'MT', :mt => 'MT',
:textpattern => 'TextPattern', :textpattern => 'TextPattern',
:tumblr => 'Tumblr',
:typo => 'Typo' :typo => 'Typo'
} }

View File

@ -8,7 +8,6 @@ require 'date'
module Jekyll module Jekyll
module Tumblr module Tumblr
def self.process(url, grab_images = false) def self.process(url, grab_images = false)
current_page = 0 current_page = 0
@ -17,7 +16,7 @@ module Jekyll
doc = Nokogiri::HTML(Iconv.conv("utf-8", f.charset, f.readlines.join("\n"))) doc = Nokogiri::HTML(Iconv.conv("utf-8", f.charset, f.readlines.join("\n")))
puts "Page: #{current_page + 1} - Posts: #{(doc/:tumblr/:posts/:post).size}" puts "Page: #{current_page + 1} - Posts: #{(doc/:tumblr/:posts/:post).size}"
FileUtils.mkdir_p "_posts/tumblr" FileUtils.mkdir_p "_posts/tumblr"
(doc/:tumblr/:posts/:post).each do |post| (doc/:tumblr/:posts/:post).each do |post|
@ -41,19 +40,19 @@ module Jekyll
content << "<br/>" + CGI::unescapeHTML(post.at("link-description").inner_html) unless post.at("link-description") == nil content << "<br/>" + CGI::unescapeHTML(post.at("link-description").inner_html) unless post.at("link-description") == nil
elsif post['type'] == "photo" elsif post['type'] == "photo"
content = "" content = ""
if post.at("photo-link-url") != nil if post.at("photo-link-url") != nil
content = "<a href=\"#{post.at("photo-link-url").inner_html}\"><img src=\"#{save_file((post/"photo-url")[1].inner_html, grab_images)}\"/></a>" content = "<a href=\"#{post.at("photo-link-url").inner_html}\"><img src=\"#{save_file((post/"photo-url")[1].inner_html, grab_images)}\"/></a>"
else else
content = "<img src=\"#{save_file((post/"photo-url")[1].inner_html, grab_images)}\"/>" content = "<img src=\"#{save_file((post/"photo-url")[1].inner_html, grab_images)}\"/>"
end end
if post.at("photo-caption") != nil if post.at("photo-caption") != nil
content << "<br/>" unless content == nil content << "<br/>" unless content == nil
content << CGI::unescapeHTML(post.at("photo-caption").inner_html) content << CGI::unescapeHTML(post.at("photo-caption").inner_html)
end end
elsif post['type'] == "audio" elsif post['type'] == "audio"
content = CGI::unescapeHTML(post.at("audio-player").inner_html) content = CGI::unescapeHTML(post.at("audio-player").inner_html)
content << CGI::unescapeHTML(post.at("audio-caption").inner_html) unless post.at("audio-caption") == nil content << CGI::unescapeHTML(post.at("audio-caption").inner_html) unless post.at("audio-caption") == nil
elsif post['type'] == "quote" elsif post['type'] == "quote"
content = "<blockquote>" + CGI::unescapeHTML(post.at("quote-text").inner_html) + "</blockquote>" content = "<blockquote>" + CGI::unescapeHTML(post.at("quote-text").inner_html) + "</blockquote>"
@ -61,23 +60,23 @@ module Jekyll
elsif post['type'] == "conversation" elsif post['type'] == "conversation"
title = post.at("conversation-title").inner_html unless post.at("conversation-title") == nil title = post.at("conversation-title").inner_html unless post.at("conversation-title") == nil
content = "<section><dialog>" content = "<section><dialog>"
(post/:conversation/:line).each do |line| (post/:conversation/:line).each do |line|
content << "<dt>" + line['label'] + "</dt><dd>" + line.inner_html + "</dd>" unless line['label'] == nil || line == nil content << "<dt>" + line['label'] + "</dt><dd>" + line.inner_html + "</dd>" unless line['label'] == nil || line == nil
end end
content << "<section><dialog>" content << "<section><dialog>"
elsif post['type'] == "video" elsif post['type'] == "video"
title = post.at("video-title").inner_html unless post.at("video-title") == nil title = post.at("video-title").inner_html unless post.at("video-title") == nil
content = CGI::unescapeHTML(post.at("video-player").inner_html) content = CGI::unescapeHTML(post.at("video-player").inner_html)
content << CGI::unescapeHTML(post.at("video-caption").inner_html) unless post.at("video-caption") == nil content << CGI::unescapeHTML(post.at("video-caption").inner_html) unless post.at("video-caption") == nil
end # End post types end # End post types
name = "#{Date.parse(post['date']).to_s}-#{post['id'].downcase.gsub(/[^a-z0-9]/, '-')}.html" name = "#{Date.parse(post['date']).to_s}-#{post['id'].downcase.gsub(/[^a-z0-9]/, '-')}.html"
if title != nil || content != nil && name != nil if title != nil || content != nil && name != nil
File.open("_posts/tumblr/#{name}", "w") do |f| File.open("_posts/tumblr/#{name}", "w") do |f|
f.puts <<-HEADER f.puts <<-HEADER
--- ---
layout: post layout: post
@ -106,16 +105,15 @@ HEADER
def self.save_file(url, grab_image = false) def self.save_file(url, grab_image = false)
unless grab_image == false unless grab_image == false
FileUtils.mkdir_p "tumblr_files" FileUtils.mkdir_p "tumblr_files"
File.open("tumblr_files/#{url.split('/').last}", "w") do |f| File.open("tumblr_files/#{url.split('/').last}", "w") do |f|
f.write(open(url).read) f.write(open(url).read)
end end
return "/tumblr_files/#{url.split('/').last}" return "/tumblr_files/#{url.split('/').last}"
else else
return url return url
end end
end end
end end
end end