class Arch
Constants
- API_URL
- BOILERPLATE_PATTERNS
- MIN_PARAGRAPH_LENGTH
- WIKI_BASE
Public Class Methods
Source
# File plugins/arch.rb, line 35 def self.archwiki_template_paragraph?(paragraph) paragraph.ancestors.any? do |ancestor| ancestor["class"].to_s.split.any? { |klass| klass.start_with?("archwiki-template") } end end
Source
# File plugins/arch.rb, line 41 def self.boilerplate_text?(text) normalized = text.to_s.gsub(/\s+/, " ").strip return true if normalized.empty? BOILERPLATE_PATTERNS.any? { |pattern| normalized.match?(pattern) } end
Source
# File plugins/arch.rb, line 48 def self.clean_paragraph_text(raw) text = raw.to_s.strip text.gsub(/\[\d+\]/, "").gsub(/\s+,/, ",").gsub(/\s+/, " ").gsub(/[\x00-\x1f]/, "").strip end
Source
# File plugins/arch.rb, line 31 def self.extract_title(search_data) search_data.dig("query", "search", 0, "title") end
Source
# File plugins/arch.rb, line 68 def self.format_result(paragraph, url) paragraph = paragraph.to_s.strip url = url.to_s.strip return "No results found" if paragraph.empty? && url.empty? return url if paragraph.empty? "#{paragraph} (#{url})" end
Source
# File plugins/arch.rb, line 53 def self.sanitize_paragraph(html) doc = Nokogiri::HTML.fragment(html.to_s) doc.css("p").each do |paragraph| next if archwiki_template_paragraph?(paragraph) text = clean_paragraph_text(paragraph.text) next if text.length < MIN_PARAGRAPH_LENGTH next if boilerplate_text?(text) return text end "" end
Source
# File plugins/arch.rb, line 77 def self.search(query, fetch_search:, fetch_page:) search_data = fetch_search.call(query) title = extract_title(search_data) return "No results found" unless title page_data = fetch_page.call(title) html = page_data.dig("parse", "text", "*") paragraph = sanitize_paragraph(html) format_result(paragraph, wiki_url(title)) rescue StandardError "No results found" end
Source
# File plugins/arch.rb, line 27 def self.wiki_url(title) "#{WIKI_BASE}#{title.to_s.gsub(" ", "_")}" end
Public Instance Methods
Source
# File plugins/arch.rb, line 104 def execute(m, query) safe_reply(m, search(query)) end
Source
# File plugins/arch.rb, line 99 def fetch_page(title) url = "#{API_URL}?action=parse&page=#{CGI.escape(title)}&format=json&prop=text" JSON.parse(URI.parse(url).open("User-Agent" => "Mozilla/5.0").read) end
Source
# File plugins/arch.rb, line 94 def fetch_search(query) url = "#{API_URL}?action=query&list=search&srsearch=#{CGI.escape(query)}&format=json&srlimit=1" JSON.parse(URI.parse(url).open("User-Agent" => "Mozilla/5.0").read) end
Source
# File plugins/arch.rb, line 90 def search(query, fetch_search: method(:fetch_search), fetch_page: method(:fetch_page)) self.class.search(query, fetch_search: fetch_search, fetch_page: fetch_page) end