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 @paragraph_element = first_paragraph_element(document.root) unless defined? @paragraph_element
116 truncated_text_content(@paragraph_element) if @paragraph_element
122 @document ||= Kramdown::Document.new(self)
125 def first_image_element(element)
126 return element if image?(element) && element.attr["src"].present?
128 element.children.find do |child|
129 nested_image = first_image_element(child)
130 break nested_image if nested_image
134 def first_paragraph_element(element)
135 return element if paragraph?(element)
137 element.children.find do |child|
138 nested_paragraph = first_paragraph_element(child)
139 break nested_paragraph if nested_paragraph
143 def truncated_text_content(element)
146 append_text = lambda do |child|
147 if child.type == :text
150 child.children.each do |c|
152 break if text.length > MAX_DESCRIPTION_LENGTH
156 append_text.call(element)
158 text.truncate(MAX_DESCRIPTION_LENGTH)
162 element.type == :img || (element.type == :html_element && element.value == "img")
165 def paragraph?(element)
166 element.type == :p || (element.type == :html_element && element.value == "p")
172 linkify(simple_format(ERB::Util.html_escape(self)))