This repository has been archived on 2025-05-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
discourse/lib/summarize.rb

28 lines
441 B
Ruby
Raw Normal View History

# Summarize a HTML field into regular text. Used currently
# for meta tags
require 'sanitize'
class Summarize
def initialize(text)
@text = text
end
def self.max_length
500
end
def summary
return nil if @text.blank?
result = Sanitize.clean(@text)
result.gsub!(/\n/, ' ')
result.strip!
return result if result.length <= Summarize.max_length
"#{result[0..Summarize.max_length]}..."
end
end