More refactorings and specs

This commit is contained in:
Marc Remolt 2011-06-05 13:38:39 +02:00
parent ddb65040a7
commit 76830ec51c
6 changed files with 94 additions and 22 deletions

View File

@ -51,7 +51,7 @@ GEM
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
diff-lcs (1.1.2)
dragonfly (0.9.2)
dragonfly (0.9.3)
rack
erubis (2.6.6)
abstract (>= 1.0.0)
@ -125,7 +125,7 @@ GEM
will_paginate (~> 3.0.pre)
refinerycms-dashboard (1.0.0)
refinerycms-core (= 1.0.0)
refinerycms-generators (1.0.1)
refinerycms-generators (1.0.2)
refinerycms-images (1.0.0)
dragonfly (~> 0.9.0)
rack-cache (>= 0.5.3)

View File

@ -23,22 +23,10 @@ module Refinery
end
def content_formatted
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content. As we trust ourselves, no sanatize.
formatted = simple_format(content)
# Support for SyntaxHighlighter (http://alexgorbatchev.com/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+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
formatted = format_syntax_highlighter(format_paragraphs(content))
# remove all tags inside <pre> that simple_format created
# TODO: replace simple_format with a method, that ignores pre-tags
# TODO: replace format_paragraphs with a method, that ignores pre-tags
formatted.gsub!(/(<pre.*?>)(.+?)(<\/pre>)/m) do |match|
"#{$1}#{strip_tags($2)}#{$3}"
end
@ -89,7 +77,10 @@ module Refinery
private
def simple_format(text, html_options={})
def format_paragraphs(text, html_options={})
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content. As we trust ourselves, no sanatize. This code is heavily
# inspired by the simple_format rails helper
text = ''.html_safe if text.nil?
start_tag = tag('p', html_options, true)
@ -99,6 +90,18 @@ module Refinery
text.html_safe.safe_concat("</p>")
end
def format_syntax_highlighter(text)
# Support for SyntaxHighlighter (http://alexgorbatchev.com/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>
text.gsub(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
end
end
end
end

View File

@ -62,7 +62,7 @@
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>hello-world</wp:post_name>
<wp:status>publish</wp:status>
<wp:status>draft</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>post</wp:post_type>
@ -107,7 +107,7 @@ As a new WordPress user, you should go to <a href="http://localhost/wordpress/wp
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>sample-page</wp:post_name>
<wp:status>publish</wp:status>
<wp:status>draft</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>page</wp:post_type>

View File

@ -1,8 +1,7 @@
require 'spec_helper'
describe Refinery::WordPress::Dump, :type => :model do
let(:file_name) { File.realpath(File.join(File.dirname(__FILE__), '../../fixtures/wordpress_dump.xml')) }
let(:dump) { Refinery::WordPress::Dump.new(file_name) }
let(:dump) { test_dump }
it "should create a Dump object given a xml file" do
dump.should be_a Refinery::WordPress::Dump
@ -46,6 +45,10 @@ describe Refinery::WordPress::Dump, :type => :model do
it "should return all included pages" do
dump.pages.should have(3).pages
end
it "should return only published pages with only_published=true" do
dump.pages(true).should have(2).pages
end
end
describe "#authors" do
@ -58,5 +61,9 @@ describe Refinery::WordPress::Dump, :type => :model do
it "should return all posts" do
dump.posts.should have(3).posts
end
it "should return only published posts with only_published=true" do
dump.posts(true).should have(2).posts
end
end
end

View File

@ -39,5 +39,67 @@ describe Refinery::WordPress::Page, :type => :model do
@page.parts.first.body.should == "#{simple_format(page.content)}"
end
end
describe "#format_paragraphs" do
let(:sample_text) do
text = <<-EOT
This is sample text.
Even more text.
But this time no paragraph.
EOT
end
before do
@result = page.send(:format_paragraphs, sample_text)
end
it "should add paragraphs to the sample text" do
@result.should include('<p>')
@result.should include('</p>')
end
end
describe "#format_syntax_highlighter" do
let(:sample_text) do
text = <<-EOT
This is sample text.
[ruby]
p "this is ruby code"
[/ruby]
This is more sample text.
EOT
end
before do
@result = page.send(:format_syntax_highlighter, sample_text)
end
it "should reformat the [ruby] tag to a pre with correct class" do
@result.should match(/<pre class="brush: ruby">/)
@result.should include('</pre>')
end
context "without correct code tags" do
let(:sample_text) do
text = <<-EOT
This is sample text.
[ruby]
p "this is ruby code"
[/php]
This is more sample text.
EOT
end
it "should not reformat the [ruby] tag" do
@result.should == sample_text
end
end
end
end