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

42 lines
949 B
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!
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
2023-06-20 00:43:36 +05:30
@filtered_text ||= ::Gitlab::Email::HtmlToMarkdownParser.convert(filtered_html)
2017-08-17 22:00:37 +05:30
end
end
end
end