discourse/script/import_scripts/socialcast/create_title.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

53 lines
1.1 KiB
Ruby

# frozen_string_literal: true
require 'uri'
class CreateTitle
def self.from_body(body)
title = remove_mentions body
title = remove_urls title
title = remove_stray_punctuation title
title = first_long_line title
return unless title
sentences = complete_sentences title
if !sentences.nil?
title = sentences[1]
else
title = complete_words title
end
return title unless title.nil? || title.size < 20
end
private
def self.remove_mentions(text)
text.gsub(/@[\w]*/, '')
end
def self.remove_urls(text)
text.gsub(URI::regexp(['http', 'https', 'mailto', 'ftp', 'ldap', 'ldaps']), '')
end
def self.remove_stray_punctuation(text)
text.gsub(/\s+?[^a-zA-Z0-9\s]\s+/, "\n")
end
def self.first_long_line(text)
lines = text.split("\n").select { |t| t.strip.size >= 20 }
return if lines.empty?
lines[0].strip
end
def self.complete_sentences(text)
/(^.*[\S]{2,}[.!?:]+)\W/.match(text[0...80] + ' ')
end
def self.complete_words(text)
return text[0...80].rpartition(/\s/)[0] + "..." if text.size >= 80
text
end
end