From dacd503d8f8402ef45d9635fbc04ed1b90f172b6 Mon Sep 17 00:00:00 2001 From: Marc Remolt Date: Fri, 3 Jun 2011 17:34:40 +0200 Subject: [PATCH] Formatting fixes * less
tags by replacing simple_format * improved regex for SyntaxHighlighter (less false positives) --- lib/wordpress/page.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/wordpress/page.rb b/lib/wordpress/page.rb index 7e9718c..cbb8b97 100644 --- a/lib/wordpress/page.rb +++ b/lib/wordpress/page.rb @@ -25,7 +25,7 @@ module Refinery 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, {}, { :sanitize => false }) + formatted = simple_format(content) # Support for SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/): # In WordPress you can (via a plugin) enclose code in [lang][/lang] @@ -35,8 +35,8 @@ module Refinery # Example: # [ruby]p "Hello World"[/ruby] # ->

p "Hello world"
- formatted.gsub!(/\[(\w+)\]/, '
')
-        formatted.gsub!(/\[\/\w+\]/, '
') + formatted.gsub!(/\[(\w+)\](.+?)\[\/\1\]/m, '
\2
') + #formatted.gsub!(/\[\/\w+\]/, '') # remove all tags inside
 that simple_format created
         # TODO: replace simple_format with a method, that ignores pre-tags
@@ -86,6 +86,20 @@ module Refinery
         page.parts.create(:title => 'Body', :body => content_formatted)
         page
       end
+
+      private 
+
+      def simple_format(text, html_options={})
+        text = ''.html_safe if text.nil?
+        start_tag = tag('p', html_options, true)
+        
+        text.gsub!(/\r\n?/, "\n")                    # \r\n and \r -> \n
+        text.gsub!(/\n\n+/, "

\n\n#{start_tag}") # 2+ newline -> paragraph + #text.gsub!(/([^\n]\n)(?=[^\n])/, '\1
') # 1 newline -> br + text.insert 0, start_tag + + text.html_safe.safe_concat("

") + end end end end