diff --git a/Gemfile.lock b/Gemfile.lock index e68ff7e..1452ed2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/lib/wordpress/page.rb b/lib/wordpress/page.rb index 90406d3..1ac5c4f 100644 --- a/lib/wordpress/page.rb +++ b/lib/wordpress/page.rb @@ -23,22 +23,10 @@ module Refinery end def content_formatted - # WordPress doesn't export

-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

-tag with a class corresponding
-        # to the language.
-        # 
-        # Example:
-        # [ruby]p "Hello World"[/ruby] 
-        # -> 
p "Hello world"
- formatted.gsub!(/\[(\w+)\](.+?)\[\/\1\]/m, '
\2
') + formatted = format_syntax_highlighter(format_paragraphs(content)) # remove all tags inside
 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>)/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 

-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("

") 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
-tag with a class corresponding
+        # to the language.
+        # 
+        # Example:
+        # [ruby]p "Hello World"[/ruby] 
+        # -> 
p "Hello world"
+ text.gsub(/\[(\w+)\](.+?)\[\/\1\]/m, '
\2
') + end end end end diff --git a/spec/fixtures/wordpress_dump.xml b/spec/fixtures/wordpress_dump.xml index a38fa0a..c63aa72 100644 --- a/spec/fixtures/wordpress_dump.xml +++ b/spec/fixtures/wordpress_dump.xml @@ -62,7 +62,7 @@ open open hello-world - publish + draft 0 0 post @@ -107,7 +107,7 @@ As a new WordPress user, you should go to :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 diff --git a/spec/lib/wordpress/page_spec.rb b/spec/lib/wordpress/page_spec.rb index 5f0b47a..b140a51 100644 --- a/spec/lib/wordpress/page_spec.rb +++ b/spec/lib/wordpress/page_spec.rb @@ -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('

') + @result.should include('

') + 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(/
/)
+      @result.should include('
') + 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 diff --git a/spec/lib/wordpress/post_spec.rb b/spec/lib/wordpress/post_spec.rb index 0f9a822..def6a12 100644 --- a/spec/lib/wordpress/post_spec.rb +++ b/spec/lib/wordpress/post_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Refinery::WordPress::Post, :type => :model do - let(:post) { test_dump.posts.last } + let(:post) { test_dump.posts.last } it { post.title.should == 'Third blog post' } it { post.content.should include('Lorem ipsum dolor sit') }