Currently I’m parsing a HTML document using Nokogiri and iterating through all the code tags like this:
html = Nokogiri::HTML(doc)
html.css("code").each do |code|
# do something with code
if /^@@@@/.match(code.text.split("\n")[0])
return "this code element is at line blah"
end
end
I don’t have to use Nokogiri, it was just convenient to use to iterate through all the code elements.
In the case where the code tag begins with @@@@ then I want to be able to reference the line number in the document where that code tag occurred. Keep in mind that two code tags can be identical.
There are a couple reasons Nokogiri might be returning a line number of 0 for nodes retrieved using the HTML parser, both involving lbxml.
Credit: Aaron Paterson AKA tenderlove https://github.com/sparklemotion/nokogiri/issues/closed#issue/347
2 . The version of Nokogiri that you are using is built against a different version of libxml than you currently have on your system.
This was my issue. I had libxml version 2.7.8 and was seeing this behavior. I had not installed the gem locally, so I went ahead and did that
If you already have the gem install it would be just be 1 extra step to uninstall it
Credit – Tin Man’s Answer to this question
To determine the version of libxml you are currently running
My current setup (working)