开发者

Rails - Building a helper that breaks long words/URLs with <WBR>

开发者 https://www.devze.com 2023-02-12 21:12 出处:网络
my app sends out emails. If there is a very long word or a long URL, it is breaking the email viewing experience by not letting the iphone zoom ad tighten in.

my app sends out emails. If there is a very long word or a long URL, it is breaking the email viewing experience by not letting the iphone zoom ad tighten in.

Here's what I've come up with so far but it's not working, thoughts?

Helper

def html_format(string, max_width=12)
  text = string.gsub("\n", '<br />').html_safe.strip

  (text.length < max_width) ?
  text :
  text.scan(/.{1,#{max_width}}/).join("<wbr>")
开发者_StackOverflow
  return text
end

View

<%= html_format(@comment.content) %>


Here's a method I found online that seems to work well for splitting long strings with <wbr>:

def split_str(str, len = 10)
  fragment = /.{#{len}}/
  str.split(/(\s+)/).map! { |word|
    (/\s/ === word) ? word : word.gsub(fragment, '\0<wbr />')
  }.join
end


This post shows how to wrap long words with regular expressions.

0

精彩评论

暂无评论...
验证码 换一张
取 消