1
0
Fork 0
forked from wezm/wezm.net

Generate a rewrite map from old to new content

This commit is contained in:
Wesley Moore 2009-11-19 19:42:22 +11:00
parent c93cb90268
commit c634c6c752
2 changed files with 22 additions and 5 deletions

View file

@ -2,11 +2,11 @@ require 'rubygems'
require 'wordpress' require 'wordpress'
if ARGV.size < 2 if ARGV.size < 3
puts "Usage importer.rb worpress-export.xml /path/to/nanoc/site" puts "Usage importer.rb worpress-export.xml /path/to/nanoc/site /path/to/rewrite_map"
exit 3 exit 3
end end
i = Importer::Wordpress.new(ARGV[0], ARGV[1]) i = Importer::Wordpress.new(ARGV[0], ARGV[1], ARGV[2])
i.run i.run

View file

@ -1,14 +1,17 @@
require 'nokogiri' require 'nokogiri'
require 'nanoc3' require 'nanoc3'
require 'uri'
module Importer module Importer
class Wordpress 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_file = File.open(wordpress_export_path)
@export = Nokogiri::XML(@export_file) @export = Nokogiri::XML(@export_file)
@site = Nanoc3::Site.new(nanoc_site_path) @site = Nanoc3::Site.new(nanoc_site_path)
@rewrite_map = []
@rewrite_map_path = rewrite_map_path
load_categories load_categories
load_tags load_tags
@ -61,6 +64,7 @@ module Importer
puts "Unknown post type: #{item_type}" puts "Unknown post type: #{item_type}"
end end
end end
write_rewrite_map
end end
protected protected
@ -118,7 +122,7 @@ module Importer
# require 'pp' # require 'pp'
# pp attributes # pp attributes
# add_item(content, attributes, identifier) add_item(content, attributes, path)
end end
def process_page(page) def process_page(page)
@ -140,10 +144,23 @@ module Importer
# :excerpt => row['post_excerpt'] # :excerpt => row['post_excerpt']
# } # }
# identifier = '/posts/' + post_date.year.to_s + '/' + post_date.month.to_s + '/' + post_name + '/' # 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) @site.data_sources.first.create_item(content, attributes, identifier)
puts "Added item at #{identifier}" puts "Added item at #{identifier}"
end 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
end end