Skip to content

Instantly share code, notes, and snippets.

@ywjno
Created May 9, 2012 08:15
Show Gist options
  • Save ywjno/2642898 to your computer and use it in GitHub Desktop.
Save ywjno/2642898 to your computer and use it in GitHub Desktop.
split text content to html content(p tag and br tag)
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