Browse files

Added #preserve_whitespace_within method which takes a tag name as an…

… argument
  • Loading branch information...
1 parent e13ad85 commit 533ec2bcecd299d5ebb732a83be3313ce44e942b @snitko committed May 30, 2012
Showing with 21 additions and 7 deletions.
  1. +13 −1 lib/string_master/string_master.rb
  2. +8 −6 spec/lib/string_master/string_master_spec.rb
View
14 lib/string_master/string_master.rb
@@ -120,8 +120,20 @@ def wrap_lines(tag, regexp)
end
# Finds all lines that start with 4 spaces and wraps them into <code> tags.
+ # It also transforms each occurence of 2 or more spaces into an &nbsp; entity,
+ # which is available as a standalone method #preserve_whitespace
def wrap_code
- wrap_lines("code", /\A\s{4}/) # wrap lines already returns `self`
+ wrap_lines("code", /\A\s{4}/)
+ preserve_whitespace_within("code") # already returns `self`
+ end
+
+ # Preserves whitespace within a given tag. Each occurence of 2 or more spaces
+ # is transformed into a &nbsp; entities.
+ def preserve_whitespace_within(tag)
+ @modified_string.gsub!(/<#{tag}>.+?<\/#{tag}>/) do |match|
+ match.gsub(/\s\s+/) { |m| "&nbsp;"*m.length }
+ end
+ self
end
def wrap_inline_code(opening_tag="<span class=\"inlineCode\">", closing_tag="</span>")
View
14 spec/lib/string_master/string_master_spec.rb
@@ -37,14 +37,18 @@
it "wraps code in <code> tags" do
code = <<CODE
I have a piece of code
- puts "hello world"
- exit
+ def say_hello
+ puts "hello world"
+ return true
+ end
and here's what my code looks like.
CODE
StringMaster.new(code).wrap_code.to_s.should == <<WRAPPED_CODE
I have a piece of code
-<code>puts "hello world"
-exit</code>
+<code>def say_hello
+ puts "hello world"
+ return true
+end</code>
and here's what my code looks like.
WRAPPED_CODE
end
@@ -87,6 +91,4 @@
"<img src=\"http://images.com/image.jpg\" alt=\"\" /> <b>Hello <a href=\"http://url.com\" target=\"_blank\">http://url.com</a> </b>"
end
-
-
end

0 comments on commit 533ec2b

Please sign in to comment.