2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-10 01:24:02 -05:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
class HTMLParser
|
|
|
|
def self.parse_reply(raw_body)
|
|
|
|
new(raw_body).filtered_text
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :raw_body
|
2022-02-28 13:14:03 -05:00
|
|
|
|
2016-11-10 01:24:02 -05:00
|
|
|
def initialize(raw_body)
|
|
|
|
@raw_body = raw_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def document
|
2016-11-10 18:10:44 -05:00
|
|
|
@document ||= Nokogiri::HTML.parse(raw_body)
|
2016-11-10 01:24:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_replies!
|
2016-11-10 23:38:36 -05:00
|
|
|
document.xpath('//blockquote').each(&:remove)
|
|
|
|
document.xpath('//table').each(&:remove)
|
2017-02-06 13:22:31 -05:00
|
|
|
|
|
|
|
# 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
|
2016-11-10 01:24:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_html
|
2016-11-10 03:40:53 -05:00
|
|
|
@filtered_html ||= begin
|
|
|
|
filter_replies!
|
|
|
|
document.inner_html
|
|
|
|
end
|
2016-11-10 01:24:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_text
|
|
|
|
@filtered_text ||= Html2Text.convert(filtered_html)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|