HTML-Formatting

* added simple_format for content content
* added support for SyntaxHighlighter JS
* finished rake tasks for blog
This commit is contained in:
Marc Remolt 2011-06-02 16:22:50 +02:00
parent a45f502f44
commit afb35f58ad
4 changed files with 43 additions and 42 deletions

View File

@ -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

View File

@ -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 <p>-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 <pre>-tag with a class corresponding
# to the language.
#
# Example:
# [ruby]p "Hello World"[/ruby]
# -> <pre class="brush: ruby">p "Hello world"</pre>
formatted.gsub!(/\[(\w+)\]/, '<pre class="brush: \1">')
formatted.gsub!(/\[\/\w+\]/, '</pre>')
# remove all tags inside <pre> that simple_format created
# TODO: replace simple_format with a method, that ignores pre-tags
formatted.gsub!(/(<pre.*?>)(.+?)(<\/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

View File

@ -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

View File

@ -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 == "<p>#{page.content}</p>"
@page.parts.first.body.should == "#{simple_format(page.content)}"
end
end
end