Remove a tag but keep the text
Asked Answered
S

2

5

So I have this <a> tag in a xml file

<a href="/www.somethinggggg.com">Something 123</a>

My desired result is to use Nokogiri and completely remove its tag so it is no longer a clickable link e.g

Something 123

My attempt:

content = Nokogiri::XML.fragment(page_content)
content.search('.//a').remove

But this removes the text too.

Any suggestions on how to achieve my desired result using Nokogiri?

Secretarial answered 8/11, 2013 at 14:25 Comment(1)
Would you consider a different gem, e.g. Sanitize?Wade
M
8

Here is what I would do :

require 'nokogiri'

doc = Nokogiri::HTML.parse <<-eot
<a href="/www.somethinggggg.com">Something 123</a>
eot

node = doc.at("a")
node.replace(node.text)

puts doc.to_html

output

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org
   /TR/REC-html40/loose.dtd">
<html>
   <body>Something 123</body>
</html>

Update

What if I have an array that holds content with links?

Hint

require 'nokogiri'

doc = Nokogiri::HTML.parse <<-eot
<a href="/www.foo.com">foo</a>
<a href="/www.bar.com">bar</a>
<a href="/www.baz.com">baz</a>
eot

arr = %w(foo bar baz)
nodes = doc.search("a")
nodes.each {|node| node.replace(node.content) if arr.include?(node.content) }

puts doc.to_html

output

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org
   /TR/REC-html40/loose.dtd">
<html>
   <body>foo
      bar
      baz
   </body>
</html>
Macklin answered 8/11, 2013 at 14:36 Comment(1)
What if I have an array that holds content with linksIndustrial
S
12

Generic way to unwrap tag is — node.replace(node.children), eg.:

doc = Nokogiri::HTML.fragment('<div>A<i>B</i>C</div>')
doc.css('div').each { |node| node.replace(node.children) }
doc.inner_html #=> "A<i>B</i>C"
Supernatant answered 9/12, 2015 at 0:14 Comment(1)
This approach preserves any HTML tags inside the parent tag. Exactly what I needed.Katricekatrina
M
8

Here is what I would do :

require 'nokogiri'

doc = Nokogiri::HTML.parse <<-eot
<a href="/www.somethinggggg.com">Something 123</a>
eot

node = doc.at("a")
node.replace(node.text)

puts doc.to_html

output

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org
   /TR/REC-html40/loose.dtd">
<html>
   <body>Something 123</body>
</html>

Update

What if I have an array that holds content with links?

Hint

require 'nokogiri'

doc = Nokogiri::HTML.parse <<-eot
<a href="/www.foo.com">foo</a>
<a href="/www.bar.com">bar</a>
<a href="/www.baz.com">baz</a>
eot

arr = %w(foo bar baz)
nodes = doc.search("a")
nodes.each {|node| node.replace(node.content) if arr.include?(node.content) }

puts doc.to_html

output

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org
   /TR/REC-html40/loose.dtd">
<html>
   <body>foo
      bar
      baz
   </body>
</html>
Macklin answered 8/11, 2013 at 14:36 Comment(1)
What if I have an array that holds content with linksIndustrial

© 2022 - 2024 — McMap. All rights reserved.