From 70aaded1e99ba6619b9c72244d790115a03483a9 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Mon, 20 Jun 2011 22:46:38 +0900 Subject: [PATCH 1/8] fix path name in syntax error message. --- lib/jekyll/convertible.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 46dbfea1..e6459683 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -78,7 +78,7 @@ module Jekyll begin self.content = Liquid::Template.parse(self.content).render(payload, info) rescue => e - puts "Liquid Exception: #{e.message} in #{self.data["layout"]}" + puts "Liquid Exception: #{e.message} in #{self.name}" end self.transform From 365f57e8b3cdae73d63e7e44fba7b41cd4cc8e26 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 28 Jun 2011 02:05:51 -0700 Subject: [PATCH 2/8] Added meta tag import goodness. This for instance allows you to preserve all your hard-worked on WP SEO keywords, images, alternative images and other yummy-ness. Replaced PubDate with wp:post_date, this is better than PubDate since some of the posts you import could be a draft (in this case the pubDate is invalid and contains a non-sensical value). Added wp:status so we now know whether the post is published, draft or in the trash. Added wp:post_type so we differentiate between posts and image or other post types --- lib/jekyll/migrators/wordpressdotcom.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/migrators/wordpressdotcom.rb b/lib/jekyll/migrators/wordpressdotcom.rb index 53218e50..62187a77 100644 --- a/lib/jekyll/migrators/wordpressdotcom.rb +++ b/lib/jekyll/migrators/wordpressdotcom.rb @@ -4,6 +4,7 @@ require 'rubygems' require 'hpricot' require 'fileutils' require 'yaml' +require 'time' module Jekyll # This importer takes a wordpress.xml file, which can be exported from your @@ -18,13 +19,26 @@ module Jekyll (doc/:channel/:item).each do |item| title = item.at(:title).inner_text.strip permalink_title = item.at('wp:post_name').inner_text - date = Time.parse(item.at(:pubDate).inner_text) + date = Time.parse(item.at('wp:post_date').inner_text) + status = item.at('wp:status').inner_text + type = item.at('wp:post_type').inner_text tags = (item/:category).map{|c| c.inner_text}.reject{|c| c == 'Uncategorized'}.uniq + + metas = Hash.new + item.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 + name = "#{date.strftime('%Y-%m-%d')}-#{permalink_title}.html" header = { 'layout' => 'post', 'title' => title, - 'tags' => tags + 'tags' => tags, + 'status' => status, + 'type' => type, + 'meta' => metas } File.open("_posts/#{name}", "w") do |f| From 3389c6d508acf0982c28132d299a8a5838f0f50b Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 28 Jun 2011 02:31:42 -0700 Subject: [PATCH 3/8] Sometimes wp:post_name can be empty (e.g. when a post is still draft), in this case we make up an appropriate permalink_title that will be used as the filename. The importer can always rename the file later on, and at least the file is unlikely to have been overwritten by another draft on the same day. --- lib/jekyll/migrators/wordpressdotcom.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/jekyll/migrators/wordpressdotcom.rb b/lib/jekyll/migrators/wordpressdotcom.rb index 62187a77..e8b5debe 100644 --- a/lib/jekyll/migrators/wordpressdotcom.rb +++ b/lib/jekyll/migrators/wordpressdotcom.rb @@ -19,6 +19,11 @@ module Jekyll (doc/:channel/:item).each do |item| title = item.at(:title).inner_text.strip permalink_title = item.at('wp:post_name').inner_text + # Fallback to "prettified" title if post_name is empty (can happen) + if permalink_title == "" + permalink_title = title.downcase.split.join('-') + end + date = Time.parse(item.at('wp:post_date').inner_text) status = item.at('wp:status').inner_text type = item.at('wp:post_type').inner_text From 29c4808f2a335265a60abfe3aa25f0fa42fb28bf Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 28 Jun 2011 06:31:03 -0700 Subject: [PATCH 4/8] Drafts are now marked as published:false --- lib/jekyll/migrators/wordpressdotcom.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/jekyll/migrators/wordpressdotcom.rb b/lib/jekyll/migrators/wordpressdotcom.rb index e8b5debe..8e8cbdcd 100644 --- a/lib/jekyll/migrators/wordpressdotcom.rb +++ b/lib/jekyll/migrators/wordpressdotcom.rb @@ -26,6 +26,12 @@ module Jekyll date = Time.parse(item.at('wp:post_date').inner_text) status = item.at('wp:status').inner_text + if status == "draft" + published = false + else + published = true + end + type = item.at('wp:post_type').inner_text tags = (item/:category).map{|c| c.inner_text}.reject{|c| c == 'Uncategorized'}.uniq @@ -43,6 +49,7 @@ module Jekyll 'tags' => tags, 'status' => status, 'type' => type, + 'published' => published, 'meta' => metas } From eb6a2b9bd07c597184463460d7190c732dcff8fc Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 28 Jun 2011 07:21:45 -0700 Subject: [PATCH 5/8] Now creates _ for each post type (e.g. _posts, _pages, _attachments) --- lib/jekyll/migrators/wordpressdotcom.rb | 27 +++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/jekyll/migrators/wordpressdotcom.rb b/lib/jekyll/migrators/wordpressdotcom.rb index 8e8cbdcd..a4504043 100644 --- a/lib/jekyll/migrators/wordpressdotcom.rb +++ b/lib/jekyll/migrators/wordpressdotcom.rb @@ -11,9 +11,7 @@ module Jekyll # wordpress.com blog (/wp-admin/export.php). module WordpressDotCom def self.process(filename = "wordpress.xml") - FileUtils.mkdir_p "_posts" - posts = 0 - + import_count = Hash.new doc = Hpricot::XML(File.read(filename)) (doc/:channel/:item).each do |item| @@ -26,10 +24,11 @@ module Jekyll date = Time.parse(item.at('wp:post_date').inner_text) status = item.at('wp:status').inner_text - if status == "draft" - published = false - else + + if status == "publish" published = true + else + published = false end type = item.at('wp:post_type').inner_text @@ -44,7 +43,7 @@ module Jekyll name = "#{date.strftime('%Y-%m-%d')}-#{permalink_title}.html" header = { - 'layout' => 'post', + 'layout' => type, 'title' => title, 'tags' => tags, 'status' => status, @@ -53,16 +52,22 @@ module Jekyll 'meta' => metas } - File.open("_posts/#{name}", "w") do |f| + FileUtils.mkdir_p "_#{type}s" + File.open("_#{type}s/#{name}", "w") do |f| f.puts header.to_yaml f.puts '---' f.puts item.at('content:encoded').inner_text end - - posts += 1 + if import_count[type] == nil + import_count[type] = 1 + else + import_count[type] += 1 + end end - puts "Imported #{posts} posts" + import_count.each do |key, value| + puts "Imported #{value} #{key}s" + end end end end From eebeaf5dfb16c998e159dfc06d0266340d27f168 Mon Sep 17 00:00:00 2001 From: Matt Palmer Date: Sat, 2 Jul 2011 10:35:41 +1000 Subject: [PATCH 6/8] Give name of file that generated YAML exception message It's not much good saying "hey, there was a problem" if you don't know where the problem *is*. Hunting through several hundred YAML files is no fun. --- lib/jekyll/convertible.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 46dbfea1..0ad8a882 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -32,7 +32,7 @@ module Jekyll begin self.data = YAML.load($1) rescue => e - puts "YAML Exception: #{e.message}" + puts "YAML Exception reading #{name}: #{e.message}" end end From 8c35cc957bcb9d3123d31e6c59ca628db796a11e Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 4 Jul 2011 04:59:40 -0700 Subject: [PATCH 7/8] Create import_hash with .new(0) so that we don't have to check if a hash exists before incrementing it. --- lib/jekyll/migrators/wordpressdotcom.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/jekyll/migrators/wordpressdotcom.rb b/lib/jekyll/migrators/wordpressdotcom.rb index a4504043..ca6752ef 100644 --- a/lib/jekyll/migrators/wordpressdotcom.rb +++ b/lib/jekyll/migrators/wordpressdotcom.rb @@ -11,7 +11,7 @@ module Jekyll # wordpress.com blog (/wp-admin/export.php). module WordpressDotCom def self.process(filename = "wordpress.xml") - import_count = Hash.new + import_count = Hash.new(0) doc = Hpricot::XML(File.read(filename)) (doc/:channel/:item).each do |item| @@ -58,11 +58,8 @@ module Jekyll f.puts '---' f.puts item.at('content:encoded').inner_text end - if import_count[type] == nil - import_count[type] = 1 - else - import_count[type] += 1 - end + + import_count[type] += 1 end import_count.each do |key, value| From 00a0d14702e26cd51c36125aaf81e84fe648a626 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 4 Jul 2011 05:09:50 -0700 Subject: [PATCH 8/8] Tidied up indentation --- lib/jekyll/migrators/wordpressdotcom.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/jekyll/migrators/wordpressdotcom.rb b/lib/jekyll/migrators/wordpressdotcom.rb index ca6752ef..701c2af4 100644 --- a/lib/jekyll/migrators/wordpressdotcom.rb +++ b/lib/jekyll/migrators/wordpressdotcom.rb @@ -18,18 +18,18 @@ module Jekyll title = item.at(:title).inner_text.strip permalink_title = item.at('wp:post_name').inner_text # Fallback to "prettified" title if post_name is empty (can happen) - if permalink_title == "" - permalink_title = title.downcase.split.join('-') - end + if permalink_title == "" + permalink_title = title.downcase.split.join('-') + end date = Time.parse(item.at('wp:post_date').inner_text) status = item.at('wp:status').inner_text - if status == "publish" - published = true - else - published = false - end + if status == "publish" + published = true + else + published = false + end type = item.at('wp:post_type').inner_text tags = (item/:category).map{|c| c.inner_text}.reject{|c| c == 'Uncategorized'}.uniq @@ -59,7 +59,7 @@ module Jekyll f.puts item.at('content:encoded').inner_text end - import_count[type] += 1 + import_count[type] += 1 end import_count.each do |key, value|