Skip to content

@ezkl /noko_html.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Nokogiri Test
require 'nokogiri'
html = <<HTML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Foo</title>
</head>
<body>
<p>Lorem ipsum</p>
</body>
</html>
HTML
puts "Original"
puts "======================"
puts html
puts "======================"
doc = Nokogiri::HTML(html)
puts "Original After Parsing"
puts "======================"
puts doc.to_s
puts "======================"
doc.css("body").first.inner_html = "<h1>Lorem ipsum</h1>"
puts "After <body> change"
puts "======================"
puts doc.to_s
puts "======================"
File.open("path/to/test.html", "w") { |f| f.write doc }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Foo</title>
</head>
<body><h1>Lorem ipsum</h1></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.