Class: Ask::RAG::Loader::HTML
Overview
Loads an HTML file and extracts text content using Nokogiri. Strips script, style, and nav elements by default.
Instance Method Summary collapse
-
#initialize(elements_to_remove: %w[script style nav footer header])) ⇒ HTML
constructor
A new instance of HTML.
- #load(path) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(elements_to_remove: %w[script style nav footer header])) ⇒ HTML
Returns a new instance of HTML.
10 11 12 |
# File 'lib/ask/rag/loader/html.rb', line 10 def initialize(elements_to_remove: %w[script style nav footer header]) @elements_to_remove = elements_to_remove end |
Instance Method Details
#load(path) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ask/rag/loader/html.rb', line 14 def load(path) content = File.read(path) doc = Nokogiri::HTML.parse(content) @elements_to_remove.each do |selector| doc.css(selector).each(&:remove) end text = doc.at_xpath("//body")&.text || doc.text text = text.gsub(/\s+/, " ").strip [Ask::Document.new( content: text, metadata: { source: path, format: "html" } )] end |