2013-06-04 04:12:24 +08:00
|
|
|
module UserNotificationsHelper
|
2014-01-23 04:30:30 +08:00
|
|
|
|
|
|
|
def self.sanitize_options
|
|
|
|
return @sanitize_options if @sanitize_options
|
|
|
|
@sanitize_options = Sanitize::Config::RELAXED.deep_dup
|
|
|
|
@sanitize_options[:elements] << 'aside' << 'div'
|
|
|
|
@sanitize_options[:attributes][:all] << 'class'
|
|
|
|
@sanitize_options
|
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
|
|
|
|
def indent(text, by=2)
|
|
|
|
spacer = " " * by
|
|
|
|
result = ""
|
|
|
|
text.each_line do |line|
|
|
|
|
result << spacer << line
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-07-24 15:13:15 +08:00
|
|
|
def correct_top_margin(html, desired)
|
|
|
|
fragment = Nokogiri::HTML.fragment(html)
|
|
|
|
if para = fragment.css("p:first").first
|
|
|
|
para["style"] = "margin-top: #{desired};"
|
|
|
|
end
|
|
|
|
fragment.to_html.html_safe
|
|
|
|
end
|
|
|
|
|
2013-11-29 06:20:56 +08:00
|
|
|
def logo_url
|
2014-03-19 03:56:17 +08:00
|
|
|
logo_url = SiteSetting.digest_logo_url
|
|
|
|
logo_url = SiteSetting.logo_url if logo_url.blank?
|
2013-11-29 06:20:56 +08:00
|
|
|
if logo_url !~ /http(s)?\:\/\//
|
|
|
|
logo_url = "#{Discourse.base_url}#{logo_url}"
|
|
|
|
end
|
|
|
|
logo_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def html_site_link
|
|
|
|
"<a href='#{Discourse.base_url}'>#{@site_name}</a>"
|
|
|
|
end
|
|
|
|
|
2014-01-23 04:30:30 +08:00
|
|
|
def first_paragraph_from(html)
|
|
|
|
doc = Nokogiri::HTML(html)
|
2014-02-14 05:11:01 +08:00
|
|
|
|
|
|
|
result = ""
|
2014-01-23 04:30:30 +08:00
|
|
|
doc.css('p').each do |p|
|
2014-02-14 05:11:01 +08:00
|
|
|
if p.text.present?
|
|
|
|
result << p.to_s
|
|
|
|
return result if result.size >= 100
|
|
|
|
end
|
2014-01-23 04:30:30 +08:00
|
|
|
end
|
2014-02-14 05:11:01 +08:00
|
|
|
return result unless result.blank?
|
2014-01-23 04:30:30 +08:00
|
|
|
|
|
|
|
# If there is no first paragaph, return the first div (onebox)
|
|
|
|
doc.css('div').first
|
|
|
|
end
|
|
|
|
|
2014-01-23 01:37:37 +08:00
|
|
|
def email_excerpt(html, posts_count)
|
|
|
|
# If there's only one post, include the whole thing.
|
|
|
|
if posts_count == 1
|
2014-01-28 04:11:51 +08:00
|
|
|
raw Sanitize.clean(html, UserNotificationsHelper.sanitize_options)
|
2014-01-23 01:37:37 +08:00
|
|
|
else
|
|
|
|
# Otherwise, try just the first paragraph.
|
2014-01-23 04:30:30 +08:00
|
|
|
para = first_paragraph_from(html)
|
2014-02-22 05:25:19 +08:00
|
|
|
raw Sanitize.clean(para.to_s, UserNotificationsHelper.sanitize_options)
|
2014-01-23 01:37:37 +08:00
|
|
|
end
|
2013-11-29 06:20:56 +08:00
|
|
|
end
|
2013-06-04 04:12:24 +08:00
|
|
|
end
|