Created
May 9, 2012 08:15
-
-
Save ywjno/2642898 to your computer and use it in GitHub Desktop.
split text content to html content(p tag and br tag)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include Rack::Utils | |
alias_method :h, :escape_html | |
def split_content(string) | |
show_html = "" | |
p_content = [] | |
lines = string.split("\n") | |
lines.each_with_index do |line, index| | |
new_line = h(line.strip) | |
if index != lines.length - 1 | |
unless new_line.empty? | |
p_content << new_line | |
else | |
show_html += "<p>#{p_content.join('<br />')}</p>" | |
p_content = [] | |
end | |
else | |
if p_content.length != 0 | |
p_content << new_line | |
show_html += "<p>#{p_content.join("<br />")}</p>" | |
else | |
show_html += "<p>#{new_line}</p>" | |
end | |
end | |
end | |
show_html | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment