diff --git a/app/helpers/html_helper.rb b/app/helpers/html_helper.rb
index c648cf3ace..522760d7d9 100644
--- a/app/helpers/html_helper.rb
+++ b/app/helpers/html_helper.rb
@@ -1,5 +1,20 @@
module HtmlHelper
def strip_html(html)
- strip_tags(html).andand.gsub(/ /i, ' ').andand.gsub(/&/i, '&')
+ squeeze_linebreaks substitute_entities strip_tags add_linebreaks html
end
+
+ def substitute_entities(html)
+ html.andand.gsub(/ /i, ' ').andand.gsub(/&/i, '&')
+ end
+
+ def add_linebreaks(html)
+ # I know Cthulu is coming for me. Forgive me.
+ # http://stackoverflow.com/a/1732454/2720566
+ html.andand.gsub(/<\/h[^>]>|
]*>|<\/p>|<\/div>/, "\\1\n")
+ end
+
+ def squeeze_linebreaks(html)
+ html.andand.squeeze "\n"
+ end
+
end
diff --git a/spec/helpers/html_helper_spec.rb b/spec/helpers/html_helper_spec.rb
index 729da60d48..4b3118ea10 100644
--- a/spec/helpers/html_helper_spec.rb
+++ b/spec/helpers/html_helper_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe HtmlHelper do
describe "stripping html from a string" do
it "strips tags" do
- helper.strip_html('
Hello world!
').should == 'Hello world!' + helper.strip_html('Hello world!
').should == "Hello world!\n" end it "removes nbsp and amp entities" do @@ -13,5 +13,30 @@ describe HtmlHelper do it "returns nil for nil input" do helper.strip_html(nil).should be_nil end + + describe "line breaks" do + it "adds a line break after heading tags" do + helper.strip_html("foo
").should == "foo\n"; + end + + it "adds a line break after div tags" do + helper.strip_html("foo