From c634c6c752d1c69bd958ee8fa2b2ed601fc6b918 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Thu, 19 Nov 2009 19:42:22 +1100 Subject: [PATCH] Generate a rewrite map from old to new content --- importer/import.rb | 6 +++--- importer/wordpress.rb | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/importer/import.rb b/importer/import.rb index 2dbbbe0..f68f6f9 100644 --- a/importer/import.rb +++ b/importer/import.rb @@ -2,11 +2,11 @@ require 'rubygems' require 'wordpress' -if ARGV.size < 2 - puts "Usage importer.rb worpress-export.xml /path/to/nanoc/site" +if ARGV.size < 3 + puts "Usage importer.rb worpress-export.xml /path/to/nanoc/site /path/to/rewrite_map" exit 3 end -i = Importer::Wordpress.new(ARGV[0], ARGV[1]) +i = Importer::Wordpress.new(ARGV[0], ARGV[1], ARGV[2]) i.run diff --git a/importer/wordpress.rb b/importer/wordpress.rb index 41491e5..2d8fa53 100644 --- a/importer/wordpress.rb +++ b/importer/wordpress.rb @@ -1,14 +1,17 @@ require 'nokogiri' require 'nanoc3' +require 'uri' module Importer class Wordpress - def initialize(wordpress_export_path, nanoc_site_path) + def initialize(wordpress_export_path, nanoc_site_path, rewrite_map_path) @export_file = File.open(wordpress_export_path) @export = Nokogiri::XML(@export_file) @site = Nanoc3::Site.new(nanoc_site_path) + @rewrite_map = [] + @rewrite_map_path = rewrite_map_path load_categories load_tags @@ -61,6 +64,7 @@ module Importer puts "Unknown post type: #{item_type}" end end + write_rewrite_map end protected @@ -118,7 +122,7 @@ module Importer # require 'pp' # pp attributes - # add_item(content, attributes, identifier) + add_item(content, attributes, path) end def process_page(page) @@ -140,10 +144,23 @@ module Importer # :excerpt => row['post_excerpt'] # } # identifier = '/posts/' + post_date.year.to_s + '/' + post_date.month.to_s + '/' + post_name + '/' + if attributes[:status] == 'publish' + @rewrite_map << [attributes[:permalink], identifier] + end @site.data_sources.first.create_item(content, attributes, identifier) puts "Added item at #{identifier}" end + def write_rewrite_map + File.open(@rewrite_map_path, 'w') do |f| + @rewrite_map.each do |old_url, new_path| + uri = URI.parse(old_url) + f.puts uri.path + "\t" + new_path + puts uri.path + " => " + new_path + end + end + end + end end