Reputation: 134
I'm new to ruby and need to parse html content and update it as required (add an attribute to 'body' tag). I have written the following code
def index
url = "/home/employee/index.html"
doc = Nokogiri::HTML::Document.parse(url)
doc.at_css("body").set_attribute("ng-init", "menu.inspired = 'true'")
File.open('/home/employee/index.txt','w') {|f| doc.write_html_to f}
@content=doc.to_html
end
The output written in the file is the following
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body ng-init="menu.inspired = 'true'"><p>/home/employee/index.html</p></body></html>
The output file contains the added attribute, but the content of the html file seems to be overwritten. So I need to figure out where is the mistake I have made.
Upvotes: 1
Views: 753
Reputation: 102001
Your are not actually manipulating the document from the file /home/employee/index.html
- Rather Nokogiri::HTML::Document.parse(url)
creates a skeleton HTML document with the body "/home/employee/index.html"
.
You need to read the document from file first.
def index
# note that this is a file path! Not an URL.
path = "/home/employee/index.html"
doc = File.open(path) { |f| Nokogiri::HTML(f) }
doc.at_css("body").set_attribute("ng-init", "menu.inspired = 'true'")
File.open(path,'w') {|f| doc.write_html_to f}
@content=doc.to_html
end
See:
Upvotes: 1