3 "Business Description:", "Additional Keywords:"
6 MAX_DESCRIPTION_LENGTH = 500
8 def self.new(format, text)
10 when "html" then HTML.new(text || "")
11 when "markdown" then Markdown.new(text || "")
12 when "text" then Text.new(text || "")
17 include ActionView::Helpers::TextHelper
18 include ActionView::Helpers::OutputSafetyHelper
20 def sanitize(text, _options = {})
21 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
26 include ActionView::Helpers::TagHelper
32 doc = Nokogiri::HTML(to_html)
37 doc.xpath("//a").each do |link|
39 link_size += link.content.length
42 link_proportion = link_size.to_f / doc.content.length
45 spammy_phrases = SPAMMY_PHRASES.count do |phrase|
46 doc.content.include?(phrase)
49 ([link_proportion - 0.2, 0.0].max * 200) +
68 def simple_format(text)
69 SimpleFormat.new.simple_format(text)
73 Sanitize.clean(text, Sanitize::Config::OSM).html_safe
76 def linkify(text, mode = :urls)
78 Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferrer")).html_safe
80 Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferrer"))
87 linkify(sanitize(simple_format(self)))
97 linkify(sanitize(document.to_html), :all)
105 @image_element = first_image_element(document.root) unless defined? @image_element
106 @image_element.attr["src"] if @image_element
110 @image_element = first_image_element(document.root) unless defined? @image_element
111 @image_element.attr["alt"] if @image_element
115 return @description if defined? @description
117 @description = first_truncated_text_content(document.root)
123 @document ||= Kramdown::Document.new(self)
126 def first_image_element(element)
127 return element if image?(element) && element.attr["src"].present?
129 element.children.find do |child|
130 nested_image = first_image_element(child)
131 break nested_image if nested_image
135 def first_truncated_text_content(element)
136 if paragraph?(element)
137 truncated_text_content(element)
139 element.children.find do |child|
140 text = first_truncated_text_content(child)
141 break text unless text.nil?
146 def truncated_text_content(element)
149 append_text = lambda do |child|
150 if child.type == :text
153 child.children.each do |c|
155 break if text.length > MAX_DESCRIPTION_LENGTH
159 append_text.call(element)
161 return nil if text.blank?
163 text.truncate(MAX_DESCRIPTION_LENGTH)
167 element.type == :img || (element.type == :html_element && element.value == "img")
170 def paragraph?(element)
171 element.type == :p || (element.type == :html_element && element.value == "p")
177 linkify(simple_format(ERB::Util.html_escape(self)))