开发者

How to wrap words in HTML document without attributes and tag names

I have an HTML document that has long words:

<div>this is a veeeeeeeeeeeerryyyyyyyyloooongwoooord<img src="/fooooooooobaaar.jof" ></div>

I want to word-wrap it without cutting the tags or its attributes:

<div>this is a veeeeeeeeeeeerryyyyy yyyloooongwoooord<img src="/fooooooooobaaar.jof" ></div>

Also, it's possible that I will not have any HTML tag at all.

I tried Nokogiri, but it inserts a paragraph in tagless input, and wraps the whole response with an HTML document, which is not my intention.

What is the best way to accomplish this?

require "Nokogiri"
class String
  def wrap()
    doc = Nokogiri::HTML(self)
    doc.at("body").traverse do |p|
      if p.is_a?(Nokogiri::XML::Text)
        input = p.content
        p.content = input.scan(/.{1,25}/).join(" ")
 开发者_运维技巧     end
    end
    doc.to_s # I want only the wrapped string, without the head/body stuff
  end
end


I think using Nokogiri::XML(self) instead of Nokogiri::HTML(self) will help you.


This looks like a starting point for you:

require 'nokogiri'

max_word_length = 30
html = '<div>this is a veeeeeeeeeeeerryyyyyyyyloooongwoooord<img src="/fooooooooobaaar.jof" ></div>'

doc = Nokogiri::HTML.fragment(html)

doc.search('text()').each do |n|
  n.content = n.content.split(' ').map { |l|
    if (l.size > max_word_length)
      l = l.scan(/.{1,#{ max_word_length }}/).join("\n")
    end
    l
  }.join(' ')
end

puts doc.to_html
# >> <div>this is a veeeeeeeeeeeerryyyyyyyyloooong
# >> woooord<img src="/fooooooooobaaar.jof">
# >> </div>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜