Skip to content

Instantly share code, notes, and snippets.

@danrabinowitz
Created July 26, 2018 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danrabinowitz/e17d8c70399354a13114d97910973fa9 to your computer and use it in GitHub Desktop.
Save danrabinowitz/e17d8c70399354a13114d97910973fa9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'erb'
require 'active_support'
require 'active_support/core_ext'
def newlines_to_br(s)
html = "".html_safe
s.split("\n").each_with_index do |line, idx|
html << "<br />".html_safe if idx > 0
html << line
end
html
end
TEST_CASES = [
["a", "a"],
["a\nb", "a<br />b"],
["a<div>b", "a&lt;div&gt;b"],
]
TEST_CASES.each do |test_case|
result = newlines_to_br(test_case[0])
rendered_string = ERB::Util.html_escape(result)
expected_result = test_case[1]
if rendered_string != expected_result
puts "ERROR: expect #{rendered_string} to equal #{expected_result}"
else
puts "PASS"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment