]> git.openstreetmap.org Git - rails.git/blob - lib/rich_text.rb
Move spam scoring to own class
[rails.git] / lib / rich_text.rb
1 # frozen_string_literal: true
2
3 module RichText
4   DESCRIPTION_MAX_LENGTH = 500
5   DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH = 450
6
7   def self.new(format, text)
8     case format
9     when "html" then HTML.new(text || "")
10     when "markdown" then Markdown.new(text || "")
11     when "text" then Text.new(text || "")
12     end
13   end
14
15   class SimpleFormat
16     include ActionView::Helpers::TextHelper
17     include ActionView::Helpers::OutputSafetyHelper
18
19     def sanitize(text, _options = {})
20       Sanitize.clean(text, Sanitize::Config::OSM).html_safe
21     end
22   end
23
24   class Base < String
25     def image
26       nil
27     end
28
29     def image_alt
30       nil
31     end
32
33     def description
34       nil
35     end
36
37     def truncate_html(max_length = nil, img_length = 1000)
38       html_doc = to_html
39       return html_doc if max_length.nil?
40
41       doc = Nokogiri::HTML::DocumentFragment.parse(html_doc)
42       keep_or_discards = %w[p h1 h2 h3 h4 h5 h6 pre a table ul ol dl]
43       accumulated_length = 0
44       exceeded_node_parent = nil
45       truncated = false
46
47       doc.traverse do |node|
48         if accumulated_length >= max_length
49           if node == exceeded_node_parent
50             exceeded_node_parent = node.parent
51             node.remove if keep_or_discards.include?(node.name)
52           else
53             node.remove
54           end
55           next
56         end
57
58         next unless node.children.empty?
59
60         if node.text?
61           accumulated_length += node.text.length
62         elsif node.name == "img"
63           accumulated_length += img_length
64         end
65
66         if accumulated_length >= max_length
67           truncated = true
68           exceeded_node_parent = node.parent
69           node.remove
70         end
71       end
72
73       {
74         :truncated => truncated,
75         :html => doc.to_html.html_safe
76       }
77     end
78
79     protected
80
81     def simple_format(text)
82       SimpleFormat.new.simple_format(text, :dir => "auto")
83     end
84
85     def sanitize(text)
86       Sanitize.clean(text, Sanitize::Config::OSM).html_safe
87     end
88
89     def linkify(text, mode = :urls)
90       link_attr = 'rel="nofollow noopener noreferrer" dir="auto"'
91       Rinku.auto_link(ERB::Util.html_escape(text), mode, link_attr) do |url|
92         url = shorten_host(url, Settings.linkify_hosts, Settings.linkify_hosts_replacement)
93         shorten_host(url, Settings.linkify_wiki_hosts, Settings.linkify_wiki_hosts_replacement) do |path|
94           path.sub(Regexp.new(Settings.linkify_wiki_optional_path_prefix || ""), "")
95         end
96       end.html_safe
97     end
98
99     private
100
101     def shorten_host(url, hosts, hosts_replacement)
102       %r{^(https?://([^/]*))(.*)$}.match(url) do |m|
103         scheme_host, host, path = m.captures
104         if hosts&.include?(host)
105           path = yield(path) if block_given?
106           if hosts_replacement
107             "#{hosts_replacement}#{path}"
108           else
109             "#{scheme_host}#{path}"
110           end
111         end || url
112       end || url
113     end
114   end
115
116   class HTML < Base
117     def to_html
118       linkify(simple_format(self))
119     end
120
121     def to_text
122       to_s
123     end
124   end
125
126   class Markdown < Base
127     def to_html
128       linkify(sanitize(document.to_html), :all)
129     end
130
131     def to_text
132       to_s
133     end
134
135     def image
136       @image_element = first_image_element(document.root) unless defined? @image_element
137       @image_element.attr["src"] if @image_element
138     end
139
140     def image_alt
141       @image_element = first_image_element(document.root) unless defined? @image_element
142       @image_element.attr["alt"] if @image_element
143     end
144
145     def description
146       return @description if defined? @description
147
148       @description = first_truncated_text_content(document.root)
149     end
150
151     private
152
153     def document
154       return @document if @document
155
156       @document = Kramdown::Document.new(self)
157
158       should_get_dir_auto = lambda do |el|
159         dir_auto_types = [:p, :header, :codespan, :codeblock, :pre, :ul, :ol, :table, :dl, :math]
160         return true if dir_auto_types.include?(el.type)
161         return true if el.type == :a && el.children.length == 1 && el.children[0].type == :text && el.children[0].value == el.attr["href"]
162
163         false
164       end
165
166       add_dir = lambda do |element|
167         element.attr["dir"] ||= "auto" if should_get_dir_auto.call(element)
168         element.children.each(&add_dir)
169       end
170       add_dir.call(@document.root)
171
172       @document
173     end
174
175     def first_image_element(element)
176       return element if image?(element) && element.attr["src"].present?
177
178       element.children.find do |child|
179         nested_image = first_image_element(child)
180         break nested_image if nested_image
181       end
182     end
183
184     def first_truncated_text_content(element)
185       if paragraph?(element)
186         truncated_text_content(element)
187       else
188         element.children.find do |child|
189           text = first_truncated_text_content(child)
190           break text unless text.nil?
191         end
192       end
193     end
194
195     def truncated_text_content(element)
196       text = +""
197
198       append_text = lambda do |child|
199         if child.type == :text
200           text << child.value
201         else
202           child.children.each do |c|
203             append_text.call(c)
204             break if text.length > DESCRIPTION_MAX_LENGTH
205           end
206         end
207       end
208       append_text.call(element)
209
210       return nil if text.blank?
211
212       text_truncated_to_word_break = text.truncate(DESCRIPTION_MAX_LENGTH, :separator => /(?<!\s)\s+/)
213
214       if text_truncated_to_word_break.length >= DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH
215         text_truncated_to_word_break
216       else
217         text.truncate(DESCRIPTION_MAX_LENGTH)
218       end
219     end
220
221     def image?(element)
222       element.type == :img || (element.type == :html_element && element.value == "img")
223     end
224
225     def paragraph?(element)
226       element.type == :p || (element.type == :html_element && element.value == "p")
227     end
228   end
229
230   class Text < Base
231     def to_html
232       linkify(simple_format(ERB::Util.html_escape(self)))
233     end
234
235     def to_text
236       to_s
237     end
238   end
239 end