debian-mirror-gitlab/lib/gitlab/email/html_parser.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module Email
class HTMLParser
def self.parse_reply(raw_body)
new(raw_body).filtered_text
end
attr_reader :raw_body
2022-05-07 20:08:51 +05:30
2017-08-17 22:00:37 +05:30
def initialize(raw_body)
@raw_body = raw_body
end
def document
@document ||= Nokogiri::HTML.parse(raw_body)
end
def filter_replies!
document.xpath('//blockquote').each(&:remove)
document.xpath('//table').each(&:remove)
2017-09-10 17:25:29 +05:30
# bogus links with no href are sometimes added by outlook,
# and can result in Html2Text adding extra square brackets
# to the text, so we unwrap them here.
document.xpath('//a[not(@href)]').each do |link|
link.replace(link.children)
end
2017-08-17 22:00:37 +05:30
end
def filtered_html
@filtered_html ||= begin
filter_replies!
document.inner_html
end
end
def filtered_text
@filtered_text ||= Html2Text.convert(filtered_html)
end
end
end
end