[Rails] Re: Using Nokogiri to insert a <span> tag into existing text
This works:
#Surround all of the text (NOT attribute value) merge fields with <span>
tags for ease of manipulation later
doc = Nokogiri::HTML.parse(html)
nodes = doc.xpath("//text()[contains(.,'MERGE')]")
nodes.each do |node|
text = node.text.dup
if md = text.match(/.{2}MERGE(\d+).{2}/)
start_index = text.index(md[0])
end_index = start_index + md[0].length
node = node.replace(text[0..start_index - 1])[0]
node.after("<span
id='merge_#{md[1]}'>#{md[0]}</span>#{text[end_index..-1]}")
end
end
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home