Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created June 16, 2015 21:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save yorickpeterse/6f21c0069c3bc89f2f7b to your computer and use it in GitHub Desktop.
require 'oga'
document = Oga.parse_html <<-EOF
<div>this should</div>
<span>really go</span>
<b>away</b>
<h1>First Title</h1>
Hello world, how are you <b>doing?</b>
EOF
document.each_node do |node|
if node.is_a?(Oga::XML::Element) and node.name == 'h1'
break
else
node.remove
end
end
document.xpath('descendant-or-self::b').each do |b_node|
text = Oga::XML::Text.new(:text => "**#{b_node.inner_text}**")
b_node.before(text)
b_node.remove
end
puts document.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment