diff --git a/lib/tasks/wordpress.rake b/lib/tasks/wordpress.rake index 8643a0a..4e7a473 100644 --- a/lib/tasks/wordpress.rake +++ b/lib/tasks/wordpress.rake @@ -1,4 +1,3 @@ -require 'nokogiri' require 'wordpress' namespace :wordpress do @@ -10,8 +9,8 @@ namespace :wordpress do p "Truncating #{table_name} ..." ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}" end - end + end desc "import blog data from a Refinery::WordPress XML dump" task :import_blog, :file_name do |task, params| @@ -20,47 +19,18 @@ namespace :wordpress do dump.authors.each(&:to_refinery) dump.posts.each(&:to_refinery) + + ENV["MODEL"] = 'BlogPost' + Rake::Task["friendly_id:redo_slugs"].invoke + ENV["MODEL"] = nil end - - - - - - - - desc "Import data from a Refinery::WordPress XML dump" - task :import, :file_name do |task, params| + desc "reset blog tables and then import blog data from a Refinery::WordPress XML dump" + task :reset_and_import_blog, :file_name do |task, params| Rake::Task["environment"].invoke + Rake::Task["wordpress:reset_blog"].invoke + Rake::Task["wordpress:import_blog"].invoke(params[:file_name]) end - desc "New import (testing)" - task :new_import, :file_name do |task, params| - Rake::Task["environment"].invoke - - file_name = File.absolute_path(params[:file_name]) - unless File.file?(file_name) && File.readable?(file_name) - raise "Given file '#{file_name}' no file or not readable." - end - - dump = Refinery::WordPress::Dump.new(file_name) - p dump.authors - p dump.pages - dump.posts.each do |post| - p post.title - p post.categories - p post.tags - p post.creator - #p post.content - end - end - - - desc "Import data from a Refinery::WordPress XML dump into a clean database (reset first)" - task :import_clean, :file_name do |task, params| - Rake::Task["wordpress:reset"].invoke - Rake::Task["wordpress:import"].invoke(params[:file_name]) - - end end diff --git a/lib/wordpress/page.rb b/lib/wordpress/page.rb index 22badf5..e4ff4d9 100644 --- a/lib/wordpress/page.rb +++ b/lib/wordpress/page.rb @@ -1,6 +1,9 @@ module Refinery module WordPress class Page + include ::ActionView::Helpers::TagHelper + include ::ActionView::Helpers::TextHelper + attr_reader :node def initialize(node) @@ -19,6 +22,31 @@ module Refinery node.xpath("content:encoded").text end + def content_formatted + # WordPress doesn't export
-Tags, so let's run a simple_format over + # the content + formatted = simple_format(content) + + # Support for SyntaxHighlighter: + # In WordPress you can (via a plugin) enclose code in [lang][/lang] + # blocks, which are converted to a
-tag with a class corresponding + # to the language. + # + # Example: + # [ruby]p "Hello World"[/ruby] + # ->p "Hello world"+ formatted.gsub!(/\[(\w+)\]/, '') + formatted.gsub!(/\[\/\w+\]/, '') + + # remove all tags insidethat simple_format created + # TODO: replace simple_format with a method, that ignores pre-tags + formatted.gsub!(/()(.+?)(<\/pre>)/m) do |match| + "#{$1}#{strip_tags($2)}#{$3}" + end + + formatted + end + def creator node.xpath("dc:creator").text end @@ -51,7 +79,7 @@ module Refinery page = ::Page.create!(:title => title, :created_at => post_date, :draft => draft?, :parent_id => parent_id) - page.parts.create(:title => 'Body', :body => content) + page.parts.create(:title => 'Body', :body => content_formatted) page end end diff --git a/lib/wordpress/post.rb b/lib/wordpress/post.rb index a5c8773..f21b7a2 100644 --- a/lib/wordpress/post.rb +++ b/lib/wordpress/post.rb @@ -31,7 +31,7 @@ module Refinery post = BlogPost.new - post = ::BlogPost.create! :title => title, :body => content, :draft => draft?, + post = ::BlogPost.create! :title => title, :body => content_formatted, :draft => draft?, :published_at => post_date, :created_at => post_date, :author => user, :tag_list => tag_list diff --git a/spec/lib/wordpress/dump_spec.rb b/spec/lib/wordpress/dump_spec.rb index ed567db..371b4e9 100644 --- a/spec/lib/wordpress/dump_spec.rb +++ b/spec/lib/wordpress/dump_spec.rb @@ -94,6 +94,9 @@ describe Refinery::WordPress::Dump, :type => :model do it { page.should_not == dump.pages.first } describe "#to_refinery" do + include ::ActionView::Helpers::TagHelper + include ::ActionView::Helpers::TextHelper + before do # "About me" has a parent page with id 8 in the XML dump, # would otherwise fails creation @@ -111,7 +114,7 @@ describe Refinery::WordPress::Dump, :type => :model do @page.title.should == page.title @page.draft.should == page.draft? @page.created_at.should == page.post_date - @page.parts.first.body.should == " #{page.content}
" + @page.parts.first.body.should == "#{simple_format(page.content)}" end end end