Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
93: def highlight(html, word)
94: doc = Nokogiri::HTML::fragment(html)
95: doc.search('.//text()').each do |text_node|
96: new_html = text_node.text.gsub(/\b(#{word})/i, '<span class="bold">\1</span>')
97: text_node.content = ""
98: text_node.after(new_html)
99: end
=> 100: binding.pry_remote
101: doc.to_s
102: end
[#<Search:0x007fc299d8de58>]>> @term
=> "strange"
[#<Search:0x007fc299d8de58>]>> html
=> "Strange strange Crismas?"
[#<Search:0x007fc299d8de58>]>> word
=> "strange"
[#<Search:0x007fc299d8de58>]>> doc
=> #(DocumentFragment:0x3fe14ced4550 {
name = "#document-fragment",
children = [ #(Text "Strange strange Crismas?")]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.